File size: 28,171 Bytes
bf75d52
3603153
bf75d52
c20f0de
a425fa9
895a686
8861533
c20f0de
d079c5e
895a686
61aa64b
 
 
 
 
 
 
 
c20f0de
 
 
 
d248797
 
db8fc2d
c20f0de
 
 
 
fdd4e85
c20f0de
 
1991ca2
c20f0de
e079737
 
c20f0de
 
e079737
c20f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e079737
c20f0de
 
 
 
 
 
 
 
 
 
 
 
 
 
8861533
c20f0de
 
 
8861533
c20f0de
 
 
 
 
 
 
 
 
 
 
fdd4e85
 
 
 
 
 
 
d503b50
 
df99dda
 
8861533
d503b50
 
 
8861533
a425fa9
61aa64b
 
711dc9d
 
 
 
 
 
 
 
 
d919aa0
bf75d52
711dc9d
61aa64b
 
 
 
 
 
 
 
 
24df3fa
61aa64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed092c2
61aa64b
ed092c2
187d915
07e7d16
61aa64b
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea5a5c
61aa64b
 
7ea5a5c
61aa64b
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea5a5c
61aa64b
 
 
 
7ea5a5c
30363c9
61aa64b
 
7ea5a5c
30363c9
61aa64b
 
d919aa0
22f31fd
560782b
 
 
d919aa0
d5c3c24
919be7b
d919aa0
 
5053d22
d919aa0
8c58af0
 
 
31d31bd
8c58af0
 
287ff70
8f41051
80eb545
8c58af0
 
 
 
f4facc1
8c58af0
5053d22
 
f4facc1
d503b50
dc4c04d
fb39ca3
fcda2fa
fb39ca3
fcda2fa
fb39ca3
fcda2fa
e525095
d5c3c24
e525095
 
 
 
 
 
f4facc1
 
d460b8a
fcda2fa
d460b8a
fcda2fa
d201c51
 
43d4f11
81eb5b7
8861533
ec65889
8861533
81eb5b7
260fa64
66cf1e2
6ee07a7
e7103e4
2b0e5c7
e7103e4
 
796ea23
e7103e4
d201c51
9b16238
 
d5c3c24
 
61aa64b
81eb5b7
2a92868
ec65889
796ea23
 
 
 
f583e02
81eb5b7
9b16238
d5c3c24
 
61aa64b
81eb5b7
2a92868
ec65889
796ea23
d201c51
43d4f11
81eb5b7
8861533
81eb5b7
22f31fd
8861533
db8fc2d
ec65889
260fa64
d079c5e
 
6ee07a7
fcda2fa
61aa64b
81eb5b7
16cedf8
ec65889
 
260fa64
 
81eb5b7
22f31fd
61aa64b
22f31fd
8861533
 
22f31fd
61aa64b
75e0da4
61aa64b
505427d
fcda2fa
 
22f31fd
61aa64b
22f31fd
81eb5b7
8861533
db8fc2d
ec65889
260fa64
7493d62
f583e02
6ee07a7
505427d
fcda2fa
61aa64b
81eb5b7
16cedf8
ec65889
 
260fa64
81eb5b7
8861533
ec65889
260fa64
d079c5e
 
6ee07a7
505427d
fcda2fa
61aa64b
81eb5b7
16cedf8
ec65889
 
6c36dd2
8861533
 
ec65889
 
 
187d915
a9f88a1
6c36dd2
22f31fd
a31c72c
fcda2fa
61aa64b
81eb5b7
16cedf8
ec65889
43d4f11
 
260fa64
8861533
43d4f11
 
d919aa0
 
8ac2e2e
 
 
7b539ba
 
 
 
 
8ac2e2e
0bc57b9
95f47b4
 
9815e83
4072549
0bc57b9
 
4072549
 
0bc57b9
95f47b4
 
8208240
4072549
62362ae
0bc57b9
 
4072549
 
0bc57b9
95f47b4
 
9815e83
4072549
0bc57b9
 
4072549
 
0bc57b9
95f47b4
 
9815e83
4072549
8ac2e2e
 
4072549
 
