Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import numpy as np
|
|
| 6 |
import pickle
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Headache Predictor API")
|
| 11 |
|
|
@@ -33,21 +34,43 @@ async def load_model():
|
|
| 33 |
import traceback
|
| 34 |
traceback.print_exc()
|
| 35 |
|
| 36 |
-
class
|
| 37 |
-
features:
|
| 38 |
|
| 39 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
prediction: int
|
| 41 |
probability: float
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
@app.get("/")
|
| 44 |
def read_root():
|
| 45 |
return {
|
| 46 |
"message": "Headache Predictor API",
|
| 47 |
"status": "running",
|
| 48 |
"endpoints": {
|
| 49 |
-
"predict": "/predict",
|
|
|
|
| 50 |
"health": "/health"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
|
@@ -58,8 +81,9 @@ def health_check():
|
|
| 58 |
"model_loaded": model is not None
|
| 59 |
}
|
| 60 |
|
| 61 |
-
@app.post("/predict", response_model=
|
| 62 |
-
def predict(request:
|
|
|
|
| 63 |
if model is None:
|
| 64 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 65 |
|
|
@@ -71,9 +95,40 @@ def predict(request: PredictionRequest):
|
|
| 71 |
prediction = model.predict(features)[0]
|
| 72 |
probability = float(model.predict_proba(features)[0][int(prediction)])
|
| 73 |
|
| 74 |
-
return
|
| 75 |
prediction=int(prediction),
|
| 76 |
probability=probability
|
| 77 |
)
|
| 78 |
except Exception as e:
|
| 79 |
raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import pickle
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import os
|
| 9 |
+
from typing import List, Union
|
| 10 |
|
| 11 |
app = FastAPI(title="Headache Predictor API")
|
| 12 |
|
|
|
|
| 34 |
import traceback
|
| 35 |
traceback.print_exc()
|
| 36 |
|
| 37 |
+
class SinglePredictionRequest(BaseModel):
|
| 38 |
+
features: List[float]
|
| 39 |
|
| 40 |
+
class BatchPredictionRequest(BaseModel):
|
| 41 |
+
instances: List[List[float]]
|
| 42 |
+
|
| 43 |
+
class DayPrediction(BaseModel):
|
| 44 |
+
day: int
|
| 45 |
+
prediction: int
|
| 46 |
+
probability: float
|
| 47 |
+
|
| 48 |
+
class SinglePredictionResponse(BaseModel):
|
| 49 |
prediction: int
|
| 50 |
probability: float
|
| 51 |
|
| 52 |
+
class BatchPredictionResponse(BaseModel):
|
| 53 |
+
predictions: List[DayPrediction]
|
| 54 |
+
|
| 55 |
@app.get("/")
|
| 56 |
def read_root():
|
| 57 |
return {
|
| 58 |
"message": "Headache Predictor API",
|
| 59 |
"status": "running",
|
| 60 |
"endpoints": {
|
| 61 |
+
"predict": "/predict - Single day prediction",
|
| 62 |
+
"predict_batch": "/predict/batch - 7-day forecast",
|
| 63 |
"health": "/health"
|
| 64 |
+
},
|
| 65 |
+
"examples": {
|
| 66 |
+
"single": {
|
| 67 |
+
"url": "/predict",
|
| 68 |
+
"body": {"features": [1, 0, 0, 0, 1, 0, 1005.0, -9.5, 85.0, 15.5, 64.0, 5.5, 41.0, 0.0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 10, 40, 4, 7.0, 50.0, 60.0, 3.5, 1.5, 6.8]}
|
| 69 |
+
},
|
| 70 |
+
"batch": {
|
| 71 |
+
"url": "/predict/batch",
|
| 72 |
+
"body": {"instances": [["array of 37 features for day 1"], ["array for day 2"], "..."]}
|
| 73 |
+
}
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
|
|
|
| 81 |
"model_loaded": model is not None
|
| 82 |
}
|
| 83 |
|
| 84 |
+
@app.post("/predict", response_model=SinglePredictionResponse)
|
| 85 |
+
def predict(request: SinglePredictionRequest):
|
| 86 |
+
"""Predict headache risk for a single day"""
|
| 87 |
if model is None:
|
| 88 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 89 |
|
|
|
|
| 95 |
prediction = model.predict(features)[0]
|
| 96 |
probability = float(model.predict_proba(features)[0][int(prediction)])
|
| 97 |
|
| 98 |
+
return SinglePredictionResponse(
|
| 99 |
prediction=int(prediction),
|
| 100 |
probability=probability
|
| 101 |
)
|
| 102 |
except Exception as e:
|
| 103 |
raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
|
| 104 |
+
|
| 105 |
+
@app.post("/predict/batch", response_model=BatchPredictionResponse)
|
| 106 |
+
def predict_batch(request: BatchPredictionRequest):
|
| 107 |
+
"""Predict headache risk for multiple days (7-day forecast)"""
|
| 108 |
+
if model is None:
|
| 109 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
# Convert all instances to numpy array
|
| 113 |
+
features = np.array(request.instances)
|
| 114 |
+
|
| 115 |
+
if features.ndim != 2:
|
| 116 |
+
raise ValueError(f"Expected 2D array, got shape {features.shape}")
|
| 117 |
+
|
| 118 |
+
# Make predictions for all days
|
| 119 |
+
predictions = model.predict(features)
|
| 120 |
+
probabilities = model.predict_proba(features)
|
| 121 |
+
|
| 122 |
+
# Format results
|
| 123 |
+
results = []
|
| 124 |
+
for i, (pred, prob_array) in enumerate(zip(predictions, probabilities), 1):
|
| 125 |
+
results.append(DayPrediction(
|
| 126 |
+
day=i,
|
| 127 |
+
prediction=int(pred),
|
| 128 |
+
probability=float(prob_array[int(pred)])
|
| 129 |
+
))
|
| 130 |
+
|
| 131 |
+
return BatchPredictionResponse(predictions=results)
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
raise HTTPException(status_code=400, detail=f"Batch prediction error: {str(e)}")
|