Mer1Alii commited on
Commit
860b2f2
·
verified ·
1 Parent(s): 2d4c3e5

Clean up unnecessary comments in UI code

Browse files
Files changed (1) hide show
  1. app.py +196 -197
app.py CHANGED
@@ -149,30 +149,32 @@ def parse_tool_calls(text):
149
  def _escape(text: str) -> str:
150
  return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
151
 
 
152
  def render_thinking(text: str) -> str:
153
- """Modelin araç çağrısı öncesi/yanındaki serbest metnini ayrı bir 'düşünce' bloğu olarak gösterir."""
154
  return (
155
- f'<div class="think-block">'
156
- f'<span class="think-label">düşünce</span>'
157
- f'<div class="think-text">{_escape(text)}</div>'
158
- f'</div>'
159
  )
160
 
161
  def render_tool(name: str, args_str: str, output_str: str, is_error: bool = False) -> str:
162
- """Bir araç çağrısını girdi/çıktısıyla birlikte ayrı bir 'araç' bloğu olarak gösterir."""
163
- cls = "tool-block tool-error" if is_error else "tool-block"
164
- mark = "⚠" if is_error else ""
165
  return (
166
- f'<div class="{cls}">'
167
- f'<span class="tool-label">araç çağrısı</span>'
168
- f'<div class="tool-head"><span class="tool-dot"></span>{_escape(str(name))}{(" " + mark) if mark else ""}</div>'
169
- f'<div class="tool-io"><span class="io-label">girdi</span>{_escape(args_str)}</div>'
170
- f'<div class="tool-io"><span class="io-label">çıktı</span>{_escape(output_str)}</div>'
171
  f'</div>'
 
172
  )
173
 
174
- def render_status(text: str = "model çalışıyor") -> str:
175
- return f'<div class="status-line"><span class="pulse-dot"></span>{_escape(text)}</div>'
176
 
177
  def chat_interface(user_input, history):
178
  if not model:
@@ -229,7 +231,7 @@ def chat_interface(user_input, history):
229
  try:
230
  result = available_functions[func_name](**args)
231
  res_json = json.dumps(result, ensure_ascii=False)
232
- out_display = res_json[:200] + ("…" if len(res_json) > 200 else "")
233
  final_output += render_tool(func_name, args_str, out_display)
234
  # Sonucu modele geri besle
