Abdullah1211 commited on
Commit
3e4137e
·
verified ·
1 Parent(s): 0a4aac1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -62
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.post("/")
16
- async def predict(request: Request):
17
- """
18
- Make a stroke risk prediction based on input features
19
- Example input:
20
- {
21
- "gender": "Male",
22
- "age": 67,
23
- "hypertension": 1,
24
- "heart_disease": 0,
25
- "ever_married": "Yes",
26
- "work_type": "Private",
27
- "Residence_type": "Urban",
28
- "avg_glucose_level": 228.69,
29
- "bmi": 36.6,
30
- "smoking_status": "formerly smoked"
31
- }
32
- """
33
- data = await request.json()
34
-
35
- # Convert to DataFrame
36
- input_df = pd.DataFrame([data])
37
-
38
- # Preprocess the input
39
- processed_df = pd.DataFrame()
40
- for col in numeric_cols:
41
- processed_df[col] = input_df[col]
42
- processed_df[encoded_cols] = preprocessor.transform(input_df)
43
- X = processed_df[numeric_cols + encoded_cols]
44
-
45
- # Get prediction probability
46
- prediction_proba = stroke_model['model'].predict_proba(X)[0, 1]
47
-
48
- # Classify risk level
49
- risk_level = "Very Low Risk"
50
- if prediction_proba > 0.8:
51
- risk_level = "Very High Risk"
52
- elif prediction_proba > 0.6:
53
- risk_level = "High Risk"
54
- elif prediction_proba > 0.4:
55
- risk_level = "Moderate Risk"
56
- elif prediction_proba > 0.2:
57
- risk_level = "Low Risk"
58
-
59
- return {
60
- "probability": float(prediction_proba),
61
- "prediction": risk_level,
62
- "stroke_prediction": int(prediction_proba > 0.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  }