Spaces:
No application file
No application file
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Fri May 2 12:27:58 2025
|
| 4 |
+
|
| 5 |
+
@author: ariam
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from OpenAI_interface import ask_openai, extract_fields, resolve_agency_index
|
| 11 |
+
from OpenAI_tools import run_report_classifier
|
| 12 |
+
|
| 13 |
+
# Load agency CSV once
|
| 14 |
+
df = pd.read_csv("high_priority_agencies.csv")
|
| 15 |
+
|
| 16 |
+
# Main function Gradio calls
|
| 17 |
+
def searchbot_interface(prompt, mode):
|
| 18 |
+
chatbot_mode = (mode == "Chatbot Mode")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# Call OpenAI
|
| 22 |
+
response = ask_openai(prompt, chatbot_mode=chatbot_mode)
|
| 23 |
+
|
| 24 |
+
if chatbot_mode:
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
# Extract info from LLM output
|
| 28 |
+
parsed = extract_fields(response)
|
| 29 |
+
agency, keyword, year = parsed["agency"], parsed["keyword"], parsed["year"]
|
| 30 |
+
|
| 31 |
+
# Match agency with embeddings
|
| 32 |
+
index, resolved_agency = resolve_agency_index(agency)
|
| 33 |
+
if index is None:
|
| 34 |
+
return f"β Could not resolve agency: {agency}"
|
| 35 |
+
|
| 36 |
+
# Run actual document search/classifier
|
| 37 |
+
run_report_classifier(
|
| 38 |
+
agency_df=df,
|
| 39 |
+
search_term=keyword,
|
| 40 |
+
fiscal_year=year if year else "",
|
| 41 |
+
start_index=index,
|
| 42 |
+
end_index=index,
|
| 43 |
+
max_results=15,
|
| 44 |
+
output_filename="openAI_bot_output.csv",
|
| 45 |
+
brave_api_key="your_brave_key_here",
|
| 46 |
+
google_api_key="your_google_key_here",
|
| 47 |
+
google_cse_id="your_cse_id_here"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return f"β
Search complete!\nAgency: {resolved_agency}\nKeyword: {keyword}\nYear: {year}"
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"β Error: {str(e)}"
|
| 54 |
+
|
| 55 |
+
# Create the UI
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=searchbot_interface,
|
| 58 |
+
inputs=[
|
| 59 |
+
gr.Textbox(lines=4, label="Ask a question"),
|
| 60 |
+
gr.Radio(["Search Mode", "Chatbot Mode"], label="Mode", value="Search Mode")
|
| 61 |
+
],
|
| 62 |
+
outputs=gr.Textbox(label="Response"),
|
| 63 |
+
title="π PIA SearchBot (OpenAI-Powered)",
|
| 64 |
+
description="Ask about agency reports or budgets. Toggle chatbot mode to talk casually."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Launch the web app
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
iface.launch()
|