File size: 27,136 Bytes
cae539d
f67a22f
 
cae539d
f6f4dae
 
ef8f7ee
d7df4ad
6857a79
3c0fef7
57cc8ad
 
d7df4ad
cae539d
d79e0fb
 
d7df4ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d79e0fb
d7df4ad
 
 
 
d79e0fb
 
6857a79
 
 
 
 
 
3c0fef7
 
 
6857a79
478321c
3c0fef7
6857a79
3c0fef7
 
 
 
 
 
 
 
 
6857a79
 
 
3c0fef7
6857a79
 
 
 
 
 
3c0fef7
6857a79
 
3c0fef7
 
 
 
478321c
 
 
 
3c0fef7
 
 
 
57cc8ad
 
3c0fef7
 
478321c
 
3c0fef7
 
478321c
3c0fef7
6857a79
3c0fef7
6857a79
 
3c0fef7
6857a79
 
cae539d
f67a22f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef8f7ee
f67a22f
 
 
 
 
 
 
 
 
 
 
 
cae539d
 
 
f67a22f
ef8f7ee
 
f67a22f
 
 
 
 
 
 
 
 
6857a79
f67a22f
6857a79
 
 
 
 
f67a22f
324f738
f67a22f
 
 
 
 
 
 
 
 
 
 
6231149
f67a22f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6231149
f67a22f
 
 
 
a07fc9a
f67a22f
6857a79
f67a22f
a07fc9a
f67a22f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1ae48a
cae539d
 
 
805e783
cae539d
b1ae48a
 
0db2db2
b1ae48a
 
f67a22f
 
 
 
 
 
 
 
 
 
 
cae539d
 
f67a22f
 
 
bbedabd
 
 
 
 
 
cae539d
 
 
 
bbedabd
cae539d
 
 
bbedabd
 
 
 
 
cae539d
bbedabd
cae539d
 
 
bbedabd
 
 
 
 
 
 
1e2bd56
cae539d
f67a22f
bbedabd
 
 
 
 
 
 
 
 
 
 
 
 
6857a79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c80e172
6857a79
bbedabd
 
 
dadca1d
bbedabd
f67a22f
d79e0fb
f67a22f
dadca1d
 
 
 
f67a22f
 
dadca1d
 
 
 
bbedabd
f67a22f
bbedabd
f67a22f
dadca1d
 
 
 
f67a22f
 
dadca1d
f67a22f
 
 
 
a07fc9a
 
f67a22f
 
 
 
 
a07fc9a
f67a22f
a07fc9a
cae539d
 
 
 
 
 
a07fc9a
cae539d
 
 
 
 
a07fc9a
f67a22f
a07fc9a
6857a79
cae539d
a07fc9a
 
 
 
f67a22f
 
 
 
6a2c460
f67a22f
 
 
 
 
 
 
c09bc8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22d9a79
c09bc8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49b5890
c09bc8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67a22f
c09bc8d
f67a22f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eeb00a
f67a22f
 
 
 
 
 
 
d79e0fb
cae539d
b1ae48a
 
 
 
 
 
cae539d
 
b1ae48a
6857a79
cae539d
 
 
f67a22f
 
 
 
 
 
 
 
 
 
 
 
805e783
 
 
 
 
a07fc9a
 
f67a22f
c09bc8d
f67a22f
 
 
6ff9b0a
f67a22f
 
 
4eeb00a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cae539d
 
4eeb00a
 
d79e0fb
4eeb00a
cae539d
 
4eeb00a
 
 
 
 
 
cae539d
 
4eeb00a
cae539d
 
4eeb00a
016ae37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619

import gradio as gr
import pandas as pd
import os
from huggingface_hub import HfApi, HfFolder
from dotenv import load_dotenv
import random
import threading
import time
import boto3
from botocore.config import Config

file_lock = threading.Lock()

