Spaces:
Sleeping
Sleeping
File size: 6,961 Bytes
a09f74e aff259f a09f74e aff259f a09f74e aff259f a09f74e aff259f d03507b aff259f d03507b aff259f 16cc6e8 d03507b aff259f d03507b aff259f d03507b aff259f d03507b aff259f d03507b 93f83bd d03507b aff259f d03507b aff259f fa5f821 aff259f d03507b fa5f821 d03507b aff259f 9ef5e02 aff259f d03507b aff259f fa5f821 aff259f fa5f821 aff259f 9ef5e02 fa5f821 230844b d03507b aff259f d03507b aff259f d03507b aff259f fa5f821 aff259f d03507b aff259f |
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 |
import os
import random
from collections import Counter
from datasets import Dataset, load_dataset
from fasthtml.common import *
from fastlite import database
from huggingface_hub import create_repo, login
login(token=os.environ.get("HF_TOKEN"))
fact_dataset = load_dataset("griffin/iclr2025_data_scores", split="train").to_list()
fact_dataset = [{"example_id": i, **example} for i, example in enumerate(fact_dataset)]
db = database("data/examples.db")
examples = db.t.examples
if examples not in db.t:
examples.create(
id=int,
example_id=int,
question_type=str,
question=str,
answer=str,
decision=str,
pk="id",
)
question_types = sorted(set(ex["question_type"] for ex in fact_dataset))
def get_stats():
total_examples = Counter(ex["question_type"] for ex in fact_dataset)
curated_examples = Counter(row["question_type"] for row in examples.rows)
stats = {
qt: {"total": total_examples[qt], "curated": curated_examples[qt]}
for qt in question_types
}
return stats
def get_example(selected_type=None):
evaluated_ids = set(row["example_id"] for row in examples())
print(f"completed: {evaluated_ids}")
available_examples = [
ex for ex in fact_dataset if ex["example_id"] not in evaluated_ids
]
if selected_type:
available_examples = [
ex for ex in available_examples if ex["question_type"] == selected_type
]
if not available_examples:
return None
example = random.choice(available_examples)
keep_keys = [
"example_id",
"question_type",
"question",
"rationale",
"answer",
"log_ll",
"oracle_log_ll",
"oracle_advantage",
"prediction",
"prediction_oracle",
"accuracy",
"accuracy_oracle",
"accuracy_status",
]
return {k: example[k] for k in keep_keys if k in example}
# app
style = Style("""
body { background-color: #1e1e1e; color: #d4d4d4; font-family: Arial, sans-serif; }
h1, h2, h3 { color: #61dafb; }
.example-container { margin-top: 20px; }
.example-table { border-collapse: collapse; width: 100%; }
.example-table th, .example-table td { border: 1px solid #3a3a3a; padding: 8px; text-align: left; }
.example-table th { background-color: #2a2a2a; color: #61dafb; }
.example-table td { color: #d4d4d4; }
#evaluation-form { margin-top: 20px; }
#evaluation-form button { margin-right: 10px; background-color: #0e639c; color: white; border: none; padding: 10px 20px; cursor: pointer; }
#evaluation-form button:hover { background-color: #1177bb; }
select { background-color: #2a2a2a; color: #d4d4d4; border: 1px solid #3a3a3a; padding: 5px; }
a { color: #61dafb; text-decoration: none; }
a:hover { text-decoration: underline; }
""")
app, rt = fast_app(hdrs=(style,))
def render_stats(stats):
return Table(
Tr(Th("Question Type"), Th("Curated"), Th("Total")),
*[
Tr(
Td(qt),
Td(
f"{stats[qt]['curated']} ({stats[qt]['curated']/stats[qt]['total']:.1%})"
),
Td(stats[qt]["total"]),
)
for qt in question_types
],
cls="stats-table",
)
def render_example(example):
return Div(
Table(
*[Tr(Th(key), Td(str(value))) for key, value in example.items()],
cls="example-table",
),
Form(
Button(
"Good Example",
name="decision",
value="good",
hx_post="/evaluate",
hx_target="#example-container",
),
Button(
"Bad Example",
name="decision",
value="bad",
hx_post="/evaluate",
hx_target="#example-container",
),
Hidden(
name="example_id",
value=str(example["example_id"]),
id="hidden-example-id",
),
),
id="example-details",
)
def upload_to_hf():
create_repo(
repo_id="rbiswasfc/iclr-eval-examples",
token=os.environ.get("HF_TOKEN"),
private=True,
repo_type="dataset",
exist_ok=True,
)
# examples = db.t.examples
annotations = examples()
hf_ds = Dataset.from_list(annotations)
hf_ds.push_to_hub("rbiswasfc/iclr-eval-examples", token=os.environ.get("HF_TOKEN"))
@rt("/")
def get(question_type: str = None):
stats = get_stats()
example = get_example(question_type)
dropdown = Select(
Option("Question Types", value="", selected=question_type is None),
*[Option(qt, value=qt, selected=qt == question_type) for qt in question_types],
name="question_type",
hx_get="/",
hx_target="body",
hx_push_url="true",
)
if example is None:
content = Div(
H2("All examples of this type have been evaluated!"), render_stats(stats)
)
else:
content = Div(
H2("Example"),
Div(
render_example(example),
id="example-container",
),
)
view_stats_link = A("Curation Stats", href="/stats", cls="view-stats-link")
return Titled(
"Example Curation",
H2("Question Type"),
dropdown,
content,
Div(),
view_stats_link,
)
@rt("/evaluate")
def post(decision: str, example_id: str):
print(f"params to post: {decision}, {example_id}")
example_id = int(example_id)
example_dict = fact_dataset[example_id]
# Insert the evaluated example into the database
examples.insert(
{
"id": len(list(examples.rows)) + 1, # Auto-increment ID
"example_id": example_dict["example_id"],
"question_type": example_dict["question_type"],
"question": example_dict["question"],
"answer": example_dict["answer"],
"decision": decision,
}
)
upload_to_hf()
new_example = get_example(example_dict["question_type"])
if new_example is None:
return Div(H2("All examples of this type have been evaluated!"))
else:
return render_example(new_example)
@rt("/stats")
def get():
stats = get_stats()
stats = render_stats(stats)
return Titled(
"Curation Statistics",
Div(
stats,
A("Back to Curation", href="/", cls="back-link"),
cls="container",
),
)
# serve()
if __name__ == "__main__":
import os
import uvicorn
# setup_hf_backup(app)
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|