Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,44 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
with open('model.pkl', 'rb') as f:
|
|
@@ -31,7 +68,7 @@ def predict(gender, age, hypertension, ever_married, work_type, heart_disease, a
|
|
| 31 |
|
| 32 |
# Transform the input using the same transformer used during training
|
| 33 |
try:
|
| 34 |
-
X_transformed = transformer.
|
| 35 |
prediction = model.predict_proba(X_transformed)[0][1]
|
| 36 |
return f"The probability of stroke is {prediction:.2%}"
|
| 37 |
except Exception as e:
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
+
from sklearn.compose import ColumnTransformer
|
| 6 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 7 |
+
from sklearn.pipeline import Pipeline
|
| 8 |
+
from sklearn.impute import SimpleImputer
|
| 9 |
+
|
| 10 |
+
def transformer_fn():
|
| 11 |
+
"""
|
| 12 |
+
Returns a preprocessing pipeline that matches the training approach.
|
| 13 |
+
"""
|
| 14 |
+
# Define numeric and categorical features
|
| 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 |
+
# Create numeric pipeline
|
| 22 |
+
numeric_transformer = Pipeline(steps=[
|
| 23 |
+
('imputer', SimpleImputer(strategy='median')),
|
| 24 |
+
('scaler', StandardScaler())
|
| 25 |
+
])
|
| 26 |
+
|
| 27 |
+
# Create categorical pipeline
|
| 28 |
+
categorical_transformer = Pipeline(steps=[
|
| 29 |
+
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
|
| 30 |
+
('onehot', OneHotEncoder(drop='first', sparse=False))
|
| 31 |
+
])
|
| 32 |
+
|
| 33 |
+
# Combine preprocessing steps
|
| 34 |
+
preprocessor = ColumnTransformer(
|
| 35 |
+
transformers=[
|
| 36 |
+
('num', numeric_transformer, numeric_features),
|
| 37 |
+
('cat', categorical_transformer, categorical_features)
|
| 38 |
+
],
|
| 39 |
+
remainder='drop' # Drop any other columns
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return preprocessor
|
| 43 |
|
| 44 |
# Load the model
|
| 45 |
with open('model.pkl', 'rb') as f:
|
|
|
|
| 68 |
|
| 69 |
# Transform the input using the same transformer used during training
|
| 70 |
try:
|
| 71 |
+
X_transformed = transformer.fit_transform(input_df) # Using fit_transform since it's a new transformer instance
|
| 72 |
prediction = model.predict_proba(X_transformed)[0][1]
|
| 73 |
return f"The probability of stroke is {prediction:.2%}"
|
| 74 |
except Exception as e:
|