Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the pre-trained translation model
|
| 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 |
+
# Translation function
|
| 10 |
+
def translate(text):
|
| 11 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 12 |
+
translated = model.generate(**inputs)
|
| 13 |
+
result = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
| 14 |
+
return result[0]
|
| 15 |
+
|
| 16 |
+
# Create Gradio Interface
|
| 17 |
+
interface = gr.Interface(
|
| 18 |
+
fn=translate,
|
| 19 |
+
inputs=gr.Textbox(label="Enter English Text"),
|
| 20 |
+
outputs=gr.Textbox(label="Translated Urdu Text"),
|
| 21 |
+
title="English to Urdu Translator",
|
| 22 |
+
description="Enter text in English and get its Urdu translation."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Launch the app
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
interface.launch(share=True)
|