File size: 23,266 Bytes
eff02e0
 
c92549e
 
 
eff02e0
 
 
 
 
 
 
 
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
d0b26e9
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b26e9
 
 
 
 
 
 
eff02e0
 
 
d0b26e9
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c92549e
 
 
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b26e9
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c92549e
 
eff02e0
c92549e
 
eff02e0
 
 
 
d0b26e9
c92549e
 
 
 
 
d0b26e9
 
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b26e9
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b26e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eff02e0
 
 
 
 
 
 
 
d0b26e9
 
 
 
eff02e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482c841
eff02e0
c92549e
 
 
d0b26e9
c92549e
 
 
eff02e0
d0b26e9
 
 
 
 
eff02e0
 
 
 
 
 
c92549e
 
 
 
 
 
d0b26e9
c92549e
 
 
eff02e0
d0b26e9
 
 
 
 
eff02e0
 
 
 
d0b26e9
eff02e0
 
 
c92549e
eff02e0
 
 
 
c92549e
eff02e0
 
 
 
c92549e
eff02e0
 
 
 
 
 
482c841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
 "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_da4e86ed-bac9-440c-ae5e-29e551e62ec0\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_code/20230404_102116_00007_ajvns_2a7caa57-9adc-48f6-900e-f87f572f8c3b\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_github_issues/20230404_102127_00022_yv77i_4b3257ed-3e44-4961-bd02-017d135e96f0\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/bigcode_python_jupyter_markdowned_clean_dedup/20230404_102137_00026_vwcg7_8778ba21-a464-4949-8d71-aa1414a45d3c\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/books3/20230404_102143_00027_t4kwf_b39fa726-6484-4103-a9a3-fd8774796e75\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/gutenberg_raw/20230404_102215_00007_x3ntt_ddbaef74-459c-40a0-8b8f-d2f17af55991\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/reddit_threaded/20230404_102241_00049_xj4uk_61f1e105-1765-4c37-a659-5895ca3398e2\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/enwiki_data/20230404_102246_00007_ye63c_c3bd1037-1438-4ab3-97cd-24fd8ede501a\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/s2orc_dedup/20230404_102252_00080_6ce5q_c45e4ff8-83fe-4b65-b5ae-f52e2b27e96c\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/stackexchange2/20230404_102308_00031_qvnh6_fc1b4f61-9b84-481f-95bc-d7e0b8542030\n",
      "Number of parquet files 30\n",
      "Reading geclm-datasets/samples/commoncrawl/20230404_124237_00026_sin5w_96df9c84-d8b3-454c-a3ce-ad9550da36bc\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"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'record_id': 'd80741ab-54df-4bb9-b566-0855bb1c7e87', 'crawl': 'CC-MAIN-2022-05', 'date': '20220121', 'segment': '1642320302715.38', 'warc_path': 'crawl-data/CC-MAIN-2022-05/segments/1642320302715.38/warc/CC-MAIN-20220121010736-20220121040736-00393.warc.gz', 'record_timestamp': Timestamp('2022-01-21 03:09:58'), 'title': \"CYLQ Speed Skating Shoes for Adult Professional Performance Japan's largest assortment High\", 'text': \"Speed,$162,Shoes,Skating,/heptarchy1466806.html,Performance,CYLQ,High,contgraf.com.br,Professional,Sports Outdoors , Outdoor Recreation,Adult,for Speed,$162,Shoes,Skating,/heptarchy1466806.html,Performance,CYLQ,High,contgraf.com.br,Professional,Sports Outdoors , Outdoor Recreation,Adult,for CYLQ Speed Skating Shoes for Adult Professional Performance Japan's largest assortment High $162 CYLQ Speed Skating Shoes for Adult Professional High Performance Sports Outdoors Outdoor Recreation CYLQ Speed Skating Shoes for Adult Professional Performance Japan's largest assortment High $162 CYLQ Speed Skating Shoes for Adult Professional High Performance Sports Outdoors Outdoor Recreation\\n\\nCYLQ Speed Skating Shoes for Adult Professional High Performance\\n\\n$162\\n\\nCYLQ Speed Skating Shoes for Adult Professional High Performance\\n\\n  * Make sure this fits by entering your model number.\\n  * ã\\x80\\x90Inline Speed Skatesã\\x80\\x91: Adults Buy According To The Size Of The Usual Sports Shoes, If Your Feet Are Fat Or Novice, It Is Recommended To Buy Shoes That Are 1 Size Larger!\\n  * ã\\x80\\x90Performanceã\\x80\\x91: High-Elastic And Wear-Resistant Pu Wheelï¼\\x8cPrecision Bearing, One-Piece Bracket, One-Piece Molding, So That You Can Enjoy The Smooth, Quick And Comfortable Ride\\n  * ã\\x80\\x90High-Elastic Wear-Resistant Pu Wheelsã\\x80\\x91: 85a High-Elastic Wear-Resistant Wheels, Eu30-Eu33: 4 * 90mm, Eu34-Eu37: 4 * 100mm, Eu38-Eu45: 4 * 110mm For Better Wear And Moderate Speed\\n  * ã\\x80\\x90Perfect Giftsã\\x80\\x91: Inline Skates Are Popular With Children And Adults, You Can Give It To Your Friends And Family As A Gift On Major Holidays Or Birthdays, They Will All Like It Very Much\\n  * ã\\x80\\x90Enjoy Skatingã\\x80\\x91: The Perfect Fit, A High Degree Of Comfort, And Smooth Handling Make The Inline Roller Skates Ideal For All Sports-Loving Children, Beginners And Skaters.\\n\\n|||\\n\\nCYLQ Speed Skating Shoes for Adult Professional High Performance\\n\\nDonate to help us beat cancer\\n\\nGet involved and support cancer research\\n\\nCancer is relentless. But so are we.\\u200b Whether you fundraise, volunteer, pledge to leave a Gift in your Will or donate, everyone has a part to play. And every part supports life-saving research. Play your part and together we will beat cancer.\\u200b\\n\\nIf you've been diagnosed with cancer, or know someone who has, we provide practical advice on everything from symptoms and screening, to coping after treatment.\\n\\nIt’s a worrying time for many people and we want to be there for you whenever - and wherever - you need us. Cancer Chat is our fully moderated forum where you can talk to others affected by cancer, share experiences, and get support. Cancer Chat is free to join and available 24 hours a day.\", 'tokens': ['speed', '162', 'shoes', 'skating', 'heptarchy1466806', 'html', 'performance', 'cylq', 'high', 'contgraf', 'com', 'br', 'professional', 'sports', 'outdoors', 'outdoor', 'recreation', 'adult', 'for', 'speed', '162', 'shoes', 'skating', 'heptarchy1466806', 'html', 'performance', 'cylq', 'high', 'contgraf', 'com', 'br', 'professional', 'sports', 'outdoors', 'outdoor', 'recreation', 'adult', 'for', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'performance', 'japan', 's', 'largest', 'assortment', 'high', '162', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'high', 'performance', 'sports', 'outdoors', 'outdoor', 'recreation', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'performance', 'japan', 's', 'largest', 'assortment', 'high', '162', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'high', 'performance', 'sports', 'outdoors', 'outdoor', 'recreation', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'high', 'performance', '162', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'high', 'performance', 'make', 'sure', 'this', 'fits', 'by', 'entering', 'your', 'model', 'number', 'ã', 'inline', 'speed', 'skatesã', 'adults', 'buy', 'according', 'to', 'the', 'size', 'of', 'the', 'usual', 'sports', 'shoes', 'if', 'your', 'feet', 'are', 'fat', 'or', 'novice', 'it', 'is', 'recommended', 'to', 'buy', 'shoes', 'that', 'are', '1', 'size', 'larger', 'ã', 'performanceã', 'high', 'elastic', 'and', 'wear', 'resistant', 'pu', 'wheelï¼', 'precision', 'bearing', 'one', 'piece', 'bracket', 'one', 'piece', 'molding', 'so', 'that', 'you', 'can', 'enjoy', 'the', 'smooth', 'quick', 'and', 'comfortable', 'ride', 'ã', 'high', 'elastic', 'wear', 'resistant', 'pu', 'wheelsã', '85a', 'high', 'elastic', 'wear', 'resistant', 'wheels', 'eu30', 'eu33', '4', '90mm', 'eu34', 'eu37', '4', '100mm', 'eu38', 'eu45', '4', '110mm', 'for', 'better', 'wear', 'and', 'moderate', 'speed', 'ã', 'perfect', 'giftsã', 'inline', 'skates', 'are', 'popular', 'with', 'children', 'and', 'adults', 'you', 'can', 'give', 'it', 'to', 'your', 'friends', 'and', 'family', 'as', 'a', 'gift', 'on', 'major', 'holidays', 'or', 'birthdays', 'they', 'will', 'all', 'like', 'it', 'very', 'much', 'ã', 'enjoy', 'skatingã', 'the', 'perfect', 'fit', 'a', 'high', 'degree', 'of', 'comfort', 'and', 'smooth', 'handling', 'make', 'the', 'inline', 'roller', 'skates', 'ideal', 'for', 'all', 'sports', 'loving', 'children', 'beginners', 'and', 'skaters', 'cylq', 'speed', 'skating', 'shoes', 'for', 'adult', 'professional', 'high', 'performance', 'donate', 'to', 'help', 'us', 'beat', 'cancer', 'get', 'involved', 'and', 'support', 'cancer', 'research', 'cancer', 'is', 'relentless', 'but', 'so', 'are', 'we', 'whether', 'you', 'fundraise', 'volunteer', 'pledge', 'to', 'leave', 'a', 'gift', 'in', 'your', 'will', 'or', 'donate', 'everyone', 'has', 'a', 'part', 'to', 'play', 'and', 'every', 'part', 'supports', 'life', 'saving', 'research', 'play', 'your', 'part', 'and', 'together', 'we', 'will', 'beat', 'cancer', 'if', 'you', 've', 'been', 'diagnosed', 'with', 'cancer', 'or', 'know', 'someone', 'who', 'has', 'we', 'provide', 'practical', 'advice', 'on', 'everything', 'from', 'symptoms', 'and', 'screening', 'to', 'coping', 'after', 'treatment', 'it', 's', 'a', 'worrying', 'time', 'for', 'many', 'people', 'and', 'we', 'want', 'to', 'be', 'there', 'for', 'you', 'whenever', 'and', 'wherever', 'you', 'need', 'us', 'cancer', 'chat', 'is', 'our', 'fully', 'moderated', 'forum', 'where', 'you', 'can', 'talk', 'to', 'others', 'affected', 'by', 'cancer', 'share', 'experiences', 'and', 'get', 'support', 'cancer', 'chat', 'is', 'free', 'to', 'join', 'and', 'available', '24', 'hours', 'a', 'day'], 'num_tokens': 420, 'language': 'en', 'lang_prob': 0.8244870901107788, 'warc_headers': [('WARC-Warcinfo-ID', '<urn:uuid:a976e930-019b-408f-adf8-425b0ee64b69>'), ('WARC-Payload-Digest', 'sha1:I3KVL3FTWZFX2Q4EXW5KTHT6H2KKVPGW'), ('WARC-IP-Address', '191.6.209.164'), ('WARC-Block-Digest', 'sha1:YAOSZBUDDYZAWSOA6C3Q7MQ2RGX7A32L'), ('WARC-Date', '2022-01-21T03:09:58Z'), ('Content-Type', 'application/http; msgtype=response'), ('WARC-Type', 'response'), ('WARC-Concurrent-To', '<urn:uuid:65bd6506-f28a-4ceb-9f6f-84243063aaaa>'), ('WARC-Identified-Payload-Type', 'text/html'), ('WARC-Record-ID', '<urn:uuid:d80741ab-54df-4bb9-b566-0855bb1c7e87>'), ('Content-Length', '60568'), ('WARC-Target-URI', 'http://contgraf.com.br/heptarchy1466806.html')], 'http_headers': [('Connection', 'Keep-Alive'), ('Keep-Alive', 'timeout=5, max=500'), ('Content-Type', 'text/html'), ('X-Crawler-Transfer-Encoding', 'chunked'), ('Date', 'Fri, 21 Jan 2022 03:09:57 GMT'), ('Vary', 'Accept-Encoding'), ('Server', 'Apache'), ('X-Crawler-Content-Encoding', 'gzip'), ('Content-Length', '60297')], 'mime_type': 'text/html', 'charset': 'UTF-8', 'url_surtkey': 'br,com,contgraf)/heptarchy1466806.html', 'url': 'http://contgraf.com.br/heptarchy1466806.html', 'url_host': 'contgraf.com.br', 'url_scheme': 'http', 'url_path': '/heptarchy1466806.html', 'text_len': 2646, 'avg_line_len': 154.05882263183594, 'avg_paragraph_len': 218.6666717529297, 'num_words': 420, 'bullet_lines_frac': 0.0, 'ellipsis_lines_frac': 0.0, 'avg_word_len': 5.019047737121582, 'hash_word_ratio': 0.0, 'ellipsis_word_ratio': 0.0, 'alpha_words_frac': 0.976190447807312, 'num_stopwords': 7, 'dupe_line_frac': 0.125, 'dupe_line_char_frac': 0.05065294727683067, 'dupe_par_frac': 0.1818181872367859, 'dupe_par_char_frac': 0.05065294727683067, 'top_dupe_2gram_char_frac': 0.02770083025097847, 'top_dupe_3gram_char_frac': 0.049861494451761246, 'top_dupe_4gram_char_frac': 0.06648199260234833, 'top_dupe_5gram_char_frac': 0.07756232470273972, 'top_dupe_6gram_char_frac': 0.0941828265786171, 'all_dupe_4gram_char_frac': 0.20419469475746155, 'all_dupe_5gram_char_frac': 0.17491096258163452, 'all_dupe_6gram_char_frac': 0.17609813809394836, 'all_dupe_7gram_char_frac': 0.17055797576904297, 'all_dupe_8gram_char_frac': 0.17530669271945953, 'all_dupe_9gram_char_frac': 0.2030075192451477, 'all_dupe_10gram_char_frac': 0.14087851345539093, 'q_logit': -3.212052583694458, 'q_probability': 0.03871467337012291}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "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",
    "BASE_S3_DIR = \"s3://geclm-datasets/samples/\"\n",
    "LABELLING_COMPLETE_TEXT = (\n",
    "    \"Completed the labelling the sample for the {} dataset. Please consider labelling other datasets.\"\n",
    ")\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=1000):\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",
    "# local_lines = {dataset: get_local_lines(dataset) for dataset in DATASETS}\n",
    "# line_generators_local = {dataset: line_generator(local_lines, dataset) for dataset in DATASETS}\n",
    "\n",
    "# Parallelize the below ?\n",
    "s3_lines = {dataset: get_parquet_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",
    "    print(sample)\n",
    "    text_col = \"text\"\n",
    "    if text_col not in sample:\n",
    "        text_col = \"content\"\n",
    "    text = sample[text_col]\n",
    "    sample.pop(text_col)\n",
    "    if \"record_timestamp\" in sample:\n",
    "        sample.pop(\"record_timestamp\")\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\": json.dumps(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",
    "def get_title_and_text_for_line(next_line):\n",
    "    text_col = \"text\"\n",
    "    if text_col not in next_line:\n",
    "        text_col = \"content\"\n",
    "    text = next_line[text_col]\n",
    "\n",
    "    label = \"\"\n",
    "    if \"title\" in next_line:\n",
    "        label = next_line[\"title\"]\n",
    "        if \"url\" in next_line:\n",
    "            label += \" | \" + next_line[\"url\"]\n",
    "    elif \"metadata\" in next_line:\n",
    "        if next_line[\"metadata\"] is not None:\n",
    "            print(next_line[\"metadata\"])\n",
    "            if isinstance(next_line[\"metadata\"], list) and len(next_line[\"metadata\"]) > 0:\n",
    "                label = next_line[\"metadata\"][0]\n",
    "            elif isinstance(next_line[\"metadata\"], str):\n",
    "                metadata = json.loads(next_line[\"metadata\"])\n",
    "                if \"document_url\" in metadata:\n",
    "                    label = metadata[\"document_url\"]\n",
    "    elif \"url\" in next_line:\n",
    "        label = next_line[\"url\"]\n",
    "\n",
    "    return text, label\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(\n",
    "            value=\"\"\"GecLM annotations. All annotations are recorded in the [data_feedback](https://huggingface.co/datasets/HuggingFaceGECLM/data_feedback) dataset.\n",
    "\"\"\",\n",
    "        )\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, max_lines=500)\n",
    "\n",
    "        def get_next_line(dataset):\n",
    "            try:\n",
    "                next_line = next(line_generators_s3[dataset])\n",
    "                text, label = get_title_and_text_for_line(next_line)\n",
    "            except StopIteration:\n",
    "                text = LABELLING_COMPLETE_TEXT.format(dataset)\n",
    "                next_line = text\n",
    "            return [\n",
    "                gr.update(\n",
    "                    value=text,\n",
    "                    visible=True,\n",
    "                    label=label,\n",
    "                ),\n",
    "                next_line,\n",
    "                gr.update(visible=True),\n",
    "                gr.update(visible=True),\n",
    "                gr.update(visible=True),\n",
    "            ]\n",
    "\n",
    "        def report_bad_line_and_next(current_sample, dataset, reason, annotator, campaign):\n",
    "            if current_sample != LABELLING_COMPLETE_TEXT.format(dataset):\n",
    "                send_report(current_sample, dataset, reason, annotator, campaign)\n",
    "\n",
    "            try:\n",
    "                next_line = next(line_generators_s3[dataset])\n",
    "                text, label = get_title_and_text_for_line(next_line)\n",
    "            except StopIteration:\n",
    "                text = LABELLING_COMPLETE_TEXT.format(dataset)\n",
    "                next_line = text\n",
    "            return [\n",
    "                gr.update(\n",
    "                    value=text,\n",
    "                    visible=True,\n",
    "                    label=label,\n",
    "                ),\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",
    "            get_next_line,\n",
    "            inputs=dataset,\n",
    "            outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],\n",
    "        )\n",
    "        dataset.change(\n",
    "            get_next_line,\n",
    "            inputs=dataset,\n",
    "            outputs=[text, current_sample_state, reason_txt, good_btn, bad_btn],\n",
    "        )\n",
    "        bad_btn.click(\n",
    "            report_bad_line_and_next,\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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4e6b194c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.23.0'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gr.__version__"
   ]
  }
 ],
 "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
}