File size: 11,171 Bytes
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "585da432",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/c4/20230404_102105_00007_t8w9z_3085d601-45f1-443a-b50d-8eb4812dd227\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_code/20230404_102116_00007_ajvns_4e5b2899-8640-4a4c-b0cd-758662178176\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_github_issues/20230404_102127_00022_yv77i_982f928f-1431-4ea7-986d-c5c5cb0f4a3f\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_jupyter_markdowned_clean_dedup/20230404_102137_00026_vwcg7_3167c932-87a1-4fec-ad01-215831d0bf6e\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/books3/20230404_102143_00027_t4kwf_198fc997-b871-4e4a-b88e-3776f1cf92fe\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/gutenberg_raw/20230404_102215_00007_x3ntt_30873bfe-c94c-439a-96e2-71165570dc99\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/reddit_threaded/20230404_102241_00049_xj4uk_d7612f5a-5107-46e1-b710-47e7db95a7e6\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/enwiki_data/20230404_102246_00007_ye63c_57166ca6-f0d2-40ef-8ae7-ed4bc7ecd28d\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/s2orc_dedup/20230404_102252_00080_6ce5q_330e23f7-1270-4a52-b277-af823baf1de6\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/stackexchange2/20230404_102308_00031_qvnh6_cec28e17-f163-4a04-9fbe-dc617d9ea03e\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/commoncrawl/20230404_124237_00026_sin5w_c2e65b68-2449-47fa-be8b-a6e6e83611d0\n",
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import math\n",
    "import os\n",
    "import random\n",
    "import uuid\n",
    "from datetime import datetime\n",
    "\n",
    "import gradio as gr\n",
    "import jsonlines\n",
    "import pyarrow as pa\n",
    "import s3fs\n",
    "from datasets import Dataset\n",
    "from huggingface_hub import HfApi\n",
    "\n",
    "S3 = s3fs.S3FileSystem(anon=False, key=os.getenv(\"AWS_ACCESS_KEY_ID\"), secret=os.getenv(\"AWS_SECRET_ACCESS_KEY\"))\n",
    "\n",
    "DEFAULT_SHUFFLE_BUFFER_SIZE_RATIO = 5\n",
    "BASE_S3_DIR = \"s3://geclm-datasets/samples/\"\n",
    "\n",
    "DATASETS = [\n",
    "    \"c4\",\n",
    "    \"bigcode_python_code\",\n",
    "    \"bigcode_python_github_issues\",\n",
    "    \"bigcode_python_jupyter_markdowned_clean_dedup\",\n",
    "    \"books3\",\n",
    "    \"gutenberg_raw\",\n",
    "    \"reddit_threaded\",\n",
    "    \"enwiki_data\",\n",
    "    \"s2orc_dedup\",\n",
    "    \"stackexchange2\",\n",
    "    \"commoncrawl\",\n",
    "]\n",
    "\n",
    "\n",
    "def get_parquet_lines(dataset, sample_size=100):\n",
    "    s3_paths = S3.glob(BASE_S3_DIR + dataset + \"/*\")\n",
    "\n",
    "    if len(s3_paths) == 0:\n",
    "        raise FileNotFoundError(f\"Nothing found at {path}\")\n",
    "\n",
    "    print(\"Number of parquet files\", len(s3_paths))\n",
    "    s3_path = random.choice(s3_paths)\n",
    "    print(\"Reading\", s3_path)\n",
    "    lines = []\n",
    "\n",
    "    with S3.open(s3_path) as f:\n",
    "        pf = pa.parquet.ParquetFile(f)\n",
    "        for ix_row_group in range(pf.metadata.num_row_groups):\n",
    "            # We load dataset by row group - 1000 rows at a time\n",
    "            # using open_input_stream would return bytes per bytes not row per row\n",
    "            table = pf.read_row_group(ix_row_group)\n",
    "            lines.extend(table.to_pylist())\n",
    "\n",
    "    random.shuffle(lines)\n",
    "    return lines[:sample_size]\n",
    "\n",
    "\n",
    "def get_local_lines(dataset):\n",
    "    lines = []\n",
    "    with jsonlines.open(\"data/{}_examples_with_stats.json\".format(dataset), \"r\") as f:\n",
    "        for line in f:\n",
    "            lines.append(line)\n",
    "    return lines\n",
    "\n",
    "\n",
    "def line_generator(lines_dict, dataset):\n",
    "    for line in lines_dict[dataset]:\n",
    "        yield line\n",
    "\n",
    "\n",
    "# Parallelize the below\n",
    "local_lines = {dataset: get_local_lines(dataset) for dataset in DATASETS}\n",
    "s3_lines = {dataset: get_parquet_lines(dataset) for dataset in DATASETS}\n",
    "\n",
    "line_generators_local = {dataset: line_generator(local_lines, dataset) for dataset in DATASETS}\n",
    "line_generators_s3 = {dataset: line_generator(s3_lines, dataset) for dataset in DATASETS}\n",
    "\n",
    "\n",
    "def send_report(sample, dataset, reason, annotator, campaign):\n",
    "    text = sample[\"text\"]\n",
    "    sample.pop(\"text\")\n",
    "\n",
    "    sample_id = \"\"\n",
    "    if \"id\" not in sample:\n",
    "        if \"title\" in sample:\n",
    "            sample_id = sample[\"title\"]\n",
    "    else:\n",
    "        sample_id = sample[\"id\"]\n",
    "\n",
    "    with jsonlines.open(\"report.jsonl\", \"w\") as f:\n",
    "        f.write(\n",
    "            {\n",
    "                \"dataset\": dataset,\n",
    "                \"docid\": sample_id,\n",
    "                \"text\": text,\n",
    "                \"metadata\": sample,\n",
    "                \"reason\": reason,\n",
    "                \"annotator\": annotator,\n",
    "                \"campaign\": campaign,\n",
    "                \"timestamp\": str(datetime.now()),\n",
    "            }\n",
    "        )\n",
    "\n",
    "    api = HfApi()\n",
    "    api.upload_file(\n",
    "        path_or_fileobj=\"report.jsonl\",\n",
    "        path_in_repo=\"report-{}.jsonl\".format(uuid.uuid4()),\n",
    "        repo_id=\"HuggingFaceGECLM/data_feedback\",\n",
    "        repo_type=\"dataset\",\n",
    "        token=os.environ.get(\"geclm_token\"),\n",
    "    )\n",
    "\n",
    "\n",
    "description = \"\"\"\n",
    "GecLM annotations. All annotations are recorded in the [data_feedback](https://huggingface.co/datasets/HuggingFaceGECLM/data_feedback) dataset.\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    demo = gr.Blocks()\n",
    "\n",
    "    with demo:\n",
    "        current_sample_state = gr.State(dict())\n",
    "\n",
    "        description = gr.Markdown(value=description)\n",
    "        with gr.Row():\n",
    "            annotator = gr.Textbox(\n",
    "                lines=1,\n",
    "                max_lines=1,\n",
    "                placeholder=\"Optionally provide your name here if you'd like it to be recorded.\",\n",
    "                label=\"Annotator\",\n",
    "            )\n",
    "            campaign = gr.Textbox(\n",
    "                lines=1,\n",
    "                max_lines=1,\n",
    "                placeholder=\"Optionally provide the name of the annotation campagin for ease of filtering the reports.\",\n",
    "                label=\"Annotation campaign\",\n",
    "            )\n",
    "        with gr.Row():\n",
    "            dataset = gr.Dropdown(\n",
    "                choices=DATASETS,\n",
    "                value=\"Pick a dataset below\",\n",
    "                label=\"Dataset\",\n",
    "            )\n",
    "        with gr.Row():\n",
    "            reason_txt = gr.Textbox(\n",
    "                label=\"Flagging reason\",\n",
    "                placeholder=\"Provide the reason for flagging if you think the sample is bad.\",\n",
    "                visible=False,\n",
    "            )\n",
    "        with gr.Row():\n",
    "            bad_btn = gr.Button(\"Bad ❌\", visible=False)\n",
    "            good_btn = gr.Button(\"Next ✅\", visible=False)\n",
    "        with gr.Row():\n",
    "            text = gr.Textbox(visible=False, label=\"Datapoint\", lines=500)\n",
    "\n",
    "        def next_line(dataset):\n",
    "            next_line = next(line_generators_s3[dataset])\n",
    "\n",
    "            text_col = \"text\"\n",
    "            if text_col not in next_line:\n",
    "                text_col = \"content\"\n",
    "            return [\n",
    "                gr.update(value=next_line[text_col], visible=True),\n",
    "                next_line,\n",
    "                gr.update(visible=True),\n",
    "                gr.update(visible=True),\n",
    "                gr.update(visible=True),\n",
    "            ]\n",
    "\n",
    "        def bad_line(current_sample, dataset, reason, annotator, campaign):\n",
    "            send_report(current_sample, dataset, reason, annotator, campaign)\n",
    "            next_line = next(line_generators_s3[dataset])\n",
    "            text_col = \"text\"\n",
    "            if text_col not in next_line:\n",
    "                text_col = \"content\"\n",
    "            return [\n",
    "                next_line[text_col],\n",
    "                gr.update(\n",
    "                    value=\"\",\n",
    "                    placeholder=\"Provide the reason for flagging if you think the sample is bad.\",\n",
    "                ),\n",
    "                next_line,\n",
    "            ]\n",
    "\n",
    "        good_btn.click(\n",
    "            next_line,\n",
    "            inputs=dataset,\n",
    "            outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],\n",
    "        )\n",
    "        dataset.change(\n",
    "            next_line,\n",
    "            inputs=dataset,\n",
    "            outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],\n",
    "        )\n",
    "        bad_btn.click(\n",
    "            bad_line,\n",
    "            inputs=[current_sample_state, dataset, reason_txt, annotator, campaign],\n",
    "            outputs=[text, reason_txt, current_sample_state],\n",
    "        )\n",
    "\n",
    "    demo.launch(enable_queue=False, debug=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}