Spaces:
iyosha
/
Running on CPU Upgrade

File size: 18,048 Bytes
d4b26c5
 
38c5e59
 
 
 
8c04306
f20f020
 
 
38c5e59
efb69be
38c5e59
d4b26c5
3bc4341
 
d4b26c5
 
3bc4341
38c5e59
 
 
c889730
38c5e59
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
 
 
d4b26c5
 
38c5e59
 
 
d4b26c5
 
38c5e59
d4b26c5
efb69be
38c5e59
d4b26c5
38c5e59
 
 
 
 
 
d4b26c5
38c5e59
 
d4b26c5
 
 
 
 
 
 
 
38c5e59
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
d4b26c5
 
 
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
d4b26c5
afa89f5
38c5e59
 
 
 
 
 
d4b26c5
 
 
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b26c5
38c5e59
 
 
 
 
 
 
 
 
 
d4b26c5
 
 
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4b26c5
 
 
 
 
 
 
 
38c5e59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c04306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c5e59
 
 
 
 
 
 
 
d4b26c5
38c5e59
8c04306
38c5e59
 
 
 
 
 
 
 
8c04306
38c5e59
 
 
 
 
 
8c04306
38c5e59
 
 
8c04306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c5e59
8c04306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c5e59
 
8c04306
 
 
 
 
 
 
 
 
 
efb69be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c04306
 
 
 
efb69be
 
 
 
 
8c04306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c5e59
 
 
 
 
 
 
 
 
 
 
83acfd0
c889730
098df75
83acfd0
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
import json
from pathlib import Path
import gradio as gr
from uuid import uuid4
from datasets import load_dataset
from collections import Counter
import numpy as np
from configs import configs
from clients import backend, logger
from backend.helpers import get_random_session_samples

dataset = load_dataset("iyosha-huji/stressEval", token=configs.HF_API_TOKEN)["test"]

INSTRUCTIONS = """<div align='center'>You are given an audio sample and a question with 2 answer options.\n\nListen to the audio and select the correct answer from the options below.\n\n<b>Note:</b> The question is the same for all samples, but the audio and the corresponding answers change.</div>"""


with open(Path(__file__).parent / "data/stage_indices.json") as f:
    STAGE_SPLITS = json.load(f)


