import os import gradio as gr import spaces from agent import answer_question, get_summaries, load_db @spaces.GPU def _gpu_placeholder(): # never called; ZeroGPU Spaces expect at least one GPU-decorated function return "ok" load_db() # load the data once at startup def ask(question: str) -> dict: print(f"[ask] {question!r}", flush=True) try: return answer_question(question) except Exception as e: import traceback; traceback.print_exc() # <-- new line return {"answer": f"Something went wrong: {type(e).__name__}", "sources": []} def summaries(scope: str) -> dict: print(f"[summaries] {scope!r}", flush=True) try: return get_summaries(scope) except Exception as e: return {"scope": scope, "feedback_summary": None, "complaint_summary": None, "available_scopes": [], "note": f"Something went wrong: {type(e).__name__}"} ask_ui = gr.Interface( fn=ask, inputs=gr.Textbox(label="Question"), outputs=gr.JSON(label="Response"), api_name="ask", ) summaries_ui = gr.Interface( fn=summaries, inputs=gr.Textbox(label="Scope: ALL, a Service_ID (e.g. SVC013) or an Entity_ID (e.g. ENT03)"), outputs=gr.JSON(label="Summaries"), api_name="summaries", ) demo = gr.TabbedInterface([ask_ui, summaries_ui], ["Ask the Data", "Summaries"]) # Login for the page and the API. Set AGENT_USER and AGENT_PASS as Space secrets. _user = os.environ.get("AGENT_USER") _pass = os.environ.get("AGENT_PASS") if _user and _pass: demo.launch(auth=(_user, _pass)) else: print("WARNING: AGENT_USER / AGENT_PASS not set, the Space is NOT password protected") demo.launch()