Bajiyo commited on
Commit
745cf6d
1 Parent(s): 1b48193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -74
app.py CHANGED
@@ -1,77 +1,26 @@
1
- from huggingface_hub import from_pretrained_keras
 
2
 
3
- # Assuming your model is a standard Keras model on Hugging Face Hub
4
- model = from_pretrained_keras("Bajiyo/Malayalam_transliteration") # Replace with your model name
 
5
 
6
- def predict(text):
7
- """
8
- This function preprocesses the input text, makes predictions using the loaded model,
9
- and returns the predictions.
10
- """
11
- # Preprocess the input text (tokenization, etc.) - Implement your pre-processing logic here
12
- processed_text = preprocess_text(text)
13
-
14
- # Make predictions using the model
15
- predictions = model.predict(processed_text)
16
-
17
- # Post-process predictions (optional)
18
- # This might involve converting probabilities to classes, decoding sequences, etc.
19
- # Implement your post-processing logic here
20
- processed_predictions = postprocess_predictions(predictions)
21
-
22
- return processed_predictions
23
-
24
- def preprocess_text(text):
25
- """
26
- This function implements your specific logic for pre-processing the input text.
27
- You'll need to replace this with your actual pre-processing steps, which might include:
28
-
29
- - Tokenization (converting text into numerical representations)
30
- - Padding sequences to a fixed length
31
- - Adding special tokens (e.g., start/end of sequence)
32
-
33
- Here's an example structure (replace with your actual implementation):
34
- """
35
- # Import necessary libraries (e.g., tokenizers)
36
- from transformers import AutoTokenizer
37
-
38
- # Replace with your actual tokenizer name from Hugging Face Hub (if applicable)
39
- tokenizer = AutoTokenizer.from_pretrained("Bajiyo/YourTokenizerName") # Adjust based on your model
40
-
41
- # Tokenize the text
42
- tokenized_text = tokenizer(text, return_tensors="tf") # Assuming TensorFlow backend
43
-
44
- # Pad the sequence (if necessary)
45
- # ... (implement padding logic)
46
 
47
- return tokenized_text
48
-
49
- def postprocess_predictions(predictions):
50
- """
51
- This function implements your specific logic for post-processing the model predictions.
52
- You'll need to replace this with your actual post-processing steps, which might include:
53
-
54
- - Converting probabilities to class labels (if applicable)
55
- - Decoding predicted sequences (if applicable)
56
- - Selecting the most likely prediction
57
-
58
- Here's an example structure (replace with your actual implementation):
59
- """
60
- # Assuming predictions are probabilities for each class
61
- predicted_class = np.argmax(predictions, axis=1) # Get the index of the highest probability class
62
-
63
- # You can further process the predicted class index here (e.g., convert to label name)
64
-
65
- return predicted_class
66
-
67
- # Integrate the predict function into your Space UI (Streamlit or Gradio)
68
- # This part will depend on the specific framework you're using (Streamlit or Gradio)
69
- # Here's an example using Streamlit:
70
-
71
- import streamlit as st
72
-
73
- st.title("Malayalam Transliteration Model")
74
- text_input = st.text_input("Enter Malayalam text to transliterate:")
75
- if st.button("Predict"):
76
- prediction = predict(text_input)
77
- st.write("Transliterated English:", prediction)
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
+ # Model and Tokenizer Names (replace if needed)
5
+ MODEL_NAME = "Bajiyo/Malayalam_transliteration"
6
+ TOKENIZER_NAME = f"{MODEL_NAME}-vocab"
7
 
8
+ # Load the tokenizer and model
9
+ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME)
10
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def predict(text):
13
+ input_ids = tokenizer.encode(text, return_tensors="pt")
14
+ outputs = model.generate(input_ids)
15
+ decoded_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
+ return decoded_text
17
+
18
+ interface = gr.Interface(
19
+ fn=predict,
20
+ inputs=gr.Textbox(label="Enter Malayalam Text"),
21
+ outputs=gr.Textbox(label="Predicted English Text"),
22
+ title="Malayalam to English Transliteration",
23
+ description="Enter Malayalam text in the box and click 'Predict' to get the English transliteration.",
24
+ )
25
+
26
+ interface.launch()