Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load built-in models
|
| 5 |
+
en_to_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
|
| 6 |
+
ur_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
|
| 7 |
+
|
| 8 |
+
# Define translation function
|
| 9 |
+
def translate(text, direction):
|
| 10 |
+
if direction == "English to Urdu":
|
| 11 |
+
return en_to_ur(text)[0]['translation_text']
|
| 12 |
+
else:
|
| 13 |
+
return ur_to_en(text)[0]['translation_text']
|
| 14 |
+
|
| 15 |
+
# Build Gradio UI
|
| 16 |
+
interface = gr.Interface(
|
| 17 |
+
fn=translate,
|
| 18 |
+
inputs=[
|
| 19 |
+
gr.Textbox(lines=5, label="Enter text"),
|
| 20 |
+
gr.Dropdown(["English to Urdu", "Urdu to English"], label="Direction")
|
| 21 |
+
],
|
| 22 |
+
outputs=gr.Textbox(label="Translated Text"),
|
| 23 |
+
title="English ↔ Urdu Translator",
|
| 24 |
+
description="Translate text between English and Urdu using Hugging Face models."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Launch the app
|
| 28 |
+
interface.launch()
|