Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,30 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Mapping for categorical variables
|
| 14 |
gender_mapping = {'Male': 1, 'Female': 0}
|
| 15 |
hypertension_mapping = {'Yes': 1, 'No': 0}
|
|
@@ -28,47 +44,41 @@ def predict(gender, age, hypertension, ever_married, work_type, heart_disease, a
|
|
| 28 |
smoking_status = smoking_status_mapping[smoking_status]
|
| 29 |
Residence_type = Residence_type_mapping[Residence_type]
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Convert
|
| 35 |
input_df = pd.DataFrame([inputs], columns=input_labels)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# Predict the stroke probability
|
| 38 |
-
prediction = model.predict_proba(input_df)[0][1]
|
| 39 |
-
|
| 40 |
-
# Return the prediction
|
| 41 |
-
result = "The probability of stroke is {:.2f}%".format(prediction * 100) # to give a percentage
|
| 42 |
-
return result
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
input_labels = [
|
| 50 |
-
'gender', 'age', 'hypertension', 'ever_married', 'work_type',
|
| 51 |
-
'heart_disease', 'avg_glucose_level', 'bmi', 'smoking_status', 'Residence_type'
|
| 52 |
-
]
|
| 53 |
# Create the Gradio interface
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=predict,
|
| 56 |
inputs=[
|
| 57 |
-
gr.
|
| 58 |
-
gr.
|
| 59 |
-
gr.
|
| 60 |
-
gr.
|
| 61 |
-
gr.
|
| 62 |
-
gr.
|
| 63 |
-
gr.
|
| 64 |
-
gr.
|
| 65 |
-
gr.
|
| 66 |
-
gr.
|
| 67 |
],
|
| 68 |
outputs='text',
|
| 69 |
title='Stroke Probability Predictor',
|
| 70 |
description='Predicts the probability of having a stroke based on input features.'
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
iface.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import pickle
|
| 5 |
+
import sklearn
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
print(f"Prediction environment scikit-learn version: {sklearn.__version__}")
|
| 9 |
+
|
| 10 |
+
def decode_file(file_path):
|
| 11 |
+
with open(file_path, 'rb') as file:
|
| 12 |
+
obj = pickle.load(file)
|
| 13 |
+
return obj
|
| 14 |
+
|
| 15 |
+
# Load the model once when starting the app
|
| 16 |
+
try:
|
| 17 |
+
model = decode_file('model.pkl')
|
| 18 |
+
print("Model loaded successfully")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Error loading model: {e}")
|
| 21 |
+
model = None
|
| 22 |
+
|
| 23 |
+
def predict(gender, age, hypertension, ever_married, work_type, heart_disease,
|
| 24 |
+
avg_glucose_level, bmi, smoking_status, Residence_type):
|
| 25 |
+
"""Make prediction using the loaded model"""
|
| 26 |
+
if model is None:
|
| 27 |
+
return "Error: Model not loaded"
|
| 28 |
+
|
| 29 |
# Mapping for categorical variables
|
| 30 |
gender_mapping = {'Male': 1, 'Female': 0}
|
| 31 |
hypertension_mapping = {'Yes': 1, 'No': 0}
|
|
|
|
| 44 |
smoking_status = smoking_status_mapping[smoking_status]
|
| 45 |
Residence_type = Residence_type_mapping[Residence_type]
|
| 46 |
|
| 47 |
+
# Create input data
|
| 48 |
+
inputs = [gender, age, hypertension, ever_married, work_type, heart_disease,
|
| 49 |
+
avg_glucose_level, bmi, smoking_status, Residence_type]
|
| 50 |
+
input_labels = ['gender', 'age', 'hypertension', 'ever_married', 'work_type',
|
| 51 |
+
'heart_disease', 'avg_glucose_level', 'bmi', 'smoking_status', 'Residence_type']
|
| 52 |
|
| 53 |
+
# Convert to DataFrame
|
| 54 |
input_df = pd.DataFrame([inputs], columns=input_labels)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
# Make prediction
|
| 58 |
+
prediction = model.predict_proba(input_df)[0][1]
|
| 59 |
+
return f"The probability of stroke is {prediction:.2%}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Error making prediction: {str(e)}"
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Create the Gradio interface
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=predict,
|
| 66 |
inputs=[
|
| 67 |
+
gr.Radio(choices=['Female', 'Male'], label="Gender"),
|
| 68 |
+
gr.Slider(minimum=0, maximum=100, label="Age"),
|
| 69 |
+
gr.Radio(choices=['Yes', 'No'], label="Hypertension"),
|
| 70 |
+
gr.Radio(choices=['Yes', 'No'], label="Ever Married"),
|
| 71 |
+
gr.Radio(choices=['Private', 'Self-employed', 'Govt_job', 'children', 'Never_worked'], label="Work Type"),
|
| 72 |
+
gr.Radio(choices=['Yes', 'No'], label="Heart Disease"),
|
| 73 |
+
gr.Number(label="Average Glucose Level"),
|
| 74 |
+
gr.Slider(minimum=10, maximum=50, label="BMI"),
|
| 75 |
+
gr.Radio(choices=['formerly smoked', 'never smoked', 'smokes', 'Unknown'], label="Smoking Status"),
|
| 76 |
+
gr.Radio(choices=['Urban', 'Rural'], label="Residence Type")
|
| 77 |
],
|
| 78 |
outputs='text',
|
| 79 |
title='Stroke Probability Predictor',
|
| 80 |
description='Predicts the probability of having a stroke based on input features.'
|
| 81 |
)
|
| 82 |
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|