Spaces:
Running
Running
File size: 11,552 Bytes
74d93ae a7915c4 b90039b a7915c4 4422b7f a7915c4 74d93ae a7915c4 74d93ae a7915c4 74d93ae 17a461e 74d93ae a7915c4 74d93ae a7915c4 74d93ae a7915c4 1d3f3e3 a7915c4 1d3f3e3 a7915c4 74d93ae a7915c4 b90039b 5aac13a 17a461e 5aac13a 17a461e 5aac13a 17a461e 5aac13a 17a461e 5aac13a 17a461e 5aac13a 17a461e db9df29 17a461e b90039b 1d3f3e3 17a461e a7915c4 17a461e a7915c4 17a461e db9df29 17a461e 74d93ae b90039b 17a461e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import gradio as gr
from transformers import pipeline
# Initialize translation pipelines
en_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
fr_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
en_to_ja = pipeline("translation", model="Helsinki-NLP/opus-mt-en-jap")
ja_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-jap-en")
en_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
es_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en")
en_to_zh = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
zh_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
fr_to_es = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-es")
es_to_fr = pipeline("translation", model="Helsinki-NLP/opus-mt-es-fr")
# Dictionary to map language pairs to translation pipelines
direct_translators = {
"English-French": en_to_fr,
"French-English": fr_to_en,
"English-Japanese": en_to_ja,
"Japanese-English": ja_to_en,
"English-Spanish": en_to_es,
"Spanish-English": es_to_en,
"English-Mandarin": en_to_zh,
"Mandarin-English": zh_to_en,
"French-Spanish": fr_to_es,
"Spanish-French": es_to_fr
}
# Two-step translation pairs (using English as pivot)
two_step_pairs = [
"French-Mandarin", "Mandarin-French",
"French-Japanese", "Japanese-French",
"Spanish-Japanese", "Japanese-Spanish",
"Spanish-Mandarin", "Mandarin-Spanish",
"Japanese-Mandarin", "Mandarin-Japanese"
]
# Language emoji mapping for visual appeal
language_emojis = {
"English": "🇬🇧",
"French": "🇫🇷",
"Japanese": "🇯🇵",
"Spanish": "🇪🇸",
"Mandarin": "🇨🇳"
}
def translate_two_step(text, source_lang, target_lang):
"""Perform translation in two steps using English as an intermediate language."""
# First step: translate to English
source_to_en_key = f"{source_lang}-English"
source_to_en = direct_translators.get(source_to_en_key)
if not source_to_en:
return f"Error: Cannot translate from {source_lang} to English."
# Translate source to English
english_text = source_to_en(text)[0]["translation_text"]
# Second step: translate from English to target
en_to_target_key = f"English-{target_lang}"
en_to_target = direct_translators.get(en_to_target_key)
if not en_to_target:
return f"Error: Cannot translate from English to {target_lang}."
# Translate English to target
return en_to_target(english_text)[0]["translation_text"]
def get_translator(source_lang, target_lang):
"""Get the appropriate translation method based on source and target languages."""
key = f"{source_lang}-{target_lang}"
# Direct translation available
if key in direct_translators:
return lambda text: direct_translators[key](text)[0]["translation_text"]
# Two-step translation needed
elif key in two_step_pairs:
return lambda text: translate_two_step(text, source_lang, target_lang)
# No translation path available
else:
return None
def translate(text, source_lang, target_lang):
"""Translate text from source language to target language."""
if not text:
return ""
if source_lang == target_lang:
return text
translator = get_translator(source_lang, target_lang)
if translator:
return translator(text)
else:
return f"Translation from {source_lang} to {target_lang} is not supported."
def update_translation_info(source, target):
"""Update information about the translation path with friendly, colorful message."""
if source == target:
return f"<div style='padding: 10px; background-color: #ffe6e6; border-radius: 8px; text-align: center;'>✨ Both languages are the same! No translation needed. ✨</div>"
key = f"{source}-{target}"
emoji_source = language_emojis.get(source, "")
emoji_target = language_emojis.get(target, "")
if key in direct_translators:
return f"<div style='padding: 10px; background-color: #e6ffe6; border-radius: 8px; text-align: center;'>✅ Direct translation: {emoji_source} {source} → {emoji_target} {target}</div>"
elif key in two_step_pairs:
return f"<div style='padding: 10px; background-color: #e6f2ff; border-radius: 8px; text-align: center;'>🔄 Two-step translation: {emoji_source} {source} → 🇬🇧 English → {emoji_target} {target}</div>"
else:
return f"<div style='padding: 10px; background-color: #fff2e6; border-radius: 8px; text-align: center;'>⚠️ Translation from {emoji_source} {source} to {emoji_target} {target} is not supported</div>"
def swap_languages(source, target):
"""Swap source and target languages."""
return target, source
# Custom CSS
custom_css = """
.gradio-container {
background: linear-gradient(135deg, #f5f7fa 0%, #e4ecfb 100%);
}
.main-div {
border-radius: 20px;
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
background-color: white;
padding: 20px;
margin: 10px;
}
.title {
text-align: center;
color: #4a69bd;
font-size: 2.5em;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
.subtitle {
text-align: center;
color: #6a89cc;
margin-bottom: 20px;
}
.translate-button {
background-color: #4a69bd !important;
color: white !important;
border-radius: 30px !important;
padding: 10px 20px !important;
font-size: 1.2em !important;
transition: transform 0.3s ease !important;
}
.translate-button:hover {
transform: scale(1.05) !important;
background-color: #6a89cc !important;
}
.swap-button {
background-color: #fbc531 !important;
border-radius: 50% !important;
width: 50px !important;
height: 50px !important;
padding: 0 !important;
display: flex !important;
justify-content: center !important;
align-items: center !important;
transition: transform 0.3s ease !important;
}
.swap-button:hover {
transform: rotate(180deg) !important;
}
.language-select {
border-radius: 10px !important;
border: 2px solid #b2bec3 !important;
}
.text-area {
border-radius: 10px !important;
border: 2px solid #b2bec3 !important;
font-size: 1.1em !important;
}
.result-area {
border-radius: 10px !important;
border: 2px solid #6a89cc !important;
background-color: #f8f9fa !important;
font-size: 1.1em !important;
}
.footer {
text-align: center;
margin-top: 20px;
color: #7f8c8d;
font-size: 0.9em;
}
.examples-container {
margin-top: 20px;
padding: 15px;
background-color: #f8f9fa;
border-radius: 10px;
border: 2px dashed #b2bec3;
}
"""
# Create Gradio interface
with gr.Blocks(css=custom_css) as demo:
with gr.Column(elem_classes="main-div"):
gr.Markdown("# 🌍 Happy Translator 🌎", elem_classes="title")
gr.Markdown("### Connect with the world through language! ✨", elem_classes="subtitle")
with gr.Row():
with gr.Column(scale=2):
source_lang = gr.Dropdown(
choices=["English", "French", "Japanese", "Spanish", "Mandarin"],
label=f"From Language",
value="English",
elem_classes="language-select"
)
with gr.Column(scale=1):
swap_btn = gr.Button("🔄", elem_classes="swap-button")
with gr.Column(scale=2):
target_lang = gr.Dropdown(
choices=["English", "French", "Japanese", "Spanish", "Mandarin"],
label=f"To Language",
value="French",
elem_classes="language-select"
)
translation_info = gr.HTML()
# Update translation path info when languages change
source_lang.change(
update_translation_info,
inputs=[source_lang, target_lang],
outputs=translation_info
)
target_lang.change(
update_translation_info,
inputs=[source_lang, target_lang],
outputs=translation_info
)
# Handle language swap
swap_btn.click(
swap_languages,
inputs=[source_lang, target_lang],
outputs=[source_lang, target_lang]
).then(
update_translation_info,
inputs=[source_lang, target_lang],
outputs=translation_info
)
with gr.Row():
with gr.Column():
source_text = gr.Textbox(
label="Type your text here",
placeholder="Enter text to translate...",
lines=5,
elem_classes="text-area"
)
with gr.Column():
target_text = gr.Textbox(
label="Translation Result",
lines=5,
elem_classes="result-area"
)
translate_btn = gr.Button("✨ Translate ✨", elem_classes="translate-button")
# Fun greeting examples with language-appropriate phrases
with gr.Column(elem_classes="examples-container"):
gr.Markdown("### Try these fun examples! 🎉", elem_classes="subtitle")
examples = [
["Hello, how are you today? I'm having a wonderful day!"],
["I would like to visit Japan someday and see the cherry blossoms."],
["Bonjour, comment allez-vous aujourd'hui? Je passe une journée merveilleuse!"],
["J'aimerais visiter le Japon un jour et voir les cerisiers en fleurs."],
["こんにちは、今日はお元気ですか?素晴らしい一日を過ごしています!"],
["いつか日本を訪れて桜を見たいです。"],
["Hola, ¿cómo estás hoy? ¡Estoy teniendo un día maravilloso!"],
["Me gustaría visitar Japón algún día y ver los cerezos en flor."],
["你好,今天好吗?我今天过得很愉快!"],
["我希望有一天能去日本看樱花。"]
]
gr.Examples(
examples=examples,
inputs=source_text
)
# Set up translation function
translate_btn.click(
translate,
inputs=[source_text, source_lang, target_lang],
outputs=target_text
)
# Also translate when Enter is pressed in the source text box
source_text.submit(
translate,
inputs=[source_text, source_lang, target_lang],
outputs=target_text
)
gr.Markdown("### Made with ❤️ for language lovers everywhere", elem_classes="footer")
# Display initial translation info on load
demo.load(
update_translation_info,
inputs=[source_lang, target_lang],
outputs=translation_info
)
if __name__ == "__main__":
demo.launch(share=True) |