samithcs commited on
Commit
ff51779
·
verified ·
1 Parent(s): f584aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -38
app.py CHANGED
@@ -1,53 +1,40 @@
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
- return local_path
18
- url = f"https://huggingface.co/{HF_REPO}/resolve/main/{filename}"
19
- with requests.get(url, stream=True) as r:
20
- r.raise_for_status()
21
- with open(local_path, "wb") as f:
22
- for chunk in r.iter_content(chunk_size=8192):
23
- f.write(chunk)
24
- return local_path
25
 
26
  # ===============================
27
- # Lifespan context
28
  # ===============================
29
- @asynccontextmanager
30
- async def lifespan(app: FastAPI):
31
- global heart_model, heart_features, anomaly_model, anomaly_features
32
 
33
- HEART_MODEL_PATH = download_from_hf(HEART_MODEL_FILENAME)
34
- ANOMALY_MODEL_PATH = download_from_hf(ANOMALY_MODEL_FILENAME)
 
35
 
36
- heart_model_artifacts = joblib.load(HEART_MODEL_PATH)
37
- heart_model = heart_model_artifacts['model']
38
- heart_features = heart_model_artifacts['feature_columns']
39
-
40
- anomaly_model_artifacts = joblib.load(ANOMALY_MODEL_PATH)
41
- anomaly_model = anomaly_model_artifacts['model']
42
- anomaly_features = anomaly_model_artifacts['feature_columns']
43
-
44
- yield
45
 
46
  # ===============================
47
  # FastAPI app
48
  # ===============================
49
- app = FastAPI(title="Health Monitoring API", lifespan=lifespan)
50
 
 
 
 
51
 
52
  # ===============================
53
  # Request schemas
@@ -92,9 +79,8 @@ class AnomalyInput(BaseModel):
92
  sleep_stage: Annotated[Literal['light_sleep','deep_sleep','rem_sleep'], Field(...)]
93
  date: Annotated[datetime, Field(...)]
94
 
95
-
96
  # ===============================
97
- # Utility: preprocess features
98
  # ===============================
99
  def preprocess_heart_features(data_dict: dict) -> pd.DataFrame:
100
  data_dict['date_encoded'] = data_dict['date'].timestamp()
@@ -117,10 +103,6 @@ def preprocess_anomaly_features(data_dict: dict) -> pd.DataFrame:
117
  # ===============================
118
  # Endpoints
119
  # ===============================
120
- @app.get("/")
121
- def home():
122
- return {"message":"Health Monitoring API is running!"}
123
-
124
  @app.post("/predict_heart_rate")
125
  def predict_heart_rate(input_data: HeartRateInput):
126
  try:
 
1
  from fastapi import FastAPI
2
+ import joblib
3
+ import pandas as pd
4
  from datetime import datetime
5
  from typing import Literal, Annotated
6
  from pydantic import BaseModel, Field
7
+ from huggingface_hub import hf_hub_download
8
 
9
+ # ===============================
10
+ # Hugging Face model config
11
+ # ===============================
12
  HF_REPO = "samithcs/heart-rate-models"
13
  HEART_MODEL_FILENAME = "Heart_Rate_Predictor_model.joblib"
14
  ANOMALY_MODEL_FILENAME = "Anomaly_Detector_model.joblib"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # ===============================
17
+ # Load models directly from HF
18
  # ===============================
19
+ HEART_MODEL_PATH = hf_hub_download(repo_id=HF_REPO, filename=HEART_MODEL_FILENAME)
20
+ ANOMALY_MODEL_PATH = hf_hub_download(repo_id=HF_REPO, filename=ANOMALY_MODEL_FILENAME)
 
21
 
22
+ heart_model_artifacts = joblib.load(HEART_MODEL_PATH)
23
+ heart_model = heart_model_artifacts['model']
24
+ heart_features = heart_model_artifacts['feature_columns']
25
 
26
+ anomaly_model_artifacts = joblib.load(ANOMALY_MODEL_PATH)
27
+ anomaly_model = anomaly_model_artifacts['model']
28
+ anomaly_features = anomaly_model_artifacts['feature_columns']
 
 
 
 
 
 
29
 
30
  # ===============================
31
  # FastAPI app
32
  # ===============================
33
+ app = FastAPI(title="Health Monitoring API")
34
 
35
+ @app.get("/")
36
+ def home():
37
+ return {"message": "Health Monitoring API is running!"}
38
 
39
  # ===============================
40
  # Request schemas
 
79
  sleep_stage: Annotated[Literal['light_sleep','deep_sleep','rem_sleep'], Field(...)]
80
  date: Annotated[datetime, Field(...)]
81
 
 
82
  # ===============================
83
+ # Utility functions
84
  # ===============================
85
  def preprocess_heart_features(data_dict: dict) -> pd.DataFrame:
86
  data_dict['date_encoded'] = data_dict['date'].timestamp()
 
103
  # ===============================
104
  # Endpoints
105
  # ===============================
 
 
 
 
106
  @app.post("/predict_heart_rate")
107
  def predict_heart_rate(input_data: HeartRateInput):
108
  try: