File size: 863 Bytes
90acaec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
import gradio as gr

# Load built-in models
en_to_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
ur_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")

# Define translation function
def translate(text, direction):
    if direction == "English to Urdu":
        return en_to_ur(text)[0]['translation_text']
    else:
        return ur_to_en(text)[0]['translation_text']

# Build Gradio UI
interface = gr.Interface(
    fn=translate,
    inputs=[
        gr.Textbox(lines=5, label="Enter text"),
        gr.Dropdown(["English to Urdu", "Urdu to English"], label="Direction")
    ],
    outputs=gr.Textbox(label="Translated Text"),
    title="English ↔ Urdu Translator",
    description="Translate text between English and Urdu using Hugging Face models."
)

# Launch the app
interface.launch()