File size: 2,152 Bytes
d9e4792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""

Created on Fri May  2 12:27:58 2025



@author: ariam

"""

import gradio as gr
import pandas as pd
from OpenAI_interface import ask_openai, extract_fields, resolve_agency_index
from OpenAI_tools import run_report_classifier

# Load agency CSV once
df = pd.read_csv("high_priority_agencies.csv")

# Main function Gradio calls
def searchbot_interface(prompt, mode):
    chatbot_mode = (mode == "Chatbot Mode")
    
    try:
        # Call OpenAI
        response = ask_openai(prompt, chatbot_mode=chatbot_mode)

        if chatbot_mode:
            return response
        
        # Extract info from LLM output
        parsed = extract_fields(response)
        agency, keyword, year = parsed["agency"], parsed["keyword"], parsed["year"]

        # Match agency with embeddings
        index, resolved_agency = resolve_agency_index(agency)
        if index is None:
            return f"❌ Could not resolve agency: {agency}"

        # Run actual document search/classifier
        run_report_classifier(
            agency_df=df,
            search_term=keyword,
            fiscal_year=year if year else "",
            start_index=index,
            end_index=index,
            max_results=15,
            output_filename="openAI_bot_output.csv",
            brave_api_key="your_brave_key_here",
            google_api_key="your_google_key_here",
            google_cse_id="your_cse_id_here"
        )

        return f"βœ… Search complete!\nAgency: {resolved_agency}\nKeyword: {keyword}\nYear: {year}"
    
    except Exception as e:
        return f"❌ Error: {str(e)}"

# Create the UI
iface = gr.Interface(
    fn=searchbot_interface,
    inputs=[
        gr.Textbox(lines=4, label="Ask a question"),
        gr.Radio(["Search Mode", "Chatbot Mode"], label="Mode", value="Search Mode")
    ],
    outputs=gr.Textbox(label="Response"),
    title="πŸ“„ PIA SearchBot (OpenAI-Powered)",
    description="Ask about agency reports or budgets. Toggle chatbot mode to talk casually."
)

# Launch the web app
if __name__ == "__main__":
    iface.launch()