Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,64 @@ import joblib
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Define prediction function
|
| 9 |
-
def predict_stroke(age, hypertension, heart_disease,
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
'heart_disease': [heart_disease],
|
| 14 |
-
'avg_glucose_level': [glucose_level],
|
| 15 |
-
'bmi': [bmi]
|
| 16 |
-
}
|
| 17 |
-
df = pd.DataFrame(data)
|
| 18 |
-
prediction = model.predict(df)
|
| 19 |
-
return "Stroke Risk" if prediction[0] == 1 else "No Stroke Risk"
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=predict_stroke,
|
| 24 |
inputs=[
|
|
|
|
| 25 |
gr.Number(label="Age"),
|
| 26 |
gr.Radio(choices=[0, 1], label="Hypertension (0=No, 1=Yes)"),
|
| 27 |
gr.Radio(choices=[0, 1], label="Heart Disease (0=No, 1=Yes)"),
|
|
|
|
|
|
|
|
|
|
| 28 |
gr.Number(label="Average Glucose Level"),
|
|
|
|
| 29 |
gr.Number(label="BMI")
|
| 30 |
],
|
| 31 |
outputs="text",
|
| 32 |
-
title="Stroke Prediction
|
| 33 |
-
description="Predict
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Launch
|
| 37 |
-
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model and preprocessor
|
| 6 |
+
try:
|
| 7 |
+
model = joblib.load("stroke_rf_model.pkl")
|
| 8 |
+
preprocessor = joblib.load("preprocessor.pkl")
|
| 9 |
+
print("✅ Model and preprocessor loaded successfully.")
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print(f"❌ Error loading model/preprocessor: {str(e)}")
|
| 12 |
+
model = None
|
| 13 |
+
preprocessor = None
|
| 14 |
|
| 15 |
# Define prediction function
|
| 16 |
+
def predict_stroke(gender, age, hypertension, heart_disease, ever_married,
|
| 17 |
+
work_type, residence_type, avg_glucose_level, smoking_status, bmi):
|
| 18 |
+
if model is None or preprocessor is None:
|
| 19 |
+
return "Error: Model or preprocessor not loaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
data = pd.DataFrame([{
|
| 22 |
+
'gender': gender,
|
| 23 |
+
'age': age,
|
| 24 |
+
'hypertension': hypertension,
|
| 25 |
+
'heart_disease': heart_disease,
|
| 26 |
+
'ever_married': ever_married,
|
| 27 |
+
'work_type': work_type,
|
| 28 |
+
'Residence_type': residence_type,
|
| 29 |
+
'avg_glucose_level': avg_glucose_level,
|
| 30 |
+
'smoking_status': smoking_status,
|
| 31 |
+
'bmi': bmi
|
| 32 |
+
}])
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
processed_data = preprocessor.transform(data)
|
| 36 |
+
prediction = model.predict(processed_data)
|
| 37 |
+
return "⚠️ Stroke Risk" if prediction[0] == 1 else "✅ No Stroke Risk"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"❌ Prediction error: {str(e)}")
|
| 40 |
+
return f"Error: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Gradio Interface
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=predict_stroke,
|
| 45 |
inputs=[
|
| 46 |
+
gr.Textbox(label="Gender (Male/Female)"),
|
| 47 |
gr.Number(label="Age"),
|
| 48 |
gr.Radio(choices=[0, 1], label="Hypertension (0=No, 1=Yes)"),
|
| 49 |
gr.Radio(choices=[0, 1], label="Heart Disease (0=No, 1=Yes)"),
|
| 50 |
+
gr.Textbox(label="Ever Married (Yes/No)"),
|
| 51 |
+
gr.Textbox(label="Work Type"),
|
| 52 |
+
gr.Textbox(label="Residence Type (Urban/Rural)"),
|
| 53 |
gr.Number(label="Average Glucose Level"),
|
| 54 |
+
gr.Textbox(label="Smoking Status"),
|
| 55 |
gr.Number(label="BMI")
|
| 56 |
],
|
| 57 |
outputs="text",
|
| 58 |
+
title="🩺 Stroke Risk Prediction App",
|
| 59 |
+
description="Predict the likelihood of stroke based on health metrics.",
|
| 60 |
+
allow_flagging="never"
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Launch Gradio app
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|