hackerbyhobby commited on
Commit
6ed3230
·
unverified ·
1 Parent(s): d59529d
Files changed (1) hide show
  1. app.py +19 -18
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
- MODEL_PATH = "Random_Foresttest_model.pkl"
 
11
 
 
12
  try:
13
- rf_model = joblib.load(MODEL_PATH)
14
- except FileNotFoundError as e:
15
- raise FileNotFoundError(
16
- f"Model file not found at {MODEL_PATH}. Please check the path."
17
- ) from e
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 = rf_model.predict(input_data)
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("# Heart Disease Prediction App")
32
- gr.Markdown("### Provide input values for the features below and get a prediction.")
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
- input_components.append(gr.Slider(0, 100, step=1, label=feature))
 
 
 
 
 
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")