forked from marimo-team/marimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_labeler.py
More file actions
184 lines (132 loc) · 3.78 KB
/
Copy pathdata_labeler.py
File metadata and controls
184 lines (132 loc) · 3.78 KB
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
import marimo
__generated_with = "0.1.4"
app = marimo.App()
@app.cell
def __(mo):
mo.md("# Data Labeler")
return
@app.cell
def __(NUMBER_OF_EXAMPLES, mo):
get_index, set_index = mo.state(0)
def increment_index():
set_index(lambda v: min(v + 1, NUMBER_OF_EXAMPLES - 1))
def decrement_index() -> int:
set_index(lambda v: max(0, v - 1))
return decrement_index, get_index, increment_index, set_index
@app.cell
def __(decrement_index, increment_index, mo):
next_button = mo.ui.button(label="next", on_change=lambda _: increment_index())
previous_button = mo.ui.button(
label="previous", on_change=lambda _: decrement_index()
)
return next_button, previous_button
@app.cell
def __(mo):
mo.md(f"**Choose an example to label.**")
return
@app.cell
def __(NUMBER_OF_EXAMPLES, get_index, mo, set_index):
index = mo.ui.number(
0,
NUMBER_OF_EXAMPLES - 1,
value=get_index(),
step=1,
debounce=True,
label="example number",
on_change=set_index,
)
return index,
@app.cell
def __(index, mo, next_button, previous_button):
mo.hstack([index, previous_button, next_button], justify="start")
return
@app.cell
def __(index, mo):
mo.md(f"").center()
return
@app.cell
def __(mo):
mo.md("### Real or AI generated?").center()
return
@app.cell
def __(LABELS_PATH, NUMBER_OF_EXAMPLES, load_labels):
labels = load_labels(LABELS_PATH, NUMBER_OF_EXAMPLES)
return labels,
@app.cell
def __(LABELS_PATH, labels, write_labels):
def update_label(value, index):
labels[index]["label"] = value
write_labels(labels, LABELS_PATH)
return update_label,
@app.cell
def __(LABELS_PATH, labels, write_labels):
def update_notes(value, index):
labels[index]["notes"] = value
write_labels(labels, LABELS_PATH)
return update_notes,
@app.cell
def __(mo, notes):
mo.stop(len(notes.value) <= 100)
_character_count = mo.md(f"`{len(notes.value)}/100` characters used").right()
mo.md(
f"""
Notes can't be longer than 100 characters. {_character_count}
"""
).callout(kind="alert")
return
@app.cell
def __(index, labels, mo, update_label, update_notes):
data = labels[index.value]
label_picker = mo.ui.radio(
["Real", "AI Generated", "Unlabeled"],
value=data["label"],
label="**Selection.**",
on_change=lambda v: update_label(v, index.value),
)
notes = mo.ui.text_area(
value=data["notes"],
label="**Notes.**",
on_change=lambda v: update_notes(v, index.value),
)
mo.hstack([label_picker, notes], justify="space-around")
return data, label_picker, notes
@app.cell
def __(json, os):
def load_labels(path, number_of_examples):
if not os.path.exists(path):
return [
{"label": "Unlabeled", "notes": ""}
for _ in range(number_of_examples)
]
with open(path, "r") as f:
labels = json.loads(f.read())
assert len(labels) == number_of_examples
return labels
def write_labels(labels, path):
# Trunacate notes
labels = [
{"label": item["label"], "notes": item["notes"][:100]}
for item in labels
]
with open(path, "w") as f:
f.write(json.dumps(labels))
return load_labels, write_labels
@app.cell
def __():
NUMBER_OF_EXAMPLES = 100
return NUMBER_OF_EXAMPLES,
@app.cell
def __():
LABELS_PATH = "labels.json"
return LABELS_PATH,
@app.cell
def __():
import marimo as mo
return mo,
@app.cell
def __():
import json
import os
return json, os
if __name__ == "__main__":
app.run()