Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
app
Browse files- app.py +50 -11
- app.py.bak +0 -86
app.py
CHANGED
@@ -14,12 +14,37 @@ def load_model(path):
|
|
14 |
raise ValueError(f"Error loading model: {e}")
|
15 |
|
16 |
# Define the prediction function
|
17 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
model = load_model(model_path)
|
20 |
-
#
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return "Heart Disease Risk" if prediction[0] == 1 else "No Risk"
|
24 |
except Exception as e:
|
25 |
return f"Error during prediction: {e}"
|
@@ -27,19 +52,33 @@ def predict_with_model(State: float, Sex: float, GeneralHealth: float, PhysicalH
|
|
27 |
# Define the Gradio interface
|
28 |
with gr.Blocks() as app:
|
29 |
gr.Markdown("# Heart Disease Prediction App")
|
30 |
-
gr.Markdown("### Provide input values
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
predict_button = gr.Button("Predict")
|
37 |
output = gr.Textbox(label="Prediction Result")
|
38 |
|
39 |
# Connect prediction logic
|
40 |
predict_button.click(
|
41 |
-
fn=
|
42 |
-
inputs=
|
|
|
|
|
|
|
|
|
43 |
outputs=output,
|
44 |
)
|
45 |
|
|
|
14 |
raise ValueError(f"Error loading model: {e}")
|
15 |
|
16 |
# Define the prediction function
|
17 |
+
def predict_heart_disease(
|
18 |
+
PhysicalHealthDays: float,
|
19 |
+
MentalHealthDays: float,
|
20 |
+
SleepHours: float,
|
21 |
+
BMI: float,
|
22 |
+
PhysicalActivities: str,
|
23 |
+
AlcoholDrinkers: str,
|
24 |
+
HIVTesting: str,
|
25 |
+
RemovedTeeth: str,
|
26 |
+
HighRiskLastYear: str,
|
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}"
|
|
|
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 |
|
app.py.bak
DELETED
@@ -1,86 +0,0 @@
|
|
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(
|
18 |
-
PhysicalHealthDays: float,
|
19 |
-
MentalHealthDays: float,
|
20 |
-
SleepHours: float,
|
21 |
-
BMI: float,
|
22 |
-
PhysicalActivities: str,
|
23 |
-
AlcoholDrinkers: str,
|
24 |
-
HIVTesting: str,
|
25 |
-
RemovedTeeth: str,
|
26 |
-
HighRiskLastYear: str,
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|