ErNewdev0 commited on
Commit
dbb50ea
·
verified ·
1 Parent(s): e279f87

fix: fixed some issues

Browse files
Files changed (1) hide show
  1. app.py +173 -11
app.py CHANGED
@@ -402,6 +402,149 @@ def create_ui():
402
  value="", label="Status Repository", elem_classes="mobile-full"
403
  )
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  # File Selection with improved handling
406
  with gr.Group():
407
  gr.Markdown("### 📎 File yang Dipilih")
@@ -508,18 +651,22 @@ def create_ui():
508
  selected_files,
509
  ):
510
  if not analyzer.current_repo:
511
- yield history + [
512
- [
513
- message,
514
- "⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan.",
515
- ]
516
- ]
 
 
517
  return
518
 
519
  history = history or []
520
- history.append([message, ""])
 
521
 
522
  try:
 
523
  file_context = ""
524
  if selected_files:
525
  file_context = "\n\nFile yang dipilih:\n"
@@ -538,7 +685,7 @@ def create_ui():
538
  full_response += chunk
539
  # Add delay between chunks for readability
540
  await asyncio.sleep(0.05)
541
- history[-1][1] = full_response
542
  yield history
543
 
544
  elif provider_choice == AIProvider.GEMINI:
@@ -548,7 +695,7 @@ def create_ui():
548
  full_response += chunk
549
  # Add delay between chunks for readability
550
  await asyncio.sleep(0.05)
551
- history[-1][1] = full_response
552
  yield history
553
 
554
  else: # OLLAMA
@@ -558,13 +705,28 @@ def create_ui():
558
  for i in range(len(words)):
559
  full_response = " ".join(words[: i + 1])
560
  await asyncio.sleep(0.05)
561
- history[-1][1] = full_response
562
  yield history
563
 
564
  except Exception as e:
565
- history[-1][1] = f"⚠️ Error: {str(e)}"
566
  yield history
567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
  # Connect chat events with file selection
569
  send_event = send_button.click(
570
  fn=handle_chat,
 
402
  value="", label="Status Repository", elem_classes="mobile-full"
403
  )
404
 
405
+ with gr.Tab("💡 Examples"):
406
+
407
+ def generate_examples(selected_files):
408
+ if not selected_files:
409
+ return "Pilih file terlebih dahulu di tab Analisis Repository untuk mendapatkan contoh pertanyaan yang relevan."
410
+
411
+ examples = "<div class='example-list'>"
412
+
413
+ # General examples for any file
414
+ examples += """
415
+ <h3>Contoh Pertanyaan Umum:</h3>
416
+ <ul>
417
+ """
418
+
419
+ file_names = ", ".join(
420
+ [f"<code>{f}</code>" for f in selected_files]
421
+ )
422
+ examples += f"""
423
+ <li><strong>Analisis Kode:</strong> "Jelaskan logika dan fungsi utama dari {file_names}"</li>
424
+ <li><strong>Deteksi Bug:</strong> "Apakah ada potensi bug atau masalah keamanan di file-file ini?"</li>
425
+ <li><strong>Best Practices:</strong> "Bagaimana cara mengoptimalkan kode di {file_names}?"</li>
426
+ """
427
+
428
+ # Specific examples based on file types
429
+ for file in selected_files:
430
+ ext = file.split(".")[-1].lower() if "." in file else ""
431
+ examples += f"<h4>Contoh untuk {file}:</h4><ul>"
432
+
433
+ if ext in ["py", "js", "java", "cpp", "c", "go"]:
434
+ examples += f"""
435
+ <li>"Jelaskan fungsi-fungsi utama di {file}"</li>
436
+ <li>"Bagaimana cara mengoptimalkan performa di {file}?"</li>
437
+ <li>"Buat unit test untuk fungsi-fungsi di {file}"</li>
438
+ """
439
+ elif ext in ["html", "css"]:
440
+ examples += f"""
441
+ <li>"Analisis struktur dan layout dari {file}"</li>
442
+ <li>"Bagaimana cara membuat {file} lebih responsif?"</li>
443
+ <li>"Optimasi untuk mobile view di {file}"</li>
444
+ """
445
+ elif ext == "md":
446
+ examples += f"""
447
+ <li>"Ringkas isi dokumentasi dari {file}"</li>
448
+ <li>"Buat tabel konten untuk {file}"</li>
449
+ <li>"Perbaiki formatting di {file}"</li>
450
+ """
451
+ elif ext in ["json", "yaml", "yml"]:
452
+ examples += f"""
453
+ <li>"Validasi struktur data di {file}"</li>
454
+ <li>"Jelaskan konfigurasi di {file}"</li>
455
+ <li>"Optimasi format di {file}"</li>
456
+ """
457
+ elif ext == "dockerfile":
458
+ examples += f"""
459
+ <li>"Analisis keamanan dari {file}"</li>
460
+ <li>"Optimasi multi-stage build di {file}"</li>
461
+ <li>"Best practices untuk {file}"</li>
462
+ """
463
+ else:
464
+ examples += f"""
465
+ <li>"Analisis isi dari {file}"</li>
466
+ <li>"Jelaskan struktur dan tujuan {file}"</li>
467
+ <li>"Saran perbaikan untuk {file}"</li>
468
+ """
469
+ examples += "</ul>"
470
+
471
+ examples += """
472
+ <h3>Tips Bertanya:</h3>
473
+ <ul>
474
+ <li>Gunakan pertanyaan yang spesifik dan fokus pada bagian tertentu</li>
475
+ <li>Sebutkan nama file jika bertanya tentang file tertentu</li>
476
+ <li>Jelaskan konteks atau masalah yang ingin diselesaikan</li>
477
+ </ul>
478
+ """
479
+
480
+ examples += "</div>"
481
+
482
+ # Add some CSS styling
483
+ examples = f"""
484
+ <style>
485
+ .example-list {{
486
+ padding: 20px;
487
+ background: #f8f9fa;
488
+ border-radius: 8px;
489
+ border: 1px solid #e9ecef;
490
+ }}
491
+ .example-list h3 {{
492
+ color: #2c3e50;
493
+ margin-top: 20px;
494
+ margin-bottom: 10px;
495
+ }}
496
+ .example-list h4 {{
497
+ color: #34495e;
498
+ margin-top: 15px;
499
+ margin-bottom: 5px;
500
+ }}
501
+ .example-list ul {{
502
+ margin-bottom: 15px;
503
+ }}
504
+ .example-list li {{
505
+ margin: 8px 0;
506
+ line-height: 1.5;
507
+ }}
508
+ .example-list code {{
509
+ background: #e9ecef;
510
+ padding: 2px 5px;
511
+ border-radius: 4px;
512
+ font-family: monospace;
513
+ }}
514
+ .example-list strong {{
515
+ color: #2c3e50;
516
+ }}
517
+ </style>
518
+ {examples}
519
+ """
520
+
521
+ return examples
522
+
523
+ # Example output area
524
+ example_output = gr.HTML(
525
+ value="Pilih file di tab Analisis Repository untuk melihat contoh pertanyaan.",
526
+ label="Contoh Pertanyaan",
527
+ )
528
+
529
+ # Update examples when switching to this tab
530
+ def on_tab_select(selected_files):
531
+ if selected_files:
532
+ return generate_examples(selected_files)
533
+ return "Pilih file terlebih dahulu di tab Analisis Repository untuk mendapatkan contoh pertanyaan yang relevan."
534
+
535
+ tabs.select(
536
+ fn=on_tab_select,
537
+ inputs=[file_selector],
538
+ outputs=[example_output],
539
+ api_name=False,
540
+ )
541
+
542
+ # Also update when file selection changes
543
+ file_selector.change(
544
+ fn=generate_examples,
545
+ inputs=[file_selector],
546
+ outputs=[example_output],
547
+ )
548
  # File Selection with improved handling
