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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from deep_translator import GoogleTranslator
|
| 3 |
+
|
| 4 |
+
# โโ Language maps โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 5 |
+
LANGUAGES = {
|
| 6 |
+
"Hindi ๐ฎ๐ณ": "hi",
|
| 7 |
+
"Spanish ๐ช๐ธ": "es",
|
| 8 |
+
"French ๐ซ๐ท": "fr",
|
| 9 |
+
"German ๐ฉ๐ช": "de",
|
| 10 |
+
"Japanese ๐ฏ๐ต": "ja",
|
| 11 |
+
"Chinese (Simplified) ๐จ๐ณ": "zh-CN",
|
| 12 |
+
"Arabic ๐ธ๐ฆ": "ar",
|
| 13 |
+
"Portuguese ๐ง๐ท": "pt",
|
| 14 |
+
"Russian ๐ท๐บ": "ru",
|
| 15 |
+
"Korean ๐ฐ๐ท": "ko",
|
| 16 |
+
"Italian ๐ฎ๐น": "it",
|
| 17 |
+
"Tamil ๐ฎ๐ณ": "ta",
|
| 18 |
+
"Telugu ๐ฎ๐ณ": "te",
|
| 19 |
+
"Bengali ๐ง๐ฉ": "bn",
|
| 20 |
+
"Marathi ๐ฎ๐ณ": "mr",
|
| 21 |
+
"Gujarati ๐ฎ๐ณ": "gu",
|
| 22 |
+
"Malayalam ๐ฎ๐ณ": "ml",
|
| 23 |
+
"Kannada ๐ฎ๐ณ": "kn",
|
| 24 |
+
"Punjabi ๐ฎ๐ณ": "pa",
|
| 25 |
+
"Urdu ๐ต๐ฐ": "ur",
|
| 26 |
+
"Turkish ๐น๐ท": "tr",
|
| 27 |
+
"Dutch ๐ณ๐ฑ": "nl",
|
| 28 |
+
"Swedish ๐ธ๐ช": "sv",
|
| 29 |
+
"Polish ๐ต๐ฑ": "pl",
|
| 30 |
+
"Greek ๐ฌ๐ท": "el",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
SOURCE_LANGUAGES = {"English ๐ฌ๐ง": "en", "Auto Detect ๐": "auto", **LANGUAGES}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# โโ Logic โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 37 |
+
def translate_text(text, src_name, tgt_name):
|
| 38 |
+
if not text or not text.strip():
|
| 39 |
+
return "", "0 chars"
|
| 40 |
+
src = SOURCE_LANGUAGES.get(src_name, "en")
|
| 41 |
+
tgt = LANGUAGES.get(tgt_name, "hi")
|
| 42 |
+
if src == tgt:
|
| 43 |
+
return text, f"{len(text)} chars"
|
| 44 |
+
try:
|
| 45 |
+
result = GoogleTranslator(source=src, target=tgt).translate(text)
|
| 46 |
+
return result, f"{len(result)} chars"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"โ ๏ธ Error: {e}", "โ"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def swap_langs(src, tgt, inp, out):
|
| 52 |
+
new_src = tgt if tgt in SOURCE_LANGUAGES else "English ๐ฌ๐ง"
|
| 53 |
+
new_tgt = src if src in LANGUAGES else "Hindi ๐ฎ๐ณ"
|
| 54 |
+
return new_src, new_tgt, out or inp, ""
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def char_count(text):
|
| 58 |
+
n = len(text) if text else 0
|
| 59 |
+
return f"{n} / 5000"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# โโ CSS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 63 |
+
CSS = """
|
| 64 |
+
.gradio-container {
|
| 65 |
+
max-width: 900px !important;
|
| 66 |
+
margin: 0 auto !important;
|
| 67 |
+
padding-bottom: 2rem !important;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
#header {
|
| 71 |
+
text-align: center;
|
| 72 |
+
padding: 1.8rem 0 0.5rem;
|
| 73 |
+
}
|
| 74 |
+
#header h1 {
|
| 75 |
+
font-size: 2rem;
|
| 76 |
+
font-weight: 700;
|
| 77 |
+
margin: 0 0 0.25rem;
|
| 78 |
+
background: linear-gradient(135deg, #818cf8, #c084fc);
|
| 79 |
+
-webkit-background-clip: text;
|
| 80 |
+
-webkit-text-fill-color: transparent;
|
| 81 |
+
}
|
| 82 |
+
#header p {
|
| 83 |
+
color: #94a3b8;
|
| 84 |
+
font-size: 0.85rem;
|
| 85 |
+
margin: 0;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
#swap-btn {
|
| 89 |
+
max-width: 80px !important;
|
| 90 |
+
min-width: 80px !important;
|
| 91 |
+
align-self: flex-end;
|
| 92 |
+
margin-bottom: 2px;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
#in-count textarea,
|
| 96 |
+
#out-count textarea {
|
| 97 |
+
border: none !important;
|
| 98 |
+
background: transparent !important;
|
| 99 |
+
color: #64748b !important;
|
| 100 |
+
font-size: 0.75rem !important;
|
| 101 |
+
text-align: right;
|
| 102 |
+
padding: 2px 4px !important;
|
| 103 |
+
resize: none !important;
|
| 104 |
+
min-height: 0 !important;
|
| 105 |
+
}
|
| 106 |
+
#in-count, #out-count {
|
| 107 |
+
border: none !important;
|
| 108 |
+
box-shadow: none !important;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
footer { display: none !important; }
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# โโ UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 116 |
+
with gr.Blocks(title="Universal Translator") as demo:
|
| 117 |
+
|
| 118 |
+
gr.HTML("""
|
| 119 |
+
<div id="header">
|
| 120 |
+
<h1>๐ Universal Translator</h1>
|
| 121 |
+
<p>25+ languages · Auto-detect · Instant swap · Powered by Google Translate</p>
|
| 122 |
+
</div>
|
| 123 |
+
""")
|
| 124 |
+
|
| 125 |
+
# Language selector row
|
| 126 |
+
with gr.Row(equal_height=True):
|
| 127 |
+
source_lang = gr.Dropdown(
|
| 128 |
+
choices=list(SOURCE_LANGUAGES.keys()),
|
| 129 |
+
value="English ๐ฌ๐ง",
|
| 130 |
+
label="From",
|
| 131 |
+
scale=5,
|
| 132 |
+
)
|
| 133 |
+
swap_btn = gr.Button("โ Swap", elem_id="swap-btn", scale=1, variant="secondary")
|
| 134 |
+
target_lang = gr.Dropdown(
|
| 135 |
+
choices=list(LANGUAGES.keys()),
|
| 136 |
+
value="Hindi ๐ฎ๐ณ",
|
| 137 |
+
label="To",
|
| 138 |
+
scale=5,
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Text panels
|
| 142 |
+
with gr.Row(equal_height=True):
|
| 143 |
+
with gr.Column(scale=1):
|
| 144 |
+
input_text = gr.Textbox(
|
| 145 |
+
label="Source text",
|
| 146 |
+
placeholder="Type or paste text hereโฆ",
|
| 147 |
+
lines=10,
|
| 148 |
+
max_lines=20,
|
| 149 |
+
)
|
| 150 |
+
in_count = gr.Textbox(
|
| 151 |
+
value="0 / 5000",
|
| 152 |
+
show_label=False,
|
| 153 |
+
interactive=False,
|
| 154 |
+
elem_id="in-count",
|
| 155 |
+
container=False,
|
| 156 |
+
lines=1,
|
| 157 |
+
max_lines=1,
|
| 158 |
+
)
|
| 159 |
+
with gr.Row():
|
| 160 |
+
clear_btn = gr.Button("๐ Clear", variant="secondary", scale=1)
|
| 161 |
+
translate_btn = gr.Button("Translate โ", variant="primary", scale=3)
|
| 162 |
+
|
| 163 |
+
with gr.Column(scale=1):
|
| 164 |
+
output_text = gr.Textbox(
|
| 165 |
+
label="Translation",
|
| 166 |
+
placeholder="Translation appears hereโฆ",
|
| 167 |
+
lines=10,
|
| 168 |
+
max_lines=20,
|
| 169 |
+
interactive=False,
|
| 170 |
+
)
|
| 171 |
+
out_count = gr.Textbox(
|
| 172 |
+
value="0 chars",
|
| 173 |
+
show_label=False,
|
| 174 |
+
interactive=False,
|
| 175 |
+
elem_id="out-count",
|
| 176 |
+
container=False,
|
| 177 |
+
lines=1,
|
| 178 |
+
max_lines=1,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
# Examples
|
| 182 |
+
with gr.Accordion("๐ Example phrases", open=False):
|
| 183 |
+
gr.Examples(
|
| 184 |
+
examples=[
|
| 185 |
+
["Hello, how are you doing today?"],
|
| 186 |
+
["India is a beautiful country with rich culture and heritage."],
|
| 187 |
+
["Technology is changing the world at a rapid pace."],
|
| 188 |
+
["Please help me find the nearest hospital."],
|
| 189 |
+
["I would like to order a coffee, please."],
|
| 190 |
+
["What time does the next train leave?"],
|
| 191 |
+
["Thank you very much for your kind assistance."],
|
| 192 |
+
["The quick brown fox jumps over the lazy dog."],
|
| 193 |
+
],
|
| 194 |
+
inputs=input_text,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# โโ Events โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 198 |
+
input_text.change(char_count, input_text, in_count)
|
| 199 |
+
|
| 200 |
+
translate_btn.click(
|
| 201 |
+
translate_text,
|
| 202 |
+
[input_text, source_lang, target_lang],
|
| 203 |
+
[output_text, out_count],
|
| 204 |
+
)
|
| 205 |
+
input_text.submit(
|
| 206 |
+
translate_text,
|
| 207 |
+
[input_text, source_lang, target_lang],
|
| 208 |
+
[output_text, out_count],
|
| 209 |
+
)
|
| 210 |
+
source_lang.change(
|
| 211 |
+
translate_text,
|
| 212 |
+
[input_text, source_lang, target_lang],
|
| 213 |
+
[output_text, out_count],
|
| 214 |
+
)
|
| 215 |
+
target_lang.change(
|
| 216 |
+
translate_text,
|
| 217 |
+
[input_text, source_lang, target_lang],
|
| 218 |
+
[output_text, out_count],
|
| 219 |
+
)
|
| 220 |
+
clear_btn.click(
|
| 221 |
+
lambda: ("", "", "0 / 5000", "0 chars"),
|
| 222 |
+
outputs=[input_text, output_text, in_count, out_count],
|
| 223 |
+
)
|
| 224 |
+
swap_btn.click(
|
| 225 |
+
swap_langs,
|
| 226 |
+
[source_lang, target_lang, input_text, output_text],
|
| 227 |
+
[source_lang, target_lang, input_text, output_text],
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
demo.launch(
|
| 232 |
+
server_name="0.0.0.0",
|
| 233 |
+
theme=gr.themes.Soft(
|
| 234 |
+
primary_hue="indigo",
|
| 235 |
+
secondary_hue="violet",
|
| 236 |
+
neutral_hue="slate",
|
| 237 |
+
),
|
| 238 |
+
css=CSS,
|
| 239 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
deep-translator
|
| 2 |
+
gradio
|