8ac2e2e
 
4072549
0c16b4c
 
 
5338729
cadf427
8e8e412
db8fc2d
22f31fd
 
 
 
 
 
 
8e8e412
 
22f31fd
 
db8fc2d
 
22f31fd
8e8e412
 
95cc5b2
 
 
 
0d8264c
8e8e412
 
 
 
 
350138b
 
8e8e412
350138b
 
 
 
 
 
 
 
 
 
8e8e412
 
 
db8fc2d
8ac2e2e
8e8e412
7493d62
8ac2e2e
 
 
0d8264c
dc4c04d
a73219f
f14dfb1
 
 
 
 
 
 
8ac2e2e
 
7aceb13
8c58af0
a9f88a1
280d303
 
8ac2e2e
 
8c58af0
 
 
8062ecd
db8fc2d
8c58af0
 
8062ecd
8c58af0
 
 
 
 
 
 
07eb932
8c48d51
 
 
 
 
7aceb13
280d303
30363c9
 
8ac2e2e
30363c9
d919aa0
 
89443f0
a9fe74f
 
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
import gradio as gr
import os

# PERSISTENT DATA STORAGE: this code is used to make commits 

import json
from huggingface_hub import hf_hub_download, file_exists, HfApi
from random import shuffle
from markdown import markdown

# Read-only reference variables
qIDs = ["mbe_46", "mbe_132", "mbe_287", "mbe_326", "mbe_334", "mbe_389", "mbe_563", "mbe_614", "mbe_642", "mbe_747", "mbe_779", "mbe_826", "mbe_845", "mbe_1042", "mbe_1134"]
mode_options = ["e5", "colbert"]
with open("question_data.json", "r") as f:
    all_questions = json.load(f)

"""
# State variables which interact with loading and unloading
user_data = {}
current_response = {}
current_question = {} # read-only within gradio blocks
user_id = "no_id"
# Control global variables
step = 0
mode = 1

def load_user_data(id):
    global user_data
    filename = id.replace('@', '_AT_').replace('.', '_DOT_')
    if file_exists(filename = "users/" + filename + ".json", repo_id = "ebrowne/test-data", repo_type = "dataset", token = os.getenv("HF_TOKEN")):
        print("File exists, downloading data.")
        # If the ID exists, download the file from HuggingFace
        path = hf_hub_download(repo_id = "ebrowne/test-data", token = os.getenv("HF_TOKEN"), filename = "users/" + filename + ".json", repo_type = "dataset")
        # Add their current status to user_data
        with open(path, "r") as f:
            user_data = json.load(f)
    else:
        # If the ID doesn't exist, create a format for the file and upload it to HuggingFace
        print("File does not exist, creating user.")
        shuffle(qIDs)
        modes = []
        for i in range(len(qIDs)): 
            temp = mode_options[:]
            shuffle(temp)
            modes.append(temp)
        # This is the format for a user's file on HuggingFace
        user_data = {
            "user_id": id, # original in email format, which was passed here
            "order": qIDs, # randomized order for each user
            "modes": modes, # randomized order for each user
            "current": 0, # user starts on first question
            "responses": [] # formatted as a list of current_responses
        }
        # Run the update method to upload the new JSON file to HuggingFace
        update_huggingface(id)

def update_huggingface(id):
    global user_data
    print("Updating data...")
    filename = id.replace('@', '_AT_').replace('.', '_DOT_')
    # Create a local file that will be uploaded to HuggingFace
    with open(filename + ".json", "w") as f:
        json.dump(user_data, f)
    # Upload to hub (overwriting existing files...)
    api = HfApi()
    api.upload_file(
        path_or_fileobj=filename + ".json",
        path_in_repo="users/" + filename + ".json",
        repo_id="ebrowne/test-data",
        repo_type="dataset",
        token = os.getenv("HF_TOKEN")
    )

def reset_current_response(qid):
    global current_response
    current_response = {
        "user_id": user_id, 
        "question_id": qid,
        "user_answer": 0, 
        "e5_scores": [], # list of ten [score, score, score, score]
        "e5_set": [], # two values
        "e5_generation": [], # two values
        "colbert_scores": [], 
        "colbert_set": [],
        "colbert_generation": [],
        "gold_set": [],
        "gold_generation": []
    }

with open("question_data.json", "r") as f:
    all_questions = json.load(f)

# Loads the user's current question — this is the first question that the user has not made any progress on.
def load_current_question():
    global current_question 
    q_index = user_data["current"]
    if q_index >= len(all_questions):
        print("Done")
        gr.Info("You've finished — thank you so much! There are no more questions. :)")
        current_question = {"question": "You're done! Thanks so much for your help.", "answers": ["I want to log out now.", "I want to keep answering questions.","I want to keep answering questions.", "I want to keep answering questions."], "correct_answer_index": 0, "top10_e5": ["You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!"], "generation_e5": "I don't know how to exit this code right now, so you're in an endless loop of this question until you quit.", "top10_colbert": ["You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!"], "generation_colbert": "I don't know how to exit this code right now, so you're in an endless loop of this question until you quit.", "top10_contains_gold_passage": False, "gold_passage": "GOLD PASSAGE: LOG OFF!", "gold_passage_generation": "what do you gain"}
        reset_current_response("USER FINISHED")
    else:
        qid = user_data["order"][q_index]
        current_question = all_questions[qid]
        reset_current_response(user_data["order"][q_index])

"""

