Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
# Load the MarianMT model and tokenizer for English to Urdu translation
|
| 5 |
+
def load_model():
|
| 6 |
+
model_name = 'Helsinki-NLP/opus-mt-en-ur' # English to Urdu pre-trained model
|
| 7 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 8 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 9 |
+
return model, tokenizer
|
| 10 |
+
|
| 11 |
+
# Define the translation function
|
| 12 |
+
def translate(text, model, tokenizer):
|
| 13 |
+
# Prepare the text for translation
|
| 14 |
+
translated = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 15 |
+
# Generate translation
|
| 16 |
+
translated = model.generate(**translated)
|
| 17 |
+
# Decode the translated text
|
| 18 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 19 |
+
return translated_text
|
| 20 |
+
|
| 21 |
+
# Load model and tokenizer
|
| 22 |
+
model, tokenizer = load_model()
|
| 23 |
+
|
| 24 |
+
# Define Gradio interface function
|
| 25 |
+
def gradio_interface(text):
|
| 26 |
+
return translate(text, model, tokenizer)
|
| 27 |
+
|
| 28 |
+
# Set up Gradio interface for the translation web app
|
| 29 |
+
interface = gr.Interface(fn=gradio_interface,
|
| 30 |
+
inputs="text", # User input type (text box)
|
| 31 |
+
outputs="text", # Output type (translated text)
|
| 32 |
+
live=True, # Updates live as user types
|
| 33 |
+
title="English to Urdu Translation Web App",
|
| 34 |
+
description="Translate English text to Urdu using the MarianMT model.")
|
| 35 |
+
|
| 36 |
+
# Launch the Gradio interface
|
| 37 |
+
interface.launch()
|