UdasriHasindu commited on
Commit
c58924d
·
1 Parent(s): d352a67

implement analyze/ route for post requests

Browse files
Files changed (4) hide show
  1. main.py +45 -0
  2. routers/__init__.py +0 -0
  3. schema/__init__.py +0 -0
  4. schema/patient_inputs.py +14 -0
main.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from contextlib import asynccontextmanager
4
+ from dotenv import load_dotenv
5
+ from routers import analyze_router
6
+ from ml.model_predictor import _download_and_cache
7
+
8
+ load_dotenv()
9
+
10
+ @asynccontextmanager
11
+ async def lifespan(app: FastAPI):
12
+ # Runs once when the Space starts — downloads + caches all 3 models
13
+ print("Startup: downloading models from HF Hub...")
14
+ _download_and_cache()
15
+ print("Startup complete. API is ready.")
16
+ yield
17
+ # Shutdown cleanup (optional)
18
+ print("Shutting down.")
19
+
20
+ app = FastAPI(
21
+ title="Parkinson's disease prediction API",
22
+ lifespan=lifespan,
23
+ )
24
+
25
+ app.add_middleware(
26
+ CORSMiddleware,
27
+ allow_origins=["*"],
28
+ allow_credentials=True,
29
+ allow_methods=["*"],
30
+ allow_headers=["*"],
31
+ )
32
+
33
+ app.include_router(analyze_router.router)
34
+
35
+ @app.get("/")
36
+ def read_root():
37
+ return {"message": "Parkinson's disease prediction API"}
38
+
39
+ @app.get("/health")
40
+ def health():
41
+ from ml.model_predictor import _cache
42
+ return {
43
+ "status": "ok",
44
+ "models_loaded": bool(_cache)
45
+ }
routers/__init__.py ADDED
File without changes
schema/__init__.py ADDED
File without changes
schema/patient_inputs.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+ from typing import Optional
3
+
4
+ class Patient(BaseModel):
5
+ id: Optional[int] = None
6
+ name: str
7
+
8
+ class BasicInfo(BaseModel):
9
+ age: int = Field(..., gt=10, lt=120)
10
+ sex: str = Field(..., example="male")
11
+ test_time: float
12
+
13
+ # Note: The /analyze/voice endpoint currently accepts raw multipart form fields
14
+ # rather than a JSON body, so these models are used as reference schemas only.