Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
import
|
| 3 |
-
import pandas as pd
|
| 4 |
from datetime import datetime
|
| 5 |
from typing import Literal, Annotated
|
| 6 |
from pydantic import BaseModel, Field
|
| 7 |
-
import os
|
| 8 |
-
import requests
|
| 9 |
|
| 10 |
-
# ===============================
|
| 11 |
-
# Configuration
|
| 12 |
-
# ===============================
|
| 13 |
HF_REPO = "samithcs/heart-rate-models"
|
| 14 |
HEART_MODEL_FILENAME = "Heart_Rate_Predictor_model.joblib"
|
| 15 |
ANOMALY_MODEL_FILENAME = "Anomaly_Detector_model.joblib"
|
| 16 |
MODEL_DIR = os.path.join("artifacts", "model_trainer")
|
| 17 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 18 |
|
| 19 |
-
# ===============================
|
| 20 |
-
# Hugging Face download helper
|
| 21 |
-
# ===============================
|
| 22 |
def download_from_hf(filename):
|
| 23 |
local_path = os.path.join(MODEL_DIR, filename)
|
| 24 |
if os.path.exists(local_path):
|
| 25 |
print(f"✅ {filename} already exists at {local_path}")
|
| 26 |
return local_path
|
| 27 |
-
|
| 28 |
url = f"https://huggingface.co/{HF_REPO}/resolve/main/{filename}"
|
| 29 |
print(f"⬇️ Downloading {filename} from {url} ...")
|
| 30 |
with requests.get(url, stream=True) as r:
|
|
@@ -35,10 +26,30 @@ def download_from_hf(filename):
|
|
| 35 |
print(f"✅ Downloaded {filename} to {local_path}")
|
| 36 |
return local_path
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# ===============================
|
| 39 |
# FastAPI app
|
| 40 |
# ===============================
|
| 41 |
-
app = FastAPI(title="Health Monitoring API")
|
| 42 |
|
| 43 |
# ===============================
|
| 44 |
# Request schemas
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from contextlib import asynccontextmanager
|
| 3 |
+
import joblib, os, requests, pandas as pd
|
| 4 |
from datetime import datetime
|
| 5 |
from typing import Literal, Annotated
|
| 6 |
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
HF_REPO = "samithcs/heart-rate-models"
|
| 9 |
HEART_MODEL_FILENAME = "Heart_Rate_Predictor_model.joblib"
|
| 10 |
ANOMALY_MODEL_FILENAME = "Anomaly_Detector_model.joblib"
|
| 11 |
MODEL_DIR = os.path.join("artifacts", "model_trainer")
|
| 12 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
def download_from_hf(filename):
|
| 15 |
local_path = os.path.join(MODEL_DIR, filename)
|
| 16 |
if os.path.exists(local_path):
|
| 17 |
print(f"✅ {filename} already exists at {local_path}")
|
| 18 |
return local_path
|
|
|
|
| 19 |
url = f"https://huggingface.co/{HF_REPO}/resolve/main/{filename}"
|
| 20 |
print(f"⬇️ Downloading {filename} from {url} ...")
|
| 21 |
with requests.get(url, stream=True) as r:
|
|
|
|
| 26 |
print(f"✅ Downloaded {filename} to {local_path}")
|
| 27 |
return local_path
|
| 28 |
|
| 29 |
+
# ===============================
|
| 30 |
+
# Lifespan context
|
| 31 |
+
# ===============================
|
| 32 |
+
@asynccontextmanager
|
| 33 |
+
async def lifespan(app: FastAPI):
|
| 34 |
+
global heart_model, heart_features, anomaly_model, anomaly_features
|
| 35 |
+
|
| 36 |
+
HEART_MODEL_PATH = download_from_hf(HEART_MODEL_FILENAME)
|
| 37 |
+
ANOMALY_MODEL_PATH = download_from_hf(ANOMALY_MODEL_FILENAME)
|
| 38 |
+
|
| 39 |
+
heart_model_artifacts = joblib.load(HEART_MODEL_PATH)
|
| 40 |
+
heart_model = heart_model_artifacts['model']
|
| 41 |
+
heart_features = heart_model_artifacts['feature_columns']
|
| 42 |
+
|
| 43 |
+
anomaly_model_artifacts = joblib.load(ANOMALY_MODEL_PATH)
|
| 44 |
+
anomaly_model = anomaly_model_artifacts['model']
|
| 45 |
+
anomaly_features = anomaly_model_artifacts['feature_columns']
|
| 46 |
+
|
| 47 |
+
yield
|
| 48 |
+
|
| 49 |
# ===============================
|
| 50 |
# FastAPI app
|
| 51 |
# ===============================
|
| 52 |
+
app = FastAPI(title="Health Monitoring API", lifespan=lifespan)
|
| 53 |
|
| 54 |
# ===============================
|
| 55 |
# Request schemas
|