From f0ea6d82bdb242c0a60e3f01db0c5f814166449b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Schmelczer?= Date: Fri, 27 May 2022 11:25:27 +0200 Subject: [PATCH] Add data engineer script --- examples/simple/data.ipynb | 341 +++++++++++++++++++++++++++++++++++++ 1 file changed, 341 insertions(+) create mode 100644 examples/simple/data.ipynb diff --git a/examples/simple/data.ipynb b/examples/simple/data.ipynb new file mode 100644 index 0000000..3f1bb72 --- /dev/null +++ b/examples/simple/data.ipynb @@ -0,0 +1,341 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Train a domain classifier on the [semantic scholar dataset](https://api.semanticscholar.org/corpus)\n", + "\n", + "## Part 1: obtain clean data\n", + "\n", + "This can be achieved by downloading a public dataset (such as in this case), or by having a Data Engineer setup and give us access to the organisations's data.\n", + "\n", + "1. **Extract**: Download the semantic scholar dataset from a public S3 bucket\n", + "2. **Transform**: Project it to only keep the necessary components (text and labels), clean the textual content using `great_ai.utilities.clean`\n", + "3. **Load**: Upload the dataset (or a part of it) to a shared infrastructure using `great_ai.LargeFile` with its S3 backend\n", + "\n", + "> We will speed up the processing using `great_ai.utilities.parallel_map`" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[38;5;39m2022-05-27 11:02:29,131 | INFO | Environment variable ENVIRONMENT is not set, defaulting to development mode\u001b[0m\n", + "\u001b[38;5;226m2022-05-27 11:02:29,132 | WARNING | Running in development mode ‼️\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:02:29,133 | INFO | Options: configured ✅\u001b[0m\n" + ] + } + ], + "source": [ + "import json\n", + "from typing import List, Tuple\n", + "import urllib.request\n", + "from itertools import chain\n", + "import gzip\n", + "from great_ai.utilities.clean import clean\n", + "from great_ai.utilities.parallel_map import parallel_map\n", + "from great_ai.utilities.language import is_english, predict_language\n", + "from great_ai import configure, LargeFile\n", + "\n", + "\n", + "DATASET_KEY = \"semantic-scholar-dataset-small\"\n", + "MAX_FILE_COUNT = 12\n", + "\n", + "configure()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# Download the list of available chunks\n", + "\n", + "manifest = (\n", + " urllib.request.urlopen(\n", + " \"https://s3-us-west-2.amazonaws.com/ai2-s2-research-public/open-corpus/2022-02-01/manifest.txt\"\n", + " )\n", + " .read()\n", + " .decode()\n", + ")\n", + "chunks = manifest.split()[:MAX_FILE_COUNT]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[38;5;39m2022-05-27 11:02:30,364 | INFO | Starting parallel map (concurrency: 12, chunk size: 1)\u001b[0m\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f6ee8c2708e24e53a5a00428fd40e7da", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/12 [00:00 List[Tuple[str, List[str]]]:\n", + " response = urllib.request.urlopen(\n", + " \"https://s3-us-west-2.amazonaws.com/ai2-s2-research-public/open-corpus/2022-02-01/\"\n", + " + chunk_key\n", + " ) # returns a gzipped JSON Lines file\n", + " decompressed = gzip.decompress(response.read())\n", + " decoded = decompressed.decode()\n", + " chunk = [json.loads(line) for line in decoded.split(\"\\n\") if line]\n", + "\n", + " all = [\n", + " # Create pairs of `(text, [domains])`\n", + " # The text is cleaned to remove PDF extraction, web scraping, and other common artifacts\n", + " (\n", + " clean(\n", + " f'{c[\"title\"]} {c[\"paperAbstract\"]} {c[\"journalName\"]} {c[\"venue\"]}',\n", + " convert_to_ascii=True,\n", + " ),\n", + " c[\"fieldsOfStudy\"],\n", + " )\n", + " for c in chunk\n", + " ]\n", + "\n", + " return [\n", + " (content, domains)\n", + " for content, domains in all\n", + " if (domains and content and is_english(predict_language(content)))\n", + " ]\n", + "\n", + "\n", + "clean_chunks = parallel_map(process_chunk, chunks)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[38;5;39m2022-05-27 11:01:09,266 | INFO | Fetching online versions of semantic-scholar-dataset-small\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:09,581 | INFO | Found versions: [0]\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:09,890 | INFO | Copying file for semantic-scholar-dataset-small-1\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:09,916 | INFO | Compressing semantic-scholar-dataset-small-1\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:11,197 | INFO | Uploading semantic-scholar-dataset-small-1 to S3 from /tmp/large-file-s55zn39v\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:13,095 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 0.79/7.46 MB (10.5%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:14,729 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 1.57/7.46 MB (21.1%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:16,608 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 2.36/7.46 MB (31.6%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:18,209 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 3.15/7.46 MB (42.2%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:19,471 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 3.93/7.46 MB (52.7%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:20,607 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 4.72/7.46 MB (63.3%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:22,727 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 5.51/7.46 MB (73.8%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:24,953 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 6.29/7.46 MB (84.4%)\u001b[0m\n", + "\u001b[38;5;39m2022-05-27 11:01:27,016 | INFO | Uploading semantic-scholar-dataset-small-1.tar.gz 7.08/7.46 MB (94.9%)\u001b[0m\n" + ] + } + ], + "source": [ + "# Load\n", + "# Using this GreatAI utility, the data will be made available on S3 (and in our local cache)\n", + "\n", + "with LargeFile(DATASET_KEY, \"w\", keep_last_n=1) as f:\n", + " json.dump(list(chain(clean_chunks)), f)" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "9583b8c15346c7ad861da481b60c8e8605a71a48eda767f1640baa8f25efebcb" + }, + "kernelspec": { + "display_name": "Python 3.9.2 ('.venv': venv)", + "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.9.2" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}