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