Download statagent/api.py from mnoorchenar/StatAgent: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/datasets/mnoorchenar/StatAgent/resolve/main/statagent/api.py
- Command line
-
hf download hf://datasets/mnoorchenar/StatAgent/statagent/api.py
-
curl -L -o api.py https://huggingface.co/datasets/mnoorchenar/StatAgent/resolve/main/statagent/api.py
1.87 kB
| import json, os | |
| from pathlib import Path | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| def _load_env(): | |
| p = Path(__file__).resolve().parent.parent / ".env" | |
| if p.exists(): | |
| for line in p.read_text().splitlines(): | |
| if "=" in line and not line.startswith("#"): | |
| k, v = line.split("=", 1) | |
| os.environ.setdefault(k.strip(), v.strip()) | |
| _load_env() | |
| from . import agent, audit, tools # noqa: E402 (after env load) | |
| app = FastAPI(title="StatAgent") | |
| REPLAY = Path(__file__).resolve().parent.parent / "examples" / "replay.json" | |
| class Q(BaseModel): | |
| question: str | |
| session: str = "demo" | |
| class D(BaseModel): | |
| approve: bool | |
| def health(): | |
| return {"status": "ok"} | |
| def ask(q: Q): | |
| try: | |
| return agent.ask(q.question, q.session) | |
| except Exception as e: | |
| # graceful fallback: replay a saved run when the LLM is unavailable / rate-limited | |
| if REPLAY.exists(): | |
| runs = json.loads(REPLAY.read_text()) | |
| pick = next((r for r in runs if r["question"].lower()[:25] == q.question.lower()[:25]), runs[0]) | |
| return {**pick, "answer": "*[Replayed saved example - live LLM unavailable]*\n\n" + pick["answer"]} | |
| raise HTTPException(503, str(e)) | |
| def datasets(): | |
| return {k: {"desc": v, "columns": list(tools.load(k).columns)} for k, v in tools.list_datasets().items()} | |
| def series(dataset: str, column: str): | |
| return tools.series(dataset, column) | |
| def proposals(): | |
| return audit.proposals() | |
| def decide(pid: int, d: D): | |
| audit.decide(pid, d.approve) | |
| return {"ok": True} | |
| def get_audit(n: int = 100): | |
| return audit.entries(n) | |