Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -147,31 +147,6 @@ async def chat(query,history,sources,reports,subtype, client_ip=None, session_id
|
|
147 |
sources=sources,subtype=subtype)
|
148 |
end_time = time.time()
|
149 |
print("Time for retriever:",end_time - start_time)
|
150 |
-
|
151 |
-
# WARNING FOR NO CONTEXT: Check if any paragraphs were retrieved, add warning if none found
|
152 |
-
# We use this in the Gradio UI below (displays in the chat dialogue box)
|
153 |
-
if not context_retrieved or len(context_retrieved) == 0:
|
154 |
-
warning_message = "⚠️ **No relevant information was found in the audit reports pertaining your query.** Please try rephrasing your question or selecting different report filters."
|
155 |
-
history[-1] = (query, warning_message)
|
156 |
-
# Update logs with the warning instead of answer
|
157 |
-
logs_data = {
|
158 |
-
"record_id": str(uuid4()),
|
159 |
-
"session_id": session_id,
|
160 |
-
"session_duration_seconds": session_duration,
|
161 |
-
"client_location": session_data['location_info'],
|
162 |
-
"platform": session_data['platform_info'],
|
163 |
-
"question": query,
|
164 |
-
"retriever": model_config.get('retriever','MODEL'),
|
165 |
-
"endpoint_type": model_config.get('reader','TYPE'),
|
166 |
-
"reader": model_config.get('reader','NVIDIA_MODEL'),
|
167 |
-
"answer": warning_message,
|
168 |
-
"no_results": True # Flag to indicate no results were found
|
169 |
-
}
|
170 |
-
yield [tuple(x) for x in history], "", logs_data, session_id
|
171 |
-
# Save log for the warning response
|
172 |
-
save_logs(scheduler, JSON_DATASET_PATH, logs_data)
|
173 |
-
return
|
174 |
-
|
175 |
context_retrieved_formatted = "||".join(doc.page_content for doc in context_retrieved)
|
176 |
context_retrieved_lst = [doc.page_content for doc in context_retrieved]
|
177 |
|
@@ -270,7 +245,6 @@ async def chat(query,history,sources,reports,subtype, client_ip=None, session_id
|
|
270 |
answer_yet += token
|
271 |
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
272 |
history[-1] = (query, parsed_answer)
|
273 |
-
# update logs_data with current answer
|
274 |
logs_data["answer"] = parsed_answer
|
275 |
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
276 |
end_time = time.time()
|
@@ -362,7 +336,7 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
362 |
avatar_images = (None,"data-collection.png"),
|
363 |
)
|
364 |
|
365 |
-
|
366 |
with gr.Column(elem_id="feedback-container"):
|
367 |
with gr.Row(visible=False) as feedback_row:
|
368 |
gr.Markdown("Was this response helpful?")
|
@@ -371,25 +345,10 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
371 |
not_okay_btn = gr.Button("👎 Not to expectations", elem_classes="feedback-button")
|
372 |
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
|
373 |
feedback_state = gr.State()
|
374 |
-
|
375 |
-
#---------------- WARNINGS ----------------------
|
376 |
-
# No filters selected warning
|
377 |
-
with gr.Row(visible=False, elem_id="warning-row", elem_classes="warning-message") as warning_row:
|
378 |
-
with gr.Column():
|
379 |
-
gr.Markdown("<span class='warning-icon'>⚠️</span> **No report filter selected. Are you sure you want to proceed?**")
|
380 |
-
with gr.Row(elem_classes="warning-buttons"):
|
381 |
-
proceed_btn = gr.Button("Proceed", elem_classes="proceed")
|
382 |
-
cancel_btn = gr.Button("Cancel", elem_classes="cancel")
|
383 |
-
|
384 |
-
# Short query warning (< 4 words)
|
385 |
-
with gr.Row(visible=False, elem_id="warning-row", elem_classes="warning-message") as short_query_warning_row:
|
386 |
-
with gr.Column():
|
387 |
-
gr.Markdown("<span class='warning-icon'>⚠️</span> **Your query is too short. Please lengthen your query to ensure the app has adequate context.**")
|
388 |
-
with gr.Row(elem_classes="warning-buttons"):
|
389 |
-
short_query_proceed_btn = gr.Button("OK", elem_classes="proceed")
|
390 |
|
391 |
|
392 |
-
|
|
|
393 |
with gr.Row(elem_id = "input-message"):
|
394 |
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,
|
395 |
lines = 1,interactive = True,elem_id="input-textbox")
|
@@ -401,122 +360,29 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
401 |
#---------------- tab for REPORTS SELECTION ----------------------
|
402 |
|
403 |
with gr.Tab("Reports",elem_id = "tab-config",id = 2):
|
|
|
404 |
|
405 |
-
#---------------- SELECTION METHOD - RADIO BUTTON ------------
|
406 |
-
search_method = gr.Radio(
|
407 |
-
choices=["Search by Report Name", "Search by Filters"],
|
408 |
-
label="Choose search method",
|
409 |
-
info= "Select the audit reports that you want to analyse directly or browse through categories and select reports",
|
410 |
-
value="Search by Report Name",
|
411 |
-
)
|
412 |
-
|
413 |
-
#---------------- SELECT BY REPORT NAME SECTION ------------
|
414 |
-
with gr.Group(visible=True) as report_name_section:
|
415 |
-
# Get default report value from config if present
|
416 |
-
default_report = model_config.get('app', 'dropdown_default', fallback=None)
|
417 |
-
# Check if it actually exists in the master list
|
418 |
-
default_report_value = [default_report] if default_report in new_report_list else None
|
419 |
-
|
420 |
-
dropdown_reports = gr.Dropdown(
|
421 |
-
new_report_list,
|
422 |
-
label="Select one or more reports (scroll or type to search)",
|
423 |
-
multiselect=True,
|
424 |
-
value=default_report_value,
|
425 |
-
interactive=True,
|
426 |
-
)
|
427 |
-
|
428 |
-
#---------------- SELECT BY FILTERS SECTION ------------
|
429 |
-
with gr.Group(visible=False) as filters_section:
|
430 |
-
#----- First level filter for selecting Report source/category ----------
|
431 |
-
dropdown_sources = gr.Dropdown(
|
432 |
-
["Consolidated","Ministry, Department, Agency and Projects","Local Government","Value for Money","Thematic"],
|
433 |
-
label="Select Report Category",
|
434 |
-
value=None,
|
435 |
-
interactive=True,
|
436 |
-
)
|
437 |
-
|
438 |
-
#------ second level filter for selecting subtype within the report category selected above
|
439 |
-
dropdown_category = gr.Dropdown(
|
440 |
-
[], # Start with empty choices
|
441 |
-
value=None,
|
442 |
-
label = "Filter for Sub-Type",
|
443 |
-
interactive=True)
|
444 |
-
|
445 |
-
#----------- update the second level filter based on values from first level ----------------
|
446 |
-
def rs_change(rs):
|
447 |
-
if rs: # Only update choices if a value is selected
|
448 |
-
return gr.update(choices=new_files[rs], value=None) # Set value to None (no preselection)
|
449 |
-
else:
|
450 |
-
return gr.update(choices=[], value=None) # Empty choices if nothing selected
|
451 |
-
dropdown_sources.change(fn=rs_change, inputs=[dropdown_sources], outputs=[dropdown_category])
|
452 |
-
|
453 |
-
# Toggle visibility based on search method
|
454 |
-
def toggle_search_method(method):
|
455 |
-
"""Note - this function removes the default value from report search when toggled"""
|
456 |
-
if method == "Search by Report Name":
|
457 |
-
# Show report selection, hide filters, and clear filter values
|
458 |
-
return (
|
459 |
-
gr.update(visible=True), # report_name_section
|
460 |
-
gr.update(visible=False), # filters_section
|
461 |
-
gr.update(value=None), # dropdown_sources
|
462 |
-
gr.update(value=None), # dropdown_category
|
463 |
-
gr.update(value=None), # dropdown_year
|
464 |
-
gr.update() # dropdown_reports
|
465 |
-
)
|
466 |
-
else: # "Search by Filters"
|
467 |
-
# Show filters, hide report selection, and clear report values
|
468 |
-
return (
|
469 |
-
gr.update(visible=False), # report_name_section
|
470 |
-
gr.update(visible=True), # filters_section
|
471 |
-
gr.update(), # dropdown_sources
|
472 |
-
gr.update(), # dropdown_category
|
473 |
-
gr.update(), # dropdown_year
|
474 |
-
gr.update(value=[]) # dropdown_reports
|
475 |
-
)
|
476 |
-
|
477 |
-
# Pass to the event handler
|
478 |
-
search_method.change(
|
479 |
-
fn=toggle_search_method,
|
480 |
-
inputs=[search_method],
|
481 |
-
outputs=[
|
482 |
-
report_name_section,
|
483 |
-
filters_section,
|
484 |
-
dropdown_sources,
|
485 |
-
dropdown_category,
|
486 |
-
dropdown_reports
|
487 |
-
]
|
488 |
-
)
|
489 |
-
#----- First level filter for selecting Report source/category ----------
|
490 |
-
|
491 |
-
#with gr.Group(visible=True) as report_name_section:
|
492 |
-
# # Get default report value from config if present
|
493 |
-
# default_report = model_config.get('app', 'dropdown_default', fallback=None)
|
494 |
-
# # Check if it actually exists in the master list
|
495 |
-
# default_report_value = [default_report] if default_report in new_report_list else None
|
496 |
-
#
|
497 |
-
# dropdown_reports = gr.Dropdown(
|
498 |
-
# new_report_list,
|
499 |
-
# label="Select one or more reports (scroll or type to search)",
|
500 |
-
# multiselect=True,
|
501 |
-
# value=default_report_value,
|
502 |
-
# interactive=True,
|
503 |
-
# )
|
504 |
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
|
|
511 |
|
512 |
#------ second level filter for selecting subtype within the report category selected above
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
|
|
|
|
|
|
|
|
520 |
|
521 |
#--------- Select the years for reports -------------------------------------
|
522 |
#dropdown_year = gr.Dropdown(
|
@@ -526,14 +392,14 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
526 |
# value=['2023'],
|
527 |
# interactive=True,
|
528 |
#)
|
529 |
-
|
530 |
#---------------- Another way to select reports across category and sub-type ------------
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
|
538 |
############### tab for Question selection ###############
|
539 |
with gr.TabItem("Examples",elem_id = "tab-examples",id = 0):
|
@@ -579,29 +445,25 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
579 |
|
580 |
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
581 |
|
582 |
-
|
583 |
-
# ---- New Guidelines Tab ----
|
584 |
with gr.Tab("Guidelines", elem_classes="max-height other-tabs"):
|
585 |
gr.Markdown("""
|
586 |
-
|
587 |
-
|
588 |
-
Clear, specific questions will give you the best results. Here are some examples:
|
589 |
-
| ❌ Less Effective | ✅ More Effective |
|
590 |
-
|------------------|-------------------|
|
591 |
-
| "What are the findings?" | "What were the main issues identified in procurement practices in the Ministry of Health in 2022?" |
|
592 |
-
| "Tell me about revenue collection" | "What specific challenges were identified in revenue collection at the local government level in 2021-2022?" |
|
593 |
-
| "Is there corruption?" | "What audit findings related to misappropriation of funds were reported in the education sector between 2020-2023?" |
|
594 |
-
## ⭐ Best Practices
|
595 |
- **Be Clear and Specific**: Frame your questions clearly and focus on what you want to learn.
|
596 |
- **One Topic at a Time**: Break complex queries into simpler, focused questions.
|
597 |
-
- **
|
598 |
-
- **Follow Up**: Ask follow-up questions to explore a topic more deeply.
|
599 |
|
600 |
-
|
|
|
|
|
|
|
|
|
|
|
601 |
- **Report Category & Subtype**: Use the "Reports" tab to choose your preferred report category and refine your query by selecting a specific sub-type. This will help narrow down the context for your question.
|
602 |
- **Year Selection**: Choose one or more years from the "Year" filter to target your query to specific time periods.
|
603 |
- **Specific Reports**: Optionally, select specific reports using the dropdown to focus on a particular document or set of documents.
|
604 |
-
|
605 |
|
606 |
- <ins>[**Short Course: Generative AI for Everyone** (3 hours)](https://www.deeplearning.ai/courses/generative-ai-for-everyone/)</ins>
|
607 |
- <ins>[**Short Course: Advanced Prompting** (1 hour)](https://www.deeplearning.ai/courses/ai-for-everyone/)</ins>
|
@@ -675,19 +537,9 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
675 |
|
676 |
|
677 |
|
678 |
-
def show_feedback(
|
679 |
-
"""
|
680 |
-
|
681 |
-
return (
|
682 |
-
gr.update(visible=False), # feedback_row
|
683 |
-
gr.update(visible=False), # feedback_thanks
|
684 |
-
None # feedback_state
|
685 |
-
)
|
686 |
-
return (
|
687 |
-
gr.update(visible=True), # feedback_row
|
688 |
-
gr.update(visible=False), # feedback_thanks
|
689 |
-
logs_data # feedback_state
|
690 |
-
)
|
691 |
|
692 |
def submit_feedback_okay(logs_data):
|
693 |
"""Handle 'okay' feedback submission"""
|
@@ -719,260 +571,30 @@ with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-
|
|
719 |
def get_client_ip_handler(dummy_input="", request: gr.Request = None):
|
720 |
"""Handler for getting client IP in Gradio context"""
|
721 |
return get_client_ip(request)
|
722 |
-
|
723 |
-
#-------------------- No Filters Set Warning -------------------------
|
724 |
-
# Warn users when no filters are selected
|
725 |
-
warning_state = gr.State(False)
|
726 |
-
pending_query = gr.State(None)
|
727 |
-
|
728 |
-
def show_warning():
|
729 |
-
"""Show warning popup when no filters selected"""
|
730 |
-
return gr.update(visible=True)
|
731 |
-
|
732 |
-
def hide_warning():
|
733 |
-
"""Hide warning popup"""
|
734 |
-
return gr.update(visible=False)
|
735 |
-
|
736 |
-
# Logic needs to be changed to accomodate default filter values (currently I have them all set to None)
|
737 |
-
def check_filters(check_status, textbox_value, sources, reports, subtype):
|
738 |
-
"""Check if any filters are selected"""
|
739 |
-
# If a previous check failed, don't continue with this check
|
740 |
-
if check_status is not None:
|
741 |
-
return (
|
742 |
-
check_status, # keep current check status
|
743 |
-
False, # keep warning state unchanged
|
744 |
-
gr.update(visible=False), # keep warning row visibility unchanged
|
745 |
-
textbox_value, # keep the textbox value
|
746 |
-
None # no need to store query
|
747 |
-
)
|
748 |
-
|
749 |
-
no_filters = (not reports) and (not sources) and (not subtype)
|
750 |
-
if no_filters:
|
751 |
-
# If no filters, show warning and set status
|
752 |
-
return (
|
753 |
-
"filter", # check status - no filters selected
|
754 |
-
True, # warning state
|
755 |
-
gr.update(visible=True), # warning row visibility
|
756 |
-
gr.update(value=""), # clear textbox
|
757 |
-
textbox_value # store the query
|
758 |
-
)
|
759 |
-
# If filters exist, proceed normally
|
760 |
-
return (
|
761 |
-
None, # no check failed
|
762 |
-
False, # normal state
|
763 |
-
gr.update(visible=False), # hide warning
|
764 |
-
textbox_value, # keep the original value
|
765 |
-
None # no need to store query
|
766 |
-
)
|
767 |
|
768 |
-
async def handle_chat_flow(check_status, warning_active, short_query_warning_active, query, chatbot, sources, reports, subtype, client_ip, session_id):
|
769 |
-
"""Handle chat flow with explicit check for status"""
|
770 |
-
# Don't proceed if any check failed or query is None
|
771 |
-
if check_status is not None or warning_active or short_query_warning_active or query is None or query == "":
|
772 |
-
yield (
|
773 |
-
chatbot, # unchanged chatbot
|
774 |
-
"", # empty sources
|
775 |
-
None, # no feedback state
|
776 |
-
session_id # keep session
|
777 |
-
)
|
778 |
-
return # Exit the generator
|
779 |
-
|
780 |
-
# Include start_chat functionality here
|
781 |
-
history = chatbot + [(query, None)]
|
782 |
-
history = [tuple(x) for x in history]
|
783 |
-
|
784 |
-
# Proceed with chat and yield each update
|
785 |
-
async for update in chat(query, history, sources, reports, subtype, client_ip, session_id):
|
786 |
-
yield update
|
787 |
|
788 |
-
#--------------------
|
789 |
-
# Warn users when query is too short (less than 4 words)
|
790 |
-
short_query_warning_state = gr.State(False)
|
791 |
-
check_status = gr.State(None)
|
792 |
-
|
793 |
-
def check_query_length(textbox_value):
|
794 |
-
"""Check if query has at least 4 words"""
|
795 |
-
if textbox_value and len(textbox_value.split()) < 3:
|
796 |
-
# If query is too short, show warning and set status
|
797 |
-
return (
|
798 |
-
"short", # check status - this query is too short
|
799 |
-
True, # short query warning state
|
800 |
-
gr.update(visible=True), # short query warning row visibility
|
801 |
-
gr.update(value=""), # clear textbox
|
802 |
-
textbox_value # store the query
|
803 |
-
)
|
804 |
-
# If query is long enough, proceed normally
|
805 |
-
return (
|
806 |
-
None, # no check failed
|
807 |
-
False, # normal state
|
808 |
-
gr.update(visible=False), # hide warning
|
809 |
-
gr.update(value=textbox_value), # keep the textbox value
|
810 |
-
None # no need to store query
|
811 |
-
)
|
812 |
-
|
813 |
-
|
814 |
-
#-------------------- Gradio Handlers -------------------------
|
815 |
-
|
816 |
-
# Hanlders: Text input from Textbox
|
817 |
-
(textbox
|
818 |
-
.submit(
|
819 |
-
check_query_length,
|
820 |
-
[textbox],
|
821 |
-
[check_status, short_query_warning_state, short_query_warning_row, textbox, pending_query],
|
822 |
-
api_name="check_query_length_textbox"
|
823 |
-
)
|
824 |
-
.then(
|
825 |
-
check_filters,
|
826 |
-
[check_status, textbox, dropdown_sources, dropdown_reports, dropdown_category],
|
827 |
-
[check_status, warning_state, warning_row, textbox, pending_query],
|
828 |
-
api_name="submit_textbox",
|
829 |
-
show_progress=False
|
830 |
-
)
|
831 |
-
.then(
|
832 |
-
get_client_ip_handler,
|
833 |
-
[textbox],
|
834 |
-
[client_ip],
|
835 |
-
show_progress=False,
|
836 |
-
api_name="get_client_ip_textbox"
|
837 |
-
)
|
838 |
-
.then(
|
839 |
-
handle_chat_flow,
|
840 |
-
[check_status, warning_state, short_query_warning_state, textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
841 |
-
[chatbot, sources_textbox, feedback_state, session_id],
|
842 |
-
queue=True,
|
843 |
-
api_name="handle_chat_flow_textbox"
|
844 |
-
)
|
845 |
-
.then(
|
846 |
-
show_feedback,
|
847 |
-
[feedback_state],
|
848 |
-
[feedback_row, feedback_thanks, feedback_state],
|
849 |
-
api_name="show_feedback_textbox"
|
850 |
-
)
|
851 |
-
.then(
|
852 |
-
finish_chat,
|
853 |
-
None,
|
854 |
-
[textbox],
|
855 |
-
api_name="finish_chat_textbox"
|
856 |
-
))
|
857 |
|
858 |
-
#
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
[client_ip],
|
879 |
-
show_progress=False,
|
880 |
-
api_name="get_client_ip_examples"
|
881 |
-
).then(
|
882 |
-
handle_chat_flow,
|
883 |
-
[check_status, warning_state, short_query_warning_state, textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
884 |
-
[chatbot, sources_textbox, feedback_state, session_id],
|
885 |
-
queue=True,
|
886 |
-
api_name="handle_chat_flow_examples"
|
887 |
-
).then(
|
888 |
-
show_feedback,
|
889 |
-
[feedback_state],
|
890 |
-
[feedback_row, feedback_thanks, feedback_state],
|
891 |
-
api_name="show_feedback_examples"
|
892 |
-
).then(
|
893 |
-
finish_chat,
|
894 |
-
None,
|
895 |
-
[textbox],
|
896 |
-
api_name="finish_chat_examples"
|
897 |
-
)
|
898 |
-
|
899 |
-
|
900 |
-
# Handlers for the warning buttons
|
901 |
-
proceed_btn.click(
|
902 |
-
lambda query: (
|
903 |
-
None, # reset check status
|
904 |
-
False, # warning state
|
905 |
-
gr.update(visible=False), # warning row
|
906 |
-
gr.update(value=query if query else "", interactive=True), # restore query
|
907 |
-
None # clear pending query
|
908 |
-
),
|
909 |
-
pending_query,
|
910 |
-
[check_status, warning_state, warning_row, textbox, pending_query]
|
911 |
-
).then(
|
912 |
-
get_client_ip_handler,
|
913 |
-
[textbox],
|
914 |
-
[client_ip]
|
915 |
-
).then(
|
916 |
-
handle_chat_flow,
|
917 |
-
[check_status, warning_state, short_query_warning_state, textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
918 |
-
[chatbot, sources_textbox, feedback_state, session_id],
|
919 |
-
queue=True
|
920 |
-
).then(
|
921 |
-
show_feedback,
|
922 |
-
[feedback_state],
|
923 |
-
[feedback_row, feedback_thanks, feedback_state]
|
924 |
-
).then(
|
925 |
-
finish_chat,
|
926 |
-
None,
|
927 |
-
[textbox]
|
928 |
-
)
|
929 |
-
|
930 |
-
# Cancel button for no filters
|
931 |
-
cancel_btn.click(
|
932 |
-
lambda: (
|
933 |
-
None, # reset check status
|
934 |
-
False, # warning state
|
935 |
-
gr.update(visible=False), # warning row
|
936 |
-
gr.update(value="", interactive=True), # clear textbox
|
937 |
-
None # clear pending query
|
938 |
-
),
|
939 |
-
None,
|
940 |
-
[check_status, warning_state, warning_row, textbox, pending_query]
|
941 |
-
)
|
942 |
-
|
943 |
-
# short query warning OK button
|
944 |
-
short_query_proceed_btn.click(
|
945 |
-
lambda query: (
|
946 |
-
None, # reset check status
|
947 |
-
False, # short query warning state
|
948 |
-
gr.update(visible=False), # short query warning row
|
949 |
-
gr.update(value=query if query else "", interactive=True), # restore query
|
950 |
-
None # clear pending query
|
951 |
-
),
|
952 |
-
pending_query,
|
953 |
-
[check_status, short_query_warning_state, short_query_warning_row, textbox, pending_query]
|
954 |
-
)
|
955 |
-
|
956 |
-
|
957 |
-
#(textbox
|
958 |
-
# .submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
959 |
-
# .then(start_chat, [textbox, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_textbox")
|
960 |
-
# .then(chat,
|
961 |
-
# [textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
962 |
-
# [chatbot, sources_textbox, feedback_state, session_id],
|
963 |
-
# queue=True, concurrency_limit=8, api_name="chat_textbox")
|
964 |
-
# .then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_textbox")
|
965 |
-
# .then(finish_chat, None, [textbox], api_name="finish_chat_textbox"))
|
966 |
-
|
967 |
-
#(examples_hidden
|
968 |
-
# .change(start_chat, [examples_hidden, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_examples")
|
969 |
-
# .then(get_client_ip_handler, [examples_hidden], [client_ip], api_name="get_ip_examples")
|
970 |
-
# .then(chat,
|
971 |
-
# [examples_hidden, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
972 |
-
# [chatbot, sources_textbox, feedback_state, session_id],
|
973 |
-
# concurrency_limit=8, api_name="chat_examples")
|
974 |
-
# .then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_examples")
|
975 |
-
# .then(finish_chat, None, [textbox], api_name="finish_chat_examples"))
|
976 |
|
977 |
demo.queue()
|
978 |
|
|
|
147 |
sources=sources,subtype=subtype)
|
148 |
end_time = time.time()
|
149 |
print("Time for retriever:",end_time - start_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
context_retrieved_formatted = "||".join(doc.page_content for doc in context_retrieved)
|
151 |
context_retrieved_lst = [doc.page_content for doc in context_retrieved]
|
152 |
|
|
|
245 |
answer_yet += token
|
246 |
parsed_answer = parse_output_llm_with_sources(answer_yet)
|
247 |
history[-1] = (query, parsed_answer)
|
|
|
248 |
logs_data["answer"] = parsed_answer
|
249 |
yield [tuple(x) for x in history], docs_html, logs_data, session_id
|
250 |
end_time = time.time()
|
|
|
336 |
avatar_images = (None,"data-collection.png"),
|
337 |
)
|
338 |
|
339 |
+
# feedback UI
|
340 |
with gr.Column(elem_id="feedback-container"):
|
341 |
with gr.Row(visible=False) as feedback_row:
|
342 |
gr.Markdown("Was this response helpful?")
|
|
|
345 |
not_okay_btn = gr.Button("👎 Not to expectations", elem_classes="feedback-button")
|
346 |
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
|
347 |
feedback_state = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
|
350 |
+
|
351 |
+
|
352 |
with gr.Row(elem_id = "input-message"):
|
353 |
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,
|
354 |
lines = 1,interactive = True,elem_id="input-textbox")
|
|
|
360 |
#---------------- tab for REPORTS SELECTION ----------------------
|
361 |
|
362 |
with gr.Tab("Reports",elem_id = "tab-config",id = 2):
|
363 |
+
gr.Markdown("Reminder: To get better results select the specific report/reports")
|
364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
|
366 |
+
#----- First level filter for selecting Report source/category ----------
|
367 |
+
dropdown_sources = gr.Dropdown(
|
368 |
+
["Consolidated","Ministry, Department and Agency","Project","Hospital","Local Government","Value for Money","Thematic"],
|
369 |
+
label="Select Report Category",
|
370 |
+
value="Consolidated",
|
371 |
+
interactive=True,
|
372 |
+
)
|
373 |
|
374 |
#------ second level filter for selecting subtype within the report category selected above
|
375 |
+
dropdown_category = gr.Dropdown(
|
376 |
+
new_files["Consolidated"],
|
377 |
+
multiselect = True,
|
378 |
+
value = new_files["Consolidated"][0],
|
379 |
+
label = "Filter for Sub-reports",
|
380 |
+
interactive=True)
|
381 |
+
|
382 |
+
#----------- update the secodn level filter abse don values from first level ----------------
|
383 |
+
def rs_change(rs):
|
384 |
+
return gr.update(choices=new_files[rs], value=new_files[rs])
|
385 |
+
dropdown_sources.change(fn=rs_change, inputs=[dropdown_sources], outputs=[dropdown_category])
|
386 |
|
387 |
#--------- Select the years for reports -------------------------------------
|
388 |
#dropdown_year = gr.Dropdown(
|
|
|
392 |
# value=['2023'],
|
393 |
# interactive=True,
|
394 |
#)
|
395 |
+
gr.Markdown("-------------------------------------------------------------------------")
|
396 |
#---------------- Another way to select reports across category and sub-type ------------
|
397 |
+
dropdown_reports = gr.Dropdown(
|
398 |
+
new_report_list,
|
399 |
+
label="Or select specific reports",
|
400 |
+
multiselect=True,
|
401 |
+
value=[],
|
402 |
+
interactive=True,)
|
403 |
|
404 |
############### tab for Question selection ###############
|
405 |
with gr.TabItem("Examples",elem_id = "tab-examples",id = 0):
|
|
|
445 |
|
446 |
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
447 |
|
448 |
+
# ---- New Guidelines Tab ----
|
|
|
449 |
with gr.Tab("Guidelines", elem_classes="max-height other-tabs"):
|
450 |
gr.Markdown("""
|
451 |
+
Welcome to Audit Q&A, your AI-powered assistant for exploring and understanding Uganda's audit reports. This tool leverages advanced language models to help you get clear and structured answers based on audit publications. To get you started, here a few tips on how to use the tool:
|
452 |
+
### Crafting Effective Prompts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
453 |
- **Be Clear and Specific**: Frame your questions clearly and focus on what you want to learn.
|
454 |
- **One Topic at a Time**: Break complex queries into simpler, focused questions.
|
455 |
+
- **Be Direct**: Instead of "What are the findings?", try "What were the main issues identified in procurement practices?" or "What challenges were found in revenue collection?"
|
|
|
456 |
|
457 |
+
### Best Practices
|
458 |
+
- Start with a simple, focused question.
|
459 |
+
- Follow up with additional questions if your initial query doesn't yield the desired results.
|
460 |
+
- Experiment with different phrasings to get the most accurate answers.
|
461 |
+
- Use the source citations as a reference to validate the provided information.
|
462 |
+
### Utilizing Filters
|
463 |
- **Report Category & Subtype**: Use the "Reports" tab to choose your preferred report category and refine your query by selecting a specific sub-type. This will help narrow down the context for your question.
|
464 |
- **Year Selection**: Choose one or more years from the "Year" filter to target your query to specific time periods.
|
465 |
- **Specific Reports**: Optionally, select specific reports using the dropdown to focus on a particular document or set of documents.
|
466 |
+
### Useful Resources
|
467 |
|
468 |
- <ins>[**Short Course: Generative AI for Everyone** (3 hours)](https://www.deeplearning.ai/courses/generative-ai-for-everyone/)</ins>
|
469 |
- <ins>[**Short Course: Advanced Prompting** (1 hour)](https://www.deeplearning.ai/courses/ai-for-everyone/)</ins>
|
|
|
537 |
|
538 |
|
539 |
|
540 |
+
def show_feedback(logs):
|
541 |
+
"""Show feedback buttons and store logs in state"""
|
542 |
+
return gr.update(visible=True), gr.update(visible=False), logs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
|
544 |
def submit_feedback_okay(logs_data):
|
545 |
"""Handle 'okay' feedback submission"""
|
|
|
571 |
def get_client_ip_handler(dummy_input="", request: gr.Request = None):
|
572 |
"""Handler for getting client IP in Gradio context"""
|
573 |
return get_client_ip(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
574 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
575 |
|
576 |
+
#-------------------- Gradio voodoo -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
577 |
|
578 |
+
# Update the event handlers
|
579 |
+
(textbox
|
580 |
+
.submit(get_client_ip_handler, [textbox], [client_ip], api_name="get_ip_textbox")
|
581 |
+
.then(start_chat, [textbox, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_textbox")
|
582 |
+
.then(chat,
|
583 |
+
[textbox, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
584 |
+
[chatbot, sources_textbox, feedback_state, session_id],
|
585 |
+
queue=True, concurrency_limit=8, api_name="chat_textbox")
|
586 |
+
.then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_textbox")
|
587 |
+
.then(finish_chat, None, [textbox], api_name="finish_chat_textbox"))
|
588 |
+
|
589 |
+
(examples_hidden
|
590 |
+
.change(start_chat, [examples_hidden, chatbot], [textbox, tabs, chatbot], queue=False, api_name="start_chat_examples")
|
591 |
+
.then(get_client_ip_handler, [examples_hidden], [client_ip], api_name="get_ip_examples")
|
592 |
+
.then(chat,
|
593 |
+
[examples_hidden, chatbot, dropdown_sources, dropdown_reports, dropdown_category, client_ip, session_id],
|
594 |
+
[chatbot, sources_textbox, feedback_state, session_id],
|
595 |
+
concurrency_limit=8, api_name="chat_examples")
|
596 |
+
.then(show_feedback, [feedback_state], [feedback_row, feedback_thanks, feedback_state], api_name="show_feedback_examples")
|
597 |
+
.then(finish_chat, None, [textbox], api_name="finish_chat_examples"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
598 |
|
599 |
demo.queue()
|
600 |
|