import gradio as gr from deep_translator import GoogleTranslator # ── Language maps ────────────────────────────────────────────────────────────── LANGUAGES = { "Hindi 🇮🇳": "hi", "Spanish 🇪🇸": "es", "French 🇫🇷": "fr", "German 🇩🇪": "de", "Japanese 🇯🇵": "ja", "Chinese (Simplified) 🇨🇳": "zh-CN", "Arabic 🇸🇦": "ar", "Portuguese 🇧🇷": "pt", "Russian 🇷🇺": "ru", "Korean 🇰🇷": "ko", "Italian 🇮🇹": "it", "Tamil 🇮🇳": "ta", "Telugu 🇮🇳": "te", "Bengali 🇧🇩": "bn", "Marathi 🇮🇳": "mr", "Gujarati 🇮🇳": "gu", "Malayalam 🇮🇳": "ml", "Kannada 🇮🇳": "kn", "Punjabi 🇮🇳": "pa", "Urdu 🇵🇰": "ur", "Turkish 🇹🇷": "tr", "Dutch 🇳🇱": "nl", "Swedish 🇸🇪": "sv", "Polish 🇵🇱": "pl", "Greek 🇬🇷": "el", } SOURCE_LANGUAGES = {"English 🇬🇧": "en", "Auto Detect 🔍": "auto", **LANGUAGES} # ── Logic ───────────────────────────────────────────────────────────────────── def translate_text(text, src_name, tgt_name): if not text or not text.strip(): return "", "0 chars" src = SOURCE_LANGUAGES.get(src_name, "en") tgt = LANGUAGES.get(tgt_name, "hi") if src == tgt: return text, f"{len(text)} chars" try: result = GoogleTranslator(source=src, target=tgt).translate(text) return result, f"{len(result)} chars" except Exception as e: return f"⚠️ Error: {e}", "—" def swap_langs(src, tgt, inp, out): new_src = tgt if tgt in SOURCE_LANGUAGES else "English 🇬🇧" new_tgt = src if src in LANGUAGES else "Hindi 🇮🇳" return new_src, new_tgt, out or inp, "" def char_count(text): n = len(text) if text else 0 return f"{n} / 5000" # ── CSS ─────────────────────────────────────────────────────────────────────── CSS = """ .gradio-container { max-width: 900px !important; margin: 0 auto !important; padding-bottom: 2rem !important; } #header { text-align: center; padding: 1.8rem 0 0.5rem; } #header h1 { font-size: 2rem; font-weight: 700; margin: 0 0 0.25rem; background: linear-gradient(135deg, #818cf8, #c084fc); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } #header p { color: #94a3b8; font-size: 0.85rem; margin: 0; } #swap-btn { max-width: 80px !important; min-width: 80px !important; align-self: flex-end; margin-bottom: 2px; } #in-count textarea, #out-count textarea { border: none !important; background: transparent !important; color: #64748b !important; font-size: 0.75rem !important; text-align: right; padding: 2px 4px !important; resize: none !important; min-height: 0 !important; } #in-count, #out-count { border: none !important; box-shadow: none !important; } footer { display: none !important; } """ # ── UI ──────────────────────────────────────────────────────────────────────── with gr.Blocks(title="Universal Translator") as demo: gr.HTML(""" """) # Language selector row with gr.Row(equal_height=True): source_lang = gr.Dropdown( choices=list(SOURCE_LANGUAGES.keys()), value="English 🇬🇧", label="From", scale=5, ) swap_btn = gr.Button("⇄ Swap", elem_id="swap-btn", scale=1, variant="secondary") target_lang = gr.Dropdown( choices=list(LANGUAGES.keys()), value="Hindi 🇮🇳", label="To", scale=5, ) # Text panels with gr.Row(equal_height=True): with gr.Column(scale=1): input_text = gr.Textbox( label="Source text", placeholder="Type or paste text here…", lines=10, max_lines=20, ) in_count = gr.Textbox( value="0 / 5000", show_label=False, interactive=False, elem_id="in-count", container=False, lines=1, max_lines=1, ) with gr.Row(): clear_btn = gr.Button("🗑 Clear", variant="secondary", scale=1) translate_btn = gr.Button("Translate →", variant="primary", scale=3) with gr.Column(scale=1): output_text = gr.Textbox( label="Translation", placeholder="Translation appears here…", lines=10, max_lines=20, interactive=False, ) out_count = gr.Textbox( value="0 chars", show_label=False, interactive=False, elem_id="out-count", container=False, lines=1, max_lines=1, ) # Examples with gr.Accordion("📚 Example phrases", open=False): gr.Examples( examples=[ ["Hello, how are you doing today?"], ["India is a beautiful country with rich culture and heritage."], ["Technology is changing the world at a rapid pace."], ["Please help me find the nearest hospital."], ["I would like to order a coffee, please."], ["What time does the next train leave?"], ["Thank you very much for your kind assistance."], ["The quick brown fox jumps over the lazy dog."], ], inputs=input_text, ) # ── Events ──────────────────────────────────────────────────────────────── input_text.change(char_count, input_text, in_count) translate_btn.click( translate_text, [input_text, source_lang, target_lang], [output_text, out_count], ) input_text.submit( translate_text, [input_text, source_lang, target_lang], [output_text, out_count], ) source_lang.change( translate_text, [input_text, source_lang, target_lang], [output_text, out_count], ) target_lang.change( translate_text, [input_text, source_lang, target_lang], [output_text, out_count], ) clear_btn.click( lambda: ("", "", "0 / 5000", "0 chars"), outputs=[input_text, output_text, in_count, out_count], ) swap_btn.click( swap_langs, [source_lang, target_lang, input_text, output_text], [source_lang, target_lang, input_text, output_text], ) demo.launch( server_name="0.0.0.0", theme=gr.themes.Soft( primary_hue="indigo", secondary_hue="violet", neutral_hue="slate", ), css=CSS, )