Spaces:
Runtime error
Runtime error
File size: 7,947 Bytes
d0b26e9 eff02e0 de3513e eff02e0 de3513e eff02e0 de3513e eff02e0 de3513e eff02e0 c92549e eff02e0 de3513e eff02e0 de3513e eff02e0 de3513e eff02e0 de3513e c92549e eff02e0 c92549e eff02e0 c92549e eff02e0 de3513e e16ff40 c92549e d0b26e9 de3513e d0b26e9 de3513e e16ff40 de3513e a4a0058 de3513e d0b26e9 e16ff40 de3513e d0b26e9 de3513e e16ff40 de3513e e16ff40 de3513e eff02e0 de3513e a322536 de3513e c92549e d0b26e9 c92549e de3513e d0b26e9 de3513e c92549e d0b26e9 c92549e de3513e d0b26e9 de3513e c92549e de3513e c92549e de3513e c92549e e16ff40 de3513e |
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 |
import json
import math
import os
import random
import uuid
from datetime import datetime
import gradio as gr
import jsonlines
import pyarrow as pa
import s3fs
from datasets import Dataset
from huggingface_hub import HfApi
S3 = s3fs.S3FileSystem(anon=False, key=os.getenv("AWS_ACCESS_KEY_ID"), secret=os.getenv("AWS_SECRET_ACCESS_KEY"))
BASE_S3_DIR = "s3://geclm-datasets/samples/"
LABELLING_COMPLETE_TEXT = (
"Completed the labelling the sample for the {} dataset. Please consider labelling other datasets."
)
DATASETS = [
"c4",
"bigcode_python_code",
"bigcode_python_github_issues",
"bigcode_python_jupyter_markdowned_clean_dedup",
"books3",
"gutenberg_raw",
"reddit_threaded",
"enwiki_data",
"s2orc_dedup",
"stackexchange2",
"commoncrawl",
]
def get_parquet_lines(dataset, sample_size=1000):
s3_paths = S3.glob(BASE_S3_DIR + dataset + "/*")
if len(s3_paths) == 0:
raise FileNotFoundError(f"Nothing found at {path}")
print("Number of parquet files", len(s3_paths))
s3_path = random.choice(s3_paths)
print("Reading", s3_path)
lines = []
with S3.open(s3_path) as f:
pf = pa.parquet.ParquetFile(f)
for ix_row_group in range(pf.metadata.num_row_groups):
# We load dataset by row group - 1000 rows at a time
# using open_input_stream would return bytes per bytes not row per row
table = pf.read_row_group(ix_row_group)
lines.extend(table.to_pylist())
random.shuffle(lines)
return lines[:sample_size]
def get_local_lines(dataset):
lines = []
with jsonlines.open("data/{}_examples_with_stats.json".format(dataset), "r") as f:
for line in f:
lines.append(line)
return lines
def line_generator(lines_dict, dataset):
for line in lines_dict[dataset]:
yield line
# local_lines = {dataset: get_local_lines(dataset) for dataset in DATASETS}
# line_generators_local = {dataset: line_generator(local_lines, dataset) for dataset in DATASETS}
# Parallelize the below ?
s3_lines = {dataset: get_parquet_lines(dataset) for dataset in DATASETS}
line_generators_s3 = {dataset: line_generator(s3_lines, dataset) for dataset in DATASETS}
def send_report(sample, dataset, reason, annotator, campaign):
text_col = "text"
if text_col not in sample:
text_col = "content"
text = sample[text_col]
sample.pop(text_col)
if "record_timestamp" in sample:
sample.pop("record_timestamp")
sample_id = ""
if "id" not in sample:
if "title" in sample:
sample_id = sample["title"]
else:
sample_id = sample["id"]
with jsonlines.open("report.jsonl", "w") as f:
f.write(
{
"dataset": dataset,
"docid": sample_id,
"text": text,
"metadata": json.dumps(sample),
"reason": reason,
"annotator": annotator,
"campaign": campaign,
"timestamp": str(datetime.now()),
}
)
api = HfApi()
api.upload_file(
path_or_fileobj="report.jsonl",
path_in_repo="report-{}.jsonl".format(uuid.uuid4()),
repo_id="HuggingFaceGECLM/data_feedback",
repo_type="dataset",
token=os.environ.get("geclm_token"),
)
def get_title_and_text_for_line(next_line):
text_col = "text"
if text_col not in next_line:
text_col = "content"
text = next_line[text_col]
label = ""
if "title" in next_line:
label = next_line["title"]
if "url" in next_line:
label += " | " + next_line["url"]
elif "metadata" in next_line:
if next_line["metadata"] is not None:
print(next_line["metadata"])
if isinstance(next_line["metadata"], list) and len(next_line["metadata"]) > 0:
label = next_line["metadata"][0]
elif isinstance(next_line["metadata"], str):
metadata = json.loads(next_line["metadata"])
if "document_url" in metadata:
label = metadata["document_url"]
elif "url" in next_line:
label = next_line["url"]
return text, label
if __name__ == "__main__":
demo = gr.Blocks()
with demo:
current_sample_state = gr.State(dict())
description = gr.Markdown(
value="""GecLM annotations. All annotations are recorded in the [data_feedback](https://huggingface.co/datasets/HuggingFaceGECLM/data_feedback) dataset.
""",
)
with gr.Row():
annotator = gr.Textbox(
lines=1,
max_lines=1,
placeholder="Optionally provide your name here if you'd like it to be recorded.",
label="Annotator",
)
campaign = gr.Textbox(
lines=1,
max_lines=1,
placeholder="Optionally provide the name of the annotation campagin for ease of filtering the reports.",
label="Annotation campaign",
)
with gr.Row():
dataset = gr.Dropdown(
choices=DATASETS,
value="Pick a dataset below",
label="Dataset",
)
with gr.Row():
reason_txt = gr.Textbox(
label="Flagging reason",
placeholder="Provide the reason for flagging if you think the sample is bad.",
visible=False,
)
with gr.Row():
bad_btn = gr.Button("Bad ❌", visible=False)
good_btn = gr.Button("Next ✅", visible=False)
with gr.Row():
text = gr.Textbox(visible=False, label="Datapoint", lines=500, max_lines=500)
def get_next_line(dataset):
try:
next_line = next(line_generators_s3[dataset])
text, label = get_title_and_text_for_line(next_line)
except StopIteration:
text = LABELLING_COMPLETE_TEXT.format(dataset)
next_line = text
return [
gr.update(
value=text,
visible=True,
label=label,
),
next_line,
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
]
def report_bad_line_and_next(current_sample, dataset, reason, annotator, campaign):
if current_sample != LABELLING_COMPLETE_TEXT.format(dataset):
send_report(current_sample, dataset, reason, annotator, campaign)
try:
next_line = next(line_generators_s3[dataset])
text, label = get_title_and_text_for_line(next_line)
except StopIteration:
text = LABELLING_COMPLETE_TEXT.format(dataset)
next_line = text
return [
gr.update(
value=text,
visible=True,
label=label,
),
gr.update(
value="",
placeholder="Provide the reason for flagging if you think the sample is bad.",
),
next_line,
]
good_btn.click(
get_next_line,
inputs=dataset,
outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],
)
dataset.change(
get_next_line,
inputs=dataset,
outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],
)
bad_btn.click(
report_bad_line_and_next,
inputs=[current_sample_state, dataset, reason_txt, annotator, campaign],
outputs=[text, reason_txt, current_sample_state],
)
demo.launch(enable_queue=False, debug=True)
|