import gradio as gr import pandas as pd import numpy as np import os import time import re import json from auditqa.sample_questions import QUESTIONS from auditqa.engine.prompts import audience_prompts from auditqa.reports import POSSIBLE_REPORTS, files from auditqa.doc_process import process_pdf from langchain_core.messages import ( HumanMessage, SystemMessage, ) from langchain_huggingface import ChatHuggingFace from langchain_core.output_parsers import StrOutputParser from langchain_huggingface import HuggingFaceEndpoint from dotenv import load_dotenv load_dotenv() HF_token = os.environ["HF_TOKEN"] vectorstores = process_pdf() async def chat(query,history,sources,reports): """taking a query and a message history, use a pipeline (reformulation, retriever, answering) to yield a tuple of: (messages in gradio format, messages in langchain format, source documents)""" print(f">> NEW QUESTION : {query}") print(f"history:{history}") #print(f"audience:{audience}") print(f"sources:{sources}") print(f"reports:{reports}") docs_html = "" output_query = "" output_language = "English" audience = "Experts" if audience == "Children": audience_prompt = audience_prompts["children"] elif audience == "General public": audience_prompt = audience_prompts["general"] elif audience == "Experts": audience_prompt = audience_prompts["experts"] else: audience_prompt = audience_prompts["experts"] # Prepare default values if len(sources) == 0: sources = ["Consolidated Reports"] if len(reports) == 0: reports = [] if sources == "Ministry": vectorstore = vectorstores["MWTS"] else: vectorstore = vectorstores["Consolidated"] # get context context_retrieved_lst = [] question_lst= [query] for question in question_lst: retriever = vectorstore.as_retriever( search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.6, "k": 3}) context_retrieved = retriever.invoke(question) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) context_retrieved_formatted = format_docs(context_retrieved) context_retrieved_lst.append(context_retrieved_formatted) print(context_retrieved_lst) # get prompt SYSTEM_PROMPT = """ You are AuditQ&A, an AI Assistant created by Auditors and Data Scientist. You are given a question and extracted passages of the consolidated/departmental/thematic focus audit reports. Provide a clear and structured answer based on the passages provided, the context and the guidelines. Guidelines: - If the passages have useful facts or numbers, use them in your answer. - When you use information from a passage, mention where it came from by using [Doc i] at the end of the sentence. i stands for the number of the document. - Do not use the sentence 'Doc i says ...' to say where information came from. - If the same thing is said in more than one document, you can mention all of them like this: [Doc i, Doc j, Doc k] - Do not just summarize each passage one by one. Group your summaries to highlight the key parts in the explanation. - If it makes sense, use bullet points and lists to make your answers easier to understand. - You do not need to use every passage. Only use the ones that help answer the question. - If the documents do not have the information needed to answer the question, just say you do not have enough information. """ USER_PROMPT = """Passages: {context} ----------------------- Question: {question} - Explained to audit expert Answer in english with the passages citations: """.format(context = context_retrieved_lst, question=query) messages = [ SystemMessage(content=SYSTEM_PROMPT), HumanMessage( content=USER_PROMPT ),] # get llm_qa # llm_qa = HuggingFaceEndpoint( # endpoint_url= "https://mnczdhmrf7lkfd9d.eu-west-1.aws.endpoints.huggingface.cloud", # task="text-generation", # huggingfacehub_api_token=HF_token, # model_kwargs={}) # trying llm new-prompt adapted for llama-3 # https://stackoverflow.com/questions/78429932/langchain-ollama-and-llama-3-prompt-and-response # https://api.python.langchain.com/en/latest/llms/langchain_community.llms.huggingface_endpoint.HuggingFaceEndpoint.html#langchain_community.llms.huggingface_endpoint.HuggingFaceEndpoint.model_kwargs # https://huggingface.co/blog/llama3#how-to-prompt-llama-3 llm_qa = HuggingFaceEndpoint( endpoint_url= "https://nhe9phsr2zhs0e36.eu-west-1.aws.endpoints.huggingface.cloud", task="text-generation", huggingfacehub_api_token=HF_token) # create rag chain chat_model = ChatHuggingFace(llm=llm_qa) chain = chat_model | StrOutputParser() # get answers answer_lst = [] for question, context in zip(question_lst , context_retrieved_lst): answer = chain.invoke(messages) answer_lst.append(answer) docs_html = [] for i, d in enumerate(context_retrieved, 1): docs_html.append(make_html_source(d, i)) docs_html = "".join(docs_html) previous_answer = history[-1][1] previous_answer = previous_answer if previous_answer is not None else "" answer_yet = previous_answer + answer_lst[0] answer_yet = parse_output_llm_with_sources(answer_yet) history[-1] = (query,answer_yet) history = [tuple(x) for x in history] yield history,docs_html,output_language def make_html_source(source,i): meta = source.metadata # content = source.page_content.split(":",1)[1].strip() content = source.page_content.strip() name = meta['source'] card = f"""

