Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
app
Browse files
app.py
CHANGED
@@ -1,9 +1,17 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
# Load the trained model
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Define the prediction function
|
9 |
def predict_heart_disease(
|
@@ -19,60 +27,60 @@ def predict_heart_disease(
|
|
19 |
CovidPos: str,
|
20 |
):
|
21 |
try:
|
|
|
22 |
# Encode categorical inputs as integers
|
23 |
-
physical_activities = 1 if PhysicalActivities == "
|
24 |
-
alcohol_drinkers = 1 if AlcoholDrinkers == "
|
25 |
-
hiv_testing = 1 if HIVTesting == "
|
26 |
-
removed_teeth = 1 if RemovedTeeth == "
|
27 |
-
high_risk_last_year = 1 if HighRiskLastYear == "
|
28 |
-
covid_pos = 1 if CovidPos == "
|
29 |
|
30 |
-
#
|
31 |
-
features = np.array([
|
32 |
-
PhysicalHealthDays,
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
physical_activities,
|
37 |
-
alcohol_drinkers,
|
38 |
-
hiv_testing,
|
39 |
-
removed_teeth,
|
40 |
-
high_risk_last_year,
|
41 |
-
covid_pos,
|
42 |
-
]])
|
43 |
|
44 |
-
#
|
45 |
prediction = model.predict(features)
|
46 |
-
|
47 |
-
return f"Heart Disease Prediction: {result}"
|
48 |
except Exception as e:
|
49 |
-
return f"Error
|
50 |
|
51 |
# Define the Gradio interface
|
52 |
-
|
53 |
-
gr.
|
54 |
-
gr.
|
55 |
-
|
56 |
-
gr.
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
gr.
|
63 |
-
]
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
|
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
# Launch the app
|
77 |
-
|
78 |
-
app.launch()
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import joblib
|
4 |
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
|
7 |
# Load the trained model
|
8 |
+
model_path = "trained_model.pkl"
|
9 |
+
|
10 |
+
def load_model(path):
|
11 |
+
try:
|
12 |
+
return joblib.load(path)
|
13 |
+
except Exception as e:
|
14 |
+
raise ValueError(f"Error loading model: {e}")
|
15 |
|
16 |
# Define the prediction function
|
17 |
def predict_heart_disease(
|
|
|
27 |
CovidPos: str,
|
28 |
):
|
29 |
try:
|
30 |
+
model = load_model(model_path)
|
31 |
# Encode categorical inputs as integers
|
32 |
+
physical_activities = 1 if PhysicalActivities.lower() == "yes" else 0
|
33 |
+
alcohol_drinkers = 1 if AlcoholDrinkers.lower() == "yes" else 0
|
34 |
+
hiv_testing = 1 if HIVTesting.lower() == "yes" else 0
|
35 |
+
removed_teeth = 1 if RemovedTeeth.lower() == "yes" else 0
|
36 |
+
high_risk_last_year = 1 if HighRiskLastYear.lower() == "yes" else 0
|
37 |
+
covid_pos = 1 if CovidPos.lower() == "yes" else 0
|
38 |
|
39 |
+
# Combine inputs into a numpy array for prediction
|
40 |
+
features = np.array([
|
41 |
+
PhysicalHealthDays, MentalHealthDays, SleepHours, BMI,
|
42 |
+
physical_activities, alcohol_drinkers, hiv_testing,
|
43 |
+
removed_teeth, high_risk_last_year, covid_pos
|
44 |
+
]).reshape(1, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Predict with the model
|
47 |
prediction = model.predict(features)
|
48 |
+
return "Heart Disease Risk" if prediction[0] == 1 else "No Risk"
|
|
|
49 |
except Exception as e:
|
50 |
+
return f"Error during prediction: {e}"
|
51 |
|
52 |
# Define the Gradio interface
|
53 |
+
with gr.Blocks() as app:
|
54 |
+
gr.Markdown("# Heart Disease Prediction App")
|
55 |
+
gr.Markdown("### Provide input values and receive a prediction.")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
PhysicalHealthDays = gr.Slider(0, 30, label="Physical Health Days")
|
59 |
+
MentalHealthDays = gr.Slider(0, 30, label="Mental Health Days")
|
60 |
+
SleepHours = gr.Slider(0, 24, label="Average Sleep Hours")
|
61 |
+
BMI = gr.Slider(10, 50, label="Body Mass Index (BMI)")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
PhysicalActivities = gr.Radio(["Yes", "No"], label="Engaged in Physical Activities?")
|
65 |
+
AlcoholDrinkers = gr.Radio(["Yes", "No"], label="Consumes Alcohol?")
|
66 |
+
HIVTesting = gr.Radio(["Yes", "No"], label="Tested for HIV?")
|
67 |
+
RemovedTeeth = gr.Radio(["Yes", "No"], label="Has Removed Teeth?")
|
68 |
+
HighRiskLastYear = gr.Radio(["Yes", "No"], label="High Risk Last Year?")
|
69 |
+
CovidPos = gr.Radio(["Yes", "No"], label="Tested Positive for COVID-19?")
|
70 |
|
71 |
+
predict_button = gr.Button("Predict")
|
72 |
+
output = gr.Textbox(label="Prediction Result")
|
73 |
|
74 |
+
# Connect prediction logic
|
75 |
+
predict_button.click(
|
76 |
+
fn=predict_heart_disease,
|
77 |
+
inputs=[
|
78 |
+
PhysicalHealthDays, MentalHealthDays, SleepHours, BMI,
|
79 |
+
PhysicalActivities, AlcoholDrinkers, HIVTesting,
|
80 |
+
RemovedTeeth, HighRiskLastYear, CovidPos
|
81 |
+
],
|
82 |
+
outputs=output,
|
83 |
+
)
|
84 |
|
85 |
# Launch the app
|
86 |
+
app.launch()
|
|