Bajiyo commited on
Commit
efee0cf
1 Parent(s): cd395e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -43
app.py CHANGED
@@ -1,51 +1,28 @@
1
- from gradio import Interface
2
- import json
3
- from tensorflow.keras.models import load_model # Assuming TensorFlow backend
4
- #from keras.preprocessing.text import Tokenizer # Assuming Keras Tokenizer
5
 
6
- # Model and tokenizer loading paths (replace with your actual paths)
7
- model_path = "Bajiyo/mal_en_transliteration"
8
- source_tokenizer_config_path = "https://huggingface.co/Bajiyo/Malayalam_transliteration/blob/main/source_tokenizer_config.json"
9
- target_tokenizer_config_path = "https://huggingface.co/Bajiyo/Malayalam_transliteration/blob/main/target_tokenizer_config.json"
10
 
11
- # Load the model
12
- model = load_model(model_path)
13
 
14
- # Load tokenizers
15
- with open(source_tokenizer_config_path, "r") as f:
16
- source_tokenizer = Tokenizer.from_config(json.load(f))
 
17
 
18
- with open(target_tokenizer_config_path, "r") as f:
19
- target_tokenizer = Tokenizer.from_config(json.load(f))
20
 
 
 
21
 
22
- def transliterate(malayalam_name):
23
- # Preprocess input (e.g., handle punctuation, special characters)
24
- processed_name = preprocess_malayalam_name(malayalam_name) # Implement your preprocessing logic
25
-
26
- # Tokenize the input
27
- sequence = source_tokenizer.texts_to_sequences([processed_name])[0]
28
-
29
- # Pad the sequence
30
- padded_sequence = pad_sequences([sequence], maxlen=MAX_SEQ_LENGTH, padding="post")
31
-
32
- # Make prediction
33
- prediction = model.predict(padded_sequence)[0]
34
-
35
- # Detokenize the predicted sequence
36
- transliterated_name = target_tokenizer.sequences_to_texts([np.argmax(prediction)])[0]
37
-
38
- return transliterated_name
39
-
40
- # Define the maximum sequence length your model was trained on
41
- MAX_SEQ_LENGTH = 49 # Replace with the actual value
42
-
43
- interface = Interface(
44
- fn=transliterate,
45
- inputs="text",
46
- outputs="text",
47
- title="Malayalam to English Transliteration",
48
- description="Enter a Malayalam name and get the transliterated English version.",
49
- )
50
 
 
 
 
 
51
  interface.launch()
 
1
+ import gradio as gr
2
+ from huggingface_hub import from_pretrained_keras
3
+ from transformers import AutoTokenizer
 
4
 
5
+ # Load the model from Hugging Face
6
+ model = from_pretrained_keras("Bajiyo/Malayalam_transliteration")
 
 
7
 
8
+ # Load the tokenizer
9
+ tokenizer = AutoTokenizer.from_pretrained("Bajiyo/Malayalam_transliteration")
10
 
11
+ # Define a function to make predictions
12
+ def predict(text):
13
+ # Tokenize the input text
14
+ inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True)
15
 
16
+ # Make predictions using the model
17
+ outputs = model.predict(inputs)
18
 
19
+ # Decode the predicted tokens
20
+ predicted_text = tokenizer.decode(outputs.logits[0], skip_special_tokens=True)
21
 
22
+ return predicted_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Create a Gradio interface
25
+ inputs = gr.inputs.Textbox(label="Enter Malayalam Text")
26
+ outputs = gr.outputs.Textbox(label="Transliteration to English")
27
+ interface = gr.Interface(predict, inputs, outputs, title="Malayalam to English Transliteration")
28
  interface.launch()