def create_excel_if_missing(filename: str):
    """Create an empty Excel file with the required columns if it does not exist."""
    with file_lock:
        if not os.path.exists(filename):
            # Define all required columns
            columns = [
                "ProlificID",         # Unique ID for participants
                "PHQ9",               # Total score for PHQ-9
                "GAD7",               # Total score for GAD-7
                "AttentionCheck1",     # First attention check pass/fail
                "AttentionCheck2",     # Second attention check pass/fail
                
                # ASQ scenario responses (long format)
                "ScenarioIndex",       # Scenario number (1-12)
                "ScenarioText",        # Full scenario text
                "Cause",               # Participant's response for cause
                
                # Attribution Dimensions
                "Internal-External",   # Scale from 1 (External) to 10 (Internal)
                "Stable-Unstable",     # Scale from 1 (Unstable) to 10 (Stable)
                "Global-Specific",     # Scale from 1 (Specific) to 10 (Global)
    
                # Emotional Response
                "EmotionalRelevance",  # Scale from 1 (None) to 10 (A lot)
                "PositiveValence",     # Scale from 1 (None) to 10 (A lot)
                "NegativeValence",     # Scale from 1 (None) to 10 (A lot)
                
                # Usability Ratings
                "EaseOfUse",           # 1-10 scale
                "ClarityOfInstr",      # 1-10 scale
                "Engagement",          # 1-10 scale
                "Satisfaction",        # 1-10 scale
                "CognitiveLoad"        # 1-10 scale
            ]
    
            # Create an empty DataFrame with these columns
            empty_df = pd.DataFrame(columns=columns)
            
            # Save it to an Excel file
            empty_df.to_excel(filename, index=False)
    
            print(f"Created empty Excel file: {filename}")


def save_to_excel(df, user_state):
    """
    Saves the user's data into a unique Excel file named:
      Cond3_<ProlificID>_<timestamp>.xlsx
    If a file with that name already exists (e.g., user
    revisits or calls this function multiple times quickly),
    it merges the new data with the existing file.
    Afterwards, uploads the file to an S3-compatible bucket 
    (e.g., Hetzner) under "Cond3/<filename>".
    """

    # 1) Grab the unique local filename
    local_file = user_state.get("excel_file")
    if not local_file:
        print("Error: No user_state['excel_file'] found.")
        return

    # 2) Read S3 credentials and details from your environment
    s3_access_key = os.getenv("HETZNER_ACCESS_KEY_ID")
    s3_secret_key = os.getenv("HETZNER_SECRET_ACCESS_KEY")
    s3_endpoint   = os.getenv("HETZNER_S3_ENDPOINT")
    bucket_name   = os.getenv("HETZNER_BUCKET_NAME")

    try:
        with file_lock:
            # 3) Merge existing data if the file already exists
            if os.path.exists(local_file):
                existing = pd.read_excel(local_file, engine="openpyxl")
                merged = pd.concat([existing, df], ignore_index=True)
            else:
                merged = df

            # 4) Save merged data locally
            merged.to_excel(local_file, index=False, engine="openpyxl")

        print(f"Local file updated: {local_file}")

        # 5) Upload to S3 only if credentials/bucket are present
        if s3_access_key and s3_secret_key and s3_endpoint and bucket_name:
            my_config = Config(
                signature_version="s3v4",
                s3={"addressing_style": "path"}
            )
            s3_client = boto3.client(
                "s3",
                aws_access_key_id=s3_access_key,
                aws_secret_access_key=s3_secret_key,
                endpoint_url=s3_endpoint,
                config=my_config
            )

            # We'll store it in a 'Cond3' subfolder, using the basename to avoid unwanted path details
            s3_key = f"Cond3/{os.path.basename(local_file)}"

            # 6) Upload the file
            s3_client.upload_file(local_file, bucket_name, s3_key)
            print(f"Uploaded {local_file} to S3 bucket '{bucket_name}' at key: {s3_key}")
        else:
            print("No S3 credentials or bucket provided, skipping S3 upload.")

    except Exception as e:
        print(f"Error in save_to_excel: {e}")
        raise


