File size: 1,054 Bytes
da59205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer

# Load Hugging Face translation model
model_name = "Helsinki-NLP/opus-mt-en-ur"  # ✅ Pretrained English to Urdu Model
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)

# Define the translation function
def translate_to_urdu(text):
    # Tokenize input text
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    
    # Generate translation
    translated = model.generate(**inputs)
    
    # Decode the translation
    output_text = tokenizer.decode(translated[0], skip_special_tokens=True)
    return output_text

# Create a Gradio interface for the translator
translator = gr.Interface(
    fn=translate_to_urdu,
    inputs=gr.Textbox(lines=2, placeholder="Enter English text..."),
    outputs="text",
    title="English to Urdu Translator",
    description="Enter English text and get the Urdu translation instantly.",
    theme="soft",
)

# Launch the Gradio app
translator.launch()