Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
def load_model():
|
| 5 |
+
# Load the English to Urdu translation model
|
| 6 |
+
model_name = "Helsinki-NLP/opus-mt-en-ur"
|
| 7 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 9 |
+
return tokenizer, model
|
| 10 |
+
|
| 11 |
+
def translate_text(text, tokenizer, model):
|
| 12 |
+
"""
|
| 13 |
+
Translate English text to Urdu using the loaded model
|
| 14 |
+
"""
|
| 15 |
+
# Tokenize the input text
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 17 |
+
|
| 18 |
+
# Generate translation
|
| 19 |
+
translated = model.generate(**inputs)
|
| 20 |
+
|
| 21 |
+
# Decode the translation
|
| 22 |
+
translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
|
| 23 |
+
|
| 24 |
+
return translated_text
|
| 25 |
+
|
| 26 |
+
def main():
|
| 27 |
+
# Load the model and tokenizer
|
| 28 |
+
tokenizer, model = load_model()
|
| 29 |
+
|
| 30 |
+
# Define the Gradio interface
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=lambda text: translate_text(text, tokenizer, model),
|
| 33 |
+
inputs=gr.Textbox(
|
| 34 |
+
lines=5,
|
| 35 |
+
placeholder="Enter English text here...",
|
| 36 |
+
label="English Text"
|
| 37 |
+
),
|
| 38 |
+
outputs=gr.Textbox(
|
| 39 |
+
lines=5,
|
| 40 |
+
label="Urdu Translation"
|
| 41 |
+
),
|
| 42 |
+
title="English to Urdu Translator",
|
| 43 |
+
description="Enter English text and get its Urdu translation",
|
| 44 |
+
examples=[
|
| 45 |
+
["Hello, how are you?"],
|
| 46 |
+
["I love learning new languages."],
|
| 47 |
+
["The weather is beautiful today."]
|
| 48 |
+
],
|
| 49 |
+
theme=gr.themes.Soft()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Launch the interface
|
| 53 |
+
iface.launch()
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
main()
|