Spaces:
Sleeping
Sleeping
filters warning implemented
Browse files
app.py
CHANGED
|
@@ -391,14 +391,16 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
| 391 |
dropdown_sources = gr.Dropdown(
|
| 392 |
["Consolidated","Ministry, Department, Agency and Projects","Local Government","Value for Money","Thematic"],
|
| 393 |
label="Select Report Category",
|
| 394 |
-
value=
|
|
|
|
| 395 |
interactive=True,
|
| 396 |
)
|
| 397 |
|
| 398 |
#------ second level filter for selecting subtype within the report category selected above
|
| 399 |
dropdown_category = gr.Dropdown(
|
| 400 |
new_files["Consolidated"],
|
| 401 |
-
value
|
|
|
|
| 402 |
label = "Filter for Sub-Type",
|
| 403 |
interactive=True)
|
| 404 |
|
|
@@ -412,7 +414,8 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
| 412 |
['2018','2019','2020','2021','2022','2023'],
|
| 413 |
label="Filter for year",
|
| 414 |
multiselect=True,
|
| 415 |
-
value=
|
|
|
|
| 416 |
interactive=True,
|
| 417 |
)
|
| 418 |
gr.Markdown("-------------------------------------------------------------------------")
|
|
@@ -421,7 +424,8 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
| 421 |
new_report_list,
|
| 422 |
label="Or select specific reports",
|
| 423 |
multiselect=True,
|
| 424 |
-
value=
|
|
|
|
| 425 |
interactive=True,)
|
| 426 |
|
| 427 |
############### tab for Question selection ###############
|
|
@@ -589,6 +593,20 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
| 589 |
"""Handle 'not okay' feedback submission"""
|
| 590 |
return submit_feedback("not_okay", logs_data)
|
| 591 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
okay_btn.click(
|
| 593 |
submit_feedback_okay,
|
| 594 |
[feedback_state],
|
|
@@ -606,24 +624,164 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
| 606 |
# Add these state components at the top level of the Blocks
|
| 607 |
session_id = gr.State(None)
|
| 608 |
client_ip = gr.State(None)
|
|
|
|
| 609 |
|
| 610 |
@demo.load(api_name="get_client_ip")
|
| 611 |
def get_client_ip_handler(dummy_input="", request: gr.Request = None):
|
| 612 |
"""Handler for getting client IP in Gradio context"""
|
| 613 |
return get_client_ip(request)
|
| 614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
#-------------------- Gradio voodoo -------------------------
|
| 616 |
|
| 617 |
# Update the event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
(textbox
|
| 619 |
-
.submit(
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
[
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
|
| 628 |
(examples_hidden
|
| 629 |
.change(start_chat, [examples_hidden, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_examples")
|
|
|
|
| 391 |
dropdown_sources = gr.Dropdown(
|
| 392 |
["Consolidated","Ministry, Department, Agency and Projects","Local Government","Value for Money","Thematic"],
|
| 393 |
label="Select Report Category",
|
| 394 |
+
value=None,
|
| 395 |
+
# value="Consolidated",
|
| 396 |
interactive=True,
|
| 397 |
)
|
| 398 |
|
| 399 |
#------ second level filter for selecting subtype within the report category selected above
|
| 400 |
dropdown_category = gr.Dropdown(
|
| 401 |
new_files["Consolidated"],
|
| 402 |
+
value=None,
|
| 403 |
+
# value = new_files["Consolidated"],
|
| 404 |
label = "Filter for Sub-Type",
|
| 405 |
interactive=True)
|
| 406 |
|
|
|
|
| 414 |
['2018','2019','2020','2021','2022','2023'],
|
| 415 |
label="Filter for year",
|
| 416 |
multiselect=True,
|
| 417 |
+
value=None,
|
| 418 |
+
# value=['2023'],
|
| 419 |
interactive=True,
|
| 420 |
)
|
| 421 |
gr.Markdown("-------------------------------------------------------------------------")
|
|
|
|
| 424 |
new_report_list,
|
| 425 |
label="Or select specific reports",
|
| 426 |
multiselect=True,
|
| 427 |
+
value=None,
|
| 428 |
+
# value=[],
|
| 429 |
interactive=True,)
|
| 430 |
|
| 431 |
############### tab for Question selection ###############
|
|
|
|
| 593 |
"""Handle 'not okay' feedback submission"""
|
| 594 |
return submit_feedback("not_okay", logs_data)
|
| 595 |
|
| 596 |
+
def handle_feedback(logs_data):
|
| 597 |
+
"""Handle feedback display with proper output format"""
|
| 598 |
+
if logs_data is None:
|
| 599 |
+
return (
|
| 600 |
+
gr.update(visible=False), # feedback_row
|
| 601 |
+
gr.update(visible=False), # feedback_thanks
|
| 602 |
+
None # feedback_state
|
| 603 |
+
)
|
| 604 |
+
return (
|
| 605 |
+
gr.update(visible=True), # feedback_row
|
| 606 |
+
gr.update(visible=False), # feedback_thanks
|
| 607 |
+
logs_data # feedback_state
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
okay_btn.click(
|
| 611 |
submit_feedback_okay,
|
| 612 |
[feedback_state],
|
|
|
|
| 624 |
# Add these state components at the top level of the Blocks
|
| 625 |
session_id = gr.State(None)
|
| 626 |
client_ip = gr.State(None)
|
| 627 |
+
|
| 628 |
|
| 629 |
@demo.load(api_name="get_client_ip")
|
| 630 |
def get_client_ip_handler(dummy_input="", request: gr.Request = None):
|
| 631 |
"""Handler for getting client IP in Gradio context"""
|
| 632 |
return get_client_ip(request)
|
| 633 |
|
| 634 |
+
#-------------------- Filter Warning -------------------------
|
| 635 |
+
warning_state = gr.State(False)
|
| 636 |
+
pending_query = gr.State(None)
|
| 637 |
+
|
| 638 |
+
with gr.Row(visible=False) as warning_row:
|
| 639 |
+
gr.Markdown("**No report filter selected. Are you sure you want to proceed?**")
|
| 640 |
+
with gr.Row():
|
| 641 |
+
proceed_btn = gr.Button("Proceed")
|
| 642 |
+
cancel_btn = gr.Button("Cancel")
|
| 643 |
+
|
| 644 |
+
def show_warning():
|
| 645 |
+
"""Show warning popup when no filters selected"""
|
| 646 |
+
return gr.update(visible=True)
|
| 647 |
+
|
| 648 |
+
def hide_warning():
|
| 649 |
+
"""Hide warning popup"""
|
| 650 |
+
return gr.update(visible=False)
|
| 651 |
+
|
| 652 |
+
def check_filters(textbox_value, sources, reports, subtype, year):
|
| 653 |
+
"""Check if any filters are selected"""
|
| 654 |
+
no_filters = (not reports) and (not sources) and (not subtype) and (not year)
|
| 655 |
+
if no_filters:
|
| 656 |
+
# If no filters, show warning and stop chain
|
| 657 |
+
return (
|
| 658 |
+
True, # warning state
|
| 659 |
+
gr.update(visible=True), # warning row visibility
|
| 660 |
+
None, # textbox value (None stops the chain)
|
| 661 |
+
textbox_value # store the query
|
| 662 |
+
)
|
| 663 |
+
# If filters exist, proceed normally
|
| 664 |
+
return (
|
| 665 |
+
False,
|
| 666 |
+
gr.update(visible=False),
|
| 667 |
+
textbox_value, # keep the original value
|
| 668 |
+
None # no need to store query
|
| 669 |
+
)
|
| 670 |
+
|
| 671 |
+
async def handle_chat_flow(warning_active, query, chatbot, sources, reports, subtype, year, client_ip, session_id):
|
| 672 |
+
"""Handle chat flow with hard stop for warnings"""
|
| 673 |
+
# Check if any filters are selected
|
| 674 |
+
no_filters = (not reports) and (not sources) and (not subtype) and (not year)
|
| 675 |
+
|
| 676 |
+
if warning_active: # Only check warning_active, not no_filters
|
| 677 |
+
# If warning is active, return unchanged components
|
| 678 |
+
return (
|
| 679 |
+
chatbot, # unchanged chatbot
|
| 680 |
+
"", # empty sources
|
| 681 |
+
None, # no feedback state
|
| 682 |
+
session_id # keep session
|
| 683 |
+
)
|
| 684 |
+
|
| 685 |
+
# Include start_chat functionality here
|
| 686 |
+
history = chatbot + [(query, None)]
|
| 687 |
+
history = [tuple(x) for x in history]
|
| 688 |
+
|
| 689 |
+
# Proceed with chat
|
| 690 |
+
async for update in chat(query, history, sources, reports, subtype, year, client_ip, session_id):
|
| 691 |
+
# The last update will be returned
|
| 692 |
+
chatbot_update, sources_update, feedback_update, session_update = update
|
| 693 |
+
|
| 694 |
+
return (
|
| 695 |
+
chatbot_update,
|
| 696 |
+
sources_update,
|
| 697 |
+
feedback_update,
|
| 698 |
+
session_update
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
#-------------------- Gradio voodoo -------------------------
|
| 702 |
|
| 703 |
# Update the event handlers
|
| 704 |
+
# (textbox
|
| 705 |
+
# .submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
| 706 |
+
# .then(start_chat, [textbox, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_textbox")
|
| 707 |
+
# .then(chat,
|
| 708 |
+
# [textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, dropdown_year, client_ip, session_id],
|
| 709 |
+
# [chatbot, sources_textbox, feedback_state, session_id],
|
| 710 |
+
# queue=True, concurrency_limit=8, api_name="chat_textbox")
|
| 711 |
+
# .then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_textbox")
|
| 712 |
+
# .then(finish_chat, None, [textbox], api_name="finish_chat_textbox"))
|
| 713 |
+
|
| 714 |
(textbox
|
| 715 |
+
.submit(
|
| 716 |
+
check_filters,
|
| 717 |
+
[textbox, dropdown_sources, dropdown_reports, dropdown_category, dropdown_year],
|
| 718 |
+
[warning_state, warning_row, textbox, pending_query]
|
| 719 |
+
)
|
| 720 |
+
.then(
|
| 721 |
+
get_client_ip_handler,
|
| 722 |
+
[textbox],
|
| 723 |
+
[client_ip],
|
| 724 |
+
show_progress=False
|
| 725 |
+
)
|
| 726 |
+
.then( # Remove start_chat from here
|
| 727 |
+
handle_chat_flow,
|
| 728 |
+
[warning_state, textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, dropdown_year, client_ip, session_id],
|
| 729 |
+
[chatbot, sources_textbox, feedback_state, session_id]
|
| 730 |
+
)
|
| 731 |
+
.then(
|
| 732 |
+
handle_feedback,
|
| 733 |
+
[feedback_state],
|
| 734 |
+
[feedback_row, feedback_thanks, feedback_state]
|
| 735 |
+
)
|
| 736 |
+
.then(
|
| 737 |
+
finish_chat,
|
| 738 |
+
None,
|
| 739 |
+
[textbox]
|
| 740 |
+
))
|
| 741 |
+
|
| 742 |
+
|
| 743 |
+
# Add handlers for the warning buttons
|
| 744 |
+
proceed_btn.click(
|
| 745 |
+
lambda query: (
|
| 746 |
+
False, # warning state
|
| 747 |
+
gr.update(visible=False), # warning row
|
| 748 |
+
gr.update(value=query if query else "", interactive=True), # restore query
|
| 749 |
+
None # clear pending query
|
| 750 |
+
),
|
| 751 |
+
pending_query,
|
| 752 |
+
[warning_state, warning_row, textbox, pending_query]
|
| 753 |
+
).then(
|
| 754 |
+
lambda: False, # Force warning_state to False
|
| 755 |
+
None,
|
| 756 |
+
warning_state
|
| 757 |
+
).then(
|
| 758 |
+
get_client_ip_handler,
|
| 759 |
+
[textbox],
|
| 760 |
+
[client_ip]
|
| 761 |
+
).then( # Remove start_chat call and let handle_chat_flow handle the history update
|
| 762 |
+
handle_chat_flow,
|
| 763 |
+
[warning_state, textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, dropdown_year, client_ip, session_id],
|
| 764 |
+
[chatbot, sources_textbox, feedback_state, session_id]
|
| 765 |
+
).then(
|
| 766 |
+
handle_feedback,
|
| 767 |
+
[feedback_state],
|
| 768 |
+
[feedback_row, feedback_thanks, feedback_state]
|
| 769 |
+
).then(
|
| 770 |
+
finish_chat,
|
| 771 |
+
None,
|
| 772 |
+
[textbox]
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
cancel_btn.click(
|
| 776 |
+
lambda: (
|
| 777 |
+
False,
|
| 778 |
+
gr.update(visible=False),
|
| 779 |
+
gr.update(value="", interactive=True),
|
| 780 |
+
None
|
| 781 |
+
),
|
| 782 |
+
None,
|
| 783 |
+
[warning_state, warning_row, textbox, pending_query]
|
| 784 |
+
)
|
| 785 |
|
| 786 |
(examples_hidden
|
| 787 |
.change(start_chat, [examples_hidden, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_examples")
|