File size: 926 Bytes
ab51985
 
181254b
ab51985
d9560fb
181254b
 
 
ab51985
 
 
 
181254b
ab51985
 
 
 
 
 
d03bc8f
da01144
ab51985
 
 
 
 
d9560fb
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
# Import necessary modules
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer

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

# Define the translation function
def translate(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    translated = model.generate(**inputs)
    output = tokenizer.decode(translated[0], skip_special_tokens=True)
    return output

# Create the Gradio interface
iface = gr.Interface(
    fn=translate, 
    inputs=gr.Textbox(lines=1, placeholder="Enter your (English) text here..."), 
    outputs=gr.Textbox(label="Translation (Urdu)"),
    title="English to Urdu Translator",
    description="Enter text in English and get the Urdu translation instantly."
)

# Launch the app
iface.launch()