235
  messages.append({
@@ -257,237 +259,234 @@ def chat_interface(user_input, history):
257
  turn += 1
258
 
259
  # Ara adımları UI'da akarak göster (stream)
260
- yield final_output + render_status("model aksiyon alıyor")
261
 
262
  # ==========================================
263
- # 4. Gradio UI — düz (flat), AMOLED siyah + gri arayüz
264
  # ==========================================
265
  css = """
266
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');
267
-
268
- :root {
269
- --bg: #000000;
270
- --fg: #ededed;
271
- --fg-dim: #9a9a9a;
272
- --fg-dimmer: #5c5c5c;
273
- --line: #232323;
274
- --think: #8f8f8f;
275
- }
276
-
277
- * { box-sizing: border-box; }
278
 
279
  body, .gradio-container {
280
- background: var(--bg) !important;
281
- color: var(--fg) !important;
282
- font-family: 'Inter', system-ui, -apple-system, sans-serif !important;
283
  }
284
 
285
  .gradio-container {
286
- border: none !important;
287
- box-shadow: none !important;
288
- max-width: 900px !important;
289
- margin: 0 auto !important;
290
  }
291
 
 
292
  footer { display: none !important; }
293
- ::selection { background: #3a3a3a; color: #fff; }
294
-
295
- /* ince, gri kaydırma çubuğu */
296
- ::-webkit-scrollbar { width: 6px; height: 6px; }
297
- ::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 4px; }
298
- ::-webkit-scrollbar-track { background: transparent; }
299
 
300
- /* ---------- başlık ---------- */
301
- .header-wrap { text-align: center; padding: 30px 0 18px 0; }
302
- .header-title { font-size: 1.35em; font-weight: 300; letter-spacing: 0.09em; color: #f2f2f2; }
303
- .header-sub { font-size: 0.72em; letter-spacing: 0.22em; text-transform: uppercase; color: var(--fg-dimmer); margin-top: 6px; }
 
 
 
 
304
 
305
- /* ---------- sohbet penceresi: tamamen düz, kutu yok ---------- */
306
  #chatbot {
307
- background: var(--bg) !important;
308
- border: none !important;
309
- box-shadow: none !important;
310
- }
311
- #chatbot .message-wrap,
312
- #chatbot .wrap,
313
- #chatbot .bot-row,
314
- #chatbot .user-row {
315
- background: transparent !important;
316
  }
317
- #chatbot .message {
318
- background: transparent !important;
319
- border: none !important;
320
- box-shadow: none !important;
321
- padding: 10px 6px !important;
322
- font-size: 0.97em;
323
- line-height: 1.65;
324
  }
325
- #chatbot .user {
326
- color: #ffffff !important;
327
- font-weight: 500;
 
 
 
 
 
328
  }
329
- #chatbot .bot {
330
- color: var(--fg) !important;
331
  }
332
- /* mesajlar arası kutu değil, ince ayraç */
333
- #chatbot .message-row:not(:first-child) {
334
- border-top: 1px solid var(--line);
335
- margin-top: 4px;
336
- padding-top: 4px;
337
  }
338
 
339
- /* ---------- düşünce bloğu ---------- */
340
- .think-block {
341
- border-left: 1px dashed #444;
342
- padding: 3px 0 3px 16px;
343
- margin: 12px 0;
 
 
 
 
 
344
  }
345
- .think-label,
346
- .tool-label {
347
- display: block;
348
- font-family: 'JetBrains Mono', monospace;
349
- font-size: 0.65em;
350
- letter-spacing: 0.14em;
351
- text-transform: uppercase;
352
- color: var(--fg-dimmer);
353
- margin-bottom: 5px;
354
  }
355
- .think-text {
356
- font-style: italic;
357
- color: var(--think);
358
- font-size: 0.92em;
359
- line-height: 1.55;
360
- white-space: pre-wrap;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  }
362
 
363
- /* ---------- araç bloğu ---------- */
364
- .tool-block {
365
- border-left: 1px solid #3a3a3a;
366
- padding: 3px 0 8px 16px;
367
- margin: 12px 0;
 
 
368
  }
369
- .tool-head {
370
- font-family: 'JetBrains Mono', monospace;
371
- font-size: 0.8em;
372
- color: #c2c2c2;
373
- display: flex;
374
- align-items: center;
375
- gap: 7px;
376
- margin-bottom: 5px;
377
  }
378
- .tool-dot {
379
- width: 5px; height: 5px; border-radius: 50%;
380
- background: #9a9a9a;
381
- display: inline-block;
382
- flex-shrink: 0;
383
  }
384
- .tool-io {
385
- font-family: 'JetBrains Mono', monospace;
386
- font-size: 0.78em;
387
- color: var(--fg-dim);
388
- padding: 2px 0;
389
- word-break: break-word;
390
- white-space: pre-wrap;
391
  }
392
- .io-label {
393
- color: var(--fg-dimmer);
394
- margin-right: 8px;
 
395
  }
396
- .tool-error .tool-head { color: #d8d8d8; }
397
- .tool-error { border-left-color: #555; }
398
-
399
- /* ---------- çalışıyor göstergesi ---------- */
400
- .status-line {
401
- display: flex;
402
- align-items: center;
403
- gap: 8px;
404
- color: var(--fg-dimmer);
405
- font-size: 0.8em;
406
- margin-top: 8px;
407
- font-style: italic;
408
  }
409
- .pulse-dot {
410
- width: 6px; height: 6px; border-radius: 50%;
411
- background: var(--fg-dim);
412
- animation: pulse 1.2s ease-in-out infinite;
413
- flex-shrink: 0;
414
  }
415
- @keyframes pulse { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }
416
-
417
- /* ---------- örnek baloncukları (textbox'ın üstünde, küçük) ---------- */
418
- .examples-row { margin-bottom: 8px !important; gap: 6px !important; }
419
- .bubble-btn {
420
- background: transparent !important;
421
- border: 1px solid #2c2c2c !important;
422
- border-radius: 999px !important;
423
- color: var(--fg-dim) !important;
424
- font-size: 0.76em !important;
425
- font-weight: 400 !important;
426
- padding: 5px 14px !important;
427
- min-width: auto !important;
428
- flex-grow: 0 !important;
429
- box-shadow: none !important;
430
- transition: color .15s ease, border-color .15s ease;
431
  }
432
- .bubble-btn:hover {
433
- color: #fff !important;
434
- border-color: #6a6a6a !important;
 
 
 
435
  }
436
-
437
- /* ---------- mesaj kutusu ---------- */
438
- #msg_box textarea {
439
- background: transparent !important;
440
- border: none !important;
441
- border-bottom: 1px solid #2a2a2a !important;
442
- border-radius: 0 !important;
443
- color: var(--fg) !important;
444
- padding: 10px 4px !important;
445
- font-size: 0.95em !important;
446
  }
447
- #msg_box textarea:focus {
448
- border-bottom-color: #808080 !important;
449
- box-shadow: none !important;
450
  }
451
- #msg_box textarea::placeholder { color: #555 !important; }
452
 
453
- #submit_btn {
454
- background: transparent !important;
455
- color: var(--fg) !important;
456
- border: 1px solid #333 !important;
457
- border-radius: 999px !important;
458
- font-weight: 400 !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
459
  }
460
- #submit_btn:hover { border-color: #7a7a7a !important; color: #fff !important; }
461
  """
462
 
463
- with gr.Blocks(theme=gr.themes.Base(), css=css) as demo:
464
- gr.Markdown(
465
- "<div class='header-wrap'>"
466
- "<div class='header-title'>🌍 AFET TAKİP</div>"
467
- "<div class='header-sub'>agentic disaster intelligence</div>"
468
- "</div>"
469
- )
470
 
471
- chatbot = gr.Chatbot(elem_id="chatbot", height=520, label="Asistan", show_label=False)
 
 
 
 
 
 
 
472
 
 
473
  with gr.Row(elem_classes="examples-row"):
474
- ex1 = gr.Button("son depremler", elem_classes="bubble-btn")
475
- ex2 = gr.Button("aktif yangınlar", elem_classes="bubble-btn")
476
- ex3 = gr.Button("deprem + yangın birleştir", elem_classes="bubble-btn")
477
-
478
- with gr.Row():
479
- msg = gr.Textbox(placeholder="Afetlerle ilgili ne öğrenmek istersiniz?", label="Mesajınız", lines=1, show_label=False, elem_id="msg_box", scale=4)
480
- submit_btn = gr.Button("Gönder", variant="primary", scale=1, elem_id="submit_btn")
481
-
 
 
 
 
 
 
 
 
 
 
482
  ex1.click(lambda: "2026-07-25 ile 2026-07-30 tarihleri arasındaki 5.5 üzeri depremleri listele.", outputs=msg)
483
  ex2.click(lambda: "Şu anki aktif büyük orman yangınları hangileri? 3 tanesini getir.", outputs=msg)
484
  ex3.click(lambda: "Son 7 günde (2026-07-23 ile 2026-07-30) 6.0'dan büyük deprem oldu mu? Ayrıca şu an aktif orman yangını var mı?", outputs=msg)
485
 
486
  def respond(user_message, chat_history):
487
- # Arayüze kullanıcının mesajını ekle
 
 
488
  chat_history.append((user_message, ""))
489
 
490
- # Jeneratörden gelen kısımları adım adım güncelle
491
  for bot_message in chat_interface(user_message, chat_history[:-1]):
492
  chat_history[-1] = (user_message, bot_message)
493
  yield chat_history, ""
 
149
  def _escape(text: str) -> str:
150
  return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
151
 
152
+ # Arayüz için HTML Oluşturucuları
153
  def render_thinking(text: str) -> str:
154
+ """Modelin araç çağrısı öncesi/yanındaki serbest metnini açılır-kapanır blok olarak gösterir."""
155
  return (
156
+ f'<details class="chatgpt-details">'
157
+ f'<summary>🤔 Düşünce Süreci</summary>'
158
+ f'<div class="details-content thinking-text">{_escape(text)}</div>'
159
+ f'</details>'
160
  )
161
 
162
  def render_tool(name: str, args_str: str, output_str: str, is_error: bool = False) -> str:
163
+ """Bir araç çağrısını açılır-kapanır şık bir blok olarak gösterir."""
164
+ icon = "⚠️" if is_error else "🛠️"
165
+ status = "Hata" if is_error else "Başarılı"
166
  return (
167
+ f'<details class="chatgpt-details">'
168
+ f'<summary>{icon} Araç Kullanımı: <code>{_escape(str(name))}</code></summary>'
169
+ f'<div class="details-content">'
170
+ f'<div class="io-section"><span class="io-label">Girdi:</span> <code>{_escape(args_str)}</code></div>'
171
+ f'<div class="io-section"><span class="io-label">Çıktı ({status}):</span> <pre><code>{_escape(output_str)}</code></pre></div>'
172
  f'</div>'
173
+ f'</details>'
174
  )
175
 
176
+ def render_status(text: str = "Model düşünüyor") -> str:
177
+ return f'<div class="status-indicator"><span class="spinner"></span>{_escape(text)}...</div>'
178
 
179
  def chat_interface(user_input, history):
180
  if not model:
 
231
  try:
232
  result = available_functions[func_name](**args)
233
  res_json = json.dumps(result, ensure_ascii=False)
234
+ out_display = res_json[:300] + ("…" if len(res_json) > 300 else "")
235
  final_output += render_tool(func_name, args_str, out_display)
236
  # Sonucu modele geri besle
237
  messages.append({
 
259
  turn += 1
260
 
261
  # Ara adımları UI'da akarak göster (stream)
262
+ yield final_output + render_status("Araç sonuçları işleniyor")
263
 
264
  # ==========================================
265
+ # 4. Gradio UI
266
  # ==========================================
267
  css = """
268
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');
 
 
 
 
 
 
 
 
 
 
 
269
 
270
  body, .gradio-container {
271
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;
 
 
272
  }
273
 
274
  .gradio-container {
275
+ max-width: 850px !important;
276
+ margin: auto !important;
 
 
277
  }
278
 
279
+ /* Footer gizleme */
280
  footer { display: none !important; }
 
 
 
 
 
 
281
 
282
+ /* Başlık */
283
+ .header-title {
284
+ text-align: center;
285
+ font-weight: 600;
286
+ font-size: 1.5rem;
287
+ margin-top: 1rem;
288
+ margin-bottom: 0.5rem;
289
+ }
290
 
291
+ /* Chatbot Alanı */
292
  #chatbot {
293
+ border: none !important;
294
+ background: transparent !important;
295
+ box-shadow: none !important;
 
 
 
 
 
 
296
  }
297
+
298
+ /* Örnek Butonları (Pills) */
299
+ .examples-row {
300
+ display: flex;
301
+ justify-content: center;
302
+ gap: 8px;
303
+ margin-bottom: 12px;
304
  }
305
+ .bubble-btn {
306
+ background: transparent !important;
307
+ border: 1px solid var(--border-color-primary) !important;
308
+ border-radius: 9999px !important;
309
+ font-size: 0.85rem !important;
310
+ padding: 6px 16px !important;
311
+ box-shadow: none !important;
312
+ transition: background 0.2s;
313
  }
314
+ .bubble-btn:hover {
315
+ background: var(--neutral-100) !important;
316
  }
317
+ .dark .bubble-btn:hover {
318
+ background: var(--neutral-800) !important;
 
 
 
319
  }
320
 
321
+ /* Modern Input Alanı */
322
+ #input-container {
323
+ display: flex;
324
+ align-items: center;
325
+ border: 1px solid var(--border-color-primary);
326
+ border-radius: 24px;
327
+ padding: 4px 8px;
328
+ background: var(--background-fill-primary);
329
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
330
+ margin-bottom: 20px;
331
  }
332
+ .dark #input-container {
333
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
 
 
 
 
 
 
 
334
  }
335
+ #msg_box, #msg_box textarea {
336
+ border: none !important;
337
+ box-shadow: none !important;
338
+ background: transparent !important;
339
+ font-size: 1rem !important;
340
+ resize: none !important;
341
+ }
342
+ #submit_btn {
343
+ background: #10a37f !important;
344
+ color: white !important;
345
+ border: none !important;
346
+ border-radius: 50% !important;
347
+ width: 38px !important;
348
+ height: 38px !important;
349
+ min-width: 38px !important;
350
+ padding: 0 !important;
351
+ display: flex;
352
+ align-items: center;
353
+ justify-content: center;
354
+ cursor: pointer;
355
+ transition: transform 0.1s, background 0.2s;
356
+ margin-left: 8px;
357
+ margin-right: 4px;
358
+ }
359
+ #submit_btn:hover {
360
+ background: #0d8a6a !important;
361
+ }
362
+ #submit_btn:active {
363
+ transform: scale(0.95);
364
  }
365
 
366
+ /* Araç ve Düşünce Çıktıları */
367
+ .chatgpt-details {
368
+ border: 1px solid var(--border-color-primary);
369
+ border-radius: 8px;
370
+ margin: 10px 0;
371
+ background-color: var(--background-fill-secondary);
372
+ overflow: hidden;
373
  }
374
+ .chatgpt-details summary {
375
+ padding: 10px 14px;
376
+ cursor: pointer;
377
+ font-weight: 500;
378
+ font-size: 0.9rem;
379
+ outline: none;
380
+ user-select: none;
 
381
  }
382
+ .chatgpt-details summary:hover {
383
+ background-color: var(--neutral-100);
 
 
 
384
  }
385
+ .dark .chatgpt-details summary:hover {
386
+ background-color: var(--neutral-800);
 
 
 
 
 
387
  }
388
+ .details-content {
389
+ padding: 12px 14px;
390
+ border-top: 1px solid var(--border-color-primary);
391
+ font-size: 0.85rem;
392
  }
393
+ .thinking-text {
394
+ font-style: italic;
395
+ color: var(--body-text-color-subdued);
396
+ white-space: pre-wrap;
 
 
 
 
 
 
 
 
397
  }
398
+ .io-section {
399
+ margin-bottom: 8px;
 
 
 
400
  }
401
+ .io-section:last-child {
402
+ margin-bottom: 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  }
404
+ .io-label {
405
+ font-weight: 600;
406
+ margin-right: 6px;
407
+ font-size: 0.8rem;
408
+ text-transform: uppercase;
409
+ letter-spacing: 0.05em;
410
  }
411
+ .details-content pre {
412
+ background: var(--neutral-100);
413
+ padding: 8px;
414
+ border-radius: 6px;
415
+ overflow-x: auto;
416
+ margin-top: 4px;
417
+ }
418
+ .dark .details-content pre {
419
+ background: #1e1e1e;
 
420
  }
421
+ .details-content code {
422
+ font-family: 'JetBrains Mono', monospace;
423
+ font-size: 0.8rem;
424
  }
 
425
 
426
+ /* Yükleniyor / Durum Göstergesi */
427
+ .status-indicator {
428
+ display: flex;
429
+ align-items: center;
430
+ gap: 8px;
431
+ font-size: 0.85rem;
432
+ color: var(--body-text-color-subdued);
433
+ margin-top: 10px;
434
+ }
435
+ .spinner {
436
+ width: 12px;
437
+ height: 12px;
438
+ border: 2px solid var(--border-color-primary);
439
+ border-top-color: var(--color-accent);
440
+ border-radius: 50%;
441
+ animation: spin 1s linear infinite;
442
+ }
443
+ @keyframes spin {
444
+ to { transform: rotate(360deg); }
445
  }
 
446
  """
447
 
448
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="zinc", neutral_hue="slate"), css=css) as demo:
449
+ gr.Markdown("<div class='header-title'>🌍 Afet Takip Asistanı</div>")
 
 
 
 
 
450
 
451
+ # Sohbet Ekranı
452
+ chatbot = gr.Chatbot(
453
+ elem_id="chatbot",
454
+ height=550,
455
+ show_label=False,
456
+ bubble_full_width=False,
457
+ avatar_images=(None, "https://upload.wikimedia.org/wikipedia/commons/0/04/ChatGPT_logo.svg")
458
+ )
459
 
460
+ # Örnek Sorular
461
  with gr.Row(elem_classes="examples-row"):
462
+ ex1 = gr.Button("Son depremler", elem_classes="bubble-btn")
463
+ ex2 = gr.Button("Aktif yangınlar", elem_classes="bubble-btn")
464
+ ex3 = gr.Button("Deprem + Yangınları analiz et", elem_classes="bubble-btn")
465
+
466
+ # Giriş Alanı
467
+ with gr.Row(elem_id="input-container"):
468
+ msg = gr.Textbox(
469
+ placeholder="Afetlerle ilgili ne öğrenmek istersiniz?",
470
+ lines=1,
471
+ max_lines=4,
472
+ show_label=False,
473
+ elem_id="msg_box",
474
+ scale=10
475
+ )
476
+ # Gönder ikonu
477
+ submit_btn = gr.Button("↑", scale=1, elem_id="submit_btn")
478
+
479
+ # Buton Fonksiyonları
480
  ex1.click(lambda: "2026-07-25 ile 2026-07-30 tarihleri arasındaki 5.5 üzeri depremleri listele.", outputs=msg)
481
  ex2.click(lambda: "Şu anki aktif büyük orman yangınları hangileri? 3 tanesini getir.", outputs=msg)
482
  ex3.click(lambda: "Son 7 günde (2026-07-23 ile 2026-07-30) 6.0'dan büyük deprem oldu mu? Ayrıca şu an aktif orman yangını var mı?", outputs=msg)
483
 
484
  def respond(user_message, chat_history):
485
+ if not user_message.strip():
486
+ return chat_history, ""
487
+
488
  chat_history.append((user_message, ""))
489
 
 
490
  for bot_message in chat_interface(user_message, chat_history[:-1]):
491
  chat_history[-1] = (user_message, bot_message)
492
  yield chat_history, ""