hackerbyhobby commited on
Commit
6669778
·
unverified ·
1 Parent(s): 5420a19

refactored all

Browse files
Files changed (2) hide show
  1. app.py +38 -57
  2. features_used_in_model.csv +21 -0
app.py CHANGED
@@ -1,70 +1,51 @@
1
  import gradio as gr
2
- import pandas as pd
3
  import joblib
 
 
4
 
5
- # Load the trained model
6
- model_path = "trained_model.pkl"
7
- rf_model = joblib.load(model_path)
8
-
9
- # Define feature ranges and labels based on data
10
- numerical_features = ['BMI', 'WeightInKilograms', 'HeightInMeters', 'PhysicalHealthDays', 'SleepHours']
11
- categorical_features = [
12
- 'HadAngina_Yes', 'HadHeartAttack_Yes', 'ChestScan_Yes',
13
- 'HadStroke_Yes', 'DifficultyWalking_Yes', 'HadDiabetes_Yes',
14
- 'PneumoVaxEver_Yes', 'HadArthritis_Yes'
15
- ]
16
-
17
- # Define sliders for numerical features
18
- sliders = {
19
- "BMI": (0, 50, 1),
20
- "WeightInKilograms": (30, 200, 1),
21
- "HeightInMeters": (1.0, 2.5, 0.01),
22
- "PhysicalHealthDays": (0, 30, 1),
23
- "SleepHours": (0, 24, 1)
24
- }
25
 
26
- # Define radio buttons for categorical features
27
- radio_options = ['Yes', 'No']
28
 
29
  # Prediction function
30
- def predict_outcome(*inputs):
31
- input_data = dict(zip(numerical_features + categorical_features, inputs))
32
-
33
- # Convert categorical inputs to numerical
34
- for feature in categorical_features:
35
- input_data[feature] = 1 if input_data[feature] == "Yes" else 0
36
-
37
- # Create input DataFrame
38
- input_df = pd.DataFrame([input_data])
39
-
40
- # Predict using the model
41
- prediction = rf_model.predict(input_df)[0]
42
- prediction_label = "High Risk" if prediction == 1 else "Low Risk"
43
-
44
- # Display input values for debugging
45
- return prediction_label, input_data
46
-
47
- # Build Gradio interface
48
- inputs = [
49
- gr.Slider(sliders[feature][0], sliders[feature][1], sliders[feature][2], label=feature)
50
- for feature in numerical_features
51
- ] + [
52
- gr.Radio(radio_options, label=feature) for feature in categorical_features
53
- ]
54
-
55
- outputs = [
56
- gr.Textbox(label="Prediction"),
57
- gr.JSON(label="Input Values (Debugging)")
58
- ]
59
 
60
  interface = gr.Interface(
61
- fn=predict_outcome,
62
  inputs=inputs,
63
- outputs=outputs,
64
- title="Health Risk Prediction with Debugging",
65
- description="Predicts health risks based on input parameters using the trained model. Includes input values for debugging."
 
 
 
66
  )
67
 
68
- # Launch the app
69
  if __name__ == "__main__":
70
  interface.launch()
 
1
  import gradio as gr
 
2
  import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
 
6
+ # Load the pre-trained model
7
+ model = joblib.load("tuned_model.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Load the features used during training
10
+ features = pd.read_csv("features_used_in_model.csv")["Feature"].tolist()
11
 
12
  # Prediction function
13
+ def predict_heart_failure(input_data):
14
+ try:
15
+ # Convert input into a DataFrame
16
+ input_df = pd.DataFrame([input_data], columns=features)
17
+
18
+ # Predict probability for heart failure (class 1)
19
+ probability = model.predict_proba(input_df)[:, 1][0]
20
+
21
+ # Predict class (0 or 1)
22
+ prediction = "At Risk of Heart Failure" if probability >= 0.3 else "No Risk Detected"
23
+
24
+ return {
25
+ "Prediction": prediction,
26
+ "Risk Probability": round(probability, 4)
27
+ }
28
+ except Exception as e:
29
+ return {"error": str(e)}
30
+
31
+ # Gradio Interface
32
+ inputs = []
33
+ for feature in features:
34
+ inputs.append(gr.inputs.Textbox(label=feature, placeholder=f"Enter value for {feature}"))
35
+
36
+ output = gr.outputs.JSON(label="Heart Failure Prediction")
 
 
 
 
 
37
 
38
  interface = gr.Interface(
39
+ fn=predict_heart_failure,
40
  inputs=inputs,
41
+ outputs=output,
42
+ title="Heart Failure Prediction Model",
43
+ description=(
44
+ "Predicts the likelihood of heart failure based on health features. "
45
+ "Enter the values for the features below and receive the prediction."
46
+ )
47
  )
48
 
49
+ # Launch the interface for local testing or Hugging Face Spaces deployment
50
  if __name__ == "__main__":
51
  interface.launch()
features_used_in_model.csv ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Feature
2
+ State
3
+ Sex
4
+ GeneralHealth
5
+ PhysicalHealthDays
6
+ MentalHealthDays
7
+ LastCheckupTime
8
+ PhysicalActivities
9
+ SleepHours
10
+ HadStroke
11
+ HadArthritis
12
+ HadDiabetes
13
+ SmokerStatus
14
+ ECigaretteUsage
15
+ RaceEthnicityCategory
16
+ AgeCategory
17
+ HeightInMeters
18
+ WeightInKilograms
19
+ BMI
20
+ AlcoholDrinkers
21
+ HighRiskLastYear