Spaces:
Sleeping
Sleeping
Download api.py from Smart-Slate/scientific-watch-agent: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/spaces/Smart-Slate/scientific-watch-agent/resolve/main/api.py
- Command line
-
hf download hf://spaces/Smart-Slate/scientific-watch-agent/api.py
-
curl -L -o api.py https://huggingface.co/spaces/Smart-Slate/scientific-watch-agent/resolve/main/api.py
3.3 kB
| """Minimal local REST wrapper around the existing pipeline (src/agents/graph.py::run_pipeline). | |
| Reuses the exact same call path app.py already uses successfully — no pipeline logic duplicated here. | |
| Consumer: local tools calling http://localhost:8000 (e.g. GRETTA's Context panel). | |
| """ | |
| from __future__ import annotations | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from src.agents.graph import run_pipeline | |
| app = FastAPI(title="Scientific Watch Agent API") | |
| # GRETTA's task pane runs at https://localhost:3000; allow it (and any localhost | |
| # origin) to call this local API. This never touches the network beyond your machine. | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["https://localhost:3000", "http://localhost:3000"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class ReviewRequest(BaseModel): | |
| topic: str | |
| chemical_name: str | None = None | |
| n_raw: int = 30 | |
| top_n: int = 10 | |
| from_year: int = 2015 | |
| narrative_mode: bool = False | |
| def _article_to_dict(a) -> dict: | |
| return { | |
| "title": getattr(a, "title", "Untitled"), | |
| "year": getattr(a, "year", None), | |
| "journal_name": getattr(a, "journal_name", None), | |
| "url": getattr(a, "url", None), | |
| } | |
| def _summary_to_dict(s) -> dict: | |
| if hasattr(s, "problem"): # structured mode | |
| return { | |
| "problem": s.problem, | |
| "method": s.method, | |
| "results": s.results, | |
| } | |
| return {"text": getattr(s, "text", str(s))} # narrative mode | |
| def review(req: ReviewRequest) -> dict: | |
| effective_topic = req.topic.strip() | |
| if req.chemical_name and req.chemical_name.strip(): | |
| effective_topic = f"{req.chemical_name.strip()} — {effective_topic}" | |
| try: | |
| state = run_pipeline( | |
| effective_topic, | |
| n_raw=req.n_raw, | |
| top_n=req.top_n, | |
| from_year=req.from_year, | |
| narrative_mode=req.narrative_mode, | |
| ) | |
| except Exception as exc: # surface pipeline errors as a proper HTTP error, not a crash | |
| raise HTTPException(status_code=500, detail=str(exc)) from exc | |
| synthesis = state.get("synthesis") | |
| trends = state.get("trend_analysis") | |
| top_articles = state.get("top_articles", []) | |
| summaries = state.get("summaries") or state.get("narrative_summaries") or [] | |
| return { | |
| "topic": state.get("topic", effective_topic), | |
| "synthesis": { | |
| "overview": synthesis.overview, | |
| "main_approaches": synthesis.main_approaches, | |
| "key_findings": synthesis.key_findings, | |
| } if synthesis else None, | |
| "trends": { | |
| "trends": [getattr(t, "name", str(t)) for t in trends.trends] if trends.trends else [], | |
| "future_perspectives": trends.future_perspectives if trends.future_perspectives else [], | |
| } if trends else None, | |
| "top_articles": [_article_to_dict(a) for a in top_articles[:15]], | |
| "summaries": [_summary_to_dict(s) for s in summaries[:15]], | |
| "errors": state.get("errors", []), | |
| } | |
| def health() -> dict: | |
| return {"status": "ok"} |