Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
refactored all
Browse files- app.py +38 -57
- features_used_in_model.csv +21 -0
app.py
CHANGED
|
@@ -1,70 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import joblib
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load the trained model
|
| 6 |
-
|
| 7 |
-
rf_model = joblib.load(model_path)
|
| 8 |
-
|
| 9 |
-
# Define feature ranges and labels based on data
|
| 10 |
-
numerical_features = ['BMI', 'WeightInKilograms', 'HeightInMeters', 'PhysicalHealthDays', 'SleepHours']
|
| 11 |
-
categorical_features = [
|
| 12 |
-
'HadAngina_Yes', 'HadHeartAttack_Yes', 'ChestScan_Yes',
|
| 13 |
-
'HadStroke_Yes', 'DifficultyWalking_Yes', 'HadDiabetes_Yes',
|
| 14 |
-
'PneumoVaxEver_Yes', 'HadArthritis_Yes'
|
| 15 |
-
]
|
| 16 |
-
|
| 17 |
-
# Define sliders for numerical features
|
| 18 |
-
sliders = {
|
| 19 |
-
"BMI": (0, 50, 1),
|
| 20 |
-
"WeightInKilograms": (30, 200, 1),
|
| 21 |
-
"HeightInMeters": (1.0, 2.5, 0.01),
|
| 22 |
-
"PhysicalHealthDays": (0, 30, 1),
|
| 23 |
-
"SleepHours": (0, 24, 1)
|
| 24 |
-
}
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
# Prediction function
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
outputs = [
|
| 56 |
-
gr.Textbox(label="Prediction"),
|
| 57 |
-
gr.JSON(label="Input Values (Debugging)")
|
| 58 |
-
]
|
| 59 |
|
| 60 |
interface = gr.Interface(
|
| 61 |
-
fn=
|
| 62 |
inputs=inputs,
|
| 63 |
-
outputs=
|
| 64 |
-
title="
|
| 65 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Launch the
|
| 69 |
if __name__ == "__main__":
|
| 70 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# Load the pre-trained model
|
| 7 |
+
model = joblib.load("tuned_model.pkl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load the features used during training
|
| 10 |
+
features = pd.read_csv("features_used_in_model.csv")["Feature"].tolist()
|
| 11 |
|
| 12 |
# Prediction function
|
| 13 |
+
def predict_heart_failure(input_data):
|
| 14 |
+
try:
|
| 15 |
+
# Convert input into a DataFrame
|
| 16 |
+
input_df = pd.DataFrame([input_data], columns=features)
|
| 17 |
+
|
| 18 |
+
# Predict probability for heart failure (class 1)
|
| 19 |
+
probability = model.predict_proba(input_df)[:, 1][0]
|
| 20 |
+
|
| 21 |
+
# Predict class (0 or 1)
|
| 22 |
+
prediction = "At Risk of Heart Failure" if probability >= 0.3 else "No Risk Detected"
|
| 23 |
+
|
| 24 |
+
return {
|
| 25 |
+
"Prediction": prediction,
|
| 26 |
+
"Risk Probability": round(probability, 4)
|
| 27 |
+
}
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return {"error": str(e)}
|
| 30 |
+
|
| 31 |
+
# Gradio Interface
|
| 32 |
+
inputs = []
|
| 33 |
+
for feature in features:
|
| 34 |
+
inputs.append(gr.inputs.Textbox(label=feature, placeholder=f"Enter value for {feature}"))
|
| 35 |
+
|
| 36 |
+
output = gr.outputs.JSON(label="Heart Failure Prediction")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
interface = gr.Interface(
|
| 39 |
+
fn=predict_heart_failure,
|
| 40 |
inputs=inputs,
|
| 41 |
+
outputs=output,
|
| 42 |
+
title="Heart Failure Prediction Model",
|
| 43 |
+
description=(
|
| 44 |
+
"Predicts the likelihood of heart failure based on health features. "
|
| 45 |
+
"Enter the values for the features below and receive the prediction."
|
| 46 |
+
)
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Launch the interface for local testing or Hugging Face Spaces deployment
|
| 50 |
if __name__ == "__main__":
|
| 51 |
interface.launch()
|
features_used_in_model.csv
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Feature
|
| 2 |
+
State
|
| 3 |
+
Sex
|
| 4 |
+
GeneralHealth
|
| 5 |
+
PhysicalHealthDays
|
| 6 |
+
MentalHealthDays
|
| 7 |
+
LastCheckupTime
|
| 8 |
+
PhysicalActivities
|
| 9 |
+
SleepHours
|
| 10 |
+
HadStroke
|
| 11 |
+
HadArthritis
|
| 12 |
+
HadDiabetes
|
| 13 |
+
SmokerStatus
|
| 14 |
+
ECigaretteUsage
|
| 15 |
+
RaceEthnicityCategory
|
| 16 |
+
AgeCategory
|
| 17 |
+
HeightInMeters
|
| 18 |
+
WeightInKilograms
|
| 19 |
+
BMI
|
| 20 |
+
AlcoholDrinkers
|
| 21 |
+
HighRiskLastYear
|