def human_eval_tab():
    with gr.Tab(label="Evaluation"):
        # ==== State =====
        i = gr.State(-1)
        selected_answer = gr.State(None)
        answers_dict = gr.State({})
        logged_in = gr.State(False)
        session_id = gr.State(None)
        user_name = gr.State(None)
        session_sample_indices = gr.State([])

        # === Login UI ===
        with gr.Group(visible=True) as login_group:
            gr.Markdown("### πŸ” Login to Continue")
            with gr.Row():
                username = gr.Text(label="Username", placeholder="Enter username")
                password = gr.Text(
                    label="Password", type="password", placeholder="Enter password"
                )
            login_error = gr.Markdown(
                "\u274c Incorrect login, try again. Enter username and password.",
                visible=False,
            )
            login_btn = gr.Button("Login")

        def login(usr, p):
            if p == configs.USER_PASSWORD and usr.strip() != "":
                new_session_id = str(uuid4())
                sample_indices, stage = get_random_session_samples(
                    backend, dataset, STAGE_SPLITS, usr, num_samples=15
                )
                logger.info(f"Session ID: {new_session_id}, Stage: {stage}")
                return (
                    True,
                    gr.update(visible=False),
                    gr.update(visible=False),
                    new_session_id,
                    sample_indices,
                    usr,
                )
            else:
                return (
                    False,
                    gr.update(visible=True),
                    gr.update(visible=True),
                    None,
                    [],
                    None,
                )

        # === Login Button ===
        login_btn.click(
            fn=login,
            inputs=[username, password],
            outputs=[
                logged_in,
                login_group,
                login_error,
                session_id,
                session_sample_indices,
                user_name,
            ],
        )

        # === UI Elements ===
        next_btn = gr.Button("Start", visible=False)
        prev_btn = gr.Button("Previous Sample", visible=False)
        warning_msg = gr.Markdown(
            "<span style='color:red;'>\u26a0\ufe0f Please select an answer before continuing.</span>",
            visible=False,
        )

        with gr.Group(visible=False) as app_group:
            with gr.Group():
                gr.Markdown("<div align='center'><big><b>Instructions</b></big></div>")
                gr.Markdown(INSTRUCTIONS)

            with gr.Group(visible=False) as question_group:
                with gr.Row(show_progress=True):
                    with gr.Column(variant="compact"):
                        sample_info = gr.Markdown()
                        gr.Markdown("**Question:**")
                        question_md = gr.Markdown()
                        radio = gr.Radio(label="Answer:", interactive=True)
                    with gr.Column(variant="compact"):
                        audio_output = gr.Audio(
                            interactive=False, type="numpy", label="Audio:"
                        )

        with gr.Group(
            visible=False, elem_id="final_page"
        ) as final_group:  # Final page, not visible until the end
            gr.Markdown(
                """
            # πŸŽ‰ Thanks for your help!

            You helped moving science forward πŸ€“

            Your responses have been recorded.

            You may now close this tab.
            """
            )

        # === Logic ===
        def update_ui(i, answers, session_sample_indices):
            if i == -1:  # We haven't started yet
                return (
                    gr.update(visible=False),
                    "",
                    "",
                    gr.update(visible=False),
                    gr.update(visible=False),
                    None,
                )
            # show the question
            true_index = session_sample_indices[i]
            sample = dataset[true_index]
            audio_data = (sample["audio"]["sampling_rate"], sample["audio"]["array"])
            previous_answer = answers.get(i, None)
            return (
                gr.update(visible=True),
                f"<div align='center'>Sample <b>{i+1}</b> out of <b>{len(session_sample_indices)}</b></div>",
                "Out of the following answers, according to the speaker's stressed words, what is most likely the underlying intention of the speaker?",
                gr.update(value=audio_data),
                gr.update(
                    choices=sample["possible_answers"],
                    value=previous_answer,
                ),
                previous_answer,
            )

        def update_next_index(
            i, answer, answers, session_id, session_sample_indices, user_name
        ):
            if answer is None and i != -1:  # if no answer is selected
                # show warning message
                return (
                    gr.update(),
                    gr.update(visible=True),
                    gr.update(),
                    answers,
                    gr.update(visible=False),
                    gr.update(visible=True),
                )

            if answer:  # if an answer is selected
                # save the answer to the backend
                answers[i] = answer
                true_index = session_sample_indices[i]
                sample = dataset[true_index]
                interp_id = sample["interpretation_id"]
                trans_id = sample["transcription_id"]
                user_id = session_id
                user_name_str = user_name or "anonymous"
                logger.info(
                    "saving answer to backend",
                    context={
                        "i": true_index,
                        "interp_id": interp_id,
                        "answer": answer,
                        "user_id": user_id,
                    },
                )
                if not backend.update_row(true_index, interp_id, user_id, answer):
                    backend.add_row(
                        true_index, interp_id, trans_id, user_id, answer, user_name_str
                    )

            if i + 1 == len(session_sample_indices):  # Last question just answered
                return (
                    -1,  # reset i to stop showing question
                    gr.update(visible=False),
                    gr.update(visible=False),
                    answers,
                    gr.update(visible=True),  # show final page
                    gr.update(visible=False),  # hide previous button
                )
            # go to the next question
            new_i = i + 1 if i + 1 < len(session_sample_indices) else 0
            return (
                new_i,
                gr.update(visible=False),
                gr.update(value="Submit answer and go to Next"),
                answers,
                gr.update(visible=False),
                gr.update(visible=True),
            )

        def update_prev_index(i):
            # prevent goint back in the first question and first page
            if i <= 0:
                return i, gr.update(visible=False)
            # go back to the previous question
            else:
                return i - 1, gr.update(visible=False)

        def answer_change_callback(answer, i, answers):
            answers[i] = answer
            return answer, answers

        def login_callback(logged_in):
            return (
                (
                    gr.update(visible=True),
                    gr.update(visible=True),
                    gr.update(visible=False),
                    gr.update(visible=False),
                )
                if logged_in
                else (
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(visible=False),
                    gr.update(visible=False),
                )
            )

        # === Events ===
        next_btn.click(
            update_next_index,
            [
                i,
                selected_answer,
                answers_dict,
                session_id,
                session_sample_indices,
                user_name,
            ],
            [i, warning_msg, next_btn, answers_dict, final_group, prev_btn],
        )
        prev_btn.click(update_prev_index, i, [i, warning_msg])
        i.change(
            update_ui,
            [i, answers_dict, session_sample_indices],
            [
                question_group,
                sample_info,
                question_md,
                audio_output,
                radio,
                selected_answer,
            ],
        )
        radio.change(
            answer_change_callback,
            [radio, i, answers_dict],
            [selected_answer, answers_dict],
        )
        logged_in.change(
            login_callback, logged_in, [app_group, next_btn, prev_btn, warning_msg]
        )


