Nadeem005 commited on
Commit
509ac8f
·
verified ·
1 Parent(s): 45505a5

Update app.py

Browse files

upload app script

Files changed (1) hide show
  1. app.py +14 -45
app.py CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
2
  import pandas as pd
3
  import joblib
4
 
5
- # --- 1. Load the Saved Model and Columns (No changes here) ---
6
  model = joblib.load('heart_disease_model.joblib')
7
  model_columns = joblib.load('model_columns.joblib')
8
 
9
- # --- 2. Define the Prediction Function (No changes here) ---
10
  def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
11
  input_data = {
12
  'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
@@ -33,60 +33,43 @@ def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
33
  result_text = "<div style='color: #22c55e; font-size: 24px; font-weight: bold;'>Low Risk of Heart Disease</div>"
34
  confidence = prediction_proba[0][0]
35
 
36
- return f"{result_text}<div style='margin-top: 10px; color: #a1a1aa;'>Confidence: {confidence*100:.2f}%</div>"
37
 
38
  # --- 3. Custom CSS for Advanced Styling ---
39
- # This CSS block now controls all the specific colors you wanted.
40
  custom_css = """
41
- /* --- Main container and theme --- */
42
  body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
43
  .gradio-container { background-color: #1f2937; border-radius: 15px !important; }
44
 
45
  /* --- Input component labels (e.g., "Age", "Sex") --- */
46
  .gradio-input label {
47
- color: #a5b4fc !important; /* Different color for feature labels (light indigo) */
48
  font-weight: bold;
49
  font-size: 1.1em !important;
50
  }
51
 
52
  /* --- The selectable numbers/options (Radio Buttons) --- */
53
  .gradio-radio-button {
54
- background-color: #374151; /* Base color for radio buttons */
55
  color: #d1d5db;
56
  border: 1px solid #4b5563;
57
  }
58
  .gradio-radio-button.selected {
59
- background-color: #34d399 !important; /* Different color for selected numbers (emerald green) */
60
  color: white !important;
61
  border-color: #34d399 !important;
62
  }
63
 
64
  /* --- Slider Color --- */
65
  .gradio-slider-track-color {
66
- background-color: #60a5fa !important; /* Different color for sliders (blue) */
67
  }
68
 
69
  /* --- Button Alignment and Interactivity --- */
70
- #buttons-row {
71
- display: flex;
72
- gap: 10px; /* Space between buttons */
73
- justify-content: center; /* Center the buttons */
74
- width: 100%;
75
- }
76
- #buttons-row > button {
77
- flex-grow: 1; /* Make buttons take up equal space */
78
- }
79
- .gradio-button {
80
- transition: all 0.2s ease-in-out !important;
81
- transform: scale(1.0);
82
- }
83
- .gradio-button:hover {
84
- transform: scale(1.03); /* Slightly grow button on hover */
85
- filter: brightness(1.1);
86
- }
87
- .gradio-button:active {
88
- transform: scale(0.98); /* Slightly shrink button when clicked */
89
- }
90
 
91
  /* --- Permanent Result Box Styling --- */
92
  #result-box {
@@ -110,7 +93,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
110
  gr.Markdown("Enter patient details to predict the likelihood of having heart disease.")
111
 
112
  with gr.Row():
113
- # --- Left Column for Inputs ---
114
  with gr.Column(scale=2):
115
  age_in = gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years")
116
  sex_in = gr.Radio(choices=[(0.0, "Female"), (1.0, "Male")], label="Sex", value=0.0)
@@ -122,30 +104,17 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
122
  ca_in = gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)")
123
  thal_in = gr.Radio(choices=[(3.0, "Normal"), (6.0, "Fixed defect"), (7.0, "Reversible defect")], label="Thalassemia (thal)")
124
 
125
- # --- Right Column for Output and Buttons ---
126
  with gr.Column(scale=1):
127
- # The result box is now permanent and has a default message
128
  result_out = gr.HTML(value="<div style='color: #9ca3af;'>Prediction will appear here...</div>", elem_id="result-box")
129
-
130
- # This row ensures the buttons are perfectly aligned
131
  with gr.Row(elem_id="buttons-row"):
132
  clear_button = gr.ClearButton(value="Clear")
133
  submit_button = gr.Button("Submit", variant="primary")
134
 
135
  # --- 5. Connect the components ---
136
- # Define the list of all input components
137
  inputs_list = [age_in, sex_in, cp_in, restbp_in, chol_in, thalach_in, exang_in, ca_in, thal_in]
138
-
139
- # When the submit button is clicked, run the function
140
- submit_button.click(
141
- fn=predict_heart_disease,
142
- inputs=inputs_list,
143
- outputs=result_out
144
- )
145
-
146
- # The clear button needs to know which components to clear
147
  clear_button.add(inputs_list)
148
 
149
  # --- 6. Launch the App ---
150
  demo.launch(share=True)
151
-
 
2
  import pandas as pd
3
  import joblib
4
 
5
+ # --- 1. Load the Saved Model and Columns ---
6
  model = joblib.load('heart_disease_model.joblib')
7
  model_columns = joblib.load('model_columns.joblib')
8
 
9
+ # --- 2. Define the Prediction Function ---
10
  def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
11
  input_data = {
12
  'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
 
33
  result_text = "<div style='color: #22c55e; font-size: 24px; font-weight: bold;'>Low Risk of Heart Disease</div>"
34
  confidence = prediction_proba[0][0]
35
 
36
+ return f"{result_text}<br><div style='margin-top: 10px; color: #a1a1aa;'>Confidence: {confidence*100:.2f}%</div>"
37
 
38
  # --- 3. Custom CSS for Advanced Styling ---
 
39
  custom_css = """
 
40
  body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
41
  .gradio-container { background-color: #1f2937; border-radius: 15px !important; }
42
 
43
  /* --- Input component labels (e.g., "Age", "Sex") --- */
44
  .gradio-input label {
45
+ color: #f97316 !important; /* <<< THIS LINE WAS CHANGED TO ORANGE */
46
  font-weight: bold;
47
  font-size: 1.1em !important;
48
  }
49
 
50
  /* --- The selectable numbers/options (Radio Buttons) --- */
51
  .gradio-radio-button {
52
+ background-color: #374151;
53
  color: #d1d5db;
54
  border: 1px solid #4b5563;
55
  }
56
  .gradio-radio-button.selected {
57
+ background-color: #34d399 !important;
58
  color: white !important;
59
  border-color: #34d399 !important;
60
  }
61
 
62
  /* --- Slider Color --- */
63
  .gradio-slider-track-color {
64
+ background-color: #60a5fa !important;
65
  }
66
 
67
  /* --- Button Alignment and Interactivity --- */
68
+ #buttons-row { display: flex; gap: 10px; justify-content: center; width: 100%; }
69
+ #buttons-row > button { flex-grow: 1; }
70
+ .gradio-button { transition: all 0.2s ease-in-out !important; transform: scale(1.0); }
71
+ .gradio-button:hover { transform: scale(1.03); filter: brightness(1.1); }
72
+ .gradio-button:active { transform: scale(0.98); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  /* --- Permanent Result Box Styling --- */
75
  #result-box {
 
93
  gr.Markdown("Enter patient details to predict the likelihood of having heart disease.")
94
 
95
  with gr.Row():
 
96
  with gr.Column(scale=2):
97
  age_in = gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years")
98
  sex_in = gr.Radio(choices=[(0.0, "Female"), (1.0, "Male")], label="Sex", value=0.0)
 
104
  ca_in = gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)")
105
  thal_in = gr.Radio(choices=[(3.0, "Normal"), (6.0, "Fixed defect"), (7.0, "Reversible defect")], label="Thalassemia (thal)")
106
 
 
107
  with gr.Column(scale=1):
 
108
  result_out = gr.HTML(value="<div style='color: #9ca3af;'>Prediction will appear here...</div>", elem_id="result-box")
 
 
109
  with gr.Row(elem_id="buttons-row"):
110
  clear_button = gr.ClearButton(value="Clear")
111
  submit_button = gr.Button("Submit", variant="primary")
112
 
113
  # --- 5. Connect the components ---
 
114
  inputs_list = [age_in, sex_in, cp_in, restbp_in, chol_in, thalach_in, exang_in, ca_in, thal_in]
115
+ submit_button.click(fn=predict_heart_disease, inputs=inputs_list, outputs=result_out)
 
 
 
 
 
 
 
 
116
  clear_button.add(inputs_list)
117
 
118
  # --- 6. Launch the App ---
119
  demo.launch(share=True)
120
+