Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
model_name = "Helsinki-NLP/opus-mt-en-ur"
|
| 5 |
+
|
| 6 |
+
print("Downloading tokenizer and model directly...")
|
| 7 |
+
# Loading the tokenizer and model directly bypasses the buggy pipeline
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
+
print("Model loaded successfully!")
|
| 11 |
+
|
| 12 |
+
def translate_en_to_ur(text):
|
| 13 |
+
if not text.strip():
|
| 14 |
+
return ""
|
| 15 |
+
|
| 16 |
+
# 1. Tokenize: Convert the text into numbers the model understands
|
| 17 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 18 |
+
|
| 19 |
+
# 2. Generate: The model predicts the Urdu translation tokens
|
| 20 |
+
translated_tokens = model.generate(**inputs)
|
| 21 |
+
|
| 22 |
+
# 3. Decode: Convert the predicted tokens back into readable text
|
| 23 |
+
result = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 24 |
+
return result
|
| 25 |
+
|
| 26 |
+
# Build the Gradio Interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=translate_en_to_ur,
|
| 29 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter English text here...", label="English Input"),
|
| 30 |
+
outputs=gr.Textbox(lines=5, label="Urdu Translation"),
|
| 31 |
+
title="English to Urdu Translator 🇵🇰",
|
| 32 |
+
description="Enter any English text and the AI will translate it into Urdu.",
|
| 33 |
+
theme="default"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the app
|
| 37 |
+
interface.launch(share=True)
|