549
  with gr.Group():
550
  gr.Markdown("### 📎 File yang Dipilih")
 
651
  selected_files,
652
  ):
653
  if not analyzer.current_repo:
654
+ new_message = {
655
+ "role": "assistant",
656
+ "content": "⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan.",
657
+ }
658
+ history = history or []
659
+ history.append({"role": "user", "content": message})
660
+ history.append(new_message)
661
+ yield history
662
  return
663
 
664
  history = history or []
665
+ history.append({"role": "user", "content": message})
666
+ history.append({"role": "assistant", "content": ""})
667
 
668
  try:
669
+ # Add context about selected files to the prompt
670
  file_context = ""
671
  if selected_files:
672
  file_context = "\n\nFile yang dipilih:\n"
 
685
  full_response += chunk
686
  # Add delay between chunks for readability
687
  await asyncio.sleep(0.05)
688
+ history[-1]["content"] = full_response
689
  yield history
690
 
691
  elif provider_choice == AIProvider.GEMINI:
 
695
  full_response += chunk
696
  # Add delay between chunks for readability
697
  await asyncio.sleep(0.05)
698
+ history[-1]["content"] = full_response
699
  yield history
700
 
701
  else: # OLLAMA
 
705
  for i in range(len(words)):
706
  full_response = " ".join(words[: i + 1])
707
  await asyncio.sleep(0.05)
708
+ history[-1]["content"] = full_response
709
  yield history
710
 
711
  except Exception as e:
712
+ history[-1]["content"] = f"⚠️ Error: {str(e)}"
713
  yield history
714
 
715
+ # Update chat interface definition
716
+ chat_history = gr.Chatbot(
717
+ label="📝 Riwayat Chat",
718
+ height=500,
719
+ show_label=True,
720
+ type="messages", # Make sure this is set
721
+ elem_classes="mobile-full",
722
+ )
723
+
724
+ def clear_chat_history():
725
+ return []
726
+
727
+ clear_button = gr.Button("🧹 Bersihkan Chat", variant="secondary")
728
+ clear_button.click(fn=clear_chat_history, outputs=[chat_history])
729
+
730
  # Connect chat events with file selection
731
  send_event = send_button.click(
732
  fn=handle_chat,