Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,23 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
# Load the
|
5 |
-
|
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 |
-
#
|
12 |
-
def
|
13 |
-
|
14 |
-
|
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 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Load the pre-trained translation model
|
5 |
+
translator = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Translation function
|
8 |
+
def translate_text(text):
|
9 |
+
translation = translator(text, max_length=400)
|
10 |
+
return translation[0]['translation_text']
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Gradio interface
|
13 |
+
interface = gr.Interface(
|
14 |
+
fn=translate_text,
|
15 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter English text here..."),
|
16 |
+
outputs=gr.Textbox(lines=2, label="Urdu Translation"),
|
17 |
+
title="English to Urdu Translation",
|
18 |
+
description="Translate English text to Urdu using a pre-trained model."
|
19 |
+
)
|
20 |
|
21 |
+
# Launch the app
|
22 |
+
if __name__ == "__main__":
|
23 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|