# THEMING: colors and styles (Gradio native)

theme = gr.themes.Soft(
    primary_hue="sky",
    secondary_hue="sky",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont('Inter'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
)

# BLOCKS: main user interface

with gr.Blocks(theme = theme) as user_eval:
    # ALL VARIABLES AND LOADING

    # State variables which interact with loading and unloading
    user_data = gr.State({})
    current_response = gr.State({})
    current_question = gr.State({}) # read-only within gradio blocks
    user_id = gr.State("no_id")
    # Control global variables
    step = gr.State(0)
    mode = gr.State(1) # mode is always 1 for now

    def load_user_data(id):
        filename = id.replace('@', '_AT_').replace('.', '_DOT_')
        if file_exists(filename = "users/" + filename + ".json", repo_id = "ebrowne/test-data", repo_type = "dataset", token = os.getenv("HF_TOKEN")):
            print("File exists, downloading data.")
            # If the ID exists, download the file from HuggingFace
            path = hf_hub_download(repo_id = "ebrowne/test-data", token = os.getenv("HF_TOKEN"), filename = "users/" + filename + ".json", repo_type = "dataset")
            # Add their current status to user_data
            with open(path, "r") as f:
                return json.load(f)
        else:
            # If the ID doesn't exist, create a format for the file and upload it to HuggingFace
            print("File does not exist, creating user.")
            shuffle(qIDs)
            modes = []
            for i in range(len(qIDs)): 
                temp = mode_options[:]
                shuffle(temp)
                modes.append(temp)
            # This is the format for a user's file on HuggingFace
            return {
                "user_id": id, # original in email format, which was passed here
                "order": qIDs, # randomized order for each user
                "modes": modes, # randomized order for each user
                "current": 0, # user starts on first question
                "responses": [] # formatted as a list of current_responses
            }
            # No longer uploading after first creation: user must answer question for that.
    
    def update_huggingface(data):
        print("Updating data...")
        id = data["user_id"]
        print(id)
        print(data)
        filename = id.replace('@', '_AT_').replace('.', '_DOT_')
        # Create a local file that will be uploaded to HuggingFace
        with open(filename + ".json", "w") as f:
            json.dump(data, f)
        # Upload to hub (overwriting existing files...)
        api = HfApi()
        api.upload_file(
            path_or_fileobj=filename + ".json",
            path_in_repo="users/" + filename + ".json",
            repo_id="ebrowne/test-data",
            repo_type="dataset",
            token = os.getenv("HF_TOKEN")
        )
    
    def reset_current_response(qid, uid):
        return {
            current_response : {
                "user_id": uid, 
                "question_id": qid,
                "user_answer": 0, 
                "e5_scores": [], # list of ten [score, score, score, score]
                "e5_set": [], # two values
                "e5_generation": [], # two values
                "colbert_scores": [], 
                "colbert_set": [],
                "colbert_generation": [],
                "gold_set": [],
                "gold_generation": []
            }
        }

    # Loads the user's current question — this is the first question that the user has not made any progress on.
    def load_current_question(user_data):
        q_index = user_data["current"]
        if q_index >= len(all_questions):
            print("Done")
            gr.Info("You've finished — thank you so much! There are no more questions. :)")
            new_response = reset_current_response("USER FINISHED", user_data["user_id"])
            return {"question": "You're done! Thanks so much for your help.", "answers": ["I want to log out now.", "I want to keep answering questions.","I want to keep answering questions.", "I want to keep answering questions."], "correct_answer_index": 0, "top10_e5": ["You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!"], "generation_e5": "I don't know how to exit this code right now, so you're in an endless loop of this question until you quit.", "top10_colbert": ["You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!", "You're done; thank you!"], "generation_colbert": "I don't know how to exit this code right now, so you're in an endless loop of this question until you quit.", "top10_contains_gold_passage": False, "gold_passage": "GOLD PASSAGE: LOG OFF!", "gold_passage_generation": "what do you gain"}, new_response
        else:
            qid = user_data["order"][q_index]
            new_response = reset_current_response(user_data["order"][q_index], user_data["user_id"])
            return all_questions[qid], new_response
            

    # Title text introducing study
    forward_btn = gr.Textbox("unchanged", visible = False, elem_id = "togglebutton") # used for toggling windows
    gr.HTML("""
    <h1> Legal Retriever Evaluation Study </h1>
    <p> Score the passages based on the question and provided answer choices. Detailed instructions are found <a href="https://docs.google.com/document/d/1ReODJ0hlXz_M3kE2UG1cwSRVoyDLQo88OvG71Gt8lUQ/edit?usp=sharing" target="_blank">here</a>. </p> 
    """)
    gr.Markdown("---")

    # Passages and user evaluations thereof
    with gr.Row(equal_height = False, visible = False) as evals:
        # Passage text
        with gr.Column(scale = 2) as passages:
            selection = gr.HTML()
            """
            selection = gr.HTML("
            <h2> Retrieved Passage </h2>
            <p> " + current_question["top10_" + user_data["modes"][user_data["current"]][mode]][0] + "</p>")
            """
            print(step)
            line = gr.Markdown("---")
            # New answers is able to render the Q and A with formatting. It doesn't change the contents of the answers.
            # new_answers = current_question["answers"].copy()
            # new_answers[current_question["correct_answer_index"]] = "**" + current_question["answers"][current_question["correct_answer_index"]] + "** ✅"
            passage_display = gr.Markdown() 
            temp = """
            ## Question and Answer
            """

        # Scoring box
        with gr.Column(scale = 1) as scores_p:
            desc_0 = gr.Markdown("Does the passage describe **a legal rule or principle?**")
            eval_0 = gr.Radio(["Yes", "No"], label = "Legal Rule?")
            desc_1 = gr.Markdown("How **relevant** is this passage to the question?")
            eval_1 = gr.Slider(1, 5, step = 0.5, label = "Relevance", value = 3)
            desc_2 = gr.Markdown("How would you rate the passage's **quality** in terms of detail, clarity, and focus?")
            eval_2 = gr.Slider(1, 5, step = 0.5, label = "Quality", value = 3)
            desc_3 = gr.Markdown("How effectively does the passage **lead you to the correct answer?**")
            eval_3 = gr.Slider(-2, 2, step = 0.5, label = "Helpfulness", value = 0)
            btn_p = gr.Button("Next", interactive = False)
            # Users must enter in a yes/no value before moving on in the radio area
            def sanitize_score(rad):
                if rad == None:
                    return {btn_p: gr.Button(interactive = False)}
                else:
                    return {btn_p: gr.Button(interactive = True)}
            eval_0.change(fn = sanitize_score, inputs = [eval_0], outputs = [btn_p])
        
        with gr.Column(scale = 1, visible = False) as scores_g: 
            helps = gr.Markdown("Does this information **help answer** the question?")
            eval_helps = gr.Slider(-2, 2, step = 0.5, label = "Helpfulness", value = 0)
            satisfied = gr.Markdown("How **satisfied** are you by this answer?")
            eval_satisfied = gr.Slider(1, 5, step = 0.5, label = "User Satisfaction", value = 3)
            btn_g = gr.Button("Next")
        
        def next_p(e0, e1, e2, e3, cur_step, mode, cr, cq):
            cur_step += 1
            # Add user data to the current response
            cr["e5_scores"].append([e0, e1, e2, e3])
            # Next item
            if cur_step >= len(cq["top10_e5"]): # should always be 10 (DEBUG: >= to avoid out of bounds)
                # Step 10: all sources 
                collapsible_string = "<h2> Set of Passages </h2>\n"
                for i, passage in enumerate(cq["top10_e5"]):
                    collapsible_string += """
                            <strong>Passage """ + str(i + 1) + """</strong>
                            <p> """ + passage + """ </p>
                            """
                return {
                    selection: gr.HTML(collapsible_string),
                    scores_p: gr.Column(visible = False),
                    scores_g: gr.Column(visible = True),
                    eval_0: gr.Radio(value = None),
                    eval_1: gr.Slider(value = 3),
                    eval_2: gr.Slider(value = 3),
                    eval_3: gr.Slider(value = 0),
                    step: cur_step,
                    # mode: 1,
                    current_response: cr
                }
            else:
                return {
                    selection: gr.HTML("""
                        <h2> Retrieved Passage </h2> 
                        <p> """ + cq["top10_e5"][cur_step] + "</p>"),
                    eval_0: gr.Radio(value = None),
                    eval_1: gr.Slider(value = 3),
                    eval_2: gr.Slider(value = 3),
                    eval_3: gr.Slider(value = 0),
                    step: cur_step,
                    # mode: 1,
                    current_response: cr
                }
        
        def next_g(e_h, e_s, cur_step, mode, ud, cr, cq): 
            cur_step += 1
            
            if cur_step == 11:
                # Step 11: guaranteed to be generation
                # Add user data to the current response as SET evaluation, which comes before the generation
                # CHANGED FROM user_data["modes"][user_data["current"]] + "_set", as are all direct references to top10_e5
                cr["e5_set"] = [e_h, e_s]
                return {
                    selection: gr.HTML("""
                        <h2> Autogenerated Response </h2>
                        <p>""" + markdown(cq["generation_e5"]) + "</p>"),
                    eval_helps: gr.Slider(value = 0),
                    eval_satisfied: gr.Slider(value = 3),
                    step: cur_step,
                    # mode: mode,
                    user_data: ud,
                    current_response: cr
                }
            # Steps 12 and 13 are gold passage + gold passage generation IF it is applicable
            if cur_step > 11: # and not current_question["top10_contains_gold_passage"]
                # When mode is 0 -> reset with mode = 1
                """
                if mode == 0:
                    # The user just evaluated a generation for mode 0
                    current_response[user_data["modes"][user_data["current"]][mode] + "_generation"] = [e_h, e_s]
                    return {
                        selection: gr.HTML(\"""
                                <h2> Retrieved Passage </h2>
                                <p> \""" + current_question["top10_" + user_data["modes"][user_data["current"]][1]][0] + "</p>"), # hard coded: first passage (0) of mode 2 (1),
                        forward_btn: gr.Textbox("load new data"),
                        eval_helps: gr.Slider(value = 0),
                        eval_satisfied: gr.Slider(value = 3)
                    }
                """
                # When mode is 1 -> display GP and GP generation, then switch
                if cur_step == 12: 
                    # The user just evaluated a generation for mode 1
                    # CHANGE from user_data["modes"][user_data["current"]] + "_generation"
                    cr["e5_generation"] = [e_h, e_s]
                    return {
                        selection: gr.HTML("""
                            <h2> Retrieved Passage </h2> 
                            <p> """ + cq["gold_passage"] + "</p>"),
                        forward_btn: gr.Textbox(),
                        eval_helps: gr.Slider(value = 0),
                        eval_satisfied: gr.Slider(value = 3),
                        step: cur_step,
                        # mode: mode,
                        user_data: ud,
                        current_response: cr
                    }
                elif cur_step == 13: 
                   # The user just evaluated the gold passage
                   cr["gold_set"] = [e_h, e_s]
                   return {
                        selection: gr.HTML("""
                            <h2> Autogenerated Response </h2>
                            <p> """ + markdown(cq["gold_passage_generation"]) + "</p>"),
                        forward_btn: gr.Textbox(),
                        eval_helps: gr.Slider(value = 0),
                        eval_satisfied: gr.Slider(value = 3),
                        step: cur_step,
                        # mode: mode,
                        user_data: ud,
                        current_response: cr
                   }
                else: # step = 14
                    # The user just evaluated the gold passage generation
                    cr["gold_generation"] = [e_h, e_s]
                    ud["current"] += 1
                    ud["responses"].append(cr) # adds new answers to current list of responses
                    update_huggingface(ud) # persistence — update progress online, save answers
                    cq, cr = load_current_question(ud)
                    return {
                        selection: gr.Markdown("Advancing to the next question..."),
                        forward_btn: gr.Textbox("changed" + str(ud["current"])), # current forces event to trigger always
                        eval_helps: gr.Slider(value = 0),
                        eval_satisfied: gr.Slider(value = 3),
                        step: 0,
                        # mode: mode,
                        user_data: ud,
                        current_response: cr,
                        current_question: cq
                    }
            
        btn_p.click(fn = next_p, inputs = [eval_0, eval_1, eval_2, eval_3, step, mode, current_response, current_question], outputs = [selection, scores_p, scores_g, eval_0, eval_1, eval_2, eval_3, step, mode, current_response])
        btn_g.click(fn = next_g, inputs = [eval_helps, eval_satisfied, step, mode, user_data, current_response, current_question], outputs = [selection, forward_btn, eval_helps, eval_satisfied, step, mode, user_data, current_response, current_question])

    # Question and answering dynamics
    with gr.Row(equal_height = False, visible = False) as question:
        with gr.Column():
            gr.Markdown("**Question**")
            q_text = gr.Markdown("Question")
            a = gr.Button("A")
            b = gr.Button("B")
            c = gr.Button("C")
            d = gr.Button("D")

            # I know this is inefficient...
            def answer_a(cr):
                # cr = current_response
                cr = list(cr.values())[0]
                cr["user_answer"] = 0
                return {
                    question: gr.Row(visible = False),
                    evals: gr.Row(visible = True),
                    current_response: cr
                }
            def answer_b(cr):
                # cr = current_response
                cr = list(cr.values())[0]
                cr["user_answer"] = 1
                print(cr)
                return {
                    question: gr.Row(visible = False),
                    evals: gr.Row(visible = True),
                    current_response: cr
                }
            def answer_c(cr):
                # cr = current_response
                cr = list(cr.values())[0]
                cr["user_answer"] = 2
                return {
                    question: gr.Row(visible = False),
                    evals: gr.Row(visible = True),
                    current_response: cr
                }
            def answer_d(cr):
                # cr = current_response
                cr = list(cr.values())[0]
                cr["user_answer"] = 3
                return {
                    question: gr.Row(visible = False),
                    evals: gr.Row(visible = True),
                    current_response: cr
                }
            
            a.click(fn = answer_a, inputs = [current_response], outputs = [question, evals, current_response])
            b.click(fn = answer_b, inputs = [current_response], outputs = [question, evals, current_response])
            c.click(fn = answer_c, inputs = [current_response], outputs = [question, evals, current_response])
            d.click(fn = answer_d, inputs = [current_response], outputs = [question, evals, current_response])
    
    def toggle(s, mode, cq): # s was step...
        # step = 0
        if mode == 0: # temporarily disabled — will never be mode 0
            mode = 1 # update mode to 1, will restart with same Q, next set of Ps
            print("Next set of passages for same question")
            return {
                scores_p: gr.Column(visible = True),
                scores_g: gr.Column(visible = False),
                evals: gr.Row(visible = True),
                question: gr.Row(visible = False),
                # step: step,
                # mode: mode
            }
        else:
            # reset mode to 0, will restart with new Q (set up new Q), first set of Ps
            # mode = 0 -> RIGHT NOW, ALWAYS 1
            print("New question")
            new_answers = cq["answers"].copy()
            new_answers[cq["correct_answer_index"]] = "**" + cq["answers"][cq["correct_answer_index"]] + "** ✅"
            return {
                scores_p: gr.Column(visible = True),
                scores_g: gr.Column(visible = False),
                evals: gr.Row(visible = False),
                question: gr.Row(visible = True),
                q_text: gr.Markdown(cq["question"]),
                a: gr.Button(cq["answers"][0]),
                b: gr.Button(cq["answers"][1]),
                c: gr.Button(cq["answers"][2]),
                d: gr.Button(cq["answers"][3]),
                passage_display: gr.Markdown("""
                        ## Question and Answer
                        *""" + cq["question"] + 
                        """* \n
                        + """ + new_answers[0] + 
                        """ \n
                        + """ + new_answers[1] + 
                        """ \n
                        + """ + new_answers[2] + 
                        """ \n
                        + """ + new_answers[3]),
                selection: gr.HTML("""
                    <h2> Retrieved Passage </h2>
                    <p> """ + cq["top10_e5"][0] + "</p>"),
                step: 0,
                # mode: mode
            }                                   # note change from "top10_" + user_data["modes"][user_data["current"]][mode]][0]

    forward_btn.change(fn = toggle, inputs = [step, mode, current_question], outputs = [scores_p, scores_g, evals, question, q_text, a, b, c, d, passage_display, selection, step, mode])

    with gr.Row() as login:
        with gr.Column():
            gr.Markdown("# Enter email to start")
            gr.Markdown("Thank you so much for your participation in our study! We're using emails to keep track of which questions you've answered and which you haven't seen. Use the same email every time to keep your progress saved. :)")
            email = gr.Textbox(label = "Email", placeholder = "[email protected]")
            s = gr.Button("Start!", interactive = False)
            
            def sanitize_login(text):
                if text == "":
                    return {s: gr.Button(interactive = False)}
                else:
                    return {s: gr.Button(interactive = True)}
            email.change(fn = sanitize_login, inputs = [email], outputs = [s])
      
            def submit_email(email):
                loaded_data = load_user_data(email) # calls login, downloads data, initializes session
                # After loading user data, update with current question
                new_q, new_r = load_current_question(loaded_data)
                new_answers = new_q["answers"].copy()
                new_answers[new_q["correct_answer_index"]] = "**" + new_q["answers"][new_q["correct_answer_index"]] + "** ✅"
                return {
                    question: gr.Row(visible = True),
                    login: gr.Row(visible = False),
                    selection: gr.HTML("""
                    <h2> Retrieved Passage </h2>
                    <p> """ + new_q["top10_e5"][0] + "</p>"),
                                            # when testing both ColBERT and E5, this was "top10_" + user_data["modes"][user_data["current"]][mode]][0]
                    passage_display: gr.Markdown("""
                        ## Question and Answer
                        *""" + new_q["question"] + 
                        """* \n
                        + """ + new_answers[0] + 
                        """ \n
                        + """ + new_answers[1] + 
                        """ \n
                        + """ + new_answers[2] + 
                        """ \n
                        + """ + new_answers[3]),
                    q_text: gr.Markdown(new_q["question"]),
                    a: gr.Button(new_q["answers"][0]),
                    b: gr.Button(new_q["answers"][1]),
                    c: gr.Button(new_q["answers"][2]),
                    d: gr.Button(new_q["answers"][3]),
                    user_id: email,
                    user_data: loaded_data,
                    current_question: new_q,
                    current_response: new_r
                }
            s.click(fn = submit_email, inputs = [email], outputs = [question, login, selection, passage_display, q_text, a, b, c, d, user_id, user_data, current_question, current_response])

# Starts on question, switches to evaluation after the user answers
user_eval.launch()

# https://github.com/gradio-app/gradio/issues/5791