VectorV17 commited on
Commit
9acf1a2
·
verified ·
1 Parent(s): 61e73de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -108
app.py CHANGED
@@ -4,10 +4,10 @@ import nltk
4
  from deep_translator import GoogleTranslator, MyMemoryTranslator
5
  from langdetect import DetectorFactory, detect
6
 
7
- # Enforce deterministic language tracking
8
  DetectorFactory.seed = 0
9
 
10
- # Ensure NLTK resources are available
11
  for resource in ['punkt', 'averaged_perceptron_tagger', 'punkt_tab']:
12
  try:
13
  if 'punkt' in resource:
@@ -17,16 +17,15 @@ for resource in ['punkt', 'averaged_perceptron_tagger', 'punkt_tab']:
17
  except LookupError:
18
  nltk.download(resource, quiet=True)
19
 
20
- # Language Mapping
21
  LANGUAGES = {
22
- 'Auto-Detect': 'auto',
23
  'English': 'en',
24
  'Hindi (हिंदी)': 'hi',
25
  'Spanish (Español)': 'es',
26
  'French (Français)': 'fr',
27
  'German (Deutsch)': 'de',
28
  'Chinese (Simplified)': 'zh-CN',
29
- 'Chinese (Traditional)': 'zh-TW',
30
  'Japanese (日本語)': 'ja',
31
  'Malay': 'ms',
32
  'Arabic': 'ar',
@@ -44,13 +43,12 @@ def execute_agent_task(
44
  text, source_lang_name, target_lang_name, service_name, action
45
  ):
46
  if not text or not text.strip():
47
- return "Input text space cannot be empty.", "Error"
48
 
49
  source_lang = LANGUAGES.get(source_lang_name, 'auto')
50
  target_lang = LANGUAGES.get(target_lang_name, 'en')
51
 
52
  try:
53
- # 1. Translation System
54
  if action == 'translate':
55
  if service_name == 'MyMemory Shared API Network':
56
  src = 'en' if source_lang == 'auto' else source_lang
@@ -61,18 +59,14 @@ def execute_agent_task(
61
  translator = GoogleTranslator(
62
  source=source_lang, target=target_lang
63
  )
 
64
 
65
- result = translator.translate(text)
66
- return result, 'Translation'
67
-
68
- # 2. Transliteration System
69
  elif action == 'transliterate':
70
  result = anyascii.anyascii(text)
71
  if result.lower() == text.lower():
72
  result = f'[Note: Text is already in Roman/Latin script form]\n\n{result}'
73
- return result, 'Transliteration'
74
 
75
- # 3. Morphological Structural Annotation System
76
  elif action == 'annotate':
77
  tokens = nltk.word_tokenize(text)
78
  pos_tags = nltk.pos_tag(tokens)
@@ -101,189 +95,274 @@ def execute_agent_task(
101
  )
102
  report.append(f'• "{word}" → {readable_label}')
103
 
104
- return '\n'.join(report), 'Annotation'
105
 
106
- # 4. Language Detection Engine
107
  elif action == 'detect':
108
  raw_code = detect(text).lower()
109
  readable_lang = LANGUAGE_NAMES.get(
110
  raw_code, f'Unknown/Unmapped Script ({raw_code.upper()})'
111
  )
112
- result = f'--- Language Detection Report ---\n\n• Identified Language: {readable_lang}\n• Standard ISO 639-1 Code: {raw_code}'
113
- return result, 'Language Detection'
114
 
115
  except Exception as e:
116
- return f'Agent processing failure: {str(e)}', 'Processing Error'
117
 
118
 
119
- # --- CUSTOM CSS FOR EXACT THEME MATCHING ---
120
  custom_css = """
121
  body, .gradio-container {
122
  background-color: #0b1818 !important;
 
123
  color: #e2f1f1 !important;
124
- font-family: 'Segoe UI', system-ui, sans-serif;
125
  }
126
 
127
- #sidebar {
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  background-color: #512da8 !important;
129
- border-radius: 12px;
130
- padding: 25px !important;
131
- color: #ffffff !important;
 
 
132
  }
133
 
134
- #sidebar h2 {
135
- color: #ffeb3b !important;
136
- font-weight: 700 !important;
137
- font-size: 20px !important;
138
- margin-bottom: 10px;
 
 
139
  }
140
 
141
- .action-btn {
142
- background-color: #1a73e8 !important;
143
- color: white !important;
144
- font-weight: bold !important;
145
  border: none !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  border-radius: 6px !important;
 
147
  }
148
 
149
- .action-btn:hover {
150
- background-color: #155cb4 !important;
 
 
 
 
 
 
 
 
 
151
  }
152
 
153
- .status-badge {
154
- background-color: #173131;
155
- color: #2e7d32;
156
- padding: 6px 12px;
157
- border-radius: 20px;
158
- font-weight: bold;
159
  }
160
  """
161
 
162
  # --- GRADIO INTERFACE BUILD ---
163
  with gr.Blocks(css=custom_css, title="XTranslate Agentic Studio") as demo:
164
 
165
- with gr.Row():
166
- # Left Info Sidebar
167
- with gr.Column(scale=1, elem_id="sidebar"):
168
- gr.Markdown("## Welcome to the Linguistic Platform")
169
- gr.Markdown(
170
- "It is an active functional platform that bridges global communication gaps, allowing users to effortlessly understand and interact in foreign languages."
171
- )
172
- gr.Markdown(
173
  """
174
- **<u>🚀 Key Features:-</u>**
175
- * ✔️ Core Translation
176
- * ✔️ Language Identification
177
- * ✔️ Universal Transliteration
178
- * ✔️ Part-of-Speech Annotation
 
 
 
 
 
 
 
 
 
179
  """
180
  )
181
- gr.Markdown("<br><br><small>Infrastructure State: Online</small>")
182
 
183
- # Right Main Control Center
184
- with gr.Column(scale=3):
185
- # Header
186
- with gr.Row():
187
- gr.Markdown(
188
- "## 🤖 XTranslate Agentic Studio <span style='font-size: 12px; background: #00b4d8; color: black; padding: 2px 6px; border-radius: 4px;'>v2.5</span>"
189
- )
190
- gr.Markdown(
191
- "<div style='text-align: right; color: #a2cfcf;'>🟢 Engine Status: Live</div>"
192
- )
193
 
194
- # Workspace Card Grid
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  with gr.Row():
196
- with gr.Column():
 
197
  source_lang = gr.Dropdown(
198
  choices=list(LANGUAGES.keys()),
199
- value="Auto-Detect",
200
- label="🌐 Source Language",
 
201
  )
202
  source_input = gr.Textbox(
203
- lines=8,
204
  placeholder="Type, paste, or input your textual string here...",
205
- label="Input Text",
 
 
206
  )
207
 
208
- with gr.Column():
 
209
  target_lang = gr.Dropdown(
210
  choices=[
211
- k for k in LANGUAGES.keys() if k != "Auto-Detect"
 
 
212
  ],
213
  value="English",
214
- label="📒 Target Language",
 
215
  )
216
  target_output = gr.Textbox(
217
- lines=8,
218
  placeholder="Engine processing outputs project here...",
219
- label="Result View",
220
- interactive=False,
221
- )
222
- mode_badge = gr.Textbox(
223
- label="Active Output Mode",
224
  interactive=False,
225
- value="Ready",
226
  )
227
 
228
- # Translation Service Network
 
 
 
229
  service_select = gr.Dropdown(
230
  choices=[
231
  "Google Cloud Neural Network",
232
  "MyMemory Shared API Network",
233
  ],
234
  value="Google Cloud Neural Network",
235
- label="ACTIVE NEURAL TRANSLATION NETWORK SERVICE SELECTOR",
 
 
236
  )
237
 
238
- gr.Markdown(
239
- "<span style='color:#2e7d32; font-weight:bold;'>ENABLED LINGUISTIC SELECT FUNCTIONALITIES:-</span>"
 
240
  )
241
 
242
- # Action Buttons Row
243
  with gr.Row():
244
  btn_translate = gr.Button(
245
- "Translate Text", elem_classes=["action-btn"]
246
  )
247
  btn_detect = gr.Button(
248
- "Detect Language", elem_classes=["action-btn"]
249
  )
250
  btn_transliterate = gr.Button(
251
- "Transliterate Script", elem_classes=["action-btn"]
252
  )
253
  btn_annotate = gr.Button(
254
- "Annotate Structure", elem_classes=["action-btn"]
255
  )
256
 
257
- # Handler Routing
258
- def run_task(text, src, tgt, service, act):
259
- return execute_agent_task(text, src, tgt, service, act)
260
-
261
  btn_translate.click(
262
- fn=lambda t, s, tg, srv: run_task(
263
- t, s, tg, srv, 'translate'
264
  ),
265
- inputs=[source_input, source_lang, target_lang, service_select],
266
- outputs=[target_output, mode_badge],
 
 
 
 
 
267
  )
268
 
269
  btn_detect.click(
270
- fn=lambda t, s, tg, srv: run_task(t, s, tg, srv, 'detect'),
271
- inputs=[source_input, source_lang, target_lang, service_select],
272
- outputs=[target_output, mode_badge],
 
 
 
 
 
 
 
273
  )
274
 
275
  btn_transliterate.click(
276
- fn=lambda t, s, tg, srv: run_task(
277
- t, s, tg, srv, 'transliterate'
278
  ),
279
- inputs=[source_input, source_lang, target_lang, service_select],
280
- outputs=[target_output, mode_badge],
 
 
 
 
 
281
  )
282
 
283
  btn_annotate.click(
284
- fn=lambda t, s, tg, srv: run_task(t, s, tg, srv, 'annotate'),
285
- inputs=[source_input, source_lang, target_lang, service_select],
286
- outputs=[target_output, mode_badge],
 
 
 
 
 
 
 
287
  )
288
 
289
  if __name__ == "__main__":
 
4
  from deep_translator import GoogleTranslator, MyMemoryTranslator
5
  from langdetect import DetectorFactory, detect
6
 
7
+ # Enforce deterministic language detection
8
  DetectorFactory.seed = 0
9
 
10
+ # Ensure required NLTK tokenizers and taggers are available
11
  for resource in ['punkt', 'averaged_perceptron_tagger', 'punkt_tab']:
12
  try:
13
  if 'punkt' in resource:
 
17
  except LookupError:
18
  nltk.download(resource, quiet=True)
19
 
20
+ # ISO Code mappings
21
  LANGUAGES = {
22
+ 'Auto-Detect Language': 'auto',
23
  'English': 'en',
24
  'Hindi (हिंदी)': 'hi',
25
  'Spanish (Español)': 'es',
26
  'French (Français)': 'fr',
27
  'German (Deutsch)': 'de',
28
  'Chinese (Simplified)': 'zh-CN',
 
29
  'Japanese (日本語)': 'ja',
30
  'Malay': 'ms',
31
  'Arabic': 'ar',
 
43
  text, source_lang_name, target_lang_name, service_name, action
44
  ):
45
  if not text or not text.strip():
46
+ return 'Input text space cannot be empty.'
47
 
48
  source_lang = LANGUAGES.get(source_lang_name, 'auto')
49
  target_lang = LANGUAGES.get(target_lang_name, 'en')
50
 
51
  try:
 
52
  if action == 'translate':
53
  if service_name == 'MyMemory Shared API Network':
54
  src = 'en' if source_lang == 'auto' else source_lang
 
59
  translator = GoogleTranslator(
60
  source=source_lang, target=target_lang
61
  )
62
+ return translator.translate(text)
63
 
 
 
 
 
64
  elif action == 'transliterate':
65
  result = anyascii.anyascii(text)
66
  if result.lower() == text.lower():
67
  result = f'[Note: Text is already in Roman/Latin script form]\n\n{result}'
68
+ return result
69
 
 
70
  elif action == 'annotate':
71
  tokens = nltk.word_tokenize(text)
72
  pos_tags = nltk.pos_tag(tokens)
 
95
  )
96
  report.append(f'• "{word}" → {readable_label}')
97
 
98
+ return '\n'.join(report)
99
 
 
100
  elif action == 'detect':
101
  raw_code = detect(text).lower()
102
  readable_lang = LANGUAGE_NAMES.get(
103
  raw_code, f'Unknown/Unmapped Script ({raw_code.upper()})'
104
  )
105
+ return f'--- Language Detection Report ---\n\n• Identified Language: {readable_lang}\n• Standard ISO 639-1 Code: {raw_code}'
 
106
 
107
  except Exception as e:
108
+ return f'Agent processing failure: {str(e)}'
109
 
110
 
111
+ # --- EXACT UI CUSTOM STYLING (MATCHING THE SCREENSHOT) ---
112
  custom_css = """
113
  body, .gradio-container {
114
  background-color: #0b1818 !important;
115
+ font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif !important;
116
  color: #e2f1f1 !important;
 
117
  }
118
 
119
+ footer { display: none !important; }
120
+
121
+ /* Main Layout Wrapper */
122
+ #main-workspace {
123
+ background-color: #112424;
124
+ border-radius: 16px;
125
+ border: 1px solid #1a3a3a;
126
+ box-shadow: 0 20px 50px rgba(0,0,0,0.5);
127
+ overflow: hidden;
128
+ padding: 0 !important;
129
+ }
130
+
131
+ /* Sidebar Custom Theme */
132
+ #sidebar-panel {
133
  background-color: #512da8 !important;
134
+ padding: 35px 25px !important;
135
+ display: flex;
136
+ flex-direction: column;
137
+ justify-content: space-between;
138
+ min-height: 100%;
139
  }
140
 
141
+ /* Card Styling for Input / Output */
142
+ .card-box {
143
+ background-color: #ffffff !important;
144
+ border-radius: 10px !important;
145
+ border: 1px solid #cfd8dc !important;
146
+ overflow: hidden !important;
147
+ padding: 0 !important;
148
  }
149
 
150
+ .input-textarea textarea {
151
+ background-color: #ffffff !important;
152
+ color: #263238 !important;
153
+ font-size: 15px !important;
154
  border: none !important;
155
+ outline: none !important;
156
+ box-shadow: none !important;
157
+ }
158
+
159
+ .output-textarea textarea {
160
+ background-color: #fafafa !important;
161
+ color: #37474f !important;
162
+ font-size: 15px !important;
163
+ border: none !important;
164
+ outline: none !important;
165
+ box-shadow: none !important;
166
+ }
167
+
168
+ /* Dropdown and Selectors */
169
+ .custom-dropdown {
170
+ background-color: #162e2e !important;
171
+ border: 1px solid #204545 !important;
172
  border-radius: 6px !important;
173
+ color: #ffffff !important;
174
  }
175
 
176
+ /* Buttons */
177
+ .blue-btn button {
178
+ background-color: #1a73e8 !important;
179
+ color: #ffffff !important;
180
+ font-weight: 600 !important;
181
+ font-size: 14px !important;
182
+ border-radius: 6px !important;
183
+ border: none !important;
184
+ padding: 12px 18px !important;
185
+ box-shadow: 0 4px 12px rgba(26,115,232,0.3) !important;
186
+ transition: all 0.2s ease !important;
187
  }
188
 
189
+ .blue-btn button:hover {
190
+ background-color: #155cb4 !important;
 
 
 
 
191
  }
192
  """
193
 
194
  # --- GRADIO INTERFACE BUILD ---
195
  with gr.Blocks(css=custom_css, title="XTranslate Agentic Studio") as demo:
196
 
197
+ with gr.Row(elem_id="main-workspace"):
198
+
199
+ # LEFT SIDEBAR
200
+ with gr.Column(scale=1, elem_id="sidebar-panel"):
201
+ gr.HTML(
 
 
 
202
  """
203
+ <h2 style="font-weight:bold; color:yellow; font-size: 22px; margin-bottom: 12px;">Welcome to the Linguistic Platform</h2>
204
+ <p style="font-size: 14px; font-weight:bold; line-height: 1.5; color: #f3e5f5;">
205
+ It is an active functional platform that bridges global communication gaps, allowing users to effortlessly understand and interact in foreign languages.
206
+ </p>
207
+ <br>
208
+ <ul style="list-style: none; padding: 0; margin-top: 10px; font-size: 14px;">
209
+ <li style="font-style:italic; text-decoration: underline; font-weight:bold; margin-bottom: 10px; color: #ffffff;">🚀 Key Features:-</li>
210
+ <li style="margin-bottom: 8px;">✔ Core Translation</li>
211
+ <li style="margin-bottom: 8px;">✔ Language Identification</li>
212
+ <li style="margin-bottom: 8px;">✔ Universal Transliteration</li>
213
+ <li style="margin-bottom: 8px;">✔ Part-of-Speech Annotation</li>
214
+ </ul>
215
+ <br><br>
216
+ <div style="font-size: 12px; color: #b39ddb;">Infrastructure State: Online</div>
217
  """
218
  )
 
219
 
220
+ # RIGHT MAIN CONTROL CENTER
221
+ with gr.Column(scale=3, style="padding: 25px;"):
 
 
 
 
 
 
 
 
222
 
223
+ # HEADER
224
+ gr.HTML(
225
+ """
226
+ <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
227
+ <div style="display: flex; align-items: center; gap: 10px;">
228
+ <span style="font-size: 26px;">🤖</span>
229
+ <h1 style="font-size: 22px; font-weight: 700; color: #ffffff; margin: 0;">
230
+ XTranslate Agentic Studio
231
+ <span style="font-size: 11px; background: #00b4d8; color: #0b1818; padding: 2px 6px; border-radius: 4px; vertical-align: middle;">v2.5</span>
232
+ </h1>
233
+ </div>
234
+ <div style="font-size: 13px; background-color: #173131; padding: 6px 12px; border-radius: 20px; color: #a2cfcf;">
235
+ 🟢 Engine Status: Live
236
+ </div>
237
+ </div>
238
+ """
239
+ )
240
+
241
+ # WORKSPACE TEXTAREA CARDS
242
  with gr.Row():
243
+ # LEFT INPUT CARD
244
+ with gr.Column(elem_classes=["card-box"]):
245
  source_lang = gr.Dropdown(
246
  choices=list(LANGUAGES.keys()),
247
+ value="Auto-Detect Language",
248
+ show_label=False,
249
+ container=False,
250
  )
251
  source_input = gr.Textbox(
252
+ lines=9,
253
  placeholder="Type, paste, or input your textual string here...",
254
+ show_label=False,
255
+ container=False,
256
+ elem_classes=["input-textarea"],
257
  )
258
 
259
+ # RIGHT OUTPUT CARD
260
+ with gr.Column(elem_classes=["card-box"]):
261
  target_lang = gr.Dropdown(
262
  choices=[
263
+ k
264
+ for k in LANGUAGES.keys()
265
+ if k != "Auto-Detect Language"
266
  ],
267
  value="English",
268
+ show_label=False,
269
+ container=False,
270
  )
271
  target_output = gr.Textbox(
272
+ lines=9,
273
  placeholder="Engine processing outputs project here...",
274
+ show_label=False,
275
+ container=False,
 
 
 
276
  interactive=False,
277
+ elem_classes=["output-textarea"],
278
  )
279
 
280
+ # SERVICE SELECTOR
281
+ gr.HTML(
282
+ """<div style="margin-top: 15px; margin-bottom: 5px; font-size: 12px; font-weight: bold; color: #81a1a1; text-transform: uppercase;">ACTIVE NEURAL TRANSLATION NETWORK SERVICE SELECTOR</div>"""
283
+ )
284
  service_select = gr.Dropdown(
285
  choices=[
286
  "Google Cloud Neural Network",
287
  "MyMemory Shared API Network",
288
  ],
289
  value="Google Cloud Neural Network",
290
+ show_label=False,
291
+ container=False,
292
+ elem_classes=["custom-dropdown"],
293
  )
294
 
295
+ # FUNCTIONALITIES LABEL
296
+ gr.HTML(
297
+ """<div style="margin-top: 20px; margin-bottom: 10px; font-size: 14px; font-weight: bold; color: #20c997;">ENABLED LINGUISTIC SELECT FUNCTIONALITIES:-</div>"""
298
  )
299
 
300
+ # ACTION BUTTONS ROW
301
  with gr.Row():
302
  btn_translate = gr.Button(
303
+ "Translate Text", elem_classes=["blue-btn"]
304
  )
305
  btn_detect = gr.Button(
306
+ "Detect Language", elem_classes=["blue-btn"]
307
  )
308
  btn_transliterate = gr.Button(
309
+ "Transliterate Script", elem_classes=["blue-btn"]
310
  )
311
  btn_annotate = gr.Button(
312
+ "Annotate Structure", elem_classes=["blue-btn"]
313
  )
314
 
315
+ # EVENT ROUTING
 
 
 
316
  btn_translate.click(
317
+ fn=lambda t, s, tg, srv: execute_agent_task(
318
+ t, s, tg, srv, "translate"
319
  ),
320
+ inputs=[
321
+ source_input,
322
+ source_lang,
323
+ target_lang,
324
+ service_select,
325
+ ],
326
+ outputs=[target_output],
327
  )
328
 
329
  btn_detect.click(
330
+ fn=lambda t, s, tg, srv: execute_agent_task(
331
+ t, s, tg, srv, "detect"
332
+ ),
333
+ inputs=[
334
+ source_input,
335
+ source_lang,
336
+ target_lang,
337
+ service_select,
338
+ ],
339
+ outputs=[target_output],
340
  )
341
 
342
  btn_transliterate.click(
343
+ fn=lambda t, s, tg, srv: execute_agent_task(
344
+ t, s, tg, srv, "transliterate"
345
  ),
346
+ inputs=[
347
+ source_input,
348
+ source_lang,
349
+ target_lang,
350
+ service_select,
351
+ ],
352
+ outputs=[target_output],
353
  )
354
 
355
  btn_annotate.click(
356
+ fn=lambda t, s, tg, srv: execute_agent_task(
357
+ t, s, tg, srv, "annotate"
358
+ ),
359
+ inputs=[
360
+ source_input,
361
+ source_lang,
362
+ target_lang,
363
+ service_select,
364
+ ],
365
+ outputs=[target_output],
366
  )
367
 
368
  if __name__ == "__main__":