Spaces:
Sleeping
Sleeping
update tambah reset_session, tidak bisa ganti model di tengah chat
Browse files
app.py
CHANGED
|
@@ -447,6 +447,7 @@ def create_gradio_interface():
|
|
| 447 |
with gr.Blocks(title="Chat with CSV using AI Models") as interface:
|
| 448 |
session_id = gr.State(lambda: str(uuid.uuid4()))
|
| 449 |
chatbot_state = gr.State(lambda: None)
|
|
|
|
| 450 |
|
| 451 |
# Get model choices
|
| 452 |
model_choices = list(MODEL_CONFIG.keys())
|
|
@@ -457,25 +458,27 @@ def create_gradio_interface():
|
|
| 457 |
|
| 458 |
with gr.Row():
|
| 459 |
with gr.Column(scale=1):
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
file_types=[".csv"]
|
| 463 |
-
)
|
| 464 |
-
|
| 465 |
-
# Model selection accordion BEFORE process button
|
| 466 |
-
with gr.Accordion("Pilih Model AI", open=True):
|
| 467 |
model_dropdown = gr.Dropdown(
|
| 468 |
label="Model",
|
| 469 |
choices=model_choices,
|
| 470 |
-
value=default_model
|
|
|
|
| 471 |
)
|
| 472 |
model_info = gr.Markdown(
|
| 473 |
value=f"**{default_model}**: {MODEL_CONFIG[default_model]['description']}"
|
| 474 |
)
|
| 475 |
-
change_model_button = gr.Button("Terapkan Perubahan Model")
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
|
| 480 |
with gr.Column(scale=2):
|
| 481 |
chatbot_interface = gr.Chatbot(
|
|
@@ -500,16 +503,34 @@ def create_gradio_interface():
|
|
| 500 |
outputs=[model_info]
|
| 501 |
)
|
| 502 |
|
| 503 |
-
# Process file handler
|
| 504 |
def handle_process_file(file, model_key, sess_id):
|
|
|
|
|
|
|
|
|
|
| 505 |
chatbot = ChatBot(sess_id, model_key)
|
| 506 |
result = chatbot.process_file(file)
|
| 507 |
-
return chatbot, [(None, result)]
|
| 508 |
|
| 509 |
process_button.click(
|
| 510 |
fn=handle_process_file,
|
| 511 |
inputs=[file_input, model_dropdown, session_id],
|
| 512 |
-
outputs=[chatbot_state, chatbot_interface]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
)
|
| 514 |
|
| 515 |
# Change model handler
|
|
|
|
| 447 |
with gr.Blocks(title="Chat with CSV using AI Models") as interface:
|
| 448 |
session_id = gr.State(lambda: str(uuid.uuid4()))
|
| 449 |
chatbot_state = gr.State(lambda: None)
|
| 450 |
+
model_selected = gr.State(lambda: False) # Track if model is already in use
|
| 451 |
|
| 452 |
# Get model choices
|
| 453 |
model_choices = list(MODEL_CONFIG.keys())
|
|
|
|
| 458 |
|
| 459 |
with gr.Row():
|
| 460 |
with gr.Column(scale=1):
|
| 461 |
+
with gr.Box():
|
| 462 |
+
gr.Markdown("### Langkah 1: Pilih Model AI")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
model_dropdown = gr.Dropdown(
|
| 464 |
label="Model",
|
| 465 |
choices=model_choices,
|
| 466 |
+
value=default_model,
|
| 467 |
+
interactive=True
|
| 468 |
)
|
| 469 |
model_info = gr.Markdown(
|
| 470 |
value=f"**{default_model}**: {MODEL_CONFIG[default_model]['description']}"
|
| 471 |
)
|
|
|
|
| 472 |
|
| 473 |
+
with gr.Box():
|
| 474 |
+
gr.Markdown("### Langkah 2: Unggah dan Proses CSV")
|
| 475 |
+
file_input = gr.File(
|
| 476 |
+
label="Upload CSV Anda",
|
| 477 |
+
file_types=[".csv"]
|
| 478 |
+
)
|
| 479 |
+
process_button = gr.Button("Proses CSV")
|
| 480 |
+
|
| 481 |
+
reset_button = gr.Button("Reset Sesi (Untuk Ganti Model)")
|
| 482 |
|
| 483 |
with gr.Column(scale=2):
|
| 484 |
chatbot_interface = gr.Chatbot(
|
|
|
|
| 503 |
outputs=[model_info]
|
| 504 |
)
|
| 505 |
|
| 506 |
+
# Process file handler - disables model selection after file is processed
|
| 507 |
def handle_process_file(file, model_key, sess_id):
|
| 508 |
+
if file is None:
|
| 509 |
+
return None, None, False, "Mohon upload file CSV terlebih dahulu."
|
| 510 |
+
|
| 511 |
chatbot = ChatBot(sess_id, model_key)
|
| 512 |
result = chatbot.process_file(file)
|
| 513 |
+
return chatbot, True, [(None, result)]
|
| 514 |
|
| 515 |
process_button.click(
|
| 516 |
fn=handle_process_file,
|
| 517 |
inputs=[file_input, model_dropdown, session_id],
|
| 518 |
+
outputs=[chatbot_state, model_selected, chatbot_interface]
|
| 519 |
+
).then(
|
| 520 |
+
# Disable model dropdown after processing file
|
| 521 |
+
fn=lambda selected: gr.update(interactive=not selected),
|
| 522 |
+
inputs=[model_selected],
|
| 523 |
+
outputs=[model_dropdown]
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
+
# Reset handler - enables model selection again
|
| 527 |
+
def reset_session():
|
| 528 |
+
return None, False, [], gr.update(interactive=True)
|
| 529 |
+
|
| 530 |
+
reset_button.click(
|
| 531 |
+
fn=reset_session,
|
| 532 |
+
inputs=[],
|
| 533 |
+
outputs=[chatbot_state, model_selected, chatbot_interface, model_dropdown]
|
| 534 |
)
|
| 535 |
|
| 536 |
# Change model handler
|