Spaces:
Sleeping
Sleeping
sync: ์ต์ ๋ฐฑ์๋ ์ฝ๋ ๋๊ธฐํ (detect-only, plans, save, push ์๋ํฌ์ธํธ ์ถ๊ฐ)
59a1470 Download api/save.py from BOWOWEEEEE/pill-advisor-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/save.py
- Command line
-
hf download hf://spaces/BOWOWEEEEE/pill-advisor-backend/api/save.py
-
curl -L -o save.py https://huggingface.co/spaces/BOWOWEEEEE/pill-advisor-backend/resolve/main/api/save.py
1.78 kB
| from datetime import datetime, timezone | |
| from typing import Any, List, Optional | |
| from fastapi import APIRouter, Header, HTTPException | |
| from pydantic import BaseModel | |
| from services.auth import auth as _auth | |
| router = APIRouter() | |
| class PillResult(BaseModel): | |
| name: str | |
| shape: Optional[str] = None | |
| color: Optional[str] = None | |
| confidence: Optional[float] = None | |
| bbox: Optional[List[float]] = None | |
| class SaveRecordRequest(BaseModel): | |
| image_url: Optional[str] = None | |
| pills: List[PillResult] = [] | |
| dur_info: List[dict[str, Any]] = [] | |
| meal_last: Optional[str] = None | |
| meal_next: Optional[str] = None | |
| advice: Optional[str] = None | |
| has_plan: bool = False | |
| async def save_record( | |
| data: SaveRecordRequest, | |
| authorization: str = Header(...), | |
| ) -> dict[str, Any]: | |
| # has_plan์ medication_records ์ปฌ๋ผ ์์ โ ํ๋ก ํธ ํ์ ์ ์ฉ | |
| db, user_id = _auth(authorization) | |
| taken_at = datetime.now(tz=timezone.utc).isoformat() | |
| detected_drugs = [pill.model_dump(exclude_none=True) for pill in data.pills] | |
| result = ( | |
| db.table("medication_records") | |
| .insert( | |
| { | |
| "user_id": user_id, | |
| "taken_at": taken_at, | |
| "detected_drugs": detected_drugs, | |
| "dur_info": data.dur_info, | |
| "llm_advice": data.advice or "", | |
| "image_url": data.image_url, | |
| "meal_timing_input": ( | |
| f"meal_last={data.meal_last}, meal_next={data.meal_next}" | |
| ), | |
| } | |
| ) | |
| .execute() | |
| ) | |
| if not result.data: | |
| raise HTTPException(status_code=500, detail="๊ธฐ๋ก ์ ์ฅ์ ์คํจํ์ต๋๋ค.") | |
| return {"id": result.data[0]["id"]} | |