File size: 14,045 Bytes
d3d19d6
 
15bfa69
 
 
4c85f84
 
15bfa69
 
 
d3d19d6
 
 
 
 
 
 
 
 
 
 
 
 
15bfa69
d3d19d6
 
 
 
15bfa69
 
 
 
 
 
d3d19d6
 
 
 
 
 
 
 
 
15bfa69
 
 
 
 
 
 
d3d19d6
 
 
 
 
 
 
 
 
15bfa69
 
 
 
d3d19d6
 
 
 
 
15bfa69
 
 
 
 
 
 
d3d19d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15bfa69
 
 
 
 
d3d19d6
 
 
15bfa69
 
 
 
d3d19d6
 
 
 
 
 
 
 
 
 
 
15bfa69
d3d19d6
15bfa69
 
d3d19d6
 
 
 
 
 
15bfa69
d3d19d6
15bfa69
 
d3d19d6
 
 
 
 
 
 
 
 
 
 
15bfa69
d3d19d6
 
 
15bfa69
 
 
 
 
 
 
 
d3d19d6
15bfa69
d3d19d6
 
 
15bfa69
 
 
 
 
 
 
 
d3d19d6
 
 
 
 
15bfa69
 
 
 
 
 
d3d19d6
 
 
 
 
 
15bfa69
 
d3d19d6
 
 
15bfa69
 
d3d19d6
 
 
15bfa69
d3d19d6
 
15bfa69
d3d19d6
 
 
15bfa69
 
d3d19d6
 
 
 
15bfa69
d3d19d6
 
 
15bfa69
d3d19d6
 
15bfa69
d3d19d6
 
 
3b9ddbc
15bfa69
d3d19d6
 
 
15bfa69
 
2a9914a
 
15bfa69
 
 
 
 
d3d19d6
15bfa69
d3d19d6
15bfa69
d3d19d6
 
 
15bfa69
 
 
 
2a9914a
15bfa69
d3d19d6
15bfa69
 
 
d3d19d6
15bfa69
d3d19d6
15bfa69
d3d19d6
 
 
15bfa69
 
 
 
2a9914a
15bfa69
d3d19d6
15bfa69
 
d3d19d6
 
15bfa69
d3d19d6
 
 
 
15bfa69
 
 
 
 
d3d19d6
 
 
 
2a9914a
15bfa69
 
d3d19d6
 
2a9914a
15bfa69
d3d19d6
 
 
2a9914a
15bfa69
 
d3d19d6
 
2a9914a
15bfa69
d3d19d6
2a9914a
 
d3d19d6
 
 
15bfa69
 
 
 
 
d3d19d6
 
 
3b9ddbc
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
import gradio as gr
import pandas as pd
import random
import os
import csv
import sys

# print("">>> Gradio imported from:", gr.__file__)
# print(">>> Gradio version          :", getattr(gr, "__version__", "n/a"))
# print(">>> sys.path contains      :", sys.path[:5])

# ─── Configuration ─────────────────────────────────────────────────────────────
DATASET_FILES = {
    "ESConv":            "./csv/esconv.csv",
    "CraigslistBargain": "./csv/craigslist.csv",
}

# ─── Helper functions ──────────────────────────────────────────────────────────
def prepare_examples(user_id: str, dataset: str):
    """
    Read CSV, create Ours-vs-[PPDPP|DPDP] pairs, shuffle + randomise L/R.
    A deterministic seed (user_id+dataset) keeps the order stable for reloads.
    """
    random.seed(f"{user_id}_{dataset}")
    df = pd.read_csv(DATASET_FILES[dataset])
    pairs = []
    for _, row in df.iterrows():
        for other in ["PPDPP", "DPDP"]:
            pairs.append({
                "background": row["Background Information"],
                "ours":        row["Ours"],
                "other":       row[other],
                "other_name":  other,
            })
    random.shuffle(pairs)
    prepared = []
    for item in pairs:
        if random.random() < 0.5:
            left_text, right_text = item["ours"], item["other"]
            left_name, right_name = "Ours", item["other_name"]
        else:
            left_text, right_text = item["other"], item["ours"]
            left_name, right_name = item["other_name"], "Ours"
        prepared.append({
            "background": item["background"],
            "left_text":  left_text,
            "right_text": right_text,
            "left_name":  left_name,
            "right_name": right_name,
        })
    return prepared


