parass13 commited on
Commit
eef2ede
Β·
verified Β·
1 Parent(s): 0381fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -110,7 +110,7 @@ with gr.Blocks(title="DiaSpark: Your Health Management Platform", css=css) as de
110
  with gr.TabItem("Find a Specialist πŸ§‘β€βš•οΈ"):
111
  render_doctor_list()
112
 
113
- # --- UPDATED Prediction Tab ---
114
  with gr.TabItem("Prediction Tool πŸ”¬", id="prediction_tab"):
115
 
116
  with gr.Column(visible=False) as prediction_view:
@@ -128,15 +128,13 @@ with gr.Blocks(title="DiaSpark: Your Health Management Platform", css=css) as de
128
  age = gr.Number(label="Age", info="Age in years")
129
 
130
  with gr.Column(scale=1):
131
- # --- New BMI UI Logic ---
132
  bmi = gr.Number(label="BMI", info="Your Body Mass Index. We can calculate this for you.")
133
 
134
  bmi_choice = gr.Radio(
135
  ["I know my BMI", "Calculate from Weight/Height"],
136
  label="Provide BMI",
137
- value="I know my BMI" # Default selection
138
  )
139
- # This section is for inputs to calculate BMI
140
  with gr.Column(visible=False) as bmi_calc_view:
141
  weight = gr.Number(label="Your Weight (kg)")
142
  height = gr.Number(label="Your Height (cm)")
@@ -173,25 +171,22 @@ with gr.Blocks(title="DiaSpark: Your Health Management Platform", css=css) as de
173
  is_female = user_data.get("gender") == "Female"
174
  username = user_data.get("username", "User")
175
  welcome_text = f"### πŸ‘‹ Welcome, {username}!"
176
- return (
177
- gr.update(visible=is_logged_in),
178
- gr.update(visible=not is_logged_in),
179
- gr.update(visible=is_female),
180
- gr.update(value=welcome_text),
181
- gr.update(visible=is_logged_in),
182
- gr.update(visible=not is_logged_in)
183
- )
184
  user_state.change(fn=handle_user_state_change, inputs=user_state, outputs=[prediction_view, logged_out_view, pregnancies_row, welcome_message, logged_in_header, auth_tab_item])
185
 
186
  signup_btn.click(fn=register, inputs=[email_signup, pwd_signup, gender_signup, username_signup], outputs=[auth_message, email_signup, pwd_signup, gender_signup, username_signup])
187
  login_btn.click(fn=login, inputs=[email_login, pwd_login, user_state], outputs=[auth_message, user_state, main_tabs])
 
188
  logout_btn.click(fn=logout, inputs=[user_state], outputs=[auth_message, user_state, main_tabs, email_login, pwd_login, preg, glucose, bp, insulin, bmi, age, result_output, pregnancies_row, weight, height, bmi_choice])
189
 
 
190
  def toggle_bmi_inputs(choice):
191
  if choice == "I know my BMI":
 
192
  return gr.update(interactive=True, value=None), None, None, gr.update(visible=False)
193
  else: # Calculate from Weight/Height
194
- return gr.update(interactive=False, value=None), gr.update(visible=True)
 
195
  bmi_choice.change(fn=toggle_bmi_inputs, inputs=bmi_choice, outputs=[bmi, weight, height, bmi_calc_view])
196
 
197
  def calculate_bmi(w, h):
@@ -207,9 +202,12 @@ with gr.Blocks(title="DiaSpark: Your Health Management Platform", css=css) as de
207
  def handle_create_report(user, p, g, b, i, bmi_val, age_val, result):
208
  if not result:
209
  return gr.update(visible=False)
210
- input_data = {"Pregnancies": p if p is not None else 0, "Glucose": g, "Blood Pressure": b, "Insulin": i, "BMI": bmi_val if bmi_val is not None else 0, "Age": age_val}
 
 
211
  file_path = create_report(user, input_data, result)
212
- return gr.update(value=file_file, visible=True)
 
213
  generate_report_btn.click(fn=handle_create_report, inputs=[user_state, preg, glucose, bp, insulin, bmi, age, result_output], outputs=[report_file_output])
