Chauhanaditya commited on
Commit
a56058f
ยท
1 Parent(s): dd93435

Add Universal Translator app with Gradio UI

Browse files
Files changed (2) hide show
  1. app.py +239 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 &middot; Auto-detect &middot; Instant swap &middot; 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