Spaces:
Running on Zero
Running on Zero
Download app.py from JA26/ask-the-data: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/JA26/ask-the-data/resolve/main/app.py
- Command line
-
hf download hf://spaces/JA26/ask-the-data/app.py
-
curl -L -o app.py https://huggingface.co/spaces/JA26/ask-the-data/resolve/main/app.py
1.69 kB
| import os | |
| import gradio as gr | |
| import spaces | |
| from agent import answer_question, get_summaries, load_db | |
| 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() |