Bajiyo commited on
Commit
51db1ee
·
verified ·
1 Parent(s): af8878c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -23
app.py CHANGED
@@ -2,34 +2,52 @@ import gradio as gr
2
  from huggingface_hub import from_pretrained_keras
3
 
4
  # Load the model from Hugging Face Hub
5
- model = from_pretrained_keras("Bajiyo/Malayalam_transliteration")
6
 
7
- # Function to preprocess text (replace with model-specific preprocessing if needed)
8
- def preprocess_text(input_text):
9
- # Assuming character-level model: convert text to sequence of integer indices
10
- # Replace with your specific preprocessing steps based on the model's requirements
11
- # You might need tokenization or other transformations
12
- # ...
13
- return preprocessed_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- def transliterate(input_text):
16
- # Preprocess the input text
17
- preprocessed_text = preprocess_text(input_text)
18
 
19
- # Make predictions using the model
20
- predictions = model.predict(preprocessed_text)
 
 
 
21
 
22
- # Post-process the predictions if needed (replace with your logic)
23
- output_text = predictions # Assuming model outputs transliteration directly
24
 
25
- return output_text
 
26
 
27
- textbox = gr.inputs.Textbox(label="Enter Malayalam Text")
28
 
29
- demo = gr.Interface(fn=transliterate,
30
- inputs=textbox,
31
- outputs=gr.outputs.Textbox(label="Transliteration to English"),
32
- title="Malayalam to English Transliteration"
33
- )
34
 
35
- demo.launch()
 
2
  from huggingface_hub import from_pretrained_keras
3
 
4
  # Load the model from Hugging Face Hub
5
+ model = from_pretrained_keras("Bajiyo/ml-en-transliteration")
6
 
7
+ import gradio as gr
8
+
9
+ # Load the saved model and tokenizers
10
+ import json
11
+ from keras.models import load_model
12
+ from keras.preprocessing.sequence import pad_sequences
13
+
14
+ # Load the saved model
15
+ model_path = "/content/drive/MyDrive/hugging_final/model"
16
+ model = load_model(model_path)
17
+
18
+ # Load tokenizer configurations
19
+ source_tokenizer_path = "https://huggingface.co/Bajiyo/ml-en-transliteration/blob/main/source_tokenizer.json"
20
+ with open(source_tokenizer_path, "r") as f:
21
+ source_tokenizer_config = json.load(f)
22
+
23
+ target_tokenizer_path = "https://huggingface.co/Bajiyo/ml-en-transliteration/blob/main/target_tokenizer.json"
24
+ with open(target_tokenizer_path, "r") as f:
25
+ target_tokenizer_config = json.load(f)
26
+
27
+ # Reconstruct tokenizers
28
+ from keras.preprocessing.text import tokenizer_from_json
29
+ source_tokenizer = tokenizer_from_json(source_tokenizer_config)
30
+ target_tokenizer = tokenizer_from_json(target_tokenizer_config)
31
 
32
+ # Define the maximum sequence length
33
+ max_seq_length = 50
 
34
 
35
+ # Function to predict transliteration
36
+ def predict_transliteration(input_text):
37
+ # Preprocess the input text
38
+ input_sequence = source_tokenizer.texts_to_sequences([input_text])
39
+ input_sequence_padded = pad_sequences(input_sequence, maxlen=max_seq_length, padding='post')
40
 
41
+ # Generate predictions
42
+ predicted_sequence = model.predict(input_sequence_padded)
43
 
44
+ # Decode the predicted sequence
45
+ predicted_text = "".join(target_tokenizer.index_word[i] for i in np.argmax(predicted_sequence, axis=-1)[0] if i != 0)
46
 
47
+ return predicted_text
48
 
49
+ # Create a Gradio interface
50
+ input_textbox = gr.inputs.Textbox(lines=2, label="Enter Malayalam text")
51
+ output_textbox = gr.outputs.Textbox(label="Predicted Transliteration")
 
 
52
 
53
+ gr.Interface(fn=predict_transliteration, inputs=input_textbox, outputs=output_textbox, title="Malayalam Transliteration", description="Enter Malayalam text to get its transliteration in English.").launch()