def save_all_to_csv(user_id, dataset, examples, responses):
    """Rewrite CSV completely (idempotent)."""
    if not responses:
        return
    filename = f"{user_id}_{dataset}_results.csv"
    metrics = list(next(iter(responses.values())).keys())
    header = [
        "UserID", "Dataset", "Background",
        "Response A Method", "Response B Method"
    ] + metrics
    with open(filename, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=header)
        writer.writeheader()
        for idx in sorted(responses):
            ex = examples[idx]
            row = {
                "UserID":               user_id,
                "Dataset":              dataset,
                "Background":           ex["background"],
                "Response A Method":    ex["left_name"],
                "Response B Method":    ex["right_name"],
            }
            row.update(responses[idx])
            writer.writerow(row)


def load_responses_from_csv(user_id, dataset, examples):
    """
    Reconstruct a {idx: metrics-dict} mapping from an existing results file.
    Matching uses Background + response-method orientation to stay robust.
    """
    filename = f"{user_id}_{dataset}_results.csv"
    if not os.path.exists(filename):
        return {}
    df = pd.read_csv(filename)
    idx_map = {
        (ex["background"], ex["left_name"], ex["right_name"]): i
        for i, ex in enumerate(examples)
    }
    responses = {}
    for _, row in df.iterrows():
        key = (
            row["Background"],
            row["Response A Method"],
            row["Response B Method"]
        )
        if key in idx_map:
            idx = idx_map[key]
            metric_cols = [
                c for c in row.index
                if c not in [
                    "UserID", "Dataset", "Background",
                    "Response A Method", "Response B Method"
                ]
            ]
            responses[idx] = {k: row[k] for k in metric_cols}
    return responses


# ─── Panel-specific loaders ────────────────────────────────────────────────────
def es_load_example(idx, examples, responses):
    ex = examples[idx]
    prev = responses.get(idx, {})
    return (
        ex["background"], ex["left_text"], ex["right_text"],
        f"Item {idx+1} of {len(examples)}",
        prev.get("Identification"), prev.get("Comforting"),
        prev.get("Suggestion"),      prev.get("Overall"), "",
    )

def cb_load_example(idx, examples, responses):
    ex = examples[idx]
    prev = responses.get(idx, {})
    return (
        ex["background"], ex["left_text"], ex["right_text"],
        f"Item {idx+1} of {len(examples)}",
        prev.get("Persuasiveness"), prev.get("Coherence"),
        prev.get("Naturalness"),     prev.get("Overall"), "",
    )


# ─── Login & Logout ────────────────────────────────────────────────────────────
def login_fn(user_id, dataset):
    if not user_id or not dataset:
        raise gr.Error("Please enter your User ID and select a dataset.")
    examples = prepare_examples(user_id, dataset)
    responses = load_responses_from_csv(user_id, dataset, examples)
    idx = 0
    if dataset == "ESConv":
        bg, lft, rgt, prog, ident, com, sug, ovl, err_es = es_load_example(idx, examples, responses)
        bg_cb = lft_cb = rgt_cb = prog_cb = ""
        per = coh = nat = ovl_cb = None
        err_cb = ""
        return (
            gr.update(visible=False),
            gr.update(visible=True),
            gr.update(visible=False),
            user_id, dataset, examples, idx, responses,
            bg, lft, rgt, prog, ident, com, sug, ovl, err_es,
            bg_cb, lft_cb, rgt_cb, prog_cb, per, coh, nat, ovl_cb, err_cb,
        )
    else:
        bg_cb, lft_cb, rgt_cb, prog_cb, per, coh, nat, ovl_cb, err_cb = cb_load_example(idx, examples, responses)
        bg = lft = rgt = prog = ""
        ident = com = sug = ovl = None
        err_es = ""
        return (
            gr.update(visible=False),
            gr.update(visible=False),
            gr.update(visible=True),
            user_id, dataset, examples, idx, responses,
            bg, lft, rgt, prog, ident, com, sug, ovl, err_es,
            bg_cb, lft_cb, rgt_cb, prog_cb, per, coh, nat, ovl_cb, err_cb,
        )

