Download src/api.py from sarra2002/RareDx: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/spaces/sarra2002/RareDx/resolve/main/src/api.py
- Command line
-
hf download hf://spaces/sarra2002/RareDx/src/api.py
-
curl -L -o api.py https://huggingface.co/spaces/sarra2002/RareDx/resolve/main/src/api.py
2.74 kB
| """REST API (FastAPI) around the system. | |
| Run: uvicorn src.api:app --reload then open http://127.0.0.1:8000/docs | |
| Never send real patient data: notes and symptoms are sent to a free external LLM API. | |
| """ | |
| from __future__ import annotations | |
| from functools import lru_cache | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, Field | |
| from .agent import QuestionAgent | |
| from .system import DISCLAIMER, DiagnosticSystem, build_system | |
| app = FastAPI(title="Rare disease diagnosis support (research)", description=DISCLAIMER, version="0.1.0") | |
| def get_system() -> DiagnosticSystem: | |
| return build_system() | |
| def get_agent() -> QuestionAgent: | |
| return QuestionAgent(get_system()) | |
| class DiagnoseRequest(BaseModel): | |
| codes: list[str] = Field(default_factory=list, description="HPO codes, e.g. HP:0001250") | |
| note: str | None = Field(None, description="Free-text note (invented or public cases only)") | |
| class UpdateRequest(BaseModel): | |
| codes: list[str] | |
| llm_sets: list[list[str]] | |
| confirmed: list[str] = Field(default_factory=list) | |
| excluded: list[str] = Field(default_factory=list) | |
| def health() -> dict: | |
| return {"status": "ok", "disclaimer": DISCLAIMER} | |
| def diagnose(req: DiagnoseRequest) -> dict: | |
| system, agent = get_system(), get_agent() | |
| understood = system.understand_note(req.note) if req.note else [] | |
| codes = list(dict.fromkeys(req.codes + [c for c, _, _ in understood])) | |
| unknown = [c for c in codes if c not in system.kb.terms] | |
| if unknown: | |
| raise HTTPException(422, f"Unknown HPO codes: {unknown}") | |
| if not codes: | |
| raise HTTPException(422, "Give HPO codes or a note.") | |
| d = system.diagnose(codes) | |
| questions = agent.choose_questions(d.ranking, codes) | |
| return { | |
| "symptoms": [{"code": c, "name": system.kb.term_name(c)} for c in codes], | |
| "to_verify": [{"code": c, "name": n, "similarity": round(s, 2)} for c, n, s in understood | |
| if s < system.settings.to_verify_threshold], | |
| "metabolic_share": d.metabolic_share, | |
| "ranking": system.explain(d.ranking, codes), | |
| "questions": [{"code": q, "name": system.kb.term_name(q), "lab": system.kb.is_metabolic(q)} for q in questions], | |
| "llm_sets": [sorted(s) for s in d.llm_sets], | |
| "disclaimer": DISCLAIMER, | |
| } | |
| def update(req: UpdateRequest) -> dict: | |
| system, agent = get_system(), get_agent() | |
| ranking = agent.update(req.codes, [set(s) for s in req.llm_sets], req.confirmed, req.excluded) | |
| codes = list(dict.fromkeys(req.codes + req.confirmed)) | |
| return {"ranking": system.explain(ranking, codes), "disclaimer": DISCLAIMER} | |