Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,90 @@
|
|
| 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 |
-
def decode_file(file_path):
|
| 9 |
-
with open(file_path, 'rb') as file:
|
| 10 |
-
obj = pickle.load(file)
|
| 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 |
-
# Mapping for categorical variables
|
| 18 |
-
gender_mapping = {'Male': 1, 'Female': 0}
|
| 19 |
-
hypertension_mapping = {'Yes': 1, 'No': 0}
|
| 20 |
-
ever_married_mapping = {'Yes': 1, 'No': 0}
|
| 21 |
-
work_type_mapping = {'Private': 2, 'Self-employed': 4, 'Govt_job': 3, 'children': 1, 'Never_worked': 0}
|
| 22 |
-
heart_disease_mapping = {'Yes': 1, 'No': 0}
|
| 23 |
-
smoking_status_mapping = {'formerly smoked': 3, 'smokes': 1, 'never smoked': 2, 'Unknown': 0}
|
| 24 |
-
Residence_type_mapping = {'Urban': 1, 'Rural': 0}
|
| 25 |
-
|
| 26 |
-
# Map categorical variables to their corresponding numerical values
|
| 27 |
-
gender = gender_mapping[gender]
|
| 28 |
-
hypertension = hypertension_mapping[hypertension]
|
| 29 |
-
ever_married = ever_married_mapping[ever_married]
|
| 30 |
-
work_type = work_type_mapping[work_type]
|
| 31 |
-
heart_disease = heart_disease_mapping[heart_disease]
|
| 32 |
-
smoking_status = smoking_status_mapping[smoking_status]
|
| 33 |
-
Residence_type = Residence_type_mapping[Residence_type]
|
| 34 |
-
|
| 35 |
-
inputs = [gender, age, hypertension, ever_married, work_type, heart_disease, avg_glucose_level, bmi, smoking_status, Residence_type]
|
| 36 |
-
input_labels = ['gender', 'age', 'hypertension', 'ever_married', 'work_type', 'heart_disease', 'avg_glucose_level', 'bmi', 'smoking_status', 'Residence_type']
|
| 37 |
-
|
| 38 |
-
# Convert the input into a pandas DataFrame
|
| 39 |
-
input_df = pd.DataFrame([inputs], columns=input_labels)
|
| 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 sklearn.preprocessing import StandardScaler
|
| 6 |
+
|
| 7 |
+
def preprocess_input(data_dict):
|
| 8 |
+
"""Preprocess input data to match the training format"""
|
| 9 |
+
df = pd.DataFrame([data_dict])
|
| 10 |
+
|
| 11 |
+
# Numeric features
|
| 12 |
+
numeric_features = ['age', 'avg_glucose_level', 'bmi']
|
| 13 |
+
|
| 14 |
+
# Scale numeric features
|
| 15 |
+
scaler = StandardScaler()
|
| 16 |
+
df[numeric_features] = scaler.fit_transform(df[numeric_features])
|
| 17 |
+
|
| 18 |
+
# Create dummy variables for categorical features
|
| 19 |
+
df = pd.get_dummies(df, columns=['gender', 'hypertension', 'heart_disease',
|
| 20 |
+
'ever_married', 'work_type', 'Residence_type',
|
| 21 |
+
'smoking_status'])
|
| 22 |
+
|
| 23 |
+
# Ensure all expected columns are present
|
| 24 |
+
expected_columns = [
|
| 25 |
+
'num__age', 'num__avg_glucose_level', 'num__bmi',
|
| 26 |
+
'cat__gender_Male', 'cat__gender_Other', 'cat__hypertension_1',
|
| 27 |
+
'cat__heart_disease_1', 'cat__ever_married_Yes',
|
| 28 |
+
'cat__work_type_Never_worked', 'cat__work_type_Private',
|
| 29 |
+
'cat__work_type_Self-employed', 'cat__work_type_children',
|
| 30 |
+
'cat__Residence_type_Urban', 'cat__smoking_status_formerly smoked',
|
| 31 |
+
'cat__smoking_status_never smoked', 'cat__smoking_status_smokes'
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
for col in expected_columns:
|
| 35 |
+
if col not in df.columns:
|
| 36 |
+
df[col] = 0
|
| 37 |
+
|
| 38 |
+
return df[expected_columns]
|
| 39 |
+
|
| 40 |
+
def predict(gender, age, hypertension, ever_married, work_type, heart_disease,
|
| 41 |
+
avg_glucose_level, bmi, smoking_status, Residence_type):
|
| 42 |
+
"""Make prediction using the loaded model"""
|
| 43 |
+
# Create input dictionary
|
| 44 |
+
input_data = {
|
| 45 |
+
'gender': gender,
|
| 46 |
+
'age': age,
|
| 47 |
+
'hypertension': 1 if hypertension == 'Yes' else 0,
|
| 48 |
+
'heart_disease': 1 if heart_disease == 'Yes' else 0,
|
| 49 |
+
'ever_married': ever_married,
|
| 50 |
+
'work_type': work_type,
|
| 51 |
+
'Residence_type': Residence_type,
|
| 52 |
+
'avg_glucose_level': avg_glucose_level,
|
| 53 |
+
'bmi': bmi,
|
| 54 |
+
'smoking_status': smoking_status
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# Preprocess the input
|
| 58 |
+
processed_input = preprocess_input(input_data)
|
| 59 |
+
|
| 60 |
+
# Load and use the model
|
| 61 |
+
try:
|
| 62 |
+
with open('model.pkl', 'rb') as file:
|
| 63 |
+
model = pickle.load(file)
|
| 64 |
+
prediction = model.predict_proba(processed_input)[0][1]
|
| 65 |
+
return f"The probability of stroke is {prediction:.2%}"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error making prediction: {str(e)}"
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Create the Gradio interface
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=predict,
|
| 72 |
inputs=[
|
| 73 |
+
gr.Radio(choices=['Female', 'Male'], label="Gender"),
|
| 74 |
+
gr.Slider(minimum=0, maximum=100, label="Age"),
|
| 75 |
+
gr.Radio(choices=['Yes', 'No'], label="Hypertension"),
|
| 76 |
+
gr.Radio(choices=['Yes', 'No'], label="Ever Married"),
|
| 77 |
+
gr.Radio(choices=['Private', 'Self-employed', 'Govt_job', 'children', 'Never_worked'], label="Work Type"),
|
| 78 |
+
gr.Radio(choices=['Yes', 'No'], label="Heart Disease"),
|
| 79 |
+
gr.Number(label="Average Glucose Level"),
|
| 80 |
+
gr.Slider(minimum=10, maximum=50, label="BMI"),
|
| 81 |
+
gr.Radio(choices=['formerly smoked', 'never smoked', 'smokes', 'Unknown'], label="Smoking Status"),
|
| 82 |
+
gr.Radio(choices=['Urban', 'Rural'], label="Residence Type")
|
| 83 |
],
|
| 84 |
outputs='text',
|
| 85 |
title='Stroke Probability Predictor',
|
| 86 |
description='Predicts the probability of having a stroke based on input features.'
|
| 87 |
)
|
| 88 |
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
iface.launch()
|