Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
app
Browse files
app.py
CHANGED
@@ -1,41 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
|
5 |
# Load the trained model
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Make prediction
|
20 |
-
prediction = model.predict(
|
21 |
-
|
|
|
22 |
except Exception as e:
|
23 |
-
return f"Error
|
24 |
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
input_sliders = [gr.Slider(0, 100, label=feature) for feature in features]
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
fn=
|
34 |
-
inputs=
|
35 |
-
outputs=
|
36 |
-
title="Heart
|
37 |
-
description="
|
38 |
)
|
39 |
|
40 |
# Launch the app
|
41 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
|
5 |
# Load the trained model
|
6 |
+
model = joblib.load("trained_model.pkl")
|
7 |
|
8 |
+
# Define the prediction function
|
9 |
+
def predict_heart_disease(
|
10 |
+
PhysicalHealthDays: float,
|
11 |
+
MentalHealthDays: float,
|
12 |
+
SleepHours: float,
|
13 |
+
BMI: float,
|
14 |
+
PhysicalActivities: str,
|
15 |
+
AlcoholDrinkers: str,
|
16 |
+
HIVTesting: str,
|
17 |
+
RemovedTeeth: str,
|
18 |
+
HighRiskLastYear: str,
|
19 |
+
CovidPos: str,
|
20 |
+
):
|
21 |
try:
|
22 |
+
# Encode categorical inputs as integers
|
23 |
+
physical_activities = 1 if PhysicalActivities == "Yes" else 0
|
24 |
+
alcohol_drinkers = 1 if AlcoholDrinkers == "Yes" else 0
|
25 |
+
hiv_testing = 1 if HIVTesting == "Yes" else 0
|
26 |
+
removed_teeth = 1 if RemovedTeeth == "Yes" else 0
|
27 |
+
high_risk_last_year = 1 if HighRiskLastYear == "Yes" else 0
|
28 |
+
covid_pos = 1 if CovidPos == "Yes" else 0
|
29 |
+
|
30 |
+
# Prepare features as a numpy array
|
31 |
+
features = np.array([[
|
32 |
+
PhysicalHealthDays,
|
33 |
+
MentalHealthDays,
|
34 |
+
SleepHours,
|
35 |
+
BMI,
|
36 |
+
physical_activities,
|
37 |
+
alcohol_drinkers,
|
38 |
+
hiv_testing,
|
39 |
+
removed_teeth,
|
40 |
+
high_risk_last_year,
|
41 |
+
covid_pos,
|
42 |
+
]])
|
43 |
+
|
44 |
# Make prediction
|
45 |
+
prediction = model.predict(features)
|
46 |
+
result = "Yes" if prediction[0] == 1 else "No"
|
47 |
+
return f"Heart Disease Prediction: {result}"
|
48 |
except Exception as e:
|
49 |
+
return f"Error in prediction: {str(e)}"
|
50 |
|
51 |
+
# Define the Gradio interface
|
52 |
+
inputs = [
|
53 |
+
gr.inputs.Number(label="Physical Health Days"),
|
54 |
+
gr.inputs.Number(label="Mental Health Days"),
|
55 |
+
gr.inputs.Number(label="Sleep Hours"),
|
56 |
+
gr.inputs.Number(label="BMI"),
|
57 |
+
gr.inputs.Radio(["Yes", "No"], label="Physical Activities"),
|
58 |
+
gr.inputs.Radio(["Yes", "No"], label="Alcohol Drinkers"),
|
59 |
+
gr.inputs.Radio(["Yes", "No"], label="HIV Testing"),
|
60 |
+
gr.inputs.Radio(["Yes", "No"], label="Removed Teeth"),
|
61 |
+
gr.inputs.Radio(["Yes", "No"], label="High Risk Last Year"),
|
62 |
+
gr.inputs.Radio(["Yes", "No"], label="Covid Positive"),
|
63 |
+
]
|
64 |
|
65 |
+
outputs = gr.outputs.Textbox(label="Heart Disease Prediction")
|
|
|
66 |
|
67 |
+
# Create the Gradio app
|
68 |
+
app = gr.Interface(
|
69 |
+
fn=predict_heart_disease,
|
70 |
+
inputs=inputs,
|
71 |
+
outputs=outputs,
|
72 |
+
title="Heart Disease Prediction App",
|
73 |
+
description="Please enter health metrics to predict the risk of heart disease."
|
74 |
)
|
75 |
|
76 |
# Launch the app
|
77 |
+
if __name__ == "__main__":
|
78 |
+
app.launch()
|