hackerbyhobby commited on
Commit
7d3a794
·
unverified ·
1 Parent(s): 98a0238
Files changed (1) hide show
  1. app.py +55 -47
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
- model = joblib.load("trained_model.pkl")
 
 
 
 
 
 
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 == "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()
 
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()