Spaces:
Sleeping
Sleeping
Download app.py from DakuSir/Translator: direct link, hf CLI and curl.
- Browser
- Download file 7.72 kB
-
https://huggingface.co/spaces/DakuSir/Translator/resolve/main/app.py
- Command line
-
hf download hf://spaces/DakuSir/Translator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DakuSir/Translator/resolve/main/app.py
7.72 kB
| 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(""" | |
| <div id="header"> | |
| <h1>๐ Universal Translator</h1> | |
| <p>25+ languages · Auto-detect · Instant swap · Powered by Google Translate</p> | |
| </div> | |
| """) | |
| # 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, | |
| ) |