ErNewdev0 commited on
Commit
957d623
ยท
verified ยท
1 Parent(s): 5152acc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -76
app.py CHANGED
@@ -279,8 +279,8 @@ class RepoAnalyzer:
279
  return False, "Tidak dapat membaca file dengan encoding yang didukung"
280
 
281
  def create_ui():
282
- # Get UTC time
283
- utc_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
284
  analyzer = RepoAnalyzer()
285
 
286
  with gr.Blocks(title="Repository Chat Analysis", theme=gr.themes.Soft()) as app:
@@ -368,11 +368,7 @@ def create_ui():
368
  """)
369
 
370
  with gr.Row(elem_classes="container"):
371
- gr.Markdown(f"""
372
- # ๐Ÿค– Repository Chat Analysis
373
-
374
- ๐Ÿ“… Current Date and Time (UTC): {utc_time}
375
- """)
376
 
377
  with gr.Tabs() as tabs:
378
  with gr.Tab("๐Ÿ› ๏ธ Konfigurasi", elem_classes="mobile-full"):
@@ -496,67 +492,69 @@ def create_ui():
496
  '<div id="loading" style="display:none">Memproses permintaan...</div>'
497
  )
498
 
499
- async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key, selected_files):
500
- if not analyzer.current_repo:
501
- yield history + [[message, "โš ๏ธ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."]]
502
- return
503
-
504
- history = history or []
505
- history.append([message, ""])
506
-
507
- try:
508
- # Add context about selected files to the prompt
509
- file_context = ""
510
- if selected_files:
511
- file_context = "\n\nFile yang dipilih:\n"
512
- for file in selected_files:
513
- content = analyzer.repo_content.get(file, "")
514
- # Wrap code blocks in custom artifact styling
515
- file_context += f"""
516
- <div class="wrapper-artifact">
517
- <div class="header-artifact">
518
- <span>{file}</span>
519
- <button class="copy-button" onclick="copyToClipboard(`{content.replace('`', '\\`')}`)">Copy</button>
520
- </div>
521
- <div class="content-artifact">
522
- <pre><code>{content}</code></pre>
523
- </div>
524
- </div>
525
- """
526
 
527
- enhanced_message = f"{message}\n{file_context}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528
 
529
- full_response = ""
530
- if provider_choice == AIProvider.XAI:
531
- async for chunk in analyzer.stream_xai_response(enhanced_message, xai_key, model_name):
532
- # Wrap code blocks in custom styling
533
- chunk = process_code_blocks(chunk)
534
- full_response += chunk
535
- await asyncio.sleep(0.08)
536
- history[-1][1] = full_response
537
- yield history
538
-
539
- elif provider_choice == AIProvider.GEMINI:
540
- async for chunk in analyzer.stream_gemini_response(enhanced_message, gemini_key or DEFAULT_GEMINI_KEY):
541
- chunk = process_code_blocks(chunk)
542
- full_response += chunk
543
- await asyncio.sleep(0.08)
544
- history[-1][1] = full_response
545
- yield history
546
-
547
- else: # OLLAMA
548
- response = analyze_with_ollama(model_name, enhanced_message)
549
- response = process_code_blocks(response)
550
- words = response.split()
551
- for i in range(len(words)):
552
- full_response = " ".join(words[:i+1])
553
- await asyncio.sleep(0.08)
554
- history[-1][1] = full_response
555
- yield history
556
-
557
- except Exception as e:
558
- history[-1][1] = f"โš ๏ธ Error: {str(e)}"
559
  yield history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
 
561
  def process_code_blocks(text):
562
  """Process markdown code blocks to use custom artifact styling"""
@@ -568,17 +566,20 @@ def process_code_blocks(text):
568
  def replace_code_block(match):
569
  language = match.group(1)
570
  code = match.group(2)
571
- return f"""
572
- <div class="wrapper-artifact">
573
- <div class="header-artifact">
574
- <span>{language}</span>
575
- <button class="copy-button" onclick="copyToClipboard(`{code.replace('`', '\\`')}`)">Copy</button>
576
- </div>
577
- <div class="content-artifact">
578
- <pre><code>{code}</code></pre>
579
- </div>
580
- </div>
581
- """
 
 
 
582
 
583
  # Replace all code blocks in the text
584
  processed_text = re.sub(pattern, replace_code_block, text, flags=re.DOTALL)
 
279
  return False, "Tidak dapat membaca file dengan encoding yang didukung"
280
 
281
  def create_ui():
282
+ # Get local time
283
+ local_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
284
  analyzer = RepoAnalyzer()
285
 
286
  with gr.Blocks(title="Repository Chat Analysis", theme=gr.themes.Soft()) as app:
 
368
  """)
