Bajiyo commited on
Commit
9568673
1 Parent(s): 3f6c5e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -1,45 +1,26 @@
1
- from tensorflow.keras.models import load_model
2
- import json
3
- import gradio as gr
4
-
5
- # Load the pre-trained Keras model (replace with actual model path if needed)
6
- model = load_model("saved_model.pb")
7
 
8
  # Load tokenizer configurations
9
- with open("source_tokenizer_config.json", "r") as f:
10
- source_tokenizer_config = json.load(f)
11
-
12
- with open("target_tokenizer_config.json", "r") as f:
13
- target_tokenizer_config = json.load(f)
14
-
15
- def predict(text):
16
- # Preprocess text (e.g., tokenization) based on your Keras model's requirements
17
- # Here's an example assuming your model expects preprocessed text (modify as needed)
18
- preprocessed_text = process_text(text, source_tokenizer_config) # Implement your preprocessing logic
19
- predictions = model.predict(np.array([preprocessed_text]))
20
-
21
- # Postprocess predictions (e.g., decoding) based on your Keras model's output format
22
- decoded_text = process_predictions(predictions[0], target_tokenizer_config) # Implement your postprocessing logic
23
- return decoded_text
24
-
25
- # Define preprocessing and postprocessing functions based on your Keras model (replace with your logic)
26
- def process_text(text, tokenizer_config):
27
- # ... Your preprocessing steps (e.g., tokenization, padding) ...
28
- return processed_text
29
-
30
- def process_predictions(predictions, tokenizer_config):
31
- # ... Your postprocessing steps (e.g., argmax, converting indices to tokens) ...
32
- return decoded_text
33
-
34
- # Create the Gradio interface
35
- interface = gr.Interface(
36
- fn=predict,
37
- inputs=gr.Textbox(label="Enter Malayalam Text"),
38
- outputs=gr.Textbox(label="Predicted English Text"),
39
- title="Malayalam to English Transliteration",
40
- description="Enter Malayalam text in the box and click 'Predict' to get the English transliteration.",
41
- thumbnail="thumbnail.jpg" # Optional: Add a thumbnail image (upload to Space)
42
  )
43
 
44
- # Launch the interface
45
  interface.launch()
 
1
+ from transformers import AutoTokenizer, TFBertForSeq2SeqLM # Assuming TFBert model
 
 
 
 
 
2
 
3
  # Load tokenizer configurations
4
+ source_tokenizer = AutoTokenizer.from_pretrained("https://huggingface.co/Bajiyo/mal_en_transliteration/tree/main/source_tokenizer_config.json")
5
+ target_tokenizer = AutoTokenizer.from_pretrained("https://huggingface.co/Bajiyo/mal_en_transliteration/tree/main/target_tokenizer_config.json")
6
+
7
+ # Load the model (replace with your actual model path)
8
+ model = TFBertForSeq2SeqLM.from_pretrained("https://huggingface.co/Bajiyo/mal_en_transliteration/tree/main/transliteration_model.h5")
9
+
10
+ def translate(malayalam_text):
11
+ """Function to perform Malayalam to English transliteration"""
12
+ source_ids = source_tokenizer(malayalam_text, return_tensors="pt")["input_ids"]
13
+ translated_tokens = model.generate(**source_ids)
14
+ english_text = target_tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
15
+ return english_text
16
+
17
+ interface = gradio.Interface(
18
+ fn=translate,
19
+ inputs="text",
20
+ outputs="text",
21
+ title="Malayalam to English Transliteration",
22
+ description="Enter Malayalam text to get the English transliteration.",
23
+ examples=[["എങ്ങനെയാണ് ഞാൻ ഇംഗ്ലീഷിൽ സംസാരിക്കേണ്ടത്?"], ["ഹലോ എങ്ങനെയിരിക്കുന്നു?"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
 
 
26
  interface.launch()