File size: 9,849 Bytes
be293db
8b77729
26742b2
be293db
08a65ff
 
6356cbd
160959f
be293db
 
 
 
 
 
cca065e
 
160959f
be293db
 
 
 
 
 
cca065e
be293db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b77729
 
 
 
 
 
 
 
 
 
 
 
 
26742b2
 
8b77729
be293db
08a65ff
26742b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08a65ff
 
 
 
 
 
 
 
 
 
 
 
 
26742b2
8b77729
 
08a65ff
 
 
be293db
 
 
 
 
08a65ff
 
 
be293db
cca065e
be293db
 
cca065e
be293db
 
 
08a65ff
b468d19
08a65ff
 
 
be293db
 
 
08a65ff
 
8b77729
 
08a65ff
8b77729
26742b2
8b77729
08a65ff
 
be293db
8b77729
 
26742b2
08a65ff
8b77729
26742b2
08a65ff
be293db
8b77729
26742b2
 
 
 
 
 
 
 
8b77729
be293db
8b77729
be293db
 
08a65ff
26742b2
 
 
08a65ff
26742b2
 
 
08a65ff
 
 
26742b2
 
 
08a65ff
26742b2
 
be293db
08a65ff
 
 
 
be293db
08a65ff
 
 
 
8b77729
08a65ff
8b77729
26742b2
 
 
08a65ff
26742b2
 
08a65ff
 
8b77729
08a65ff
 
 
 
be293db
08a65ff
 
8b77729
26742b2
 
 
 
08a65ff
26742b2
 
8b77729
08a65ff
be293db
8b77729
08a65ff
26742b2
 
 
08a65ff
26742b2
 
08a65ff
26742b2
 
 
08a65ff
26742b2
 
08a65ff
8b77729
26742b2
 
 
 
08a65ff
26742b2
 
08a65ff
 
 
 
 
 
be293db
08a65ff
 
26742b2
 
08a65ff
26742b2
08a65ff
26742b2
08a65ff
26742b2
 
 
08a65ff
26742b2
 
08a65ff
 
26742b2
 
08a65ff
26742b2
 
 
 
08a65ff
26742b2
 
be293db
08a65ff
 
 
 
be293db
08a65ff
 
 
8b77729
 
26742b2
8b77729
 
 
08a65ff
 
8b77729
 
be293db
cca065e
08a65ff
 
 
 
8b77729
 
 
 
 
 
 
 
 
 
 
 
 
08a65ff
 
 
8b77729
 
 
08a65ff
8b77729
 
08a65ff
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
import json
from pathlib import Path
from typing import Callable, List, Tuple

import streamlit as st
import yaml

st.set_page_config(
    page_title="HF Dataset Tagging App",
    page_icon="https://huggingface.co/front/assets/huggingface_logo.svg",
    layout="wide",
    initial_sidebar_state="auto",
)

task_set = json.load(open("task_set.json"))
license_set = json.load(open("license_set.json"))
language_set_restricted = json.load(open("language_set.json"))

multilinguality_set = {
    "monolingual": "contains a single language",
    "multilingual": "contains multiple languages",
    "translation": "contains translated or aligned text",
    "other": "other type of language distribution",
}

creator_set = {
    "language": [
        "found",
        "crowdsourced",
        "expert-generated",
        "machine-generated",
        "other",
    ],
    "annotations": [
        "found",
        "crowdsourced",
        "expert-generated",
        "machine-generated",
        "no-annotation",
        "other",
    ],
}

########################
## Helper functions
########################


def load_ds_datas():
    metada_exports = sorted(
        [f for f in Path.cwd().iterdir() if f.name.startswith("metadata_")],
        key=lambda f: f.lstat().st_mtime,
        reverse=True,
    )
    if len(metada_exports) == 0:
        raise ValueError("need to run ./build_metada_file.py at least once")
    with metada_exports[0].open() as fi:
        return json.load(fi)


def split_known(vals: List[str], okset: List[str]) -> Tuple[List[str], List[str]]:
    if vals is None:
        return [], []
    return [v for v in vals if v in okset], [v for v in vals if v not in okset]


def multiselect(
    w: st.delta_generator.DeltaGenerator,
    title: str,
    markdown: str,
    values: List[str],
    valid_set: List[str],
    format_func: Callable = str,
):
    valid_values, invalid_values = split_known(values, valid_set)
    w.markdown(
        """
#### {title}
{errors}
""".format(
            title=title, errors="" if len(invalid_values) == 0 else f"_Found invalid values:_ `{invalid_values}`"
        )
    )
    return w.multiselect(markdown, valid_set, default=valid_values, format_func=format_func)


def new_state():
    return {
        "task_categories": [],
        "task_ids": [],
        "multilinguality": [],
        "languages": [],
        "language_creators": [],
        "annotations_creators": [],
        "source_datasets": [],
        "size_categories": [],
        "licenses": [],
    }


state = new_state()
datasets_md = load_ds_datas()
existing_tag_sets = {name: mds["metadata"] for name, mds in datasets_md.items()}
all_dataset_ids = list(existing_tag_sets.keys())


########################
## Dataset selection
########################


st.sidebar.markdown(
    """
# HuggingFace Dataset Tagger

This app aims to make it easier to add structured tags to the datasets present in the library.

Each configuration requires its own tasks, as these often correspond to distinct sub-tasks. However, we provide the opportunity
to pre-load the tag sets from another dataset or configuration to avoid too much redundancy.

The tag sets are saved in JSON format, but you can print a YAML version in the right-most column to copy-paste to the config README.md

### Preloading an existing tag set

You can load an existing tag set to get started if you want.
Beware that clicking pre-load will overwrite the current state!
"""
)


qp = st.experimental_get_query_params()
preload = qp.get("preload_dataset", list())
preloaded_id = None
did_index = 0
if len(preload) == 1 and preload[0] in all_dataset_ids:
    preloaded_id, *_ = preload
    state = existing_tag_sets[preloaded_id] or new_state()
    did_index = all_dataset_ids.index(preloaded_id)

did = st.sidebar.selectbox(label="Choose dataset to load tag set from", options=all_dataset_ids, index=did_index)

leftbtn, rightbtn = st.sidebar.beta_columns(2)
if leftbtn.button("pre-load tagset"):
    state = existing_tag_sets[did] or new_state()
    st.experimental_set_query_params(preload_dataset=did)
if rightbtn.button("flush state"):
    state = new_state()
    st.experimental_set_query_params()

if preloaded_id is not None:
    st.sidebar.markdown(
        f"""
Took [`{preloaded_id}`](https://huggingface.co/datasets/{preloaded_id}) as base tagset:
```yaml
{yaml.dump(state)}
```
"""
    )


leftcol, _, rightcol = st.beta_columns([12, 1, 12])


leftcol.markdown("### Supported tasks")
task_categories = multiselect(
    leftcol,
    "Task category",
    "What categories of task does the dataset support?",
    values=state["task_categories"],
    valid_set=list(task_set.keys()),
    format_func=lambda tg: f"{tg}: {task_set[tg]['description']}",
)
task_specifics = []
for tg in task_categories:
    task_specs = multiselect(
        leftcol,
        "Specific tasks",
        f"What specific *{tg}* tasks does the dataset support?",
        values=[ts for ts in state["task_ids"] if ts in task_set[tg]["options"]],
        valid_set=task_set[tg]["options"],
    )
    if "other" in task_specs:
        other_task = st.text_input(
            "You selected 'other' task. Please enter a short hyphen-separated description for the task:",
            value="my-task-description",
        )
        st.write(f"Registering {tg}-other-{other_task} task")
        task_specs[task_specs.index("other")] = f"{tg}-other-{other_task}"
    task_specifics += task_specs


leftcol.markdown("### Languages")

multilinguality = multiselect(
    leftcol,
    "Monolingual?",
    "Does the dataset contain more than one language?",
    values=state["multilinguality"],
    valid_set=list(multilinguality_set.keys()),
    format_func=lambda m: f"{m} : {multilinguality_set[m]}",
)

if "other" in multilinguality:
    other_multilinguality = st.text_input(
        "You selected 'other' type of multilinguality. Please enter a short hyphen-separated description:",
        value="my-multilinguality",
    )
    st.write(f"Registering other-{other_multilinguality} multilinguality")
    multilinguality[multilinguality.index("other")] = f"other-{other_multilinguality}"


languages = multiselect(
    leftcol,
    "Languages",
    "What languages are represented in the dataset?",
    values=state["languages"],
    valid_set=list(language_set_restricted.keys()),
    format_func=lambda m: f"{m} : {language_set_restricted[m]}",
)


leftcol.markdown("### Dataset creators")
language_creators = multiselect(
    leftcol,
    "Data origin",
    "Where does the text in the dataset come from?",
    values=state["language_creators"],
    valid_set=creator_set["language"],
)
annotations_creators = multiselect(
    leftcol,
    "Annotations origin",
    "Where do the annotations in the dataset come from?",
    values=state["annotations_creators"],
    valid_set=creator_set["annotations"],
)


licenses = multiselect(
    leftcol,
    "Licenses",
    "What licenses is the dataset under?",
    valid_set=list(license_set.keys()),
    values=state["licenses"],
    format_func=lambda l: f"{l} : {license_set[l]}",
)
if "other" in licenses:
    other_license = st.text_input(
        "You selected 'other' type of license. Please enter a short hyphen-separated description:",
        value="my-license",
    )
    st.write(f"Registering other-{other_license} license")
    licenses[licenses.index("other")] = f"other-{other_license}"

# link to supported datasets
pre_select_ext_a = []
if "original" in state["source_datasets"]:
    pre_select_ext_a += ["original"]
if any([p.startswith("extended") for p in state["source_datasets"]]):
    pre_select_ext_a += ["extended"]
extended = multiselect(
    leftcol,
    "Relations to existing work",
    "Does the dataset contain original data and/or was it extended from other datasets?",
    values=pre_select_ext_a,
    valid_set=["original", "extended"],
)
source_datasets = ["original"] if "original" in extended else []

# todo: show bad tags
if "extended" in extended:
    pre_select_ext_b = [p.split("|")[1] for p in state["source_datasets"] if p.startswith("extended")]
    extended_sources = multiselect(
        leftcol,
        "Linked datasets",
        "Which other datasets does this one use data from?",
        values=pre_select_ext_b,
        valid_set=all_dataset_ids + ["other"],
    )
    if "other" in extended_sources:
        other_extended_sources = st.text_input(
            "You selected 'other' dataset. Please enter a short hyphen-separated description:",
            value="my-dataset",
        )
        st.write(f"Registering other-{other_extended_sources} dataset")
        extended_sources[extended_sources.index("other")] = f"other-{other_extended_sources}"
    source_datasets += [f"extended|{src}" for src in extended_sources]

size_cats = ["unknown", "n<1K", "1K<n<10K", "10K<n<100K", "100K<n<1M", "n>1M"]
current_size_cats = state.get("size_categories") or ["unknown"]
ok, nonok = split_known(current_size_cats, size_cats)
if len(nonok) > 0:
    leftcol.markdown(f"**Found bad codes in existing tagset**:\n{nonok}")
size_category = leftcol.selectbox(
    "What is the size category of the dataset?",
    options=size_cats,
    index=size_cats.index(ok[0]) if len(ok) > 0 else 0,
)


########################
## Show results
########################
yamlblock = yaml.dump(
    {
        "task_categories": task_categories,
        "task_ids": task_specifics,
        "multilinguality": multilinguality,
        "languages": languages,
        "language_creators": language_creators,
        "annotations_creators": annotations_creators,
        "source_datasets": source_datasets,
        "size_categories": size_category,
        "licenses": licenses,
    }
)
rightcol.markdown(
    f"""
### Finalized tag set

Copy it into your dataset's `README.md` header! 🤗 

```yaml
{yamlblock}
```""",
)