def logout_fn(user_id, dataset, examples, idx, responses):
    if dataset:
        save_all_to_csv(user_id, dataset, examples, responses)
    return (
        gr.update(visible=True),
        gr.update(visible=False),
        gr.update(visible=False),
        "", "", [], 0, {},
        *[""] * 9,
        *[""] * 10
    )


# ─── Navigation callback helpers ───────────────────────────────────────────────
def es_next_fn(user_id, dataset, examples, idx, responses, ident, com, sug, ovl):
    if None in (ident, com, sug, ovl):
        return (*es_load_example(idx, examples, responses)[:4], idx, responses, ident, com, sug, ovl, "All metrics must be answered before proceeding.")
    responses[idx] = {"Identification": ident, "Comforting": com, "Suggestion": sug, "Overall": ovl}
    save_all_to_csv(user_id, dataset, examples, responses)
    idx += 1
    if idx >= len(examples):
        return ("🚩 Survey complete! Thank you.",) * 4 + (idx, responses, None, None, None, None, "")
    return (*es_load_example(idx, examples, responses)[:4], idx, responses, *es_load_example(idx, examples, responses)[4:])

def es_prev_fn(user_id, dataset, examples, idx, responses, ident, com, sug, ovl):
    if None not in (ident, com, sug, ovl):
        responses[idx] = {"Identification": ident, "Comforting": com, "Suggestion": sug, "Overall": ovl}
        save_all_to_csv(user_id, dataset, examples, responses)
    idx = max(0, idx - 1)
    return (*es_load_example(idx, examples, responses)[:4], idx, responses, *es_load_example(idx, examples, responses)[4:])

def cb_next_fn(user_id, dataset, examples, idx, responses, per, coh, nat, ovl_cb):
    if None in (per, coh, nat, ovl_cb):
        return (*cb_load_example(idx, examples, responses)[:4], idx, responses, per, coh, nat, ovl_cb, "All metrics must be answered before proceeding.")
    responses[idx] = {"Persuasiveness": per, "Coherence": coh, "Naturalness": nat, "Overall": ovl_cb}
    save_all_to_csv(user_id, dataset, examples, responses)
    idx += 1
    if idx >= len(examples):
        return ("🚩 Survey complete! Thank you.",) * 5 + (None, "")
    return (*cb_load_example(idx, examples, responses)[:4], idx, responses, *cb_load_example(idx, examples, responses)[4:])

def cb_prev_fn(user_id, dataset, examples, idx, responses, per, coh, nat, ovl_cb):
    if None not in (per, coh, nat, ovl_cb):
        responses[idx] = {"Persuasiveness": per, "Coherence": coh, "Naturalness": nat, "Overall": ovl_cb}
        save_all_to_csv(user_id, dataset, examples, responses)
    idx = max(0, idx - 1)
    return (*cb_load_example(idx, examples, responses)[:4], idx, responses, *cb_load_example(idx, examples, responses)[4:])


