Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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/
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
preprocessed_text = preprocess_text(input_text)
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
title="Malayalam to English Transliteration"
|
33 |
-
)
|
34 |
|
35 |
-
|
|
|
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()
|