Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
revamed a lot
Browse files- .DS_Store +0 -0
- app.py +34 -71
- feature_names.json +0 -1
- selected_features.json +1 -0
- optimized_heart_failure_model.pkl → trained_model.pkl +2 -2
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
CHANGED
@@ -1,86 +1,49 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
-
import pandas as pd
|
4 |
import json
|
|
|
5 |
|
6 |
# Load the trained model
|
7 |
-
model = joblib.load(
|
8 |
|
9 |
-
# Load feature names
|
10 |
-
with open(
|
11 |
feature_names = json.load(f)
|
12 |
|
13 |
-
# Define
|
14 |
-
def predict_heart_failure(
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
pd.set_option('display.max_columns', None) # Display all columns
|
37 |
-
pd.set_option('display.width', None) # Disable line-wrapping for wide DataFrames
|
38 |
-
|
39 |
-
|
40 |
-
# Ensure all required features are present
|
41 |
-
for feature in feature_names:
|
42 |
-
if feature not in input_df.columns:
|
43 |
-
input_df[feature] = 0 # Fill missing features with default value (e.g., 0)
|
44 |
-
|
45 |
-
print("Inputs received in the app:")
|
46 |
-
print(input_df)
|
47 |
-
|
48 |
-
# Ensure no extra features are included
|
49 |
-
input_df = input_df[feature_names]
|
50 |
-
|
51 |
-
# Make a prediction
|
52 |
-
prediction = model.predict(input_df)[0]
|
53 |
-
print(f"Prediction: {prediction}")
|
54 |
-
return "High likelihood of heart failure" if prediction == 1 else "Low likelihood of heart failure"
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Define Gradio inputs
|
61 |
-
gradio_inputs = [
|
62 |
-
gr.Radio(
|
63 |
-
["18-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-79", "80 or older"],
|
64 |
-
label="Age Category"
|
65 |
-
),
|
66 |
-
gr.Radio(["Yes", "No"], label="Alcohol Drinkers"),
|
67 |
-
gr.Radio(["Yes", "No"], label="Chest Scan"),
|
68 |
-
gr.Radio(["Yes", "No"], label="COVID Positive"),
|
69 |
-
gr.Slider(0, 30, step=1, label="Physical Health Days"),
|
70 |
-
gr.Slider(0, 30, step=1, label="Mental Health Days"),
|
71 |
-
gr.Slider(0, 12, step=0.5, label="Sleep Hours"),
|
72 |
-
gr.Slider(10, 50, step=0.1, label="BMI")
|
73 |
]
|
74 |
|
75 |
-
|
76 |
-
interface = gr.Interface(
|
77 |
fn=predict_heart_failure,
|
78 |
-
inputs=
|
79 |
-
outputs=
|
80 |
-
title="Heart Failure
|
81 |
-
description="
|
82 |
)
|
83 |
|
84 |
# Launch the app
|
85 |
if __name__ == "__main__":
|
86 |
-
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import joblib
|
|
|
4 |
import json
|
5 |
+
import numpy as np
|
6 |
|
7 |
# Load the trained model
|
8 |
+
model = joblib.load('trained_model_selected.pkl')
|
9 |
|
10 |
+
# Load feature names
|
11 |
+
with open('selected_features.json', 'r') as f:
|
12 |
feature_names = json.load(f)
|
13 |
|
14 |
+
# Define prediction function
|
15 |
+
def predict_heart_failure(*args):
|
16 |
+
"""
|
17 |
+
Predict heart failure likelihood based on input features.
|
18 |
+
|
19 |
+
Args:
|
20 |
+
*args: Input values for each feature.
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
str: Prediction result.
|
24 |
+
str: Prediction probability as a percentage.
|
25 |
+
"""
|
26 |
+
input_data = np.array(args).reshape(1, -1)
|
27 |
+
prediction = model.predict(input_data)
|
28 |
+
probability = model.predict_proba(input_data)
|
29 |
+
result = "Likely" if prediction[0] == 1 else "Unlikely"
|
30 |
+
return result, f"{probability[0][1] * 100:.2f}%"
|
31 |
+
|
32 |
+
# Create Gradio interface
|
33 |
+
inputs = [gr.inputs.Number(label=feature) for feature in feature_names]
|
34 |
+
outputs = [
|
35 |
+
gr.outputs.Textbox(label="Prediction"),
|
36 |
+
gr.outputs.Textbox(label="Probability (%)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
]
|
38 |
|
39 |
+
app = gr.Interface(
|
|
|
40 |
fn=predict_heart_failure,
|
41 |
+
inputs=inputs,
|
42 |
+
outputs=outputs,
|
43 |
+
title="Heart Failure Prediction",
|
44 |
+
description="Enter the values for the features to predict the likelihood of heart failure."
|
45 |
)
|
46 |
|
47 |
# Launch the app
|
48 |
if __name__ == "__main__":
|
49 |
+
app.launch()
|
feature_names.json
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
["PhysicalHealthDays", "MentalHealthDays", "SleepHours", "HeightInMeters", "WeightInKilograms", "BMI", "State_Alaska", "State_Arizona", "State_Arkansas", "State_California", "State_Colorado", "State_Connecticut", "State_Delaware", "State_District of Columbia", "State_Florida", "State_Georgia", "State_Guam", "State_Hawaii", "State_Idaho", "State_Illinois", "State_Indiana", "State_Iowa", "State_Kansas", "State_Kentucky", "State_Louisiana", "State_Maine", "State_Maryland", "State_Massachusetts", "State_Michigan", "State_Minnesota", "State_Mississippi", "State_Missouri", "State_Montana", "State_Nebraska", "State_Nevada", "State_New Hampshire", "State_New Jersey", "State_New Mexico", "State_New York", "State_North Carolina", "State_North Dakota", "State_Ohio", "State_Oklahoma", "State_Oregon", "State_Pennsylvania", "State_Puerto Rico", "State_Rhode Island", "State_South Carolina", "State_South Dakota", "State_Tennessee", "State_Texas", "State_Utah", "State_Vermont", "State_Virgin Islands", "State_Virginia", "State_Washington", "State_West Virginia", "State_Wisconsin", "State_Wyoming", "Sex_Male", "GeneralHealth_Fair", "GeneralHealth_Good", "GeneralHealth_Poor", "GeneralHealth_Very good", "LastCheckupTime_Within past 2 years (1 year but less than 2 years ago)", "LastCheckupTime_Within past 5 years (2 years but less than 5 years ago)", "LastCheckupTime_Within past year (anytime less than 12 months ago)", "PhysicalActivities_Yes", "RemovedTeeth_6 or more, but not all", "RemovedTeeth_All", "RemovedTeeth_None of them", "HadStroke_Yes", "HadAsthma_Yes", "HadSkinCancer_Yes", "HadCOPD_Yes", "HadDepressiveDisorder_Yes", "HadKidneyDisease_Yes", "HadArthritis_Yes", "HadDiabetes_No, pre-diabetes or borderline diabetes", "HadDiabetes_Yes", "HadDiabetes_Yes, but only during pregnancy (female)", "DeafOrHardOfHearing_Yes", "BlindOrVisionDifficulty_Yes", "DifficultyConcentrating_Yes", "DifficultyWalking_Yes", "DifficultyDressingBathing_Yes", "DifficultyErrands_Yes", "SmokerStatus_Current smoker - now smokes some days", "SmokerStatus_Former smoker", "SmokerStatus_Never smoked", "ECigaretteUsage_Not at all (right now)", "ECigaretteUsage_Use them every day", "ECigaretteUsage_Use them some days", "ChestScan_Yes", "RaceEthnicityCategory_Hispanic", "RaceEthnicityCategory_Multiracial, Non-Hispanic", "RaceEthnicityCategory_Other race only, Non-Hispanic", "RaceEthnicityCategory_White only, Non-Hispanic", "AgeCategory_Age 25 to 29", "AgeCategory_Age 30 to 34", "AgeCategory_Age 35 to 39", "AgeCategory_Age 40 to 44", "AgeCategory_Age 45 to 49", "AgeCategory_Age 50 to 54", "AgeCategory_Age 55 to 59", "AgeCategory_Age 60 to 64", "AgeCategory_Age 65 to 69", "AgeCategory_Age 70 to 74", "AgeCategory_Age 75 to 79", "AgeCategory_Age 80 or older", "AlcoholDrinkers_Yes", "HIVTesting_Yes", "FluVaxLast12_Yes", "PneumoVaxEver_Yes", "TetanusLast10Tdap_Yes, received Tdap", "TetanusLast10Tdap_Yes, received tetanus shot but not sure what type", "TetanusLast10Tdap_Yes, received tetanus shot, but not Tdap", "HighRiskLastYear_Yes", "CovidPos_Tested positive using home test without a health professional", "CovidPos_Yes"]
|
|
|
|
selected_features.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
["HadAngina_Yes", "HadHeartAttack_Yes", "ChestScan_Yes", "BMI", "WeightInKilograms", "HadStroke_Yes", "DifficultyWalking_Yes", "HeightInMeters", "HadDiabetes_Yes", "PhysicalHealthDays", "SleepHours", "PneumoVaxEver_Yes", "HadArthritis_Yes"]
|
optimized_heart_failure_model.pkl → trained_model.pkl
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:671e17b4726b0253e73d9a3b6af013302cdd2c32086e9b67357d44f145af01cd
|
3 |
+
size 7804185
|