Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gradio/certificate.pem +31 -0
- .ipynb_checkpoints/ui-checkpoint.py +191 -0
- README.md +2 -8
- output_eval_cs.csv +0 -0
- ui.py +191 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+s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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# paragraph_annotation_tool.py
|
| 2 |
+
"""
|
| 3 |
+
Paragraph-level annotation tool for rating two prompts from multiple LLMs.
|
| 4 |
+
-------------------------------------------------------------------------
|
| 5 |
+
* Shows Content_Paragraph (the context) on top.
|
| 6 |
+
* Five models; for each model we display:
|
| 7 |
+
ββββββββββββββββ¬ββββββββββββββ
|
| 8 |
+
β prompt-1 out β prompt-2 outβ
|
| 9 |
+
ββββββββββββββββΌββββββββββββββ€
|
| 10 |
+
β A/B/C radio β A/B/C radio β
|
| 11 |
+
ββββββββββββββββ΄ββββββββββββββ
|
| 12 |
+
* Model rows are shuffled per example and the permutation is stored
|
| 13 |
+
in column 'perm_models' so the order is stable across sessions.
|
| 14 |
+
* Ratings are written to rating_<model>__prompt1 / __prompt2
|
| 15 |
+
* Back button always works; Save&Next is enabled only when every radio
|
| 16 |
+
has a value. Tool resumes at the first example with any missing rating.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import gradio as gr
|
| 20 |
+
import pandas as pd
|
| 21 |
+
import time, os, random
|
| 22 |
+
from typing import List
|
| 23 |
+
|
| 24 |
+
# ---------- CONFIG ----------
|
| 25 |
+
CONTENT_COL = "Content_Paragraph"
|
| 26 |
+
PROMPT1_SUFFIX, PROMPT2_SUFFIX = "_prompt1", "_prompt2"
|
| 27 |
+
PERM_COL = "perm_models"
|
| 28 |
+
RATING_OPTS = ["A", "B", "C"] # 3-level scale
|
| 29 |
+
|
| 30 |
+
# ---------- LOAD CSV ----------
|
| 31 |
+
csv_path = input("Enter CSV filename: ").strip()
|
| 32 |
+
if not os.path.exists(csv_path):
|
| 33 |
+
raise FileNotFoundError(csv_path)
|
| 34 |
+
|
| 35 |
+
df = pd.read_csv(csv_path, keep_default_na=False)
|
| 36 |
+
TOTAL = len(df)
|
| 37 |
+
|
| 38 |
+
if CONTENT_COL not in df.columns:
|
| 39 |
+
raise ValueError(f"Missing required column '{CONTENT_COL}' in CSV")
|
| 40 |
+
|
| 41 |
+
# ---------- DISCOVER MODELS ----------
|
| 42 |
+
models: List[str] = []
|
| 43 |
+
for c in df.columns:
|
| 44 |
+
if c.endswith(PROMPT1_SUFFIX) and not c.startswith("rating_"):
|
| 45 |
+
m = c[:-len(PROMPT1_SUFFIX)]
|
| 46 |
+
if f"{m}{PROMPT2_SUFFIX}" not in df.columns:
|
| 47 |
+
raise ValueError(
|
| 48 |
+
f"Found '{c}' but no matching '{m}{PROMPT2_SUFFIX}'")
|
| 49 |
+
models.append(m)
|
| 50 |
+
|
| 51 |
+
if len(models) < 1:
|
| 52 |
+
raise ValueError(f"No '*{PROMPT1_SUFFIX}' columns found")
|
| 53 |
+
|
| 54 |
+
# ---------- ADD HELPER COLUMNS IF NEEDED ----------
|
| 55 |
+
if PERM_COL not in df.columns:
|
| 56 |
+
df[PERM_COL] = ""
|
| 57 |
+
|
| 58 |
+
for m in models:
|
| 59 |
+
for p in ("prompt1", "prompt2"):
|
| 60 |
+
rc = f"rating_{m}__{p}"
|
| 61 |
+
if rc not in df.columns:
|
| 62 |
+
df[rc] = ""
|
| 63 |
+
|
| 64 |
+
for col in ("annotator", "annotation_time"):
|
| 65 |
+
if col not in df.columns:
|
| 66 |
+
df[col] = "" if col == "annotator" else 0.0
|
| 67 |
+
|
| 68 |
+
# ---------- ANNOTATOR ----------
|
| 69 |
+
annotator = input("Annotator name: ").strip()
|
| 70 |
+
while not annotator:
|
| 71 |
+
annotator = input("Name cannot be empty β try again: ").strip()
|
| 72 |
+
|
| 73 |
+
current_start: float | None = None # per-example timer
|
| 74 |
+
|
| 75 |
+
# ---------- UTILS ----------
|
| 76 |
+
def first_incomplete() -> int:
|
| 77 |
+
for i, row in df.iterrows():
|
| 78 |
+
for m in models:
|
| 79 |
+
if row[f"rating_{m}__prompt1"] == "" or row[f"rating_{m}__prompt2"] == "":
|
| 80 |
+
return i
|
| 81 |
+
return 0
|
| 82 |
+
|
| 83 |
+
def get_perm(idx: int) -> List[str]:
|
| 84 |
+
cell = str(df.at[idx, PERM_COL])
|
| 85 |
+
if not cell:
|
| 86 |
+
seq = models.copy(); random.shuffle(seq)
|
| 87 |
+
df.at[idx, PERM_COL] = "|".join(seq)
|
| 88 |
+
df.to_csv(csv_path, index=False)
|
| 89 |
+
return df.at[idx, PERM_COL].split("|")
|
| 90 |
+
|
| 91 |
+
N_OUT = 2 * len(models) # total textboxes / radios per example
|
| 92 |
+
|
| 93 |
+
# ---------- BUILD ONE ROW ----------
|
| 94 |
+
def build_row(idx: int):
|
| 95 |
+
global current_start
|
| 96 |
+
row = df.loc[idx]
|
| 97 |
+
order = get_perm(idx)
|
| 98 |
+
outputs, ratings = [], []
|
| 99 |
+
|
| 100 |
+
for m in order:
|
| 101 |
+
outputs.append(row[f"{m}{PROMPT1_SUFFIX}"])
|
| 102 |
+
outputs.append(row[f"{m}{PROMPT2_SUFFIX}"])
|
| 103 |
+
ratings.append(row[f"rating_{m}__prompt1"] or None)
|
| 104 |
+
ratings.append(row[f"rating_{m}__prompt2"] or None)
|
| 105 |
+
|
| 106 |
+
current_start = time.time()
|
| 107 |
+
ready = all(r in RATING_OPTS for r in ratings)
|
| 108 |
+
header = f"Example {idx + 1}/{TOTAL}"
|
| 109 |
+
return [idx, idx, header, row[CONTENT_COL]] + outputs + ratings + \
|
| 110 |
+
[gr.update(), gr.update(interactive=ready)]
|
| 111 |
+
|
| 112 |
+
# ---------- SAVE ----------
|
| 113 |
+
def save_row(idx: int, ratings: List[str]):
|
| 114 |
+
if not all(r in RATING_OPTS for r in ratings):
|
| 115 |
+
return # ignore partial rows
|
| 116 |
+
elapsed = time.time() - current_start if current_start else 0.0
|
| 117 |
+
p = 0
|
| 118 |
+
for m in get_perm(idx):
|
| 119 |
+
df.at[idx, f"rating_{m}__prompt1"] = ratings[p]; p += 1
|
| 120 |
+
df.at[idx, f"rating_{m}__prompt2"] = ratings[p]; p += 1
|
| 121 |
+
df.at[idx, "annotator"] = annotator
|
| 122 |
+
df.at[idx, "annotation_time"] = float(elapsed)
|
| 123 |
+
df.to_csv(csv_path, index=False)
|
| 124 |
+
|
| 125 |
+
# ---------- GRADIO UI ----------
|
| 126 |
+
with gr.Blocks(title="Paragraph Annotation Tool") as demo:
|
| 127 |
+
state = gr.State(first_incomplete())
|
| 128 |
+
|
| 129 |
+
gr.Markdown("# Paragraph Annotation Tool")
|
| 130 |
+
gr.Markdown(f"**Annotator:** {annotator}")
|
| 131 |
+
|
| 132 |
+
idx_box = gr.Number(label="Index", interactive=False)
|
| 133 |
+
hdr_box = gr.Markdown()
|
| 134 |
+
para_box = gr.Textbox(label="Content Paragraph",
|
| 135 |
+
interactive=False, lines=6)
|
| 136 |
+
|
| 137 |
+
# --- dynamic widgets for models ---
|
| 138 |
+
out_boxes, radio_widgets = [], []
|
| 139 |
+
|
| 140 |
+
for _ in models: # actual order varies per row
|
| 141 |
+
with gr.Row():
|
| 142 |
+
# prompt-1 column
|
| 143 |
+
with gr.Column():
|
| 144 |
+
out1 = gr.Textbox(interactive=False, lines=6)
|
| 145 |
+
rad1 = gr.Radio(RATING_OPTS, label="Rating (P1)")
|
| 146 |
+
# prompt-2 column
|
| 147 |
+
with gr.Column():
|
| 148 |
+
out2 = gr.Textbox(interactive=False, lines=6)
|
| 149 |
+
rad2 = gr.Radio(RATING_OPTS, label="Rating (P2)")
|
| 150 |
+
out_boxes.extend((out1, out2))
|
| 151 |
+
radio_widgets.extend((rad1, rad2))
|
| 152 |
+
|
| 153 |
+
back_btn = gr.Button("β΅ Back")
|
| 154 |
+
next_btn = gr.Button("Save & Next βΆ", interactive=False)
|
| 155 |
+
|
| 156 |
+
# ---- enable NEXT when complete ----
|
| 157 |
+
def toggle_next(*vals):
|
| 158 |
+
ready = all(v in RATING_OPTS for v in vals)
|
| 159 |
+
return gr.update(interactive=ready)
|
| 160 |
+
|
| 161 |
+
for r in radio_widgets:
|
| 162 |
+
r.change(toggle_next, inputs=radio_widgets, outputs=next_btn)
|
| 163 |
+
|
| 164 |
+
# ---- navigation callbacks ----
|
| 165 |
+
def goto(step: int):
|
| 166 |
+
def _fn(idx: int, *vals):
|
| 167 |
+
ratings = list(vals[:-1]) # last arg is next_btn state
|
| 168 |
+
if step != -1 or all(r in RATING_OPTS for r in ratings):
|
| 169 |
+
save_row(idx, ratings)
|
| 170 |
+
new_idx = max(0, min(idx + step, TOTAL - 1))
|
| 171 |
+
return build_row(new_idx)
|
| 172 |
+
return _fn
|
| 173 |
+
|
| 174 |
+
back_btn.click(goto(-1),
|
| 175 |
+
inputs=[state] + radio_widgets + [next_btn],
|
| 176 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 177 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 178 |
+
|
| 179 |
+
next_btn.click(goto(1),
|
| 180 |
+
inputs=[state] + radio_widgets + [next_btn],
|
| 181 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 182 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 183 |
+
|
| 184 |
+
demo.load(lambda: tuple(build_row(first_incomplete())), inputs=[],
|
| 185 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 186 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
demo.queue() # enables request queueing
|
| 190 |
+
demo.launch(share=True)
|
| 191 |
+
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: CS
|
| 3 |
-
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
title: CS
|
| 3 |
+
app_file: ui.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 5.23.3
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
output_eval_cs.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
ui.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# paragraph_annotation_tool.py
|
| 2 |
+
"""
|
| 3 |
+
Paragraph-level annotation tool for rating two prompts from multiple LLMs.
|
| 4 |
+
-------------------------------------------------------------------------
|
| 5 |
+
* Shows Content_Paragraph (the context) on top.
|
| 6 |
+
* Five models; for each model we display:
|
| 7 |
+
ββββββββββββββββ¬ββββββββββββββ
|
| 8 |
+
β prompt-1 out β prompt-2 outβ
|
| 9 |
+
ββββββββββββββββΌββββββββββββββ€
|
| 10 |
+
β A/B/C radio β A/B/C radio β
|
| 11 |
+
ββββββββββββββββ΄ββββββββββββββ
|
| 12 |
+
* Model rows are shuffled per example and the permutation is stored
|
| 13 |
+
in column 'perm_models' so the order is stable across sessions.
|
| 14 |
+
* Ratings are written to rating_<model>__prompt1 / __prompt2
|
| 15 |
+
* Back button always works; Save&Next is enabled only when every radio
|
| 16 |
+
has a value. Tool resumes at the first example with any missing rating.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
import gradio as gr
|
| 20 |
+
import pandas as pd
|
| 21 |
+
import time, os, random
|
| 22 |
+
from typing import List
|
| 23 |
+
|
| 24 |
+
# ---------- CONFIG ----------
|
| 25 |
+
CONTENT_COL = "Content_Paragraph"
|
| 26 |
+
PROMPT1_SUFFIX, PROMPT2_SUFFIX = "_prompt1", "_prompt2"
|
| 27 |
+
PERM_COL = "perm_models"
|
| 28 |
+
RATING_OPTS = ["A", "B", "C"] # 3-level scale
|
| 29 |
+
|
| 30 |
+
# ---------- LOAD CSV ----------
|
| 31 |
+
csv_path = input("Enter CSV filename: ").strip()
|
| 32 |
+
if not os.path.exists(csv_path):
|
| 33 |
+
raise FileNotFoundError(csv_path)
|
| 34 |
+
|
| 35 |
+
df = pd.read_csv(csv_path, keep_default_na=False)
|
| 36 |
+
TOTAL = len(df)
|
| 37 |
+
|
| 38 |
+
if CONTENT_COL not in df.columns:
|
| 39 |
+
raise ValueError(f"Missing required column '{CONTENT_COL}' in CSV")
|
| 40 |
+
|
| 41 |
+
# ---------- DISCOVER MODELS ----------
|
| 42 |
+
models: List[str] = []
|
| 43 |
+
for c in df.columns:
|
| 44 |
+
if c.endswith(PROMPT1_SUFFIX) and not c.startswith("rating_"):
|
| 45 |
+
m = c[:-len(PROMPT1_SUFFIX)]
|
| 46 |
+
if f"{m}{PROMPT2_SUFFIX}" not in df.columns:
|
| 47 |
+
raise ValueError(
|
| 48 |
+
f"Found '{c}' but no matching '{m}{PROMPT2_SUFFIX}'")
|
| 49 |
+
models.append(m)
|
| 50 |
+
|
| 51 |
+
if len(models) < 1:
|
| 52 |
+
raise ValueError(f"No '*{PROMPT1_SUFFIX}' columns found")
|
| 53 |
+
|
| 54 |
+
# ---------- ADD HELPER COLUMNS IF NEEDED ----------
|
| 55 |
+
if PERM_COL not in df.columns:
|
| 56 |
+
df[PERM_COL] = ""
|
| 57 |
+
|
| 58 |
+
for m in models:
|
| 59 |
+
for p in ("prompt1", "prompt2"):
|
| 60 |
+
rc = f"rating_{m}__{p}"
|
| 61 |
+
if rc not in df.columns:
|
| 62 |
+
df[rc] = ""
|
| 63 |
+
|
| 64 |
+
for col in ("annotator", "annotation_time"):
|
| 65 |
+
if col not in df.columns:
|
| 66 |
+
df[col] = "" if col == "annotator" else 0.0
|
| 67 |
+
|
| 68 |
+
# ---------- ANNOTATOR ----------
|
| 69 |
+
annotator = input("Annotator name: ").strip()
|
| 70 |
+
while not annotator:
|
| 71 |
+
annotator = input("Name cannot be empty β try again: ").strip()
|
| 72 |
+
|
| 73 |
+
current_start: float | None = None # per-example timer
|
| 74 |
+
|
| 75 |
+
# ---------- UTILS ----------
|
| 76 |
+
def first_incomplete() -> int:
|
| 77 |
+
for i, row in df.iterrows():
|
| 78 |
+
for m in models:
|
| 79 |
+
if row[f"rating_{m}__prompt1"] == "" or row[f"rating_{m}__prompt2"] == "":
|
| 80 |
+
return i
|
| 81 |
+
return 0
|
| 82 |
+
|
| 83 |
+
def get_perm(idx: int) -> List[str]:
|
| 84 |
+
cell = str(df.at[idx, PERM_COL])
|
| 85 |
+
if not cell:
|
| 86 |
+
seq = models.copy(); random.shuffle(seq)
|
| 87 |
+
df.at[idx, PERM_COL] = "|".join(seq)
|
| 88 |
+
df.to_csv(csv_path, index=False)
|
| 89 |
+
return df.at[idx, PERM_COL].split("|")
|
| 90 |
+
|
| 91 |
+
N_OUT = 2 * len(models) # total textboxes / radios per example
|
| 92 |
+
|
| 93 |
+
# ---------- BUILD ONE ROW ----------
|
| 94 |
+
def build_row(idx: int):
|
| 95 |
+
global current_start
|
| 96 |
+
row = df.loc[idx]
|
| 97 |
+
order = get_perm(idx)
|
| 98 |
+
outputs, ratings = [], []
|
| 99 |
+
|
| 100 |
+
for m in order:
|
| 101 |
+
outputs.append(row[f"{m}{PROMPT1_SUFFIX}"])
|
| 102 |
+
outputs.append(row[f"{m}{PROMPT2_SUFFIX}"])
|
| 103 |
+
ratings.append(row[f"rating_{m}__prompt1"] or None)
|
| 104 |
+
ratings.append(row[f"rating_{m}__prompt2"] or None)
|
| 105 |
+
|
| 106 |
+
current_start = time.time()
|
| 107 |
+
ready = all(r in RATING_OPTS for r in ratings)
|
| 108 |
+
header = f"Example {idx + 1}/{TOTAL}"
|
| 109 |
+
return [idx, idx, header, row[CONTENT_COL]] + outputs + ratings + \
|
| 110 |
+
[gr.update(), gr.update(interactive=ready)]
|
| 111 |
+
|
| 112 |
+
# ---------- SAVE ----------
|
| 113 |
+
def save_row(idx: int, ratings: List[str]):
|
| 114 |
+
if not all(r in RATING_OPTS for r in ratings):
|
| 115 |
+
return # ignore partial rows
|
| 116 |
+
elapsed = time.time() - current_start if current_start else 0.0
|
| 117 |
+
p = 0
|
| 118 |
+
for m in get_perm(idx):
|
| 119 |
+
df.at[idx, f"rating_{m}__prompt1"] = ratings[p]; p += 1
|
| 120 |
+
df.at[idx, f"rating_{m}__prompt2"] = ratings[p]; p += 1
|
| 121 |
+
df.at[idx, "annotator"] = annotator
|
| 122 |
+
df.at[idx, "annotation_time"] = float(elapsed)
|
| 123 |
+
df.to_csv(csv_path, index=False)
|
| 124 |
+
|
| 125 |
+
# ---------- GRADIO UI ----------
|
| 126 |
+
with gr.Blocks(title="Paragraph Annotation Tool") as demo:
|
| 127 |
+
state = gr.State(first_incomplete())
|
| 128 |
+
|
| 129 |
+
gr.Markdown("# Paragraph Annotation Tool")
|
| 130 |
+
gr.Markdown(f"**Annotator:** {annotator}")
|
| 131 |
+
|
| 132 |
+
idx_box = gr.Number(label="Index", interactive=False)
|
| 133 |
+
hdr_box = gr.Markdown()
|
| 134 |
+
para_box = gr.Textbox(label="Content Paragraph",
|
| 135 |
+
interactive=False, lines=6)
|
| 136 |
+
|
| 137 |
+
# --- dynamic widgets for models ---
|
| 138 |
+
out_boxes, radio_widgets = [], []
|
| 139 |
+
|
| 140 |
+
for _ in models: # actual order varies per row
|
| 141 |
+
with gr.Row():
|
| 142 |
+
# prompt-1 column
|
| 143 |
+
with gr.Column():
|
| 144 |
+
out1 = gr.Textbox(interactive=False, lines=6)
|
| 145 |
+
rad1 = gr.Radio(RATING_OPTS, label="Rating (P1)")
|
| 146 |
+
# prompt-2 column
|
| 147 |
+
with gr.Column():
|
| 148 |
+
out2 = gr.Textbox(interactive=False, lines=6)
|
| 149 |
+
rad2 = gr.Radio(RATING_OPTS, label="Rating (P2)")
|
| 150 |
+
out_boxes.extend((out1, out2))
|
| 151 |
+
radio_widgets.extend((rad1, rad2))
|
| 152 |
+
|
| 153 |
+
back_btn = gr.Button("β΅ Back")
|
| 154 |
+
next_btn = gr.Button("Save & Next βΆ", interactive=False)
|
| 155 |
+
|
| 156 |
+
# ---- enable NEXT when complete ----
|
| 157 |
+
def toggle_next(*vals):
|
| 158 |
+
ready = all(v in RATING_OPTS for v in vals)
|
| 159 |
+
return gr.update(interactive=ready)
|
| 160 |
+
|
| 161 |
+
for r in radio_widgets:
|
| 162 |
+
r.change(toggle_next, inputs=radio_widgets, outputs=next_btn)
|
| 163 |
+
|
| 164 |
+
# ---- navigation callbacks ----
|
| 165 |
+
def goto(step: int):
|
| 166 |
+
def _fn(idx: int, *vals):
|
| 167 |
+
ratings = list(vals[:-1]) # last arg is next_btn state
|
| 168 |
+
if step != -1 or all(r in RATING_OPTS for r in ratings):
|
| 169 |
+
save_row(idx, ratings)
|
| 170 |
+
new_idx = max(0, min(idx + step, TOTAL - 1))
|
| 171 |
+
return build_row(new_idx)
|
| 172 |
+
return _fn
|
| 173 |
+
|
| 174 |
+
back_btn.click(goto(-1),
|
| 175 |
+
inputs=[state] + radio_widgets + [next_btn],
|
| 176 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 177 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 178 |
+
|
| 179 |
+
next_btn.click(goto(1),
|
| 180 |
+
inputs=[state] + radio_widgets + [next_btn],
|
| 181 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 182 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 183 |
+
|
| 184 |
+
demo.load(lambda: tuple(build_row(first_incomplete())), inputs=[],
|
| 185 |
+
outputs=[state, idx_box, hdr_box, para_box] +
|
| 186 |
+
out_boxes + radio_widgets + [back_btn, next_btn])
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
demo.queue() # enables request queueing
|
| 190 |
+
demo.launch(share=True)
|
| 191 |
+
|