hackerbyhobby commited on
Commit
98a0238
·
unverified ·
1 Parent(s): c48f883
Files changed (1) hide show
  1. app.py +65 -28
app.py CHANGED
@@ -1,41 +1,78 @@
1
  import gradio as gr
2
- import pickle
3
- import pandas as pd
4
 
5
  # Load the trained model
6
- model_path = "tuned_model.pkl"
7
 
8
- def load_model():
9
- """Load the model from the pickle file."""
10
- with open(model_path, "rb") as file:
11
- return pickle.load(file)
12
-
13
- # Prediction function
14
- def predict_with_model(*inputs):
 
 
 
 
 
 
15
  try:
16
- model = load_model() # Load the model dynamically
17
- # Create a DataFrame for prediction
18
- input_data = pd.DataFrame([inputs], columns=features)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Make prediction
20
- prediction = model.predict(input_data)
21
- return f"Prediction: {'Risk of Heart Failure' if prediction[0] == 1 else 'No Risk'}"
 
22
  except Exception as e:
23
- return f"Error during prediction: {str(e)}"
24
 
25
- # Features derived from the CSV file
26
- features = ["Feature1", "Feature2", "Feature3"] # Replace with actual feature names
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Create input sliders
29
- input_sliders = [gr.Slider(0, 100, label=feature) for feature in features]
30
 
31
- # Define Gradio interface
32
- iface = gr.Interface(
33
- fn=predict_with_model,
34
- inputs=input_sliders,
35
- outputs="text",
36
- title="Heart Failure Prediction App",
37
- description="Adjust the sliders to simulate feature values and predict heart failure risk.",
38
  )
39
 
40
  # Launch the app
41
- iface.launch()
 
 
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(
10
+ PhysicalHealthDays: float,
11
+ MentalHealthDays: float,
12
+ SleepHours: float,
13
+ BMI: float,
14
+ PhysicalActivities: str,
15
+ AlcoholDrinkers: str,
16
+ HIVTesting: str,
17
+ RemovedTeeth: str,
18
+ HighRiskLastYear: str,
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()