Nadeem005 commited on
Commit
45505a5
·
verified ·
1 Parent(s): 22dd25c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -127
app.py CHANGED
@@ -1,22 +1,12 @@
1
- # -*- coding: utf-8 -*-
2
- """app
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1GVLBcgTt2EmZTg1eSA2_ez5k25akVsN-
8
- """
9
-
10
  import gradio as gr
11
  import pandas as pd
12
  import joblib
13
 
14
- # --- Load the Saved Model and Columns ---
15
- # Ensure these files are in the same directory as your app.py
16
  model = joblib.load('heart_disease_model.joblib')
17
  model_columns = joblib.load('model_columns.joblib')
18
 
19
- # --- Define the Prediction Function ---
20
  def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
21
  input_data = {
22
  'age': age, 'sex': sex, 'cp': cp, 'restbp': restbp, 'chol': chol,
@@ -36,147 +26,126 @@ def predict_heart_disease(age, sex, cp, restbp, chol, thalach, exang, ca, thal):
36
  prediction = model.predict(input_df)
37
  prediction_proba = model.predict_proba(input_df)
38
 
39
- # --- Format the Output with HTML for styling ---
40
  if prediction[0] == 1:
41
- result_text = "<span style='color: #FF6347; font-weight: bold;'>High Risk of Heart Disease</span>"
42
  confidence = prediction_proba[0][1]
43
  else:
44
- result_text = "<span style='color: #3CB371; font-weight: bold;'>Low Risk of Heart Disease</span>"
45
  confidence = prediction_proba[0][0]
 
 
46
 
47
- return f"{result_text}<br>Confidence: {confidence*100:.2f}%"
48
-
49
- # --- Gradio Interface Setup ---
50
-
51
- # Define the input components for the UI
52
- # We're making some inputs more visually appealing and grouped
53
- inputs = [
54
- gr.Slider(minimum=29, maximum=77, value=35, label="Age", info="Patient's age in years"),
55
- gr.Radio(choices=[0.0, 1.0], label="Sex", value=0.0, info="0=Female, 1=Male"),
56
- gr.Radio(choices=[1.0, 2.0, 3.0, 4.0], label="Chest Pain Type (cp)", value=1.0,
57
- info="1: Typical Angina, 2: Atypical Angina, 3: Non-anginal Pain, 4: Asymptomatic"),
58
- gr.Slider(minimum=94, maximum=200, value=120, label="Resting Blood Pressure (trestbps)", info="in mm Hg"),
59
- gr.Slider(minimum=126, maximum=564, value=200, label="Cholesterol (chol)", info="in mg/dl"),
60
- gr.Slider(minimum=71, maximum=202, value=150, label="Max Heart Rate Achieved (thalach)", info="Max heart rate during exercise"),
61
- gr.Radio(choices=[0.0, 1.0], label="Exercise Induced Angina (exang)", value=0.0,
62
- info="1=Yes, 0=No"),
63
- gr.Radio(choices=[0.0, 1.0, 2.0, 3.0], label="Number of Major Vessels (ca)", value=0.0,
64
- info="Number of major vessels (0-3) colored by fluoroscopy"),
65
- gr.Radio(choices=[3.0, 6.0, 7.0], label="Thalassemia (thal)", value=3.0,
66
- info="3: Normal, 6: Fixed defect, 7: Reversible defect")
67
- ]
68
-
69
- # Output component - using gr.HTML to display rich text, avoiding the scrollbar
70
- outputs = gr.HTML(label="Prediction Result") # Use HTML to render our styled output
71
-
72
- # --- Custom CSS for a more attractive and colorful look ---
73
  custom_css = """
74
- body {
75
- background-color: #2c2c2c; /* Dark background */
76
- color: #e0e0e0; /* Light text */
77
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
78
- }
79
- .gradio-container {
80
- background-color: #3a3a3a;
81
- border-radius: 15px;
82
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
83
- padding: 30px;
84
- }
85
- .gradio-header {
86
- background-color: #e91e63; /* Pink header */
87
- color: white;
88
- padding: 20px;
89
- border-radius: 10px 10px 0 0;
90
- font-size: 2.2em;
91
  font-weight: bold;
92
- text-align: center;
93
- margin-bottom: 20px;
94
- }
95
- .gradio-description {
96
- color: #bdbdbd;
97
- text-align: center;
98
- margin-bottom: 30px;
99
- font-size: 1.1em;
100
  }
101
- .gradio-label {
102
- color: #81c784; /* Green for labels */
103
- font-weight: bold;
104
- font-size: 1.1em;
105
- margin-bottom: 5px;
 
106
  }
107
- .gradio-input label { /* Targeting specific input labels */
108
- color: #81c784 !important;
 
 
109
  }
110
- .gradio-input input[type='number'],
111
- .gradio-input input[type='text'] {
112
- background-color: #4a4a4a;
113
- color: #e0e0e0;
114
- border: 1px solid #5a5a5a;
115
- border-radius: 5px;
116
  }
117
- .gradio-slider-track-color { /* Slider color */
118
- background-color: #e91e63 !important;
 
 
 
 
 
119
  }
120
- .gradio-radio-button.selected { /* Selected radio button */
121
- background-color: #e91e63 !important;
122
- color: white !important;
123
- border-color: #e91e63 !important;
124
  }
125
  .gradio-button {
126
- background-color: #e91e63 !important; /* Pink button */
127
- color: white !important;
128
- border: none;
129
- padding: 12px 25px;
130
- font-size: 1.1em;
131
- border-radius: 8px;
132
- transition: background-color 0.3s ease;
133
  }
134
  .gradio-button:hover {
135
- background-color: #c2185b !important; /* Darker pink on hover */
 
136
  }
137
- /* Style for the output box */
138
- .gradio-output-text {
139
- background-color: #4a4a4a;
140
- padding: 20px;
141
- border-radius: 10px;
142
- font-size: 1.2em;
143
- text-align: center;
144
- margin-top: 20px;
145
- min-height: 80px; /* Ensure sufficient height */
146
- display: flex;
147
- align-items: center;
148
- justify-content: center;
149
- flex-direction: column;
150
  }
151
- /* Further refined styling for the HTML output */
152
- #component-20 { /* Adjust this ID if your output component ID changes */
153
- background-color: #4a4a4a;
 
 
154
  padding: 20px;
155
  border-radius: 10px;
156
- font-size: 1.2em;
157
  text-align: center;
158
- margin-top: 20px;
159
- min-height: 80px;
160
  display: flex;
161
  align-items: center;
162
  justify-content: center;
163
  flex-direction: column;
164
- color: #e0e0e0; /* Default text color for the output box */
165
  }
166
  """
167
 
168
- # --- Create the Interface and Launch ---
169
- iface = gr.Interface(
170
- fn=predict_heart_disease,
171
- inputs=inputs,
172
- outputs=outputs,
173
- title="❤️ CardioCompass: Heart Disease Predictor",
174
- description="Enter patient details to predict the likelihood of having heart disease. Based on the UCI Heart Disease dataset.",
175
- css=custom_css, # Apply our custom CSS
176
- theme=gr.themes.Soft() # Use a modern Gradio theme as a base
177
- )
178
-
179
- # Launch the app. The 'share=True' will create a temporary public link in Colab!
180
- # For Hugging Face Spaces, you'd typically remove share=True as the space itself is public.
181
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
 
 
 
 
 
 
 
 
 
 
1
  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,
 
26
  prediction = model.predict(input_df)
27
  prediction_proba = model.predict_proba(input_df)
28
 
 
29
  if prediction[0] == 1:
30
+ result_text = "<div style='color: #ef4444; font-size: 24px; font-weight: bold;'>High Risk of Heart Disease</div>"
31
  confidence = prediction_proba[0][1]
32
  else:
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 {
93
+ background-color: #374151;
94
+ border: 1px solid #4b5563;
95
  padding: 20px;
96
  border-radius: 10px;
 
97
  text-align: center;
98
+ min-height: 120px;
 
99
  display: flex;
100
  align-items: center;
101
  justify-content: center;
102
  flex-direction: column;
103
+ color: #e5e7eb;
104
  }
105
  """
106
 
107
+ # --- 4. Build the UI with Gradio Blocks for full control ---
108
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
109
+ gr.Markdown("# ❤️ CardioCompass: Heart Disease Predictor")
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)
117
+ cp_in = gr.Radio(choices=[(1.0, "Typical Angina"), (2.0, "Atypical Angina"), (3.0, "Non-anginal Pain"), (4.0, "Asymptomatic")], label="Chest Pain Type (cp)")
118
+ restbp_in = gr.Slider(minimum=94, maximum=200, value=120, label="Resting Blood Pressure (trestbps)", info="in mm Hg")
119
+ chol_in = gr.Slider(minimum=126, maximum=564, value=200, label="Cholesterol (chol)", info="in mg/dl")
120
+ thalach_in = gr.Slider(minimum=71, maximum=202, value=150, label="Max Heart Rate Achieved (thalach)")
121
+ exang_in = gr.Radio(choices=[(0.0, "No"), (1.0, "Yes")], label="Exercise Induced Angina (exang)")
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