Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("abdulwaheed1/urdu_to_english_translation_mbart")
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("abdulwaheed1/urdu_to_english_translation_mbart")
|
| 8 |
+
|
| 9 |
+
# Function to translate Urdu text to English
|
| 10 |
+
def translate_urdu_to_english(urdu_text):
|
| 11 |
+
try:
|
| 12 |
+
# Tokenize the input Urdu text
|
| 13 |
+
inputs = tokenizer(urdu_text, return_tensors="pt", padding=True, truncation=True)
|
| 14 |
+
|
| 15 |
+
# Generate translation using the model
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
translated_tokens = model.generate(**inputs, max_length=512)
|
| 18 |
+
|
| 19 |
+
# Decode the generated tokens into English text
|
| 20 |
+
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
return translated_text
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
# Return an error message if something goes wrong
|
| 26 |
+
return f"Error in translation: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Set up Gradio interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=translate_urdu_to_english, # Function to call
|
| 31 |
+
inputs=gr.Textbox(label="Enter Urdu Text"), # Textbox for user input
|
| 32 |
+
outputs=gr.Textbox(label="Translated English Text"), # Textbox for displaying output
|
| 33 |
+
live=True # Optionally, enable live translation (i.e., as the user types)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the Gradio interface
|
| 37 |
+
iface.launch()
|