Spaces:
Sleeping
Sleeping
lodhrangpt
commited on
Commit
•
54473e1
1
Parent(s):
b2ffe46
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
3 |
|
4 |
-
#
|
5 |
-
model_name = "Helsinki-NLP/opus-mt-en-fr" # Example model for English to French
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
8 |
-
|
9 |
-
# Define the translation function
|
10 |
-
def translate_text(text, target_language):
|
11 |
-
model_name = f"Helsinki-NLP/opus-mt-en-{target_language}"
|
12 |
-
translator = pipeline("translation", model=model_name)
|
13 |
-
translation = translator(text)[0]['translation_text']
|
14 |
-
return translation
|
15 |
-
|
16 |
-
# Define available languages
|
17 |
languages = {
|
18 |
-
"english": "
|
19 |
-
"spanish": "
|
20 |
-
"french": "
|
21 |
-
"german": "
|
22 |
-
"chinese": "
|
23 |
-
"japanese": "
|
24 |
-
"korean": "
|
25 |
-
"italian": "
|
26 |
-
"portuguese": "
|
27 |
-
"russian": "
|
28 |
-
"hindi": "
|
29 |
-
"arabic": "
|
30 |
-
"dutch": "
|
31 |
-
"turkish": "
|
32 |
-
"greek": "
|
33 |
-
"urdu": "
|
34 |
}
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Set up the Gradio interface with a submit button and side-by-side layout
|
38 |
with gr.Blocks() as iface:
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
3 |
|
4 |
+
# Define available languages with their corresponding model suffixes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
languages = {
|
6 |
+
"english": "en",
|
7 |
+
"spanish": "es",
|
8 |
+
"french": "fr",
|
9 |
+
"german": "de",
|
10 |
+
"chinese": "zh",
|
11 |
+
"japanese": "ja",
|
12 |
+
"korean": "ko",
|
13 |
+
"italian": "it",
|
14 |
+
"portuguese": "pt",
|
15 |
+
"russian": "ru",
|
16 |
+
"hindi": "hi",
|
17 |
+
"arabic": "ar",
|
18 |
+
"dutch": "nl",
|
19 |
+
"turkish": "tr",
|
20 |
+
"greek": "el",
|
21 |
+
"urdu": "ur"
|
22 |
}
|
23 |
|
24 |
+
# Define the translation function
|
25 |
+
def translate_text(text, target_language):
|
26 |
+
# Get the correct model name based on the target language
|
27 |
+
target_language_code = languages.get(target_language.lower())
|
28 |
+
|
29 |
+
if target_language_code:
|
30 |
+
model_name = f"Helsinki-NLP/opus-mt-en-{target_language_code}"
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
32 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
33 |
+
translator = pipeline("translation", model=model, tokenizer=tokenizer)
|
34 |
+
translation = translator(text)[0]['translation_text']
|
35 |
+
return translation
|
36 |
+
else:
|
37 |
+
return "Error: Language not supported or incorrect."
|
38 |
|
39 |
# Set up the Gradio interface with a submit button and side-by-side layout
|
40 |
with gr.Blocks() as iface:
|