Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import joblib
|
| 7 |
-
import numpy as np
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
# Load the model
|
| 11 |
-
model = joblib.load("/
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def predict_stroke_risk(age, gender, hypertension, heart_disease, ever_married,
|
| 16 |
-
work_type, residence_type, avg_glucose_level, bmi, smoking_status):
|
| 17 |
-
"""Make stroke prediction with the trained model"""
|
| 18 |
-
|
| 19 |
-
# Create a DataFrame with the input data
|
| 20 |
-
data = pd.DataFrame({
|
| 21 |
-
'age': [age],
|
| 22 |
-
'gender': [gender.lower()],
|
| 23 |
-
'hypertension': [hypertension],
|
| 24 |
-
'heart_disease': [heart_disease],
|
| 25 |
-
'ever_married': [ever_married.lower()],
|
| 26 |
-
'work_type': [work_type.lower()],
|
| 27 |
-
'Residence_type': [residence_type.lower()],
|
| 28 |
-
'avg_glucose_level': [avg_glucose_level],
|
| 29 |
-
'bmi': [bmi],
|
| 30 |
-
'smoking_status': [smoking_status.lower()]
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
# Make prediction
|
| 34 |
-
prediction = model.predict(data)
|
| 35 |
-
probability = model.predict_proba(data)[0][1]
|
| 36 |
-
|
| 37 |
-
# Create result message
|
| 38 |
-
if prediction[0] == 1:
|
| 39 |
-
result = f"High Risk of Stroke (Probability: {probability:.2%})"
|
| 40 |
-
else:
|
| 41 |
-
result = f"Low Risk of Stroke (Probability: {probability:.2%})"
|
| 42 |
-
|
| 43 |
-
return result
|
| 44 |
-
|
| 45 |
-
# Create the Gradio interface
|
| 46 |
-
iface = gr.Interface(
|
| 47 |
-
fn=predict_stroke_risk,
|
| 48 |
-
inputs=[
|
| 49 |
-
gr.Slider(minimum=0, maximum=120, step=1, label="Age"),
|
| 50 |
-
gr.Dropdown(choices=["Male", "Female", "Other"], label="Gender"),
|
| 51 |
-
gr.Checkbox(label="Hypertension"),
|
| 52 |
-
gr.Checkbox(label="Heart Disease"),
|
| 53 |
-
gr.Dropdown(choices=["Yes", "No"], label="Ever Married"),
|
| 54 |
-
gr.Dropdown(
|
| 55 |
-
choices=["Private", "Self-employed", "Govt_job", "Children", "Never_worked"],
|
| 56 |
-
label="Work Type"
|
| 57 |
-
),
|
| 58 |
-
gr.Dropdown(choices=["Urban", "Rural"], label="Residence Type"),
|
| 59 |
-
gr.Slider(minimum=50, maximum=300, step=1, label="Average Glucose Level"),
|
| 60 |
-
gr.Slider(minimum=10, maximum=70, step=0.1, label="BMI"),
|
| 61 |
-
gr.Dropdown(
|
| 62 |
-
choices=["formerly smoked", "never smoked", "smokes", "Unknown"],
|
| 63 |
-
label="Smoking Status"
|
| 64 |
-
)
|
| 65 |
-
],
|
| 66 |
-
outputs=gr.Text(label="Prediction Result"),
|
| 67 |
-
title="Stroke Risk Prediction",
|
| 68 |
-
description="Enter patient information to predict stroke risk.",
|
| 69 |
-
examples=[
|
| 70 |
-
[45, "Male", True, False, "Yes", "Private", "Urban", 95.5, 28.5, "never smoked"],
|
| 71 |
-
[67, "Female", True, True, "Yes", "Self-employed", "Rural", 228.7, 36.6, "formerly smoked"]
|
| 72 |
-
]
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
# Launch the app
|
| 76 |
-
if __name__ == "__main__":
|
| 77 |
iface.launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import joblib
|
| 7 |
+
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Load the model
|
| 11 |
+
model = joblib.load("/home/user/app/model.joblib")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def predict_stroke_risk(age, gender, hypertension, heart_disease, ever_married,
|
| 16 |
+
work_type, residence_type, avg_glucose_level, bmi, smoking_status):
|
| 17 |
+
"""Make stroke prediction with the trained model"""
|
| 18 |
+
|
| 19 |
+
# Create a DataFrame with the input data
|
| 20 |
+
data = pd.DataFrame({
|
| 21 |
+
'age': [age],
|
| 22 |
+
'gender': [gender.lower()],
|
| 23 |
+
'hypertension': [hypertension],
|
| 24 |
+
'heart_disease': [heart_disease],
|
| 25 |
+
'ever_married': [ever_married.lower()],
|
| 26 |
+
'work_type': [work_type.lower()],
|
| 27 |
+
'Residence_type': [residence_type.lower()],
|
| 28 |
+
'avg_glucose_level': [avg_glucose_level],
|
| 29 |
+
'bmi': [bmi],
|
| 30 |
+
'smoking_status': [smoking_status.lower()]
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
# Make prediction
|
| 34 |
+
prediction = model.predict(data)
|
| 35 |
+
probability = model.predict_proba(data)[0][1]
|
| 36 |
+
|
| 37 |
+
# Create result message
|
| 38 |
+
if prediction[0] == 1:
|
| 39 |
+
result = f"High Risk of Stroke (Probability: {probability:.2%})"
|
| 40 |
+
else:
|
| 41 |
+
result = f"Low Risk of Stroke (Probability: {probability:.2%})"
|
| 42 |
+
|
| 43 |
+
return result
|
| 44 |
+
|
| 45 |
+
# Create the Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=predict_stroke_risk,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Slider(minimum=0, maximum=120, step=1, label="Age"),
|
| 50 |
+
gr.Dropdown(choices=["Male", "Female", "Other"], label="Gender"),
|
| 51 |
+
gr.Checkbox(label="Hypertension"),
|
| 52 |
+
gr.Checkbox(label="Heart Disease"),
|
| 53 |
+
gr.Dropdown(choices=["Yes", "No"], label="Ever Married"),
|
| 54 |
+
gr.Dropdown(
|
| 55 |
+
choices=["Private", "Self-employed", "Govt_job", "Children", "Never_worked"],
|
| 56 |
+
label="Work Type"
|
| 57 |
+
),
|
| 58 |
+
gr.Dropdown(choices=["Urban", "Rural"], label="Residence Type"),
|
| 59 |
+
gr.Slider(minimum=50, maximum=300, step=1, label="Average Glucose Level"),
|
| 60 |
+
gr.Slider(minimum=10, maximum=70, step=0.1, label="BMI"),
|
| 61 |
+
gr.Dropdown(
|
| 62 |
+
choices=["formerly smoked", "never smoked", "smokes", "Unknown"],
|
| 63 |
+
label="Smoking Status"
|
| 64 |
+
)
|
| 65 |
+
],
|
| 66 |
+
outputs=gr.Text(label="Prediction Result"),
|
| 67 |
+
title="Stroke Risk Prediction",
|
| 68 |
+
description="Enter patient information to predict stroke risk.",
|
| 69 |
+
examples=[
|
| 70 |
+
[45, "Male", True, False, "Yes", "Private", "Urban", 95.5, 28.5, "never smoked"],
|
| 71 |
+
[67, "Female", True, True, "Yes", "Self-employed", "Rural", 228.7, 36.6, "formerly smoked"]
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Launch the app
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
iface.launch()
|