Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -386,7 +386,7 @@ def create_ui():
|
|
| 386 |
placeholder="Klik icon (?) untuk panduan",
|
| 387 |
elem_classes="mobile-full",
|
| 388 |
)
|
| 389 |
-
gr.Markdown(GITHUB_TOKEN_HELP)
|
| 390 |
with gr.Column(scale=1):
|
| 391 |
branch = gr.Textbox(
|
| 392 |
label="Branch (opsional)",
|
|
@@ -402,7 +402,7 @@ def create_ui():
|
|
| 402 |
value="", label="Status Repository", elem_classes="mobile-full"
|
| 403 |
)
|
| 404 |
|
| 405 |
-
# File Selection
|
| 406 |
with gr.Group():
|
| 407 |
gr.Markdown("### 📎 File yang Dipilih")
|
| 408 |
|
|
@@ -411,9 +411,10 @@ def create_ui():
|
|
| 411 |
label="Pilih File dari Repository",
|
| 412 |
choices=[],
|
| 413 |
multiselect=True,
|
| 414 |
-
value=[],
|
|
|
|
|
|
|
| 415 |
elem_classes="mobile-full",
|
| 416 |
-
allow_custom_value=False, # Prevent custom values
|
| 417 |
)
|
| 418 |
|
| 419 |
file_list = gr.HTML(
|
|
@@ -421,14 +422,58 @@ def create_ui():
|
|
| 421 |
label="Daftar File Terpilih",
|
| 422 |
)
|
| 423 |
|
| 424 |
-
#
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
# Chat Interface
|
| 434 |
with gr.Group():
|
|
@@ -453,152 +498,105 @@ def create_ui():
|
|
| 453 |
'<div id="loading" style="display:none">Memproses permintaan...</div>'
|
| 454 |
)
|
| 455 |
|
| 456 |
-
|
| 457 |
-
def handle_clone(repo_url, github_token, branch):
|
| 458 |
-
if not repo_url:
|
| 459 |
-
return "⚠️ URL repository diperlukan!", [], ""
|
| 460 |
-
|
| 461 |
-
success, message = analyzer.clone_repository(
|
| 462 |
-
repo_url, github_token, branch
|
| 463 |
-
)
|
| 464 |
-
|
| 465 |
-
if success:
|
| 466 |
-
# Get list of files from cloned repository
|
| 467 |
-
files = sorted(
|
| 468 |
-
list(analyzer.repo_content.keys())
|
| 469 |
-
) # Sort files alphabetically
|
| 470 |
-
return (
|
| 471 |
-
message,
|
| 472 |
-
files,
|
| 473 |
-
"<div class='file-list'>Belum ada file yang dipilih</div>",
|
| 474 |
-
)
|
| 475 |
-
|
| 476 |
-
return (
|
| 477 |
message,
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
def update_file_list(selected):
|
| 483 |
-
if not selected:
|
| 484 |
-
return "<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 485 |
-
|
| 486 |
-
html = "<div class='file-list'>"
|
| 487 |
-
for file in selected:
|
| 488 |
-
html += f"<div class='file-item'><span>{file}</span></div>"
|
| 489 |
-
html += "</div>"
|
| 490 |
-
return html
|
| 491 |
-
|
| 492 |
-
# Connect Events
|
| 493 |
-
clone_button.click(
|
| 494 |
-
fn=handle_clone,
|
| 495 |
-
inputs=[repo_url, github_token, branch],
|
| 496 |
-
outputs=[clone_status, file_selector, file_list],
|
| 497 |
-
)
|
| 498 |
-
|
| 499 |
-
file_selector.change(
|
| 500 |
-
fn=update_file_list, inputs=[file_selector], outputs=[file_list]
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
async def handle_chat(
|
| 504 |
-
message,
|
| 505 |
-
history,
|
| 506 |
-
provider_choice,
|
| 507 |
-
model_name,
|
| 508 |
-
xai_key,
|
| 509 |
-
gemini_key,
|
| 510 |
-
selected_files,
|
| 511 |
-
):
|
| 512 |
-
if not analyzer.current_repo:
|
| 513 |
-
yield history + [
|
| 514 |
-
[
|
| 515 |
-
message,
|
| 516 |
-
"⚠️ Mohon clone repository terlebih dahulu sebelum mengajukan pertanyaan.",
|
| 517 |
-
]
|
| 518 |
-
]
|
| 519 |
-
return
|
| 520 |
-
|
| 521 |
-
history = history or []
|
| 522 |
-
history.append([message, ""])
|
| 523 |
-
|
| 524 |
-
try:
|
| 525 |
-
# Add context about selected files to the prompt
|
| 526 |
-
file_context = ""
|
| 527 |
-
if selected_files:
|
| 528 |
-
file_context = "\n\nFile yang dipilih:\n"
|
| 529 |
-
for file in selected_files:
|
| 530 |
-
content = analyzer.repo_content.get(file, "")
|
| 531 |
-
file_context += f"\n{file}:\n```\n{content}\n```\n"
|
| 532 |
-
|
| 533 |
-
enhanced_message = f"{message}\n{file_context}"
|
| 534 |
-
|
| 535 |
-
full_response = ""
|
| 536 |
-
if provider_choice == AIProvider.XAI:
|
| 537 |
-
async for chunk in analyzer.stream_xai_response(
|
| 538 |
-
enhanced_message, xai_key, model_name
|
| 539 |
-
):
|
| 540 |
-
full_response += chunk
|
| 541 |
-
# Add delay between chunks for readability
|
| 542 |
-
await asyncio.sleep(0.05)
|
| 543 |
-
history[-1][1] = full_response
|
| 544 |
-
yield history
|
| 545 |
-
|
| 546 |
-
elif provider_choice == AIProvider.GEMINI:
|
| 547 |
-
async for chunk in analyzer.stream_gemini_response(
|
| 548 |
-
enhanced_message, gemini_key or DEFAULT_GEMINI_KEY
|
| 549 |
-
):
|
| 550 |
-
full_response += chunk
|
| 551 |
-
# Add delay between chunks for readability
|
| 552 |
-
await asyncio.sleep(0.05)
|
| 553 |
-
history[-1][1] = full_response
|
| 554 |
-
yield history
|
| 555 |
-
|
| 556 |
-
else: # OLLAMA
|
| 557 |
-
response = analyze_with_ollama(model_name, enhanced_message)
|
| 558 |
-
# Simulate streaming for OLLAMA with delay
|
| 559 |
-
words = response.split()
|
| 560 |
-
for i in range(len(words)):
|
| 561 |
-
full_response = " ".join(words[: i + 1])
|
| 562 |
-
await asyncio.sleep(0.05)
|
| 563 |
-
history[-1][1] = full_response
|
| 564 |
-
yield history
|
| 565 |
-
|
| 566 |
-
except Exception as e:
|
| 567 |
-
history[-1][1] = f"⚠️ Error: {str(e)}"
|
| 568 |
-
yield history
|
| 569 |
-
|
| 570 |
-
# Connect chat events with file selection
|
| 571 |
-
send_event = send_button.click(
|
| 572 |
-
fn=handle_chat,
|
| 573 |
-
inputs=[
|
| 574 |
-
chat_input,
|
| 575 |
-
chat_history,
|
| 576 |
-
provider,
|
| 577 |
-
model_dropdown,
|
| 578 |
xai_key,
|
| 579 |
gemini_key,
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
|
| 603 |
|
| 604 |
if __name__ == "__main__":
|
|
|
|
| 386 |
placeholder="Klik icon (?) untuk panduan",
|
| 387 |
elem_classes="mobile-full",
|
| 388 |
)
|
| 389 |
+
gr.Markdown(GITHUB_TOKEN_HELP)
|
| 390 |
with gr.Column(scale=1):
|
| 391 |
branch = gr.Textbox(
|
| 392 |
label="Branch (opsional)",
|
|
|
|
| 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")
|
| 408 |
|
|
|
|
| 411 |
label="Pilih File dari Repository",
|
| 412 |
choices=[],
|
| 413 |
multiselect=True,
|
| 414 |
+
value=[],
|
| 415 |
+
allow_custom_value=True, # Allow custom values
|
| 416 |
+
max_choices=None, # Allow multiple selections
|
| 417 |
elem_classes="mobile-full",
|
|
|
|
| 418 |
)
|
| 419 |
|
| 420 |
file_list = gr.HTML(
|
|
|
|
| 422 |
label="Daftar File Terpilih",
|
| 423 |
)
|
| 424 |
|
| 425 |
+
# Event Handlers
|
| 426 |
+
def handle_clone(repo_url, github_token, branch):
|
| 427 |
+
if not repo_url:
|
| 428 |
+
return (
|
| 429 |
+
"⚠️ URL repository diperlukan!",
|
| 430 |
+
gr.Dropdown(choices=[]),
|
| 431 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>",
|
| 432 |
+
)
|
| 433 |
+
|
| 434 |
+
success, message = analyzer.clone_repository(
|
| 435 |
+
repo_url, github_token, branch
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
if success:
|
| 439 |
+
# Get list of files and sort them
|
| 440 |
+
files = sorted(list(analyzer.repo_content.keys()))
|
| 441 |
+
# Update dropdown with new choices
|
| 442 |
+
return (
|
| 443 |
+
message,
|
| 444 |
+
gr.Dropdown(choices=files, value=[]),
|
| 445 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>",
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
return (
|
| 449 |
+
message,
|
| 450 |
+
gr.Dropdown(choices=[]),
|
| 451 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>",
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
+
def update_file_list(selected):
|
| 455 |
+
if not selected:
|
| 456 |
+
return (
|
| 457 |
+
"<div class='file-list'>Belum ada file yang dipilih</div>"
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
html = "<div class='file-list'>"
|
| 461 |
+
for file in selected:
|
| 462 |
+
html += f"<div class='file-item'><span>{file}</span></div>"
|
| 463 |
+
html += "</div>"
|
| 464 |
+
return html
|
| 465 |
+
|
| 466 |
+
# Connect clone button
|
| 467 |
+
clone_button.click(
|
| 468 |
+
fn=handle_clone,
|
| 469 |
+
inputs=[repo_url, github_token, branch],
|
| 470 |
+
outputs=[clone_status, file_selector, file_list],
|
| 471 |
+
)
|
| 472 |
+
|
| 473 |
+
# Update file list when selection changes
|
| 474 |
+
file_selector.change(
|
| 475 |
+
fn=update_file_list, inputs=[file_selector], outputs=[file_list]
|
| 476 |
+
)
|
| 477 |
|
| 478 |
# Chat Interface
|
| 479 |
with gr.Group():
|
|
|
|
| 498 |
'<div id="loading" style="display:none">Memproses permintaan...</div>'
|
| 499 |
)
|
| 500 |
|
| 501 |
+
async def handle_chat(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
message,
|
| 503 |
+
history,
|
| 504 |
+
provider_choice,
|
| 505 |
+
model_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
xai_key,
|
| 507 |
gemini_key,
|
| 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"
|
| 526 |
+
for file in selected_files:
|
| 527 |
+
content = analyzer.repo_content.get(file, "")
|
| 528 |
+
if content: # Only include files that exist
|
| 529 |
+
file_context += f"\n{file}:\n```\n{content}\n```\n"
|
| 530 |
+
|
| 531 |
+
enhanced_message = f"{message}\n{file_context}"
|
| 532 |
+
|
| 533 |
+
full_response = ""
|
| 534 |
+
if provider_choice == AIProvider.XAI:
|
| 535 |
+
async for chunk in analyzer.stream_xai_response(
|
| 536 |
+
enhanced_message, xai_key, model_name
|
| 537 |
+
):
|
| 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:
|
| 545 |
+
async for chunk in analyzer.stream_gemini_response(
|
| 546 |
+
enhanced_message, gemini_key or DEFAULT_GEMINI_KEY
|
| 547 |
+
):
|
| 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
|
| 555 |
+
response = analyze_with_ollama(model_name, enhanced_message)
|
| 556 |
+
# Simulate streaming for OLLAMA with delay
|
| 557 |
+
words = response.split()
|
| 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,
|
| 571 |
+
inputs=[
|
| 572 |
+
chat_input,
|
| 573 |
+
chat_history,
|
| 574 |
+
provider,
|
| 575 |
+
model_dropdown,
|
| 576 |
+
xai_key,
|
| 577 |
+
gemini_key,
|
| 578 |
+
file_selector,
|
| 579 |
+
],
|
| 580 |
+
outputs=chat_history,
|
| 581 |
+
show_progress=True,
|
| 582 |
+
).then(fn=lambda: gr.update(value=""), outputs=chat_input)
|
| 583 |
+
|
| 584 |
+
input_event = chat_input.submit(
|
| 585 |
+
fn=handle_chat,
|
| 586 |
+
inputs=[
|
| 587 |
+
chat_input,
|
| 588 |
+
chat_history,
|
| 589 |
+
provider,
|
| 590 |
+
model_dropdown,
|
| 591 |
+
xai_key,
|
| 592 |
+
gemini_key,
|
| 593 |
+
file_selector,
|
| 594 |
+
],
|
| 595 |
+
outputs=chat_history,
|
| 596 |
+
show_progress=True,
|
| 597 |
+
).then(fn=lambda: gr.update(value=""), outputs=chat_input)
|
| 598 |
+
|
| 599 |
+
return app
|
| 600 |
|
| 601 |
|
| 602 |
if __name__ == "__main__":
|