Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
3 |
+
|
4 |
+
# Define translation models
|
5 |
+
models = {
|
6 |
+
"English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
|
7 |
+
"Urdu to English": "Helsinki-NLP/opus-mt-ur-en"
|
8 |
+
}
|
9 |
+
|
10 |
+
# Load models
|
11 |
+
loaded_models = {}
|
12 |
+
for direction, model_name in models.items():
|
13 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
14 |
+
model = MarianMTModel.from_pretrained(model_name)
|
15 |
+
loaded_models[direction] = (tokenizer, model)
|
16 |
+
|
17 |
+
# Translation function
|
18 |
+
def translate(text, direction):
|
19 |
+
tokenizer, model = loaded_models[direction]
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
21 |
+
translated = model.generate(**inputs)
|
22 |
+
output = tokenizer.decode(translated[0], skip_special_tokens=True)
|
23 |
+
return output
|
24 |
+
|
25 |
+
# Gradio interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=translate,
|
28 |
+
inputs=[
|
29 |
+
gr.Textbox(label="Enter Text"),
|
30 |
+
gr.Radio(["English to Urdu", "Urdu to English"], label="Translation Direction")
|
31 |
+
],
|
32 |
+
outputs=gr.Textbox(label="Translated Text"),
|
33 |
+
title="English ↔ Urdu Translator 🌐",
|
34 |
+
description="Translate between English and Urdu using Hugging Face models."
|
35 |
+
)
|
36 |
+
|
37 |
+
iface.launch()
|