Spaces:
Running
Running
Commit
·
74d93ae
1
Parent(s):
a7915c4
remove useless models
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Initialize translation pipelines
|
@@ -12,19 +12,9 @@ en_to_zh = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
|
|
12 |
zh_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
13 |
fr_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-es")
|
14 |
es_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-es-fr")
|
15 |
-
fr_to_zh = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-zh")
|
16 |
-
zh_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-fr")
|
17 |
-
fr_to_ja = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-jap")
|
18 |
-
ja_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-jap-fr")
|
19 |
-
es_to_ja = pipeline("translation", model="Helsinki-NLP/opus-mt-es-jap")
|
20 |
-
ja_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-jap-es")
|
21 |
-
es_to_zh = pipeline("translation", model="Helsinki-NLP/opus-mt-es-zh")
|
22 |
-
zh_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-es")
|
23 |
-
ja_to_zh = pipeline("translation", model="Helsinki-NLP/opus-mt-jap-zh")
|
24 |
-
zh_to_ja = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-jap")
|
25 |
|
26 |
# Dictionary to map language pairs to translation pipelines
|
27 |
-
|
28 |
"English-French": en_to_fr,
|
29 |
"French-English": fr_to_en,
|
30 |
"English-Japanese": en_to_ja,
|
@@ -34,23 +24,53 @@ translators = {
|
|
34 |
"English-Mandarin": en_to_zh,
|
35 |
"Mandarin-English": zh_to_en,
|
36 |
"French-Spanish": fr_to_es,
|
37 |
-
"Spanish-French": es_to_fr
|
38 |
-
"French-Mandarin": fr_to_zh,
|
39 |
-
"Mandarin-French": zh_to_fr,
|
40 |
-
"French-Japanese": fr_to_ja,
|
41 |
-
"Japanese-French": ja_to_fr,
|
42 |
-
"Spanish-Japanese": es_to_ja,
|
43 |
-
"Japanese-Spanish": ja_to_es,
|
44 |
-
"Spanish-Mandarin": es_to_zh,
|
45 |
-
"Mandarin-Spanish": zh_to_es,
|
46 |
-
"Japanese-Mandarin": ja_to_zh,
|
47 |
-
"Mandarin-Japanese": zh_to_ja
|
48 |
}
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def get_translator(source_lang, target_lang):
|
51 |
-
"""Get the appropriate translation
|
52 |
key = f"{source_lang}-{target_lang}"
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def translate(text, source_lang, target_lang):
|
56 |
"""Translate text from source language to target language."""
|
@@ -62,7 +82,7 @@ def translate(text, source_lang, target_lang):
|
|
62 |
|
63 |
translator = get_translator(source_lang, target_lang)
|
64 |
if translator:
|
65 |
-
return translator(text)
|
66 |
else:
|
67 |
return f"Translation from {source_lang} to {target_lang} is not supported."
|
68 |
|
@@ -78,6 +98,7 @@ examples = {
|
|
78 |
# Create Gradio interface
|
79 |
with gr.Blocks() as demo:
|
80 |
gr.Markdown("# Multilingual Translation")
|
|
|
81 |
|
82 |
with gr.Row():
|
83 |
source_lang = gr.Dropdown(
|
@@ -99,6 +120,21 @@ with gr.Blocks() as demo:
|
|
99 |
with gr.Column():
|
100 |
target_text = gr.Textbox(label="Translation Result", lines=5)
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
# Set up examples based on source language
|
103 |
example_list = gr.Examples(
|
104 |
examples=examples["English"],
|
@@ -111,6 +147,19 @@ with gr.Blocks() as demo:
|
|
111 |
|
112 |
source_lang.change(update_examples, inputs=source_lang, outputs=example_list)
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Set up translation function
|
115 |
translate_btn.click(
|
116 |
translate,
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Initialize translation pipelines
|
|
|
12 |
zh_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
13 |
fr_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-es")
|
14 |
es_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-es-fr")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Dictionary to map language pairs to translation pipelines
|
17 |
+
direct_translators = {
|
18 |
"English-French": en_to_fr,
|
19 |
"French-English": fr_to_en,
|
20 |
"English-Japanese": en_to_ja,
|
|
|
24 |
"English-Mandarin": en_to_zh,
|
25 |
"Mandarin-English": zh_to_en,
|
26 |
"French-Spanish": fr_to_es,
|
27 |
+
"Spanish-French": es_to_fr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
+
# Two-step translation pairs (using English as pivot)
|
31 |
+
two_step_pairs = [
|
32 |
+
"French-Mandarin", "Mandarin-French",
|
33 |
+
"French-Japanese", "Japanese-French",
|
34 |
+
"Spanish-Japanese", "Japanese-Spanish",
|
35 |
+
"Spanish-Mandarin", "Mandarin-Spanish",
|
36 |
+
"Japanese-Mandarin", "Mandarin-Japanese"
|
37 |
+
]
|
38 |
+
|
39 |
+
def translate_two_step(text, source_lang, target_lang):
|
40 |
+
"""Perform translation in two steps using English as an intermediate language."""
|
41 |
+
# First step: translate to English
|
42 |
+
source_to_en_key = f"{source_lang}-English"
|
43 |
+
source_to_en = direct_translators.get(source_to_en_key)
|
44 |
+
if not source_to_en:
|
45 |
+
return f"Error: Cannot translate from {source_lang} to English."
|
46 |
+
|
47 |
+
# Translate source to English
|
48 |
+
english_text = source_to_en(text)[0]["translation_text"]
|
49 |
+
|
50 |
+
# Second step: translate from English to target
|
51 |
+
en_to_target_key = f"English-{target_lang}"
|
52 |
+
en_to_target = direct_translators.get(en_to_target_key)
|
53 |
+
if not en_to_target:
|
54 |
+
return f"Error: Cannot translate from English to {target_lang}."
|
55 |
+
|
56 |
+
# Translate English to target
|
57 |
+
return en_to_target(english_text)[0]["translation_text"]
|
58 |
+
|
59 |
def get_translator(source_lang, target_lang):
|
60 |
+
"""Get the appropriate translation method based on source and target languages."""
|
61 |
key = f"{source_lang}-{target_lang}"
|
62 |
+
|
63 |
+
# Direct translation available
|
64 |
+
if key in direct_translators:
|
65 |
+
return lambda text: direct_translators[key](text)[0]["translation_text"]
|
66 |
+
|
67 |
+
# Two-step translation needed
|
68 |
+
elif key in two_step_pairs:
|
69 |
+
return lambda text: translate_two_step(text, source_lang, target_lang)
|
70 |
+
|
71 |
+
# No translation path available
|
72 |
+
else:
|
73 |
+
return None
|
74 |
|
75 |
def translate(text, source_lang, target_lang):
|
76 |
"""Translate text from source language to target language."""
|
|
|
82 |
|
83 |
translator = get_translator(source_lang, target_lang)
|
84 |
if translator:
|
85 |
+
return translator(text)
|
86 |
else:
|
87 |
return f"Translation from {source_lang} to {target_lang} is not supported."
|
88 |
|
|
|
98 |
# Create Gradio interface
|
99 |
with gr.Blocks() as demo:
|
100 |
gr.Markdown("# Multilingual Translation")
|
101 |
+
gr.Markdown("### Some language pairs use two-step translation via English")
|
102 |
|
103 |
with gr.Row():
|
104 |
source_lang = gr.Dropdown(
|
|
|
120 |
with gr.Column():
|
121 |
target_text = gr.Textbox(label="Translation Result", lines=5)
|
122 |
|
123 |
+
translation_info = gr.Markdown("")
|
124 |
+
|
125 |
+
# Function to update translation path information
|
126 |
+
def update_translation_info(source, target):
|
127 |
+
if source == target:
|
128 |
+
return "No translation needed (same language)"
|
129 |
+
|
130 |
+
key = f"{source}-{target}"
|
131 |
+
if key in direct_translators:
|
132 |
+
return f"Using direct translation from {source} to {target}"
|
133 |
+
elif key in two_step_pairs:
|
134 |
+
return f"Using two-step translation: {source} → English → {target}"
|
135 |
+
else:
|
136 |
+
return f"Translation from {source} to {target} is not supported"
|
137 |
+
|
138 |
# Set up examples based on source language
|
139 |
example_list = gr.Examples(
|
140 |
examples=examples["English"],
|
|
|
147 |
|
148 |
source_lang.change(update_examples, inputs=source_lang, outputs=example_list)
|
149 |
|
150 |
+
# Update translation path info when languages change
|
151 |
+
source_lang.change(
|
152 |
+
update_translation_info,
|
153 |
+
inputs=[source_lang, target_lang],
|
154 |
+
outputs=translation_info
|
155 |
+
)
|
156 |
+
|
157 |
+
target_lang.change(
|
158 |
+
update_translation_info,
|
159 |
+
inputs=[source_lang, target_lang],
|
160 |
+
outputs=translation_info
|
161 |
+
)
|
162 |
+
|
163 |
# Set up translation function
|
164 |
translate_btn.click(
|
165 |
translate,
|