Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Load pre-trained translation model (English to Urdu)
|
5 |
+
model_name = "Helsinki-NLP/opus-mt-en-ur"
|
6 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
7 |
+
model = MarianMTModel.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Function to translate English text to Urdu
|
10 |
+
def translate_text(text):
|
11 |
+
if not text.strip():
|
12 |
+
return "Please enter some text to translate."
|
13 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
14 |
+
translated_tokens = model.generate(**inputs)
|
15 |
+
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
16 |
+
return translated_text
|
17 |
+
|
18 |
+
# Gradio Interface
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=translate_text,
|
21 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter English text here..."),
|
22 |
+
outputs=gr.Textbox(label="Translated Urdu Text"),
|
23 |
+
title="English to Urdu Translator",
|
24 |
+
description="Enter text in English and get the Urdu translation."
|
25 |
+
)
|
26 |
+
|
27 |
+
# Launch the Gradio app
|
28 |
+
iface.launch()
|