translator / app.py
imeesam's picture
Create app.py
4d37fc3 verified
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import gradio as gr
model_name = "Helsinki-NLP/opus-mt-en-ur"
print("Downloading tokenizer and model directly...")
# Loading the tokenizer and model directly bypasses the buggy pipeline
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
print("Model loaded successfully!")
def translate_en_to_ur(text):
if not text.strip():
return ""
# 1. Tokenize: Convert the text into numbers the model understands
inputs = tokenizer(text, return_tensors="pt", padding=True)
# 2. Generate: The model predicts the Urdu translation tokens
translated_tokens = model.generate(**inputs)
# 3. Decode: Convert the predicted tokens back into readable text
result = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
return result
# Build the Gradio Interface
interface = gr.Interface(
fn=translate_en_to_ur,
inputs=gr.Textbox(lines=5, placeholder="Enter English text here...", label="English Input"),
outputs=gr.Textbox(lines=5, label="Urdu Translation"),
title="English to Urdu Translator 🇵🇰",
description="Enter any English text and the AI will translate it into Urdu.",
theme="default"
)
# Launch the app
interface.launch(share=True)