214
 
215
  result_output.change(fn=lambda x: gr.update(visible=bool(x)), inputs=result_output, outputs=generate_report_btn)
 
110
  with gr.TabItem("Find a Specialist πŸ§‘β€βš•οΈ"):
111
  render_doctor_list()
112
 
113
+ # --- Prediction Tab ---
114
  with gr.TabItem("Prediction Tool πŸ”¬", id="prediction_tab"):
115
 
116
  with gr.Column(visible=False) as prediction_view:
 
128
  age = gr.Number(label="Age", info="Age in years")
129
 
130
  with gr.Column(scale=1):
 
131
  bmi = gr.Number(label="BMI", info="Your Body Mass Index. We can calculate this for you.")
132
 
133
  bmi_choice = gr.Radio(
134
  ["I know my BMI", "Calculate from Weight/Height"],
135
  label="Provide BMI",
136
+ value="I know my BMI"
137
  )
 
138
  with gr.Column(visible=False) as bmi_calc_view:
139
  weight = gr.Number(label="Your Weight (kg)")
140
  height = gr.Number(label="Your Height (cm)")
 
171
  is_female = user_data.get("gender") == "Female"
172
  username = user_data.get("username", "User")
173
  welcome_text = f"### πŸ‘‹ Welcome, {username}!"
174
+ return (gr.update(visible=is_logged_in), gr.update(visible=not is_logged_in), gr.update(visible=is_female), gr.update(value=welcome_text), gr.update(visible=is_logged_in), gr.update(visible=not is_logged_in))
 
 
 
 
 
 
 
175
  user_state.change(fn=handle_user_state_change, inputs=user_state, outputs=[prediction_view, logged_out_view, pregnancies_row, welcome_message, logged_in_header, auth_tab_item])
176
 
177
  signup_btn.click(fn=register, inputs=[email_signup, pwd_signup, gender_signup, username_signup], outputs=[auth_message, email_signup, pwd_signup, gender_signup, username_signup])
178
  login_btn.click(fn=login, inputs=[email_login, pwd_login, user_state], outputs=[auth_message, user_state, main_tabs])
179
+ # The logout handler must now also clear the weight and height fields.
180
  logout_btn.click(fn=logout, inputs=[user_state], outputs=[auth_message, user_state, main_tabs, email_login, pwd_login, preg, glucose, bp, insulin, bmi, age, result_output, pregnancies_row, weight, height, bmi_choice])
181
 
182
+ # --- CORRECTED BMI LOGIC ---
183
  def toggle_bmi_inputs(choice):
184
  if choice == "I know my BMI":
185
+ # Return 4 values: one for each output component
186
  return gr.update(interactive=True, value=None), None, None, gr.update(visible=False)
187
  else: # Calculate from Weight/Height
188
+ # Also return 4 values here to match the outputs list
189
+ return gr.update(interactive=False, value=None), gr.update(), gr.update(), gr.update(visible=True)
190
  bmi_choice.change(fn=toggle_bmi_inputs, inputs=bmi_choice, outputs=[bmi, weight, height, bmi_calc_view])
191
 
192
  def calculate_bmi(w, h):
 
202
  def handle_create_report(user, p, g, b, i, bmi_val, age_val, result):
203
  if not result:
204
  return gr.update(visible=False)
205
+ # Recalculate is no longer needed, just use the value from the bmi box
206
+ final_bmi_report = bmi_val if bmi_val is not None else 0
207
+ input_data = {"Pregnancies": p if p is not None else 0, "Glucose": g, "Blood Pressure": b, "Insulin": i, "BMI": final_bmi_report, "Age": age_val}
208
  file_path = create_report(user, input_data, result)
209
+ # CORRECTED TYPO: It should be 'file_path', not 'file_file'
210
+ return gr.update(value=file_path, visible=True)
211
  generate_report_btn.click(fn=handle_create_report, inputs=[user_state, preg, glucose, bp, insulin, bmi, age, result_output], outputs=[report_file_output])
212
 
213
  result_output.change(fn=lambda x: gr.update(visible=bool(x)), inputs=result_output, outputs=generate_report_btn)