Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import dill
|
| 3 |
import pandas as pd
|
| 4 |
-
import xgboost as xgb
|
| 5 |
import numpy as np
|
| 6 |
import pickle
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return obj
|
| 12 |
-
|
| 13 |
-
model = decode_file('model.pkl')
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def predict(gender, age, hypertension, ever_married, work_type, heart_disease, avg_glucose_level, bmi, smoking_status, Residence_type):
|
| 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 |
-
# Predict the stroke probability
|
| 42 |
-
prediction = model.predict_proba(input_df)[0][1]
|
| 43 |
-
|
| 44 |
-
# Return the prediction
|
| 45 |
-
result = "The probability of stroke is {:.2f}%".format(prediction * 100) # to give a percentage
|
| 46 |
-
return result
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
input_labels = [
|
| 54 |
-
'gender', 'age', 'hypertension', 'ever_married', 'work_type',
|
| 55 |
-
'heart_disease', 'avg_glucose_level', 'bmi', 'smoking_status', 'Residence_type'
|
| 56 |
-
]
|
| 57 |
# Create the Gradio interface
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=predict,
|
| 60 |
inputs=[
|
| 61 |
-
gr.
|
| 62 |
-
gr.
|
| 63 |
-
gr.
|
| 64 |
-
gr.
|
| 65 |
-
gr.
|
| 66 |
-
gr.
|
| 67 |
-
gr.
|
| 68 |
-
gr.
|
| 69 |
-
gr.
|
| 70 |
-
gr.
|
| 71 |
],
|
| 72 |
outputs='text',
|
| 73 |
title='Stroke Probability Predictor',
|
| 74 |
description='Predicts the probability of having a stroke based on input features.'
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
+
from steps.transform import transformer_fn
|
| 6 |
|
| 7 |
+
# Load the model
|
| 8 |
+
with open('model.pkl', 'rb') as f:
|
| 9 |
+
model = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Get the transformer
|
| 12 |
+
transformer = transformer_fn()
|
| 13 |
|
| 14 |
def predict(gender, age, hypertension, ever_married, work_type, heart_disease, avg_glucose_level, bmi, smoking_status, Residence_type):
|
| 15 |
+
# Create input data
|
| 16 |
+
input_data = {
|
| 17 |
+
'gender': [gender],
|
| 18 |
+
'age': [float(age)], # Convert to float as expected by the model
|
| 19 |
+
'hypertension': [int(hypertension == 'Yes')], # Convert to int
|
| 20 |
+
'heart_disease': [int(heart_disease == 'Yes')],
|
| 21 |
+
'ever_married': [ever_married],
|
| 22 |
+
'work_type': [work_type],
|
| 23 |
+
'Residence_type': [Residence_type],
|
| 24 |
+
'avg_glucose_level': [float(avg_glucose_level)],
|
| 25 |
+
'bmi': [float(bmi)],
|
| 26 |
+
'smoking_status': [smoking_status]
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Convert to DataFrame
|
| 30 |
+
input_df = pd.DataFrame(input_data)
|
| 31 |
+
|
| 32 |
+
# Transform the input using the same transformer used during training
|
| 33 |
+
try:
|
| 34 |
+
X_transformed = transformer.transform(input_df)
|
| 35 |
+
prediction = model.predict_proba(X_transformed)[0][1]
|
| 36 |
+
return f"The probability of stroke is {prediction:.2%}"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error making prediction: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Create the Gradio interface
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=predict,
|
| 43 |
inputs=[
|
| 44 |
+
gr.Radio(choices=['Female', 'Male'], label="Gender"),
|
| 45 |
+
gr.Slider(minimum=0, maximum=100, label="Age"),
|
| 46 |
+
gr.Radio(choices=['Yes', 'No'], label="Hypertension"),
|
| 47 |
+
gr.Radio(choices=['Yes', 'No'], label="Ever Married"),
|
| 48 |
+
gr.Radio(choices=['Private', 'Self-employed', 'Govt_job', 'children', 'Never_worked'], label="Work Type"),
|
| 49 |
+
gr.Radio(choices=['Yes', 'No'], label="Heart Disease"),
|
| 50 |
+
gr.Number(label="Average Glucose Level"),
|
| 51 |
+
gr.Slider(minimum=10, maximum=50, label="BMI"),
|
| 52 |
+
gr.Radio(choices=['formerly smoked', 'never smoked', 'smokes', 'Unknown'], label="Smoking Status"),
|
| 53 |
+
gr.Radio(choices=['Urban', 'Rural'], label="Residence Type")
|
| 54 |
],
|
| 55 |
outputs='text',
|
| 56 |
title='Stroke Probability Predictor',
|
| 57 |
description='Predicts the probability of having a stroke based on input features.'
|
| 58 |
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch()
|