Doc {i} - {meta['file_path']} - Page {int(meta['page'])}

{content}

""" return card def parse_output_llm_with_sources(output): # Split the content into a list of text and "[Doc X]" references content_parts = re.split(r'\[(Doc\s?\d+(?:,\s?Doc\s?\d+)*)\]', output) parts = [] for part in content_parts: if part.startswith("Doc"): subparts = part.split(",") subparts = [subpart.lower().replace("doc","").strip() for subpart in subparts] subparts = [f"""{subpart}""" for subpart in subparts] parts.append("".join(subparts)) else: parts.append(part) content_parts = "".join(parts) return content_parts # -------------------------------------------------------------------- # Gradio # -------------------------------------------------------------------- # Set up Gradio Theme theme = gr.themes.Base( primary_hue="blue", secondary_hue="red", font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"], text_size = gr.themes.utils.sizes.text_sm, ) init_prompt = """ Hello, I am Audit Q&A, a conversational assistant designed to help you understand audit Reports. I will answer your questions by using **Audit reports publishsed by Auditor General Office**. 💡 How to use (tabs on right) - **Reports**: You can choose to address your question to either specific report or a collection of report like District or Ministry focused reports. \ If you dont select any then the Consolidated report is relied upon to answer your question. - **Examples**: We have curated some example questions,select a particular question from category of questions. - **Sources**: This tab will display the relied upon paragraphs from the report, to help you in assessing or fact checking if the answer provided by Audit Q&A assitant is correct or not. ⚠️ For limitations of the tool please check **Disclaimer** tab. """ # Setting Tabs with gr.Blocks(title="Audit Q&A", css= "style.css", theme=theme,elem_id = "main-component") as demo: # user_id_state = gr.State([user_id]) with gr.Tab("AuditQ&A"): with gr.Row(elem_id="chatbot-row"): with gr.Column(scale=2): # state = gr.State([system_template]) chatbot = gr.Chatbot( value=[(None,init_prompt)], show_copy_button=True,show_label = False,elem_id="chatbot",layout = "panel", avatar_images = (None,"data-collection.png"), )#,avatar_images = ("assets/logo4.png",None)) # bot.like(vote,None,None) with gr.Row(elem_id = "input-message"): textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,lines = 1,interactive = True,elem_id="input-textbox") # submit = gr.Button("",elem_id = "submit-button",scale = 1,interactive = True,icon = "https://static-00.iconduck.com/assets.00/settings-icon-2048x2046-cw28eevx.png") with gr.Column(scale=1, variant="panel",elem_id = "right-panel"): with gr.Tabs() as tabs: with gr.Tab("Reports",elem_id = "tab-config",id = 2): gr.Markdown("Reminder: To get better results select the specific report/reports") dropdown_sources = gr.Radio( ["Consolidated", "District","Ministry"], label="Select Report Source", value="Consolidated", interactive=True, ) dropdown_category = gr.Dropdown( list(files["Consolidated"].keys()), value = list(files["Consolidated"].keys())[0], label = "Filter for Sub-Type", interactive=True) def rs_change(rs): return gr.update(choices=files[rs], value=list(files[rs].keys())[0]) dropdown_sources.change(fn=rs_change, inputs=[dropdown_sources], outputs=[dropdown_category]) dropdown_year = gr.Dropdown( [2018,2019,2020,2021,2022,2023], label="Filter for year", multiselect=True, value=[2023], interactive=True, ) dropdown_reports = gr.Dropdown( POSSIBLE_REPORTS, label="Or select specific reports", multiselect=True, value=None, interactive=True, ) #output_query = gr.Textbox(label="Query used for retrieval",show_label = True,elem_id = "reformulated-query",lines = 2,interactive = False) #output_language = gr.Textbox(label="Language",show_label = True,elem_id = "language",lines = 1,interactive = False) with gr.TabItem("Examples",elem_id = "tab-examples",id = 0): examples_hidden = gr.Textbox(visible = False) first_key = list(QUESTIONS.keys())[0] dropdown_samples = gr.Dropdown(QUESTIONS.keys(),value = first_key,interactive = True,show_label = True,label = "Select a category of sample questions",elem_id = "dropdown-samples") samples = [] for i,key in enumerate(QUESTIONS.keys()): examples_visible = True if i == 0 else False with gr.Row(visible = examples_visible) as group_examples: examples_questions = gr.Examples( QUESTIONS[key], [examples_hidden], examples_per_page=8, run_on_click=False, elem_id=f"examples{i}", api_name=f"examples{i}", # label = "Click on the example question or enter your own", # cache_examples=True, ) samples.append(group_examples) with gr.Tab("Sources",elem_id = "tab-citations",id = 1): sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox") docs_textbox = gr.State("") # with Modal(visible = False) as config_modal: with gr.Tab("About",elem_classes = "max-height other-tabs"): with gr.Row(): with gr.Column(scale=1): gr.Markdown("""The [**Office of the Auditor General (OAG)**](https://www.oag.go.ug/welcome) in Uganda, \ consistent with the mandate of Supreme Audit Institutions (SAIs),\ remains integral in ensuring transparency and fiscal responsibility.\ Regularly, the OAG submits comprehensive audit reports to Parliament, \ which serve as instrumental references for both policymakers and the public, \ facilitating informed decisions regarding public expenditure. However, the prevalent underutilization of these audit reports, \ leading to numerous unimplemented recommendations, has posed significant challenges\ to the effectiveness and impact of the OAG's operations. The audit reports made available \ to the public have not been effectively used by them and other relevant stakeholders. \ The current format of the audit reports is considered a challenge to the \ stakeholders' accessibility and usability. This in one way constrains transparency \ and accountability in the utilization of public funds and effective service delivery. In the face of this, modern advancements in Artificial Intelligence (AI),\ particularly Retrieval Augmented Generation (RAG) technology, \ emerge as a promising solution. By harnessing the capabilities of such AI tools, \ there is an opportunity not only to improve the accessibility and understanding \ of these audit reports but also to ensure that their insights are effectively \ translated into actionable outcomes, thereby reinforcing public transparency \ and service delivery in Uganda. To address these issues, the OAG has initiated several projects, \ such as the Audit Recommendation Tracking (ART) System and the Citizens Feedback Platform (CFP). \ These systems are designed to increase the transparency and relevance of audit activities. \ However, despite these efforts, engagement and awareness of the audit findings remain low, \ and the complexity of the information often hinders effective public utilization. Recognizing the need for further\ enhancement in how audit reports are processed and understood, \ the **Civil Society and Budget Advocacy Group (CSBAG)** in partnership with the **GIZ**, \ has recognizing the need for further enhancement in how audit reports are processed and understood. This prototype tool leveraging AI (Artificial Intelligence) aims at offering critical capabilities such as ' summarizing complex texts, extracting thematic insights, and enabling interactive, \ user-friendly analysis through a chatbot interface. By making the audit reports more accessible,\ this aims to increase readership and utilization among stakeholders, \ which can lead to better accountability and improve service delivery """) with gr.Tab("Disclaimer",elem_classes = "max-height other-tabs"): with gr.Row(): with gr.Column(scale=1): gr.Markdown(""" - This chatbot is intended for specific use of answering the questions based on audit reports published by OAG, for any use beyond this scope we have no liability to response provided by chatbot. - We do not guarantee the accuracy, reliability, or completeness of any information provided by the chatbot and disclaim any liability or responsibility for actions taken based on its responses. - The chatbot may occasionally provide inaccurate or inappropriate responses, and it is important to exercise judgment and critical thinking when interpreting its output. - The chatbot responses should not be considered professional or authoritative advice and are generated based on patterns in the data it has been trained on. - The chatbot's responses do not reflect the opinions or policies of our organization or its affiliates. - Any personal or sensitive information shared with the chatbot is at the user's own risk, and we cannot guarantee complete privacy or confidentiality. - the chatbot is not deterministic, so there might be change in answer to same question when asked by different users or multiple times. - By using this chatbot, you agree to these terms and acknowledge that you are solely responsible for any reliance on or actions taken based on its responses. - **This is just a prototype and being tested and worked upon, so its not perfect and may sometimes give irrelevant answers**. If you are not satisfied with the answer, please ask a more specific question or report your feedback to help us improve the system. """) def start_chat(query,history): history = history + [(query,None)] history = [tuple(x) for x in history] return (gr.update(interactive = False),gr.update(selected=1),history) def finish_chat(): return (gr.update(interactive = True,value = "")) (textbox .submit(start_chat, [textbox,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_textbox") .then(chat, [textbox,chatbot, dropdown_sources,dropdown_reports], [chatbot,sources_textbox],concurrency_limit = 8,api_name = "chat_textbox") .then(finish_chat, None, [textbox],api_name = "finish_chat_textbox") ) (examples_hidden .change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples") .then(chat, [examples_hidden,chatbot, dropdown_sources,dropdown_reports], [chatbot,sources_textbox],concurrency_limit = 8,api_name = "chat_examples") .then(finish_chat, None, [textbox],api_name = "finish_chat_examples") ) def change_sample_questions(key): index = list(QUESTIONS.keys()).index(key) visible_bools = [False] * len(samples) visible_bools[index] = True return [gr.update(visible=visible_bools[i]) for i in range(len(samples))] dropdown_samples.change(change_sample_questions,dropdown_samples,samples) demo.queue() demo.launch()