Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,40 @@
|
|
1 |
|
2 |
-
"""
|
3 |
-
Webapp Front End
|
4 |
-
"""
|
5 |
-
|
6 |
import gradio as gr
|
7 |
import joblib
|
8 |
import pandas as pd
|
9 |
|
10 |
-
|
|
|
11 |
|
|
|
12 |
try:
|
13 |
-
|
14 |
-
except FileNotFoundError
|
15 |
-
raise FileNotFoundError(
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Define the prediction function
|
20 |
-
def predict_with_model(State: float, Sex: float, GeneralHealth: float, PhysicalHealthDays: float, MentalHealthDays: float, LastCheckupTime: float, PhysicalActivities: float, SleepHours: float, HadStroke: float, HadArthritis: float, HadDiabetes: float, SmokerStatus: float, ECigaretteUsage: float, RaceEthnicityCategory: float, AgeCategory: float, HeightInMeters: float, WeightInKilograms: float, BMI: float, AlcoholDrinkers: float, HighRiskLastYear: float):
|
21 |
try:
|
22 |
-
# Prepare input as a DataFrame
|
23 |
input_data = pd.DataFrame([[State, Sex, GeneralHealth, PhysicalHealthDays, MentalHealthDays, LastCheckupTime, PhysicalActivities, SleepHours, HadStroke, HadArthritis, HadDiabetes, SmokerStatus, ECigaretteUsage, RaceEthnicityCategory, AgeCategory, HeightInMeters, WeightInKilograms, BMI, AlcoholDrinkers, HighRiskLastYear]], columns=['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear'])
|
24 |
-
prediction =
|
25 |
return "Heart Disease Risk" if prediction[0] == 1 else "No Risk"
|
26 |
except Exception as e:
|
27 |
return f"Error during prediction: {e}"
|
28 |
|
29 |
# Define the Gradio interface
|
30 |
with gr.Blocks() as app:
|
31 |
-
gr.Markdown("#
|
32 |
-
gr.Markdown("###
|
33 |
|
34 |
input_components = []
|
35 |
for feature in ['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear']:
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
predict_button = gr.Button("Predict")
|
39 |
output = gr.Textbox(label="Prediction Result")
|
|
|
1 |
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
import joblib
|
4 |
import pandas as pd
|
5 |
|
6 |
+
# Path to the trained model
|
7 |
+
MODEL_PATH = "tuned_model.pkl"
|
8 |
|
9 |
+
# Load the model
|
10 |
try:
|
11 |
+
model = joblib.load(MODEL_PATH)
|
12 |
+
except FileNotFoundError:
|
13 |
+
raise FileNotFoundError(f"Model file not found at {MODEL_PATH}.")
|
14 |
+
|
15 |
+
# Define prediction function
|
16 |
+
def predict_with_model(State: str, Sex: str, GeneralHealth: str, PhysicalHealthDays: str, MentalHealthDays: str, LastCheckupTime: str, PhysicalActivities: str, SleepHours: str, HadStroke: str, HadArthritis: str, HadDiabetes: str, SmokerStatus: str, ECigaretteUsage: str, RaceEthnicityCategory: str, AgeCategory: str, HeightInMeters: str, WeightInKilograms: str, BMI: str, AlcoholDrinkers: str, HighRiskLastYear: str):
|
|
|
|
|
17 |
try:
|
18 |
+
# Prepare input data as a DataFrame
|
19 |
input_data = pd.DataFrame([[State, Sex, GeneralHealth, PhysicalHealthDays, MentalHealthDays, LastCheckupTime, PhysicalActivities, SleepHours, HadStroke, HadArthritis, HadDiabetes, SmokerStatus, ECigaretteUsage, RaceEthnicityCategory, AgeCategory, HeightInMeters, WeightInKilograms, BMI, AlcoholDrinkers, HighRiskLastYear]], columns=['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear'])
|
20 |
+
prediction = model.predict(input_data)
|
21 |
return "Heart Disease Risk" if prediction[0] == 1 else "No Risk"
|
22 |
except Exception as e:
|
23 |
return f"Error during prediction: {e}"
|
24 |
|
25 |
# Define the Gradio interface
|
26 |
with gr.Blocks() as app:
|
27 |
+
gr.Markdown("# Health Risk Prediction App")
|
28 |
+
gr.Markdown("### Input the feature values below to predict health risks.")
|
29 |
|
30 |
input_components = []
|
31 |
for feature in ['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear']:
|
32 |
+
if "Days" in feature or "Hours" in feature or "BMI" in feature:
|
33 |
+
input_components.append(gr.Slider(0, 100, step=1, label=feature))
|
34 |
+
elif "Sex" in feature:
|
35 |
+
input_components.append(gr.Radio(["Male", "Female"], label=feature))
|
36 |
+
else:
|
37 |
+
input_components.append(gr.Textbox(label=feature))
|
38 |
|
39 |
predict_button = gr.Button("Predict")
|
40 |
output = gr.Textbox(label="Prediction Result")
|