Spaces:
Runtime error
Runtime error
Create app.py
#1
by Sammman - opened
app.py
CHANGED
|
@@ -1,128 +0,0 @@
|
|
| 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 |
-
import sklearn
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
print(f"Prediction environment scikit-learn version: {sklearn.__version__}")
|
| 10 |
-
|
| 11 |
-
def decode_file(file_path):
|
| 12 |
-
with open(file_path, 'rb') as file:
|
| 13 |
-
obj = pickle.load(file)
|
| 14 |
-
return obj
|
| 15 |
-
|
| 16 |
-
# Load the model without try/except
|
| 17 |
-
model = decode_file('./model.pkl')
|
| 18 |
-
|
| 19 |
-
def preprocess_input(data_dict):
|
| 20 |
-
"""Preprocess input data to match the training format"""
|
| 21 |
-
# First create DataFrame with original columns
|
| 22 |
-
df = pd.DataFrame([data_dict])
|
| 23 |
-
|
| 24 |
-
# Store original features before transformation
|
| 25 |
-
original_df = df.copy()
|
| 26 |
-
|
| 27 |
-
# Numeric features
|
| 28 |
-
numeric_features = ['age', 'avg_glucose_level', 'bmi']
|
| 29 |
-
|
| 30 |
-
# Scale numeric features and add prefix
|
| 31 |
-
scaler = StandardScaler()
|
| 32 |
-
scaled_features = scaler.fit_transform(df[numeric_features])
|
| 33 |
-
for i, feat in enumerate(numeric_features):
|
| 34 |
-
df[f'num__{feat}'] = scaled_features[:, i]
|
| 35 |
-
|
| 36 |
-
# Create categorical features
|
| 37 |
-
# Gender
|
| 38 |
-
df['cat__gender_Male'] = (df['gender'] == 'Male').astype(float)
|
| 39 |
-
df['cat__gender_Other'] = 0.0
|
| 40 |
-
|
| 41 |
-
# Hypertension
|
| 42 |
-
df['cat__hypertension_1'] = df['hypertension'].astype(float)
|
| 43 |
-
|
| 44 |
-
# Heart Disease
|
| 45 |
-
df['cat__heart_disease_1'] = df['heart_disease'].astype(float)
|
| 46 |
-
|
| 47 |
-
# Ever Married
|
| 48 |
-
df['cat__ever_married_Yes'] = (df['ever_married'] == 'Yes').astype(float)
|
| 49 |
-
|
| 50 |
-
# Work Type
|
| 51 |
-
df['cat__work_type_Never_worked'] = (df['work_type'] == 'Never_worked').astype(float)
|
| 52 |
-
df['cat__work_type_Private'] = (df['work_type'] == 'Private').astype(float)
|
| 53 |
-
df['cat__work_type_Self-employed'] = (df['work_type'] == 'Self-employed').astype(float)
|
| 54 |
-
df['cat__work_type_children'] = (df['work_type'] == 'children').astype(float)
|
| 55 |
-
|
| 56 |
-
# Residence Type
|
| 57 |
-
df['cat__Residence_type_Urban'] = (df['Residence_type'] == 'Urban').astype(float)
|
| 58 |
-
|
| 59 |
-
# Smoking Status
|
| 60 |
-
df['cat__smoking_status_formerly smoked'] = (df['smoking_status'] == 'formerly smoked').astype(float)
|
| 61 |
-
df['cat__smoking_status_never smoked'] = (df['smoking_status'] == 'never smoked').astype(float)
|
| 62 |
-
df['cat__smoking_status_smokes'] = (df['smoking_status'] == 'smokes').astype(float)
|
| 63 |
-
|
| 64 |
-
# Select only the transformed columns in the correct order
|
| 65 |
-
feature_columns = [
|
| 66 |
-
'num__age', 'num__avg_glucose_level', 'num__bmi',
|
| 67 |
-
'cat__gender_Male', 'cat__gender_Other', 'cat__hypertension_1',
|
| 68 |
-
'cat__heart_disease_1', 'cat__ever_married_Yes',
|
| 69 |
-
'cat__work_type_Never_worked', 'cat__work_type_Private',
|
| 70 |
-
'cat__work_type_Self-employed', 'cat__work_type_children',
|
| 71 |
-
'cat__Residence_type_Urban', 'cat__smoking_status_formerly smoked',
|
| 72 |
-
'cat__smoking_status_never smoked', 'cat__smoking_status_smokes'
|
| 73 |
-
]
|
| 74 |
-
|
| 75 |
-
return df[feature_columns]
|
| 76 |
-
|
| 77 |
-
def predict(gender, age, hypertension, ever_married, work_type, heart_disease,
|
| 78 |
-
avg_glucose_level, bmi, smoking_status, Residence_type):
|
| 79 |
-
"""Make prediction using the loaded model"""
|
| 80 |
-
if model is None:
|
| 81 |
-
return "Error: Model not loaded"
|
| 82 |
-
|
| 83 |
-
# Create input dictionary
|
| 84 |
-
input_data = {
|
| 85 |
-
'gender': gender,
|
| 86 |
-
'age': float(age), # Ensure numeric
|
| 87 |
-
'hypertension': 1 if hypertension == 'Yes' else 0,
|
| 88 |
-
'heart_disease': 1 if heart_disease == 'Yes' else 0,
|
| 89 |
-
'ever_married': ever_married,
|
| 90 |
-
'work_type': work_type,
|
| 91 |
-
'Residence_type': Residence_type,
|
| 92 |
-
'avg_glucose_level': float(avg_glucose_level), # Ensure numeric
|
| 93 |
-
'bmi': float(bmi), # Ensure numeric
|
| 94 |
-
'smoking_status': smoking_status
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
# Preprocess the input
|
| 98 |
-
processed_input = preprocess_input(input_data)
|
| 99 |
-
|
| 100 |
-
# Use the loaded model
|
| 101 |
-
try:
|
| 102 |
-
prediction = model.predict_proba(processed_input)[0][1]
|
| 103 |
-
return f"The probability of stroke is {prediction:.2%}"
|
| 104 |
-
except Exception as e:
|
| 105 |
-
return f"Error making prediction: {str(e)}, model is not valid" # updated here
|
| 106 |
-
|
| 107 |
-
# Create the Gradio interface
|
| 108 |
-
iface = gr.Interface(
|
| 109 |
-
fn=predict,
|
| 110 |
-
inputs=[
|
| 111 |
-
gr.Radio(choices=['Female', 'Male'], label="Gender"),
|
| 112 |
-
gr.Slider(minimum=0, maximum=100, label="Age"),
|
| 113 |
-
gr.Radio(choices=['Yes', 'No'], label="Hypertension"),
|
| 114 |
-
gr.Radio(choices=['Yes', 'No'], label="Ever Married"),
|
| 115 |
-
gr.Radio(choices=['Private', 'Self-employed', 'Govt_job', 'children', 'Never_worked'], label="Work Type"),
|
| 116 |
-
gr.Radio(choices=['Yes', 'No'], label="Heart Disease"),
|
| 117 |
-
gr.Number(label="Average Glucose Level"),
|
| 118 |
-
gr.Slider(minimum=10, maximum=50, label="BMI"),
|
| 119 |
-
gr.Radio(choices=['formerly smoked', 'never smoked', 'smokes', 'Unknown'], label="Smoking Status"),
|
| 120 |
-
gr.Radio(choices=['Urban', 'Rural'], label="Residence Type")
|
| 121 |
-
],
|
| 122 |
-
outputs='text',
|
| 123 |
-
title='Stroke Probability Predictor',
|
| 124 |
-
description='Predicts the probability of having a stroke based on input features.'
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
if __name__ == "__main__":
|
| 128 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|