def compute_random_sampled_accuracy(df, dataset, n_rounds=100, seed=42):
    rng = np.random.default_rng(seed)

    # Filter to interpretation_ids with at least 3 user answers
    counts = df.groupby("interpretation_id")["user_id"].nunique()
    eligible_ids = set(counts[counts >= 3].index)

    # Group answers by interpretation_id
    grouped = df[df["interpretation_id"].isin(eligible_ids)].groupby(
        "interpretation_id"
    )

    all_scores = []
    total_answered_per_round = []

    for _ in range(n_rounds):
        correct = 0
        total = 0

        for interp_id, group in grouped:
            if group.empty:
                continue

            # Randomly pick one row
            row = group.sample(1, random_state=rng.integers(1e6)).iloc[0]
            answer = row["answer"]
            idx = int(row["index_in_dataset"])
            sample = dataset[idx]
            gt = sample["possible_answers"][sample["label"]]
            total += 1
            if answer == gt:
                correct += 1

        if total > 0:
            all_scores.append(correct / total)
            total_answered_per_round.append(total)

    if all_scores:
        mean_acc = np.mean(all_scores)
        mean_total = int(np.mean(total_answered_per_round))
        std_acc = np.std(all_scores, ddof=1)  # sample std
        ci_95 = 1.96 * std_acc / np.sqrt(n_rounds)
        return mean_acc, std_acc, mean_total, ci_95

    return None, None, 0, None