# ─── Build Gradio App ──────────────────────────────────────────────────────────
with gr.Blocks(title="Human Evaluation Survey") as demo:
    # Login panel
    with gr.Column() as login_panel:
        gr.Markdown("## Human Evaluation Survey")
        user_id_in = gr.Textbox(label="User ID")
        ds_dd      = gr.Dropdown(list(DATASET_FILES.keys()), label="Select dataset")
        start_btn  = gr.Button("Start survey")

    # Shared state
    uid_state  = gr.State(value="")
    ds_state   = gr.State(value="")
    ex_state   = gr.State(value=[])
    idx_state  = gr.State(value=0)
    resp_state = gr.State(value={})

    # ESConv Panel
    with gr.Column(visible=False) as es_panel:
        bg      = gr.Textbox(label="Background context", interactive=False)
        with gr.Row():
            lbox = gr.Textbox(label="Response A", interactive=False)
            rbox = gr.Textbox(label="Response B", interactive=False)
        ident  = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Identification (Ident.)")
        com    = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Comforting (Com.)")
        sug    = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Suggestion (Sug.)")
        ovl_es = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Overall (Ov.)")
        err_es = gr.HTML(visible=False)
        prog   = gr.Textbox(label="Progress", interactive=False)
        with gr.Row():
            prev_btn   = gr.Button("β—€ Prev")
            next_btn   = gr.Button("β–Ά Next")
            logout_es  = gr.Button("πŸšͺ Logout")

    # CraigslistBargain Panel
    with gr.Column(visible=False) as cb_panel:
        bg_cb  = gr.Textbox(label="Background context", interactive=False)
        with gr.Row():
            lbox_cb = gr.Textbox(label="Response A", interactive=False)
            rbox_cb = gr.Textbox(label="Response B", interactive=False)
        per    = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Persuasiveness (Per.)")
        coh    = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Coherence (Coh.)")
        nat    = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Naturalness (Nat.)")
        ovl_cb = gr.Radio(["Prefer Response A", "Prefer Response B", "No preference"], label="Overall")
        err_cb = gr.HTML(visible=False)
        prog_cb = gr.Textbox(label="Progress", interactive=False)
        with gr.Row():
            prev_cb  = gr.Button("β—€ Prev")
            next_cb  = gr.Button("β–Ά Next")
            logout_cb = gr.Button("πŸšͺ Logout")

    # Wiring callbacks
    start_btn.click(
        login_fn,
        inputs=[user_id_in, ds_dd],
        outputs=[
            login_panel, es_panel, cb_panel,
            uid_state, ds_state, ex_state, idx_state, resp_state,
            bg, lbox, rbox, prog, ident, com, sug, ovl_es, err_es,
            bg_cb, lbox_cb, rbox_cb, prog_cb, per, coh, nat, ovl_cb, err_cb
        ]
    )

    next_btn.click(
        es_next_fn,
        inputs=[uid_state, ds_state, ex_state, idx_state, resp_state, ident, com, sug, ovl_es],
        outputs=[bg, lbox, rbox, prog, idx_state, resp_state, ident, com, sug, ovl_es, err_es] );

    prev_btn.click(
        es_prev_fn,
        inputs=[uid_state, ds_state, ex_state, idx_state, resp_state, ident, com, sug, ovl_es],
        outputs=[bg, lbox, rbox, prog, idx_state, resp_state, ident, com, sug, ovl_es, err_es] );

    next_cb.click(
        cb_next_fn,
        inputs=[uid_state, ds_state, ex_state, idx_state, resp_state, per, coh, nat, ovl_cb],
        outputs=[bg_cb, lbox_cb, rbox_cb, prog_cb, idx_state, resp_state, per, coh, nat, ovl_cb, err_cb]);

    prev_cb.click(
        cb_prev_fn,
        inputs=[uid_state, ds_state, ex_state, idx_state, resp_state, per, coh, nat, ovl_cb],
        outputs=[bg_cb, lbox_cb, rbox_cb, prog_cb, idx_state, resp_state, per, coh, nat, ovl_cb, err_cb]);

    for logout_btn in (logout_es, logout_cb):
        logout_btn.click(
            logout_fn,
            inputs=[uid_state, ds_state, ex_state, idx_state, resp_state],
            outputs=[
                login_panel, es_panel, cb_panel,
                uid_state, ds_state, ex_state, idx_state, resp_state,
                bg, lbox, rbox, prog, ident, com, sug, ovl_es, err_es,
                bg_cb, lbox_cb, rbox_cb, prog_cb, per, coh, nat, ovl_cb, err_cb
            ]
        )

if __name__ == "__main__":
    demo.launch(share=True)