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("""
25+ languages · Auto-detect · Instant swap · Powered by Google Translate