def get_admin_tab():
    with gr.Tab("Admin Console"):
        admin_password = gr.Text(label="Enter Admin Password", type="password")
        check_btn = gr.Button("Enter")
        error_box = gr.Markdown("", visible=False)
        output_box = gr.Markdown("", visible=False)

        def calculate_majority_vote_accuracy(pw):
            if pw != configs.ADMIN_PASSWORD:
                return gr.update(
                    visible=True, value="❌ Incorrect password."
                ), gr.update(visible=False)

            df = backend.get_all_rows()
            if df.empty:
                return gr.update(visible=True, value="No data available."), gr.update(
                    visible=False
                )

            # Majority vote per interpretation_id
            majority_answers = {}
            for interp_id, group in df.groupby("interpretation_id"):
                answer_counts = Counter(group["answer"])
                if answer_counts:
                    majority_answers[interp_id] = answer_counts.most_common(1)[0][0]

            counts = df.groupby("interpretation_id")["user_id"].nunique().to_dict()
            total_answers = len(df)
            users_count = df["user_id"].nunique()

            stage_acc = {}
            stage_completes = {}
            stage_counts = {}
            stage_remaining = {}

            # global_correct = 0
            # global_total = 0

            for stage in ["stage1", "stage2", "stage3"]:
                correct, total = 0, 0
                complete = 0
                for i in STAGE_SPLITS[stage]:
                    sample = dataset[i]
                    interp_id = sample["interpretation_id"]
                    label = sample["label"]
                    gt = sample["possible_answers"][label]

                    n = counts.get(interp_id, 0)
                    if n >= 3:
                        complete += 1
                    if interp_id in majority_answers:
                        pred = majority_answers[interp_id]
                        total += 1
                        if pred == gt:
                            correct += 1

                stage_counts[stage] = len(STAGE_SPLITS[stage])
                stage_completes[stage] = complete
                stage_remaining[stage] = 3 * len(STAGE_SPLITS[stage]) - sum(
                    counts.get(dataset[i]["interpretation_id"], 0)
                    for i in STAGE_SPLITS[stage]
                )

                if complete == len(STAGE_SPLITS[stage]):
                    acc = correct / total if total > 0 else 0
                    stage_acc[stage] = (acc, correct, total)
                else:
                    stage_acc[stage] = None  # not shown yet

            # Determine active stage
            if stage_completes["stage1"] < stage_counts["stage1"]:
                current_stage = "Stage 1"
            elif stage_completes["stage2"] < stage_counts["stage2"]:
                current_stage = "Stage 2"
            else:
                current_stage = "Stage 3"

            # Majority Vote Accuracy Section
            agg_lines = []
            if stage_acc["stage1"]:
                acc1, c1, t1 = stage_acc["stage1"]
                agg_lines.append(f"- **Stage 1:** {acc1:.2%} ({c1}/{t1})")
            if stage_acc["stage2"]:
                acc2, c2, t2 = stage_acc["stage2"]
                agg_lines.append(
                    f"- **Stage 1+2:** {(c1 + c2) / (t1 + t2):.2%} ({c1 + c2}/{t1 + t2})"
                )
            if stage_acc["stage3"]:
                acc3, c3, t3 = stage_acc["stage3"]
                agg_lines.append(
                    f"- **All Stages:** {(c1 + c2 + c3) / (t1 + t2 + t3):.2%} ({c1 + c2 + c3}/{t1 + t2 + t3})"
                )
            agg_msg = "\n".join(agg_lines) if agg_lines else "No completed stages yet."
            # Compute random-sampled accuracy
            n_rounds = 100
            rand_acc, rand_std, rand_total, rand_ci = compute_random_sampled_accuracy(
                df, dataset, n_rounds=n_rounds
            )

            # Random-sampled Accuracy
            if rand_acc is not None:
                rand_acc_msg = (
                    f"**Accuracy:** {rand_acc:.2%} Β± {rand_ci:.2%} (95% CI)\n\n"
                    f"Standard deviation: {rand_std:.2%}\n\n"
                    f"Samples used: {rand_total} Γ— {n_rounds} rounds"
                )
            else:
                rand_acc_msg = "Random sampling failed (no data)."

            
            correct = 0
            total = 0

            for _, row in df.iterrows():
                idx = int(row["index_in_dataset"])
                if idx >= len(dataset):
                    continue  # skip out-of-range
                sample = dataset[idx]
                gt_answer = sample["possible_answers"][sample["label"]]
                if row["answer"] == gt_answer:
                    correct += 1
                total += 1

            overall_acc = correct / total if total > 0 else None
            if overall_acc is not None:
                overall_acc_msg = (
                    f"Overall Accuracy: {overall_acc:.2%} ({correct}/{total})"
                )
            else:
                overall_acc_msg = "No data available."
            
            # Final message (no indentation!)
            msg = f"""
## βœ… Accuracy Summary

### Overall Accuracy
{overall_acc_msg}

---

### Majority Vote
{agg_msg}

---

### Random-Sampled Accuracy
{rand_acc_msg}

---

## πŸ“Š Answer Progress

- **Total answers submitted:** {total_answers}  
- **Answers to go (global):** {3 * len(dataset) - total_answers}  
- **Unique users:** {users_count}

---

## 🧱 Stage Breakdown

| Stage | Completed | Total | Remaining Answers |
|-------|-----------|--------|-------------------|
|  1    | {stage_completes['stage1']} / {stage_counts['stage1']} | {stage_counts['stage1']} | {stage_remaining['stage1']} |
|  2    | {stage_completes['stage2']} / {stage_counts['stage2']} | {stage_counts['stage2']} | {stage_remaining['stage2']} |
|  3    | {stage_completes['stage3']} / {stage_counts['stage3']} | {stage_counts['stage3']} | {stage_remaining['stage3']} |

**➑️ Current Active Stage:** {current_stage}
"""

            return gr.update(visible=False), gr.update(visible=True, value=msg)

        check_btn.click(
            fn=calculate_majority_vote_accuracy,
            inputs=admin_password,
            outputs=[error_box, output_box],
        )


# App UI
with gr.Blocks() as demo:
    human_eval_tab()
    get_admin_tab()

# Launch app
demo.launch()