Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,74 +2,66 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
-
from sklearn.
|
| 6 |
-
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 7 |
-
from sklearn.pipeline import Pipeline
|
| 8 |
-
from sklearn.impute import SimpleImputer
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
numeric_features = ['age', 'avg_glucose_level', 'bmi']
|
| 16 |
-
categorical_features = [
|
| 17 |
-
'gender', 'hypertension', 'heart_disease', 'ever_married',
|
| 18 |
-
'work_type', 'Residence_type', 'smoking_status'
|
| 19 |
-
]
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
('scaler', StandardScaler())
|
| 25 |
-
])
|
| 26 |
|
| 27 |
-
# Create categorical
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
])
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Get the transformer
|
| 49 |
-
transformer = transformer_fn()
|
| 50 |
|
| 51 |
-
def predict(gender, age, hypertension, ever_married, work_type, heart_disease,
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
input_data = {
|
| 54 |
-
'gender':
|
| 55 |
-
'age':
|
| 56 |
-
'hypertension':
|
| 57 |
-
'heart_disease':
|
| 58 |
-
'ever_married':
|
| 59 |
-
'work_type':
|
| 60 |
-
'Residence_type':
|
| 61 |
-
'avg_glucose_level':
|
| 62 |
-
'bmi':
|
| 63 |
-
'smoking_status':
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
try:
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
return f"The probability of stroke is {prediction:.2%}"
|
| 74 |
except Exception as e:
|
| 75 |
return f"Error making prediction: {str(e)}"
|
|
|
|
| 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)}"
|