Commit
·
55a67a5
1
Parent(s):
85c04da
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
# Load pre-trained
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Define the
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Create Gradio interface
|
| 15 |
iface = gr.Interface(
|
| 16 |
-
fn=
|
| 17 |
inputs=gr.Textbox(),
|
| 18 |
outputs=gr.Textbox(),
|
| 19 |
live=True,
|
| 20 |
-
title="
|
| 21 |
-
description="
|
| 22 |
)
|
| 23 |
|
| 24 |
# Launch the Gradio app
|
| 25 |
iface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
|
| 4 |
+
# Load pre-trained MarianMT model and tokenizer for translation
|
| 5 |
+
model_name = "Helsinki-NLP/opus-mt-en-de"
|
| 6 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 7 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
# Define the translation function
|
| 10 |
+
def translate_text(text):
|
| 11 |
+
# Tokenize the input text
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Perform translation
|
| 15 |
+
translation = model.generate(**inputs)
|
| 16 |
+
|
| 17 |
+
# Decode the translated text
|
| 18 |
+
translated_text = tokenizer.decode(translation[0], skip_special_tokens=True)
|
| 19 |
+
|
| 20 |
+
return translated_text
|
| 21 |
|
| 22 |
# Create Gradio interface
|
| 23 |
iface = gr.Interface(
|
| 24 |
+
fn=translate_text,
|
| 25 |
inputs=gr.Textbox(),
|
| 26 |
outputs=gr.Textbox(),
|
| 27 |
live=True,
|
| 28 |
+
title="Language Translation App",
|
| 29 |
+
description="Translate text from English to German using MarianMT.",
|
| 30 |
)
|
| 31 |
|
| 32 |
# Launch the Gradio app
|
| 33 |
iface.launch()
|
| 34 |
+
|