Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary modules
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 4 |
+
|
| 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 |
+
|
| 10 |
+
# Define the translation function
|
| 11 |
+
def translate(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 13 |
+
translated = model.generate(**inputs)
|
| 14 |
+
output = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 15 |
+
return output
|
| 16 |
+
|
| 17 |
+
# Create the Gradio interface
|
| 18 |
+
iface = gr.Interface(
|
| 19 |
+
fn=translate,
|
| 20 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter English text here..."),
|
| 21 |
+
outputs=gr.Textbox(label="Translated Urdu Text"),
|
| 22 |
+
title="English to Urdu Translator",
|
| 23 |
+
description="Enter text in English and get the Urdu translation instantly."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Launch the app
|
| 27 |
+
iface.launch()
|