Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
updated app and requirements
Browse files- app.py +51 -37
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,48 +1,62 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import joblib
|
3 |
-
import json
|
4 |
-
import numpy as np
|
5 |
|
6 |
# Load the trained model
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
Predict heart failure likelihood based on input features.
|
17 |
-
|
18 |
-
Args:
|
19 |
-
*args: Input values for each feature.
|
20 |
-
|
21 |
-
Returns:
|
22 |
-
str: Prediction result.
|
23 |
-
str: Prediction probability as a percentage.
|
24 |
-
"""
|
25 |
-
input_data = np.array(args).reshape(1, -1)
|
26 |
-
prediction = model.predict(input_data)
|
27 |
-
probability = model.predict_proba(input_data)
|
28 |
-
result = "Likely" if prediction[0] == 1 else "Unlikely"
|
29 |
-
return result, f"{probability[0][1] * 100:.2f}%"
|
30 |
-
|
31 |
-
# Create Gradio interface
|
32 |
-
inputs = [gr.inputs.Number(label=feature) for feature in feature_names]
|
33 |
-
outputs = [
|
34 |
-
gr.outputs.Textbox(label="Prediction"),
|
35 |
-
gr.outputs.Textbox(label="Probability (%)")
|
36 |
]
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
inputs=inputs,
|
41 |
-
outputs=
|
42 |
-
title="
|
43 |
-
description="
|
44 |
)
|
45 |
|
46 |
# Launch the app
|
47 |
if __name__ == "__main__":
|
48 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
import joblib
|
|
|
|
|
4 |
|
5 |
# Load the trained model
|
6 |
+
model_path = "trained_model.pkl"
|
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 |
+
# Define radio buttons for categorical features
|
27 |
+
radio_options = ['Yes', 'No']
|
28 |
+
|
29 |
+
# Prediction function
|
30 |
+
def predict_outcome(*inputs):
|
31 |
+
input_data = dict(zip(numerical_features + categorical_features, inputs))
|
32 |
+
|
33 |
+
# Convert categorical inputs to numerical
|
34 |
+
for feature in categorical_features:
|
35 |
+
input_data[feature] = 1 if input_data[feature] == "Yes" else 0
|
36 |
+
|
37 |
+
# Create input DataFrame
|
38 |
+
input_df = pd.DataFrame([input_data])
|
39 |
+
|
40 |
+
# Predict using the model
|
41 |
+
prediction = rf_model.predict(input_df)[0]
|
42 |
+
return "High Risk" if prediction == 1 else "Low Risk"
|
43 |
+
|
44 |
+
# Build Gradio interface
|
45 |
+
inputs = [
|
46 |
+
gr.Slider(sliders[feature][0], sliders[feature][1], sliders[feature][2], label=feature)
|
47 |
+
for feature in numerical_features
|
48 |
+
] + [
|
49 |
+
gr.Radio(radio_options, label=feature) for feature in categorical_features
|
50 |
+
]
|
51 |
+
|
52 |
+
interface = gr.Interface(
|
53 |
+
fn=predict_outcome,
|
54 |
inputs=inputs,
|
55 |
+
outputs="text",
|
56 |
+
title="Health Risk Prediction",
|
57 |
+
description="Predicts health risks based on input parameters using the trained model."
|
58 |
)
|
59 |
|
60 |
# Launch the app
|
61 |
if __name__ == "__main__":
|
62 |
+
interface.launch()
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ gradio
|
|
2 |
numpy
|
3 |
joblib
|
4 |
scikit-learn
|
|
|
|
2 |
numpy
|
3 |
joblib
|
4 |
scikit-learn
|
5 |
+
pandas
|