mojakob commited on
Commit
f571fdc
·
verified ·
1 Parent(s): b925fe1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -45
app.py CHANGED
@@ -426,29 +426,20 @@ def save_to_pickle(standardized_data):
426
  # Functions for Gradio Flow
427
  ##############################################################################
428
 
429
- def store_pid(manual_pid, app_state= None):
430
- # if app_state is None:
431
- # raise ValueError("app_state = None in store_pid().")
432
-
433
- # if isinstance(app_state, gr.BrowserState):
434
- # app_state = app_state.value # Extract the dictionary from State
435
-
436
  pid = manual_pid.strip()
437
  if not pid:
438
- return "Error: Please enter your Prolific ID."
439
- # app_state["prolific_id"] = pid
440
- return f"Prolific ID {pid} has been recorded."
441
 
442
- def process_phq9(*scores, app=None):
443
  if app_state is None:
444
  raise ValueError("app_state = None in process_phq9().")
445
 
446
- if isinstance(app_state, gr.BrowserState):
447
- app_state = app_state.value # Extract the dictionary from State
448
-
449
  total_score = sum(scores)
450
  app_state["phq_score"] = total_score
451
- return f"PHQ-9 completed. Score: {total_score}. Proceed to GAD-7."
452
 
453
  def process_gad7(*scores, app_state=None):
454
  if app_state is None:
@@ -1049,9 +1040,8 @@ with gr.Blocks(css=css) as app:
1049
 
1050
  pid_button.click(
1051
  fn=store_pid,
1052
- inputs=[pid_input],
1053
- outputs=pid_output
1054
- )
1055
 
1056
  # (B) PHQ-9
1057
  gr.Markdown("""## Pretest: PHQ-9
@@ -1203,7 +1193,7 @@ with gr.Blocks(css=css) as app:
1203
  next_button.click(
1204
  fn=process_phq9,
1205
  inputs=[q1, q2, q3, q4, q5, q6, q7, q8, q9, app_state],
1206
- outputs=phq_result,
1207
  )
1208
  next_button.click(show_gad7, inputs=[], outputs=[page_1, page_2])
1209
 
@@ -1263,7 +1253,7 @@ with gr.Blocks(css=css) as app:
1263
  gr.update(visible=False), # Hide profile page
1264
  gr.update(visible=True), # Show chatbot page
1265
  "Profile submitted successfully. Proceeding to the chatbot." # Feedback message
1266
- )
1267
 
1268
  def transition_to_chat(age, profession, interest, app_state):
1269
  """
@@ -1290,36 +1280,36 @@ with gr.Blocks(css=css) as app:
1290
  app_state
1291
  )
1292
 
1293
- def process_user_profile(age, profession, interest, app_state):
1294
- """
1295
- Handles user profile input and transitions to the chatbot.
1296
- Combines app_state updates and UI transitions.
1297
- """
1298
- # Extract app_state from BrowserState
1299
- if isinstance(app_state, gr.BrowserState):
1300
- app_state = app_state.value
1301
-
1302
- # Step 1: Update app_state with user profile
1303
- store_user_profile(age, profession, interest, app_state)
1304
-
1305
- # Debugging log
1306
- print("app_state after store_user_profile:", app_state)
1307
-
1308
- # Step 2: Transition to Chatbot
1309
- return (
1310
- gr.update(visible=False), # Hide the profile page
1311
- gr.update(visible=True), # Show the chatbot page
1312
- "Profile submitted successfully. Proceeding to the chatbot.", # Confirmation message
1313
- app_state
1314
- )
1315
-
1316
  user_profile_button.click(
1317
  fn=process_user_profile,
1318
  inputs=[age_input, profession_input, interest_input, app_state],
1319
  outputs=[page_3b, page_3, user_profile_output, app_state]
1320
  )
1321
-
1322
- # 5) After chatbot is done, go to Usability (button initially hidden)
1323
  def go_to_usability():
1324
  return gr.update(visible=False), gr.update(visible=True)
1325
 
 
426
  # Functions for Gradio Flow
427
  ##############################################################################
428
 
429
+ def store_pid(manual_pid, app_state):
 
 
 
 
 
 
430
  pid = manual_pid.strip()
431
  if not pid:
432
+ return "Error: Please enter your Prolific ID.", app_state
433
+ app_state["prolific_id"] = pid
434
+ return f"Prolific ID {pid} has been recorded.", app_state
435
 
436
+ def process_phq9(*scores, app_state):
437
  if app_state is None:
438
  raise ValueError("app_state = None in process_phq9().")
439
 
 
 
 
440
  total_score = sum(scores)
441
  app_state["phq_score"] = total_score
442
+ return f"PHQ-9 completed. Score: {total_score}. Proceed to GAD-7.", app_state
443
 
444
  def process_gad7(*scores, app_state=None):
445
  if app_state is None:
 
1040
 
1041
  pid_button.click(
1042
  fn=store_pid,
1043
+ inputs=[pid_input, app_state],
1044
+ outputs=[pid_output, app_state])
 
1045
 
1046
  # (B) PHQ-9
1047
  gr.Markdown("""## Pretest: PHQ-9
 
1193
  next_button.click(
1194
  fn=process_phq9,
1195
  inputs=[q1, q2, q3, q4, q5, q6, q7, q8, q9, app_state],
1196
+ outputs=[phq_result, app_state]
1197
  )
1198
  next_button.click(show_gad7, inputs=[], outputs=[page_1, page_2])
1199
 
 
1253
  gr.update(visible=False), # Hide profile page
1254
  gr.update(visible=True), # Show chatbot page
1255
  "Profile submitted successfully. Proceeding to the chatbot." # Feedback message
1256
+ )
1257
 
1258
  def transition_to_chat(age, profession, interest, app_state):
1259
  """
 
1280
  app_state
1281
  )
1282
 
1283
+ def process_user_profile(age, profession, interest, app_state):
1284
+ """
1285
+ Handles user profile input and transitions to the chatbot.
1286
+ Combines app_state updates and UI transitions.
1287
+ """
1288
+ # Extract app_state from BrowserState
1289
+ if isinstance(app_state, gr.BrowserState):
1290
+ app_state = app_state.value
1291
+
1292
+ # Step 1: Update app_state with user profile
1293
+ store_user_profile(age, profession, interest, app_state)
1294
+
1295
+ # Debugging log
1296
+ print("app_state after store_user_profile:", app_state)
1297
+
1298
+ # Step 2: Transition to Chatbot
1299
+ return (
1300
+ gr.update(visible=False), # Hide the profile page
1301
+ gr.update(visible=True), # Show the chatbot page
1302
+ "Profile submitted successfully. Proceeding to the chatbot.", # Confirmation message
1303
+ app_state
1304
+ )
1305
+
1306
  user_profile_button.click(
1307
  fn=process_user_profile,
1308
  inputs=[age_input, profession_input, interest_input, app_state],
1309
  outputs=[page_3b, page_3, user_profile_output, app_state]
1310
  )
1311
+
1312
+ # 5) After chatbot is done, go to Usability (button initially hidden)
1313
  def go_to_usability():
1314
  return gr.update(visible=False), gr.update(visible=True)
1315