369
 
370
  with gr.Row(elem_classes="container"):
371
+ gr.Markdown(f"# ๐Ÿค– Repository Chat Analysis\n\n๐Ÿ“… {local_time}")
 
 
 
 
372
 
373
  with gr.Tabs() as tabs:
374
  with gr.Tab("๐Ÿ› ๏ธ Konfigurasi", elem_classes="mobile-full"):
 
492
  '<div id="loading" style="display:none">Memproses permintaan...</div>'
493
  )
494
 
495
+ async def handle_chat(message, history, provider_choice, model_name, xai_key, gemini_key, selected_files):
496
+ if not analyzer.current_repo:
497
+ yield history + [[message, "โš ๏ธ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan."]]
498
+ return
499
+
500
+ history = history or []
501
+ history.append([message, ""])
502
+
503
+ try:
504
+ # Add context about selected files to the prompt
505
+ file_context = ""
506
+ if selected_files:
507
+ file_context = "\n\nFile yang dipilih:\n"
508
+ for file in selected_files:
509
+ content = analyzer.repo_content.get(file, "")
510
+ escaped_content = content.replace('`', r'\`')
 
 
 
 
 
 
 
 
 
 
 
511
 
512
+ html = (
513
+ '<div class="wrapper-artifact">'
514
+ f'<div class="header-artifact">'
515
+ f'<span>{file}</span>'
516
+ f'<button class="copy-button" onclick="copyToClipboard(`{escaped_content}`)">Copy</button>'
517
+ '</div>'
518
+ '<div class="content-artifact">'
519
+ f'<pre><code>{content}</code></pre>'
520
+ '</div>'
521
+ '</div>'
522
+ )
523
+ file_context += html
524
+
525
+ enhanced_message = f"{message}\n{file_context}"
526
+
527
+ full_response = ""
528
+ if provider_choice == AIProvider.XAI:
529
+ async for chunk in analyzer.stream_xai_response(enhanced_message, xai_key, model_name):
530
+ # Wrap code blocks in custom styling
531
+ chunk = process_code_blocks(chunk)
532
+ full_response += chunk
533
+ await asyncio.sleep(0.08)
534
+ history[-1][1] = full_response
535
+ yield history
536
 
537
+ elif provider_choice == AIProvider.GEMINI:
538
+ async for chunk in analyzer.stream_gemini_response(enhanced_message, gemini_key or DEFAULT_GEMINI_KEY):
539
+ chunk = process_code_blocks(chunk)
540
+ full_response += chunk
541
+ await asyncio.sleep(0.08)
542
+ history[-1][1] = full_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
  yield history
544
+
545
+ else: # OLLAMA
546
+ response = analyze_with_ollama(model_name, enhanced_message)
547
+ response = process_code_blocks(response)
548
+ words = response.split()
549
+ for i in range(len(words)):
550
+ full_response = " ".join(words[:i+1])
551
+ await asyncio.sleep(0.08)
552
+ history[-1][1] = full_response
553
+ yield history
554
+
555
+ except Exception as e:
556
+ history[-1][1] = f"โš ๏ธ Error: {str(e)}"
557
+ yield history
558
 
559
  def process_code_blocks(text):
560
  """Process markdown code blocks to use custom artifact styling"""
 
566
  def replace_code_block(match):
567
  language = match.group(1)
568
  code = match.group(2)
569
+ escaped_code = code.replace('`', r'\`')
570
+
571
+ html = (
572
+ '<div class="wrapper-artifact">'
573
+ f'<div class="header-artifact">'
574
+ f'<span>{language}</span>'
575
+ f'<button class="copy-button" onclick="copyToClipboard(`{escaped_code}`)">Copy</button>'
576
+ '</div>'
577
+ '<div class="content-artifact">'
578
+ f'<pre><code>{code}</code></pre>'
579
+ '</div>'
580
+ '</div>'
581
+ )
582
+ return html
583
 
584
  # Replace all code blocks in the text
585
  processed_text = re.sub(pattern, replace_code_block, text, flags=re.DOTALL)