###############################################################################
# Define your 12 ASQ Scenarios
###############################################################################

ASQ_SCENARIOS = [
    # Good Outcomes (Achievement)
    "You become very rich.",
    "You apply for a position that you want very badly (important job...) and you get it.",
    "You get a raise.",
    # Good Outcomes (Affiliation)
    "You meet a friend who compliments you on your appearance.",
    "You do a project that is highly praised.",
    "Your partner has been treating you more lovingly.",
    # Bad Outcomes (Achievement)
    "You have been looking for a job unsuccessfully for some time.",
    "You give an important talk, and the audience reacts negatively.",
    "You can’t get all the work done that others expect of you.",
    # Bad Outcomes (Affiliation)
    "A friend comes to you with a problem, and you don’t try to help.",
    "You meet a friend who acts hostilely toward you.",
    "You go out on a date, and it goes badly."
]

def get_default_state():
    state = {
        "prolific_id": None,
        "phq_score": 0,
        "gad_score": 0,
        # We'll store scenario data in a list of dicts
        "asq_responses": [],
        "usability": {
            "Ease of Use": None,
            "Clarity": None,
            "Engagement": None,
            "Satisfaction": None,
            "Cognitive Load": None
        },
        "current_scenario_index": 0,
        "Attention Check 1": None,
        "Attention Check 2": None        
    }
    random.shuffle(ASQ_SCENARIOS)
    return state

###############################################################################
# 1) PAGE 1: Briefing & Prolific ID
###############################################################################

def store_pid(pid_input, user_state):
    pid = pid_input.strip()
    if not pid:
        return "Please enter a valid Prolific ID", user_state, gr.update(visible=True), gr.update(visible=False)
        
    user_state["prolific_id"] = pid

    # Generate a single file name for this user, used throughout the session
    timestamp = time.strftime("%Y%m%d_%H%M%S")
    user_state["excel_file"] = f"Cond3_{pid}_{timestamp}.xlsx"    
    
    return (
        pid,
        user_state,
        gr.update(visible=False),   # Hide page_1
        gr.update(visible=True)     # Show page_2
    )

###############################################################################
# 2) PAGE 2: PHQ-9
###############################################################################

def process_phq9(q1,q2,q3,q4,q5,q6,q7,q8, user_state):
    # Convert them from str to int
    scores = [int(x) if x is not None else 0 for x in (q1,q2,q3,q4,q5,q6,q7,q8)]
    total_phq = sum(scores)
    user_state["phq_score"] = total_phq
    # Move to GAD-7
    return (
        f"PHQ-9 complete, score={total_phq}. Next is GAD-7.",
        user_state,
        gr.update(visible=False), # Hide page_2
        gr.update(visible=True)   # Show page_3
    )

###############################################################################
# 3) PAGE 3: GAD-7
###############################################################################

def process_gad7(g1,g2,g3,g4,g5,g6,g7, user_state):
    scores = [int(x) if x is not None else 0 for x in (g1,g2,g3,g4,g5,g6,g7)]
    total_gad = sum(scores)
    user_state["gad_score"] = total_gad
    # Move on, check if user is eligible
    combined = user_state["phq_score"] + user_state["gad_score"]
    if combined>25:
        return (
            "Sorry, your combined score indicates you do not meet the inclusion criteria. Please click the following link to return to Prolific: https://app.prolific.com/submissions/complete?cc=C11ALNAE",
            user_state,
            gr.update(visible=True), # page_3
            gr.update(visible=False)  # We won't show next pages
        )
    return (
        f"GAD-7 complete (score={total_gad}). Proceeding to ASQ items...",
        user_state,
        gr.update(visible=False),
        gr.update(visible=True)
    )

###############################################################################
# 4) PAGE 4: ASQ Items (12 scenarios)
###############################################################################

def show_asq_item(user_state):
    idx = user_state["current_scenario_index"]
    scenario = ASQ_SCENARIOS[idx]
    instructions = f"""
Instructions:
1) Read each situation carefully and imagine it happening to you.
2) Decide what you believe would be the major cause of the situation if it happened to you.
3) Write this cause in the blank provided.
4) Answer the questions about the cause by moving the sliders.
5) Go on to the next situation.

*Scenario {idx+1} of {len(ASQ_SCENARIOS)}*
\nPlease imagine the following situation:

**{scenario}**
"""
    return instructions

def submit_asq_item(
    cause_text,
    internal_slider,
    stable_slider,
    global_slider,
    emotional_relevance,
    positive_valence,
    negative_valence,
    user_state,
    attention_check_slider
):
    idx = user_state["current_scenario_index"]
    scenario = ASQ_SCENARIOS[idx]
    
    # Now, if we reach this point, we're processing the submission (or attention check response)
    # If it's an attention check scenario, record the pass/fail:
    if idx in [3, 7]:
        if int(attention_check_slider) == 10:
            if idx == 3:
                user_state["Attention Check 1"] = True
            else:
                user_state["Attention Check 2"] = True
        else:
            if idx == 3:
                user_state["Attention Check 1"] = False
            else:
                user_state["Attention Check 2"] = False

    # For non-attention check scenarios, validate that 'cause_text' isn't empty.
    if not cause_text.strip():
        error_msg = "**Error:** Please provide a cause before proceeding."
        scenario_text = show_asq_item(user_state) + f"\n\n{error_msg}"
        return (
            scenario_text,
            user_state,
            gr.update(visible=True),
            gr.update(visible=False),
            cause_text,  # Keep existing text
            internal_slider,
            stable_slider,
            global_slider,
            emotional_relevance,
            positive_valence,
            negative_valence,
            gr.update(visible=False, value=1)
        )

    # Store user input for the scenario (applies to both attention check and normal scenarios)
    user_state["asq_responses"].append({
        "ScenarioIndex": idx + 1,
        "ScenarioText": scenario,
        "Cause": cause_text,
        "Internal-External": internal_slider,
        "Stable-Unstable": 11 - stable_slider,
        "Global-Specific": global_slider,
        "EmotionalRelevance": emotional_relevance,
        "PositiveValence": positive_valence,
        "NegativeValence": negative_valence
    })

    df = pd.DataFrame([{
        "ProlificID": user_state["prolific_id"],
        "ScenarioIndex": idx + 1,
        "ScenarioText": scenario,
        "Cause": cause_text,
        "Internal-External": internal_slider,
        "Stable-Unstable": 11 - stable_slider,
        "Global-Specific": global_slider,
        "EmotionalRelevance": emotional_relevance,
        "PositiveValence": positive_valence,
        "NegativeValence": negative_valence,
        "AttentionCheck1": user_state["Attention Check 1"],
        "AttentionCheck2": user_state["Attention Check 2"]
    }])
    
    save_to_excel(df, user_state)
    print("Row successfully added to file!")

    # Advance the scenario index
    user_state["current_scenario_index"] += 1

    # If we finished all scenarios:
    if user_state["current_scenario_index"] >= len(ASQ_SCENARIOS):
        return (
            "Thank you for completing the scenarios. Please also answer the following questions.",
            user_state,
            gr.update(visible=False), # Hide page_4
            gr.update(visible=True),  # Show page_5
            "", 1,1,1,1,1,1,
            gr.update(visible=False, value=1)  # Hide attention_check_slider
        )
    else:
        # There's a next scenario. Show or hide the slider depending on if it's #4 or #8
        next_idx = user_state["current_scenario_index"]
        show_slider = (next_idx in [3, 7])  # scenario #4 or #8
    
        next_msg = show_asq_item(user_state)
        return (
            next_msg,
            user_state,
            gr.update(visible=True),  # keep page_4 visible
            gr.update(visible=False), # hide page_5
            "", 1,1,1,1,1,1,          # reset text box & main sliders
            gr.update(visible=show_slider, value=1) 
        )


###############################################################################
# 5) PAGE 5: Usability
###############################################################################

def submit_usability(ease, clarity, engagement, satisfaction, cogload, comments, user_state):
    # Store usability ratings
    user_state["usability"]["Ease of Use"] = ease
    user_state["usability"]["Clarity"] = clarity
    user_state["usability"]["Engagement"] = engagement
    user_state["usability"]["Satisfaction"] = satisfaction
    user_state["usability"]["Cognitive Load"] = cogload
    user_state["usability"]["Additional Comments"] = comments

    # Build a DataFrame from asq_responses
    df = pd.DataFrame(user_state["asq_responses"])
    df["ProlificID"] = user_state["prolific_id"]
    df["PHQ9"] = user_state["phq_score"]
    df["GAD7"] = user_state["gad_score"]
    df["AttentionCheck1"] = user_state["Attention Check 1"]
    df["AttentionCheck2"] = user_state["Attention Check 2"]
    # Usability data
    df["EaseOfUse"] = ease
    df["ClarityOfInstr"] = clarity
    df["Engagement"] = engagement
    df["Satisfaction"] = satisfaction
    df["CognitiveLoad"] = cogload
    df["Additional Comments"] = comments

    # Save partial data
    save_to_excel(df, user_state)

    return (
        "Your responses have been saved. Please use the following link to submit your results and return to Prolific: https://app.prolific.com/submissions/complete?cc=C1ECU6HA",
        user_state
    )

###############################################################################
# BUILD THE GRADIO APP
###############################################################################
with gr.Blocks(theme= gr.themes.Glass()) as cond3_app:
    user_state = gr.State(get_default_state())

    ############################################################################
    # Page 1: Briefing & ID
    ############################################################################
    with gr.Row(visible=True) as page_1:
        with gr.Column():
            gr.Markdown("""
            # **Information for Participants and Declaration of Consent**  
            ## **Evaluating the Validity of LLM-Based Psychological Assessments**
            
            ---
            
            ## **Dear Participant,**  
            ### We invite you to participate in the study described above. Your participation is entirely voluntary. You may refuse to participate or withdraw at any time without needing to provide a reason, and doing so will have no negative consequences for you.  
            
            ###  This study is designed to contribute to academic research by exploring a novel type of psychological assessment using advanced AI technology.  
            
            ###  Please read the following information carefully before deciding whether to participate. You may proceed only after acknowledging that you understand the study and your rights as a participant.  
            
            ---
            
            ## **Purpose of the Study**  
            ### This study aims to evaluate whether an AI-based psychological assessment can provide insights into cognitive patterns comparable to traditional methods.
            
            ---
            
            ## **Study Procedure**  
            ### - The study consists of three online sessions. Each session lasts approximately 20–30 minutes. The assessment will be conducted over the course of one week with three days in between each session. You must complete all three sessions to receive full compensation.  
            ### - You will reflect on various scenarios and provide feedback on your experience.  
            
            ---
            
            ## **Potential Benefits**  
            ### While there are no direct personal benefits, your participation will contribute to research that could help improve psychological diagnostic tools using AI.  
            
            ---
            
            ## **Potential Risks**  
            ### - The study presents minimal risks.  
            ### - Some scenarios may ask you to reflect on personal responsibility, which could cause minor emotional discomfort.  
            ### - If you feel uncomfortable, you can withdraw at any time.  
            
            ## **Eligibility Criteria:**  
            ### - Participants must be 18 or older, fluent in English, and score below the clinical threshold on pre-screening mental health questionnaires.  
            ### - Participants with diagnosed mental health disorders or high scores on these questionnaires are excluded.  
            
            ---
            
            ## **Data Handling and Privacy**  
            ### - Your data will be anonymised and used only for statistical purposes.  
            ### - No personal identifying information will be collected.  
            ### - The data will be securely stored and accessed only by authorised study personnel.
            
            ---
            
            ## **Reimbursement**  
            ### - You will not incur any costs for participating.  
            ### - Compensation will be provided through Prolific based on the time spent participating.
            ### - You will receive a partial completion code after each session, but full payment will only be processed upon completing all three sessions.
            
            ---
            
            ## **Withdrawal and Contact Information**  
            ### - You may withdraw at any time without providing a reason.  
            ### - If you experience any discomfort or have questions about the study, please contact the coordinators:  
            ####  - **Nace Mikus**: nace.mikus@univie.ac.at  
            ####  - **Jakob Möller**: a12028569@unet.univie.ac.at  
            
            ---
            
            ## **Acknowledgment**  
            ### By clicking "I Acknowledge and Consent" you confirm that:  
            ### - You have read and understood the information provided.  
            ### - You are aware of your right to withdraw at any time without consequences.  
            ### - You agree to participate in the study and for your anonymised data to be used for academic research.
            ---
            ### Please enter your Prolific ID before moving on:
            """)
            pid_in = gr.Textbox(label="Prolific ID (required)")
            pid_out = gr.Markdown()
            pid_button = gr.Button("I Acknowledge and Consent")

    ############################################################################
    # Page 2: PHQ-9
    ############################################################################
    with gr.Row(visible=False) as page_2:
        with gr.Column():
            gr.Markdown("""#### Over the last 2 weeks, how often have you been bothered by 
            #### any of the following problems (0 = "Never", 3 = "Very often")?""")
            q1 = gr.Radio(["0","1","2","3"], label="Little interest or pleasure in doing things?")
            q2 = gr.Radio(["0","1","2","3"], label="Feeling down, depressed, or hopeless?")
            q3 = gr.Radio(["0","1","2","3"], label="Trouble falling or staying asleep?")
            q4 = gr.Radio(["0","1","2","3"], label="Feeling tired or little energy?")
            q5 = gr.Radio(["0","1","2","3"], label="Poor appetite or overeating?")
            q6 = gr.Radio(["0","1","2","3"], label="Feeling bad about yourself?")
            q7 = gr.Radio(["0","1","2","3"], label="Trouble concentrating on things?")
            q8 = gr.Radio(["0","1","2","3"], label="Moving or speaking slowly or fidgety?")
            phq_out = gr.Markdown()
            phq_next = gr.Button("Next")


    ############################################################################
    # Page 3: GAD-7
    ############################################################################
    with gr.Row(visible=False) as page_3:
        with gr.Column():
            gr.Markdown("""#### Over the last 2 weeks, how often have you been bothered by 
            #### any of the following problems (0 = "Never", 3 = "Very often")?""")
            g1 = gr.Radio(["0","1","2","3"], label="Feeling nervous, anxious, on edge?")
            g2 = gr.Radio(["0","1","2","3"], label="Not being able to stop worrying?")
            g3 = gr.Radio(["0","1","2","3"], label="Worrying too much about different things?")
            g4 = gr.Radio(["0","1","2","3"], label="Trouble relaxing?")
            g5 = gr.Radio(["0","1","2","3"], label="Being so restless it's hard to sit still?")
            g6 = gr.Radio(["0","1","2","3"], label="Becoming easily annoyed or irritable?")
            g7 = gr.Radio(["0","1","2","3"], label="Feeling afraid something awful might happen?")
            gad_out = gr.Markdown()
            gad_next = gr.Button("Next")


    ############################################################################
    # Page 4: ASQ items
    ############################################################################
    with gr.Row(visible=False) as page_4:
        with gr.Column():
            item_instructions = gr.Markdown("", elem_id="scenario_instructions")
            cause_box = gr.Textbox(label="Write down the major cause:")
            internal_slider = gr.Slider(1,10,step=0.1,value=1,label="Is the cause due to something about you or something about other people or circumstances? (1 = Totally due to me, 10 = Totally due to other people)")
            stable_slider = gr.Slider(1,10,step=0.1,value=1,label="In the future, will this cause again be present? (1 = Will never be present, 10 = Will always be present)")
            global_slider = gr.Slider(1,10,step=0.1,value=1,label="Is the cause something that just affects this situation or does it also influence other areas of your life? (1 = Influences just this situation, 10 = Influences all situations)")
            emotional_relevance = gr.Slider(1,10,step=0.1,value=1,label="How much emotional impact did this scenario have on you? (1 = None, 10 = A lot)")
            positive_valence = gr.Slider(1,10,step=0.1,value=1,label="How much joy, excitement, or satisfaction did this scenario evoke in you? (1 = None, 10 = A lot)")
            negative_valence = gr.Slider(1,10,step=0.1,value=1,label="How much discomfort or tension did you feel while imagining this scenario? (1 = None, 10 = A lot)")

            attention_check_slider = gr.Slider(
                minimum=1, maximum=10, step=1, value=1,
                label="ATTENTION CHECK: Please select 10 here (option on the very right) to indicate you're paying attention.",
                visible=False
            )          
            
            asq_out = gr.Markdown()
            asq_button = gr.Button("Next Scenario")
            # On first load, show item 1
            item_instructions.value = show_asq_item(user_state.value)


    ############################################################################
    # Page 5: Usability
    ############################################################################
    with gr.Row(visible=False) as page_5:
        with gr.Column():
            gr.Markdown("## Usability Questions")
            ease_box = gr.Slider(1,10,step=0.1,value=1,label="How easy was it to complete the assessment? (1 = Not at all, 10 = Very much)")
            clarity_box = gr.Slider(1,10,step=0.1,value=1,label="How clear were the instructions? (1 = Not at all, 10 = Very much)")
            engagement_box = gr.Slider(1,10,step=0.1,value=1,label="How engaging was the assessment experience? (1 = Not at all, 10 = Very much)")
            satisfaction_box = gr.Slider(1,10,step=0.1,value=1,label="How satisfied are you with this assessment? (1 = Not at all, 10 = Very much)")
            cogload_box = gr.Slider(1,10,step=0.1,value=1,label="How mentally demanding was the assessment? (1 = Not at all, 10 = Very much)")
            comments_box = gr.Textbox(label="Additional Comments", placeholder="Any further comments or suggestions?", lines=2)
            
            usability_out = gr.Markdown()
            usability_button = gr.Button("Finish")

            usability_button.click(
                fn=submit_usability,
                inputs=[ease_box, clarity_box, engagement_box, satisfaction_box, cogload_box, comments_box, user_state],
                outputs=[usability_out, user_state]
            )

    
    # Transition Logic #########################################################
    pid_button.click(
        fn=store_pid,
        inputs=[pid_in, user_state],
        outputs=[pid_out, user_state, page_1, page_2]
    )

    phq_next.click(
        fn=process_phq9,
        inputs=[q1,q2,q3,q4,q5,q6,q7,q8, user_state],
        outputs=[phq_out, user_state, page_2, page_3]
    )  

    gad_next.click(
        fn=process_gad7,
        inputs=[g1,g2,g3,g4,g5,g6,g7, user_state],
        outputs=[gad_out, user_state, page_3, page_4]
    )    

    asq_button.click(
        fn=submit_asq_item,
        inputs=[
            cause_box,
            internal_slider,
            stable_slider,
            global_slider,
            emotional_relevance,
            positive_valence,
            negative_valence,
            user_state,
            attention_check_slider
        ],
        outputs=[
            item_instructions,
            user_state,
            page_4,
            page_5,
            cause_box,
            internal_slider,
            stable_slider,
            global_slider,
            emotional_relevance,
            positive_valence,
            negative_valence,
            attention_check_slider
        ]
    )


cond3_app.launch()