Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,85 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import joblib
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
app = FastAPI()
|
| 7 |
-
|
| 8 |
-
# Load the model
|
| 9 |
-
print("Loading model...")
|
| 10 |
-
stroke_model = joblib.load("model.joblib")
|
| 11 |
-
encoded_cols = stroke_model["encoded_cols"]
|
| 12 |
-
numeric_cols = stroke_model["numeric_cols"]
|
| 13 |
-
preprocessor = stroke_model["preprocessor"]
|
| 14 |
-
|
| 15 |
-
@app.
|
| 16 |
-
async def
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
{
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
print("Loading model...")
|
| 10 |
+
stroke_model = joblib.load("model.joblib")
|
| 11 |
+
encoded_cols = stroke_model["encoded_cols"]
|
| 12 |
+
numeric_cols = stroke_model["numeric_cols"]
|
| 13 |
+
preprocessor = stroke_model["preprocessor"]
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
async def root():
|
| 17 |
+
"""
|
| 18 |
+
Root endpoint for health check and documentation
|
| 19 |
+
"""
|
| 20 |
+
return {
|
| 21 |
+
"message": "Stroke Prediction API is running",
|
| 22 |
+
"usage": "Send a POST request to / with patient data",
|
| 23 |
+
"example": {
|
| 24 |
+
"gender": "Male",
|
| 25 |
+
"age": 67,
|
| 26 |
+
"hypertension": 1,
|
| 27 |
+
"heart_disease": 0,
|
| 28 |
+
"ever_married": "Yes",
|
| 29 |
+
"work_type": "Private",
|
| 30 |
+
"Residence_type": "Urban",
|
| 31 |
+
"avg_glucose_level": 228.69,
|
| 32 |
+
"bmi": 36.6,
|
| 33 |
+
"smoking_status": "formerly smoked"
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
@app.post("/")
|
| 38 |
+
async def predict(request: Request):
|
| 39 |
+
"""
|
| 40 |
+
Make a stroke risk prediction based on input features
|
| 41 |
+
Example input:
|
| 42 |
+
{
|
| 43 |
+
"gender": "Male",
|
| 44 |
+
"age": 67,
|
| 45 |
+
"hypertension": 1,
|
| 46 |
+
"heart_disease": 0,
|
| 47 |
+
"ever_married": "Yes",
|
| 48 |
+
"work_type": "Private",
|
| 49 |
+
"Residence_type": "Urban",
|
| 50 |
+
"avg_glucose_level": 228.69,
|
| 51 |
+
"bmi": 36.6,
|
| 52 |
+
"smoking_status": "formerly smoked"
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
+
data = await request.json()
|
| 56 |
+
|
| 57 |
+
# Convert to DataFrame
|
| 58 |
+
input_df = pd.DataFrame([data])
|
| 59 |
+
|
| 60 |
+
# Preprocess the input
|
| 61 |
+
processed_df = pd.DataFrame()
|
| 62 |
+
for col in numeric_cols:
|
| 63 |
+
processed_df[col] = input_df[col]
|
| 64 |
+
processed_df[encoded_cols] = preprocessor.transform(input_df)
|
| 65 |
+
X = processed_df[numeric_cols + encoded_cols]
|
| 66 |
+
|
| 67 |
+
# Get prediction probability
|
| 68 |
+
prediction_proba = stroke_model['model'].predict_proba(X)[0, 1]
|
| 69 |
+
|
| 70 |
+
# Classify risk level
|
| 71 |
+
risk_level = "Very Low Risk"
|
| 72 |
+
if prediction_proba > 0.8:
|
| 73 |
+
risk_level = "Very High Risk"
|
| 74 |
+
elif prediction_proba > 0.6:
|
| 75 |
+
risk_level = "High Risk"
|
| 76 |
+
elif prediction_proba > 0.4:
|
| 77 |
+
risk_level = "Moderate Risk"
|
| 78 |
+
elif prediction_proba > 0.2:
|
| 79 |
+
risk_level = "Low Risk"
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"probability": float(prediction_proba),
|
| 83 |
+
"prediction": risk_level,
|
| 84 |
+
"stroke_prediction": int(prediction_proba > 0.5)
|
| 85 |
}
|