Translator / app.py
Chauhanaditya's picture
Add Universal Translator app with Gradio UI
a56058f
Raw History Blame Contribute Delete
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 &middot; Auto-detect &middot; Instant swap &middot; 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,
)