Update src/app.py
Browse files- src/app.py +7 -10
src/app.py
CHANGED
|
@@ -2,23 +2,21 @@ import streamlit as st
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
-
from xgboost import XGBClassifier
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
# Load Saved Model & Encoders
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
model = joblib.load("src/best_stroke_model_1.pkl")
|
|
|
|
|
|
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
-
st.error(f"
|
| 14 |
st.stop()
|
| 15 |
|
| 16 |
-
x_num_scaler = joblib.load("src/x_num_scaler_1.pkl")
|
| 17 |
-
x_cat_encoder = joblib.load("src/x_cat_encoder_1.pkl")
|
| 18 |
-
x_cat_ordinal_encoder = joblib.load("src/x_cat_ordinal_encoder_1.pkl")
|
| 19 |
-
|
| 20 |
# ----------------------------
|
| 21 |
-
# Streamlit
|
| 22 |
# ----------------------------
|
| 23 |
st.set_page_config(page_title="Stroke Prediction App", page_icon="🧠", layout="centered")
|
| 24 |
st.title("🧠 Stroke Prediction App")
|
|
@@ -43,7 +41,7 @@ if st.button("Predict"):
|
|
| 43 |
input_df = pd.DataFrame([[gender, age, hypertension, heart_disease, work_type, avg_glucose_level, bmi, smoking_status]],
|
| 44 |
columns=["gender", "age", "hypertension", "heart_disease", "work_type", "avg_glucose_level", "bmi", "smoking_status"])
|
| 45 |
|
| 46 |
-
#
|
| 47 |
scaled_x_num = x_num_scaler.transform(input_df[["age", "avg_glucose_level", "bmi"]])
|
| 48 |
binary_x = input_df[["hypertension", "heart_disease"]].values
|
| 49 |
encoded_x_cat = x_cat_encoder.transform(input_df[["work_type", "smoking_status"]])
|
|
@@ -51,10 +49,9 @@ if st.button("Predict"):
|
|
| 51 |
|
| 52 |
final_x = np.hstack([scaled_x_num, binary_x, encoded_x_cat, ordinal_encoded_x_cat])
|
| 53 |
|
| 54 |
-
# Predict
|
| 55 |
prediction = model.predict(final_x)
|
| 56 |
result = "⚠️ High Risk of Stroke" if prediction[0] == 1 else "✅ Low Risk of Stroke"
|
| 57 |
|
| 58 |
st.success(f"**Prediction:** {result}")
|
| 59 |
st.info("This prediction is based on machine learning and should not replace professional medical advice.")
|
| 60 |
-
|
|
|
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
# Load Saved Model & Encoders
|
| 8 |
# ----------------------------
|
| 9 |
try:
|
| 10 |
model = joblib.load("src/best_stroke_model_1.pkl")
|
| 11 |
+
x_num_scaler = joblib.load("src/x_num_scaler_1.pkl")
|
| 12 |
+
x_cat_encoder = joblib.load("src/x_cat_encoder_1.pkl")
|
| 13 |
+
x_cat_ordinal_encoder = joblib.load("src/x_cat_ordinal_encoder_1.pkl")
|
| 14 |
except Exception as e:
|
| 15 |
+
st.error(f"Failed to load model or encoders: {e}")
|
| 16 |
st.stop()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# ----------------------------
|
| 19 |
+
# Streamlit Layout
|
| 20 |
# ----------------------------
|
| 21 |
st.set_page_config(page_title="Stroke Prediction App", page_icon="🧠", layout="centered")
|
| 22 |
st.title("🧠 Stroke Prediction App")
|
|
|
|
| 41 |
input_df = pd.DataFrame([[gender, age, hypertension, heart_disease, work_type, avg_glucose_level, bmi, smoking_status]],
|
| 42 |
columns=["gender", "age", "hypertension", "heart_disease", "work_type", "avg_glucose_level", "bmi", "smoking_status"])
|
| 43 |
|
| 44 |
+
# Preprocessing
|
| 45 |
scaled_x_num = x_num_scaler.transform(input_df[["age", "avg_glucose_level", "bmi"]])
|
| 46 |
binary_x = input_df[["hypertension", "heart_disease"]].values
|
| 47 |
encoded_x_cat = x_cat_encoder.transform(input_df[["work_type", "smoking_status"]])
|
|
|
|
| 49 |
|
| 50 |
final_x = np.hstack([scaled_x_num, binary_x, encoded_x_cat, ordinal_encoded_x_cat])
|
| 51 |
|
| 52 |
+
# Predict
|
| 53 |
prediction = model.predict(final_x)
|
| 54 |
result = "⚠️ High Risk of Stroke" if prediction[0] == 1 else "✅ Low Risk of Stroke"
|
| 55 |
|
| 56 |
st.success(f"**Prediction:** {result}")
|
| 57 |
st.info("This prediction is based on machine learning and should not replace professional medical advice.")
|
|
|