You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "damccorm (via GitHub)" <gi...@apache.org> on 2023/07/20 13:57:17 UTC
[GitHub] [beam] damccorm commented on a diff in pull request #27516: Healthcare NLP Colab Example
damccorm commented on code in PR #27516:
URL: https://github.com/apache/beam/pull/27516#discussion_r1269491633
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
Review Comment:
This renders poorly in GitHub, could you just make it one line? Also, this sentence is just unclear to me; what is the expected user action from instruction number 2?
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
+ ],
+ "metadata": {
+ "id": "ZLBB0PTG5CHw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pip install apache-beam[gcp]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "O7hq2sse8K4u",
+ "outputId": "c6f559a6-1419-43f2-b88d-49fedca1c686"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Requirement already satisfied: apache-beam[gcp] in /usr/local/lib/python3.10/dist-packages (2.49.0)\n",
+ "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.7)\n",
+ "Requirement already satisfied: orjson<4.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.9.2)\n",
+ "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.3.1.1)\n",
+ "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.2.1)\n",
+ "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.0)\n",
+ "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.18)\n",
+ "Requirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.56.0)\n",
+ "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.7.0)\n",
+ "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.4)\n",
+ "Requirement already satisfied: objsize<0.7.0,>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.6.1)\n",
+ "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.4.1)\n",
+ "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.3)\n",
+ "Requirement already satisfied: protobuf<4.24.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.20.3)\n",
+ "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.4.2)\n",
+ "Requirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.8.2)\n",
+ "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.7.1)\n",
+ "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.10.31)\n",
+ "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.27.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.7.1)\n",
+ "Requirement already satisfied: zstandard<1,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: pyarrow<12.0.0,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (9.0.0)\n",
+ "Requirement already satisfied: cachetools<6,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (5.3.1)\n",
+ "Requirement already satisfied: google-apitools<0.5.32,>=0.5.31 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.5.31)\n",
+ "Requirement already satisfied: google-auth<3,>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.17.3)\n",
+ "Requirement already satisfied: google-auth-httplib2<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.1.0)\n",
+ "Requirement already satisfied: google-cloud-datastore<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.15.2)\n",
+ "Requirement already satisfied: google-cloud-pubsub<3,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.18.0)\n",
+ "Requirement already satisfied: google-cloud-pubsublite<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.3)\n",
+ "Requirement already satisfied: google-cloud-bigquery<4,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.10.0)\n",
+ "Requirement already satisfied: google-cloud-bigquery-storage<3,>=2.6.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.22.0)\n",
+ "Requirement already satisfied: google-cloud-core<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.3.3)\n",
+ "Requirement already satisfied: google-cloud-bigtable<3,>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.19.0)\n",
+ "Requirement already satisfied: google-cloud-spanner<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.36.0)\n",
+ "Requirement already satisfied: google-cloud-dlp<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.12.2)\n",
+ "Requirement already satisfied: google-cloud-language<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.9.1)\n",
+ "Requirement already satisfied: google-cloud-videointelligence<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.11.3)\n",
+ "Requirement already satisfied: google-cloud-vision<4,>=2 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.4.4)\n",
+ "Requirement already satisfied: google-cloud-recommendations-ai<0.11.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.10.4)\n",
+ "Requirement already satisfied: google-cloud-aiplatform<2.0,>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.28.0)\n",
+ "Requirement already satisfied: oauth2client>=1.4.12 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (4.1.3)\n",
+ "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (1.16.0)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (4.9)\n",
+ "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.11.1)\n",
+ "Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (23.1)\n",
+ "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.8.0)\n",
+ "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.10.2)\n",
+ "Requirement already satisfied: shapely<2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.8.5.post1)\n",
+ "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (2.5.0)\n",
+ "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigtable<3,>=2.19.0->apache-beam[gcp]) (0.12.6)\n",
+ "Requirement already satisfied: grpcio-status>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsub<3,>=2.1.0->apache-beam[gcp]) (1.48.2)\n",
+ "Requirement already satisfied: overrides<7.0.0,>=6.0.1 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsublite<2,>=1.2.0->apache-beam[gcp]) (6.5.0)\n",
+ "Requirement already satisfied: sqlparse>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-spanner<4,>=3.0.0->apache-beam[gcp]) (0.4.4)\n",
+ "Requirement already satisfied: docopt in /usr/local/lib/python3.10/dist-packages (from hdfs<3.0.0,>=2.1.0->apache-beam[gcp]) (0.6.2)\n",
+ "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.10/dist-packages (from httplib2<0.23.0,>=0.8->apache-beam[gcp]) (3.1.0)\n",
+ "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (2.4.0)\n",
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2023.5.7)\n",
+ "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2.0.12)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (3.4)\n",
+ "Requirement already satisfied: httpcore>=0.17.3 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.17.3)\n",
+ "Requirement already satisfied: sniffio<2.0,>=1.1 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.3.0)\n",
+ "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.59.1)\n",
+ "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (1.5.0)\n",
+ "Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from oauth2client>=1.4.12->google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (0.5.0)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.14.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (3.7.1)\n",
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.1.2)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Note that below, **us-central1** is hardcoded as the location. This is because of the limited number of [locations](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) the API currently supports."
+ ],
+ "metadata": {
+ "id": "D7lJqW2PRFcN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Change this variable to True if you want to debug the Interactive Runner Pipeline else it uses Dataflow\n",
+ "debug = False\n",
+ "DATASET=\"<YOUR_DATASET_NAME>\"\n",
+ "TEMP_LOCATION=\"<YOUR_TEMP_LOCATION>\"\n",
+ "PROJECT='<YOUR_PROJECT_ID>'\n",
+ "LOCATION='us-central1'\n",
+ "URL=f'https://healthcare.googleapis.com/v1/projects/{PROJECT}/locations/{LOCATION}/services/nlp:analyzeEntities'\n",
+ "NLP_SERVICE=f'projects/{PROJECT}/locations/{LOCATION}/services/nlp'\n",
+ "GCS_BUCKET=PROJECT"
+ ],
+ "metadata": {
+ "id": "s9lhe5CZ5F3o"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**BigQuery Setup**\n",
+ "\n",
+ "We will be using BigQuery to warehouse the structured data revealed in the output of the Healthcare NLP API. For this purpose, we create 3 tables to organize the data"
+ ],
+ "metadata": {
+ "id": "DI_Qkyn75LO-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.cloud import bigquery\n",
Review Comment:
This is a pretty big block/hard to read - maybe we could split it into 3 cells (one for each table)?
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
+ ],
+ "metadata": {
+ "id": "ZLBB0PTG5CHw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pip install apache-beam[gcp]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "O7hq2sse8K4u",
+ "outputId": "c6f559a6-1419-43f2-b88d-49fedca1c686"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Requirement already satisfied: apache-beam[gcp] in /usr/local/lib/python3.10/dist-packages (2.49.0)\n",
+ "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.7)\n",
+ "Requirement already satisfied: orjson<4.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.9.2)\n",
+ "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.3.1.1)\n",
+ "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.2.1)\n",
+ "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.0)\n",
+ "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.18)\n",
+ "Requirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.56.0)\n",
+ "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.7.0)\n",
+ "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.4)\n",
+ "Requirement already satisfied: objsize<0.7.0,>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.6.1)\n",
+ "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.4.1)\n",
+ "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.3)\n",
+ "Requirement already satisfied: protobuf<4.24.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.20.3)\n",
+ "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.4.2)\n",
+ "Requirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.8.2)\n",
+ "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.7.1)\n",
+ "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.10.31)\n",
+ "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.27.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.7.1)\n",
+ "Requirement already satisfied: zstandard<1,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: pyarrow<12.0.0,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (9.0.0)\n",
+ "Requirement already satisfied: cachetools<6,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (5.3.1)\n",
+ "Requirement already satisfied: google-apitools<0.5.32,>=0.5.31 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.5.31)\n",
+ "Requirement already satisfied: google-auth<3,>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.17.3)\n",
+ "Requirement already satisfied: google-auth-httplib2<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.1.0)\n",
+ "Requirement already satisfied: google-cloud-datastore<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.15.2)\n",
+ "Requirement already satisfied: google-cloud-pubsub<3,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.18.0)\n",
+ "Requirement already satisfied: google-cloud-pubsublite<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.3)\n",
+ "Requirement already satisfied: google-cloud-bigquery<4,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.10.0)\n",
+ "Requirement already satisfied: google-cloud-bigquery-storage<3,>=2.6.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.22.0)\n",
+ "Requirement already satisfied: google-cloud-core<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.3.3)\n",
+ "Requirement already satisfied: google-cloud-bigtable<3,>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.19.0)\n",
+ "Requirement already satisfied: google-cloud-spanner<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.36.0)\n",
+ "Requirement already satisfied: google-cloud-dlp<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.12.2)\n",
+ "Requirement already satisfied: google-cloud-language<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.9.1)\n",
+ "Requirement already satisfied: google-cloud-videointelligence<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.11.3)\n",
+ "Requirement already satisfied: google-cloud-vision<4,>=2 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.4.4)\n",
+ "Requirement already satisfied: google-cloud-recommendations-ai<0.11.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.10.4)\n",
+ "Requirement already satisfied: google-cloud-aiplatform<2.0,>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.28.0)\n",
+ "Requirement already satisfied: oauth2client>=1.4.12 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (4.1.3)\n",
+ "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (1.16.0)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (4.9)\n",
+ "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.11.1)\n",
+ "Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (23.1)\n",
+ "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.8.0)\n",
+ "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.10.2)\n",
+ "Requirement already satisfied: shapely<2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.8.5.post1)\n",
+ "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (2.5.0)\n",
+ "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigtable<3,>=2.19.0->apache-beam[gcp]) (0.12.6)\n",
+ "Requirement already satisfied: grpcio-status>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsub<3,>=2.1.0->apache-beam[gcp]) (1.48.2)\n",
+ "Requirement already satisfied: overrides<7.0.0,>=6.0.1 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsublite<2,>=1.2.0->apache-beam[gcp]) (6.5.0)\n",
+ "Requirement already satisfied: sqlparse>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-spanner<4,>=3.0.0->apache-beam[gcp]) (0.4.4)\n",
+ "Requirement already satisfied: docopt in /usr/local/lib/python3.10/dist-packages (from hdfs<3.0.0,>=2.1.0->apache-beam[gcp]) (0.6.2)\n",
+ "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.10/dist-packages (from httplib2<0.23.0,>=0.8->apache-beam[gcp]) (3.1.0)\n",
+ "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (2.4.0)\n",
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2023.5.7)\n",
+ "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2.0.12)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (3.4)\n",
+ "Requirement already satisfied: httpcore>=0.17.3 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.17.3)\n",
+ "Requirement already satisfied: sniffio<2.0,>=1.1 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.3.0)\n",
+ "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.59.1)\n",
+ "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (1.5.0)\n",
+ "Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from oauth2client>=1.4.12->google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (0.5.0)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.14.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (3.7.1)\n",
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.1.2)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Note that below, **us-central1** is hardcoded as the location. This is because of the limited number of [locations](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) the API currently supports."
+ ],
+ "metadata": {
+ "id": "D7lJqW2PRFcN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Change this variable to True if you want to debug the Interactive Runner Pipeline else it uses Dataflow\n",
+ "debug = False\n",
+ "DATASET=\"<YOUR_DATASET_NAME>\"\n",
+ "TEMP_LOCATION=\"<YOUR_TEMP_LOCATION>\"\n",
+ "PROJECT='<YOUR_PROJECT_ID>'\n",
+ "LOCATION='us-central1'\n",
+ "URL=f'https://healthcare.googleapis.com/v1/projects/{PROJECT}/locations/{LOCATION}/services/nlp:analyzeEntities'\n",
+ "NLP_SERVICE=f'projects/{PROJECT}/locations/{LOCATION}/services/nlp'\n",
+ "GCS_BUCKET=PROJECT"
+ ],
+ "metadata": {
+ "id": "s9lhe5CZ5F3o"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**BigQuery Setup**\n",
+ "\n",
+ "We will be using BigQuery to warehouse the structured data revealed in the output of the Healthcare NLP API. For this purpose, we create 3 tables to organize the data"
Review Comment:
This would benefit from a little more explanation of the tables/their purposes
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
+ ],
+ "metadata": {
+ "id": "ZLBB0PTG5CHw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pip install apache-beam[gcp]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "O7hq2sse8K4u",
+ "outputId": "c6f559a6-1419-43f2-b88d-49fedca1c686"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Requirement already satisfied: apache-beam[gcp] in /usr/local/lib/python3.10/dist-packages (2.49.0)\n",
+ "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.7)\n",
+ "Requirement already satisfied: orjson<4.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.9.2)\n",
+ "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.3.1.1)\n",
+ "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.2.1)\n",
+ "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.0)\n",
+ "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.18)\n",
+ "Requirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.56.0)\n",
+ "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.7.0)\n",
+ "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.4)\n",
+ "Requirement already satisfied: objsize<0.7.0,>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.6.1)\n",
+ "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.4.1)\n",
+ "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.3)\n",
+ "Requirement already satisfied: protobuf<4.24.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.20.3)\n",
+ "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.4.2)\n",
+ "Requirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.8.2)\n",
+ "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.7.1)\n",
+ "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.10.31)\n",
+ "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.27.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.7.1)\n",
+ "Requirement already satisfied: zstandard<1,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: pyarrow<12.0.0,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (9.0.0)\n",
+ "Requirement already satisfied: cachetools<6,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (5.3.1)\n",
+ "Requirement already satisfied: google-apitools<0.5.32,>=0.5.31 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.5.31)\n",
+ "Requirement already satisfied: google-auth<3,>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.17.3)\n",
+ "Requirement already satisfied: google-auth-httplib2<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.1.0)\n",
+ "Requirement already satisfied: google-cloud-datastore<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.15.2)\n",
+ "Requirement already satisfied: google-cloud-pubsub<3,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.18.0)\n",
+ "Requirement already satisfied: google-cloud-pubsublite<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.3)\n",
+ "Requirement already satisfied: google-cloud-bigquery<4,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.10.0)\n",
+ "Requirement already satisfied: google-cloud-bigquery-storage<3,>=2.6.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.22.0)\n",
+ "Requirement already satisfied: google-cloud-core<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.3.3)\n",
+ "Requirement already satisfied: google-cloud-bigtable<3,>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.19.0)\n",
+ "Requirement already satisfied: google-cloud-spanner<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.36.0)\n",
+ "Requirement already satisfied: google-cloud-dlp<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.12.2)\n",
+ "Requirement already satisfied: google-cloud-language<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.9.1)\n",
+ "Requirement already satisfied: google-cloud-videointelligence<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.11.3)\n",
+ "Requirement already satisfied: google-cloud-vision<4,>=2 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.4.4)\n",
+ "Requirement already satisfied: google-cloud-recommendations-ai<0.11.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.10.4)\n",
+ "Requirement already satisfied: google-cloud-aiplatform<2.0,>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.28.0)\n",
+ "Requirement already satisfied: oauth2client>=1.4.12 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (4.1.3)\n",
+ "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (1.16.0)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (4.9)\n",
+ "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.11.1)\n",
+ "Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (23.1)\n",
+ "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.8.0)\n",
+ "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.10.2)\n",
+ "Requirement already satisfied: shapely<2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.8.5.post1)\n",
+ "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (2.5.0)\n",
+ "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigtable<3,>=2.19.0->apache-beam[gcp]) (0.12.6)\n",
+ "Requirement already satisfied: grpcio-status>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsub<3,>=2.1.0->apache-beam[gcp]) (1.48.2)\n",
+ "Requirement already satisfied: overrides<7.0.0,>=6.0.1 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsublite<2,>=1.2.0->apache-beam[gcp]) (6.5.0)\n",
+ "Requirement already satisfied: sqlparse>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-spanner<4,>=3.0.0->apache-beam[gcp]) (0.4.4)\n",
+ "Requirement already satisfied: docopt in /usr/local/lib/python3.10/dist-packages (from hdfs<3.0.0,>=2.1.0->apache-beam[gcp]) (0.6.2)\n",
+ "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.10/dist-packages (from httplib2<0.23.0,>=0.8->apache-beam[gcp]) (3.1.0)\n",
+ "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (2.4.0)\n",
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2023.5.7)\n",
+ "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2.0.12)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (3.4)\n",
+ "Requirement already satisfied: httpcore>=0.17.3 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.17.3)\n",
+ "Requirement already satisfied: sniffio<2.0,>=1.1 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.3.0)\n",
+ "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.59.1)\n",
+ "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (1.5.0)\n",
+ "Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from oauth2client>=1.4.12->google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (0.5.0)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.14.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (3.7.1)\n",
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.1.2)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Note that below, **us-central1** is hardcoded as the location. This is because of the limited number of [locations](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) the API currently supports."
+ ],
+ "metadata": {
+ "id": "D7lJqW2PRFcN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Change this variable to True if you want to debug the Interactive Runner Pipeline else it uses Dataflow\n",
+ "debug = False\n",
+ "DATASET=\"<YOUR_DATASET_NAME>\"\n",
+ "TEMP_LOCATION=\"<YOUR_TEMP_LOCATION>\"\n",
+ "PROJECT='<YOUR_PROJECT_ID>'\n",
+ "LOCATION='us-central1'\n",
+ "URL=f'https://healthcare.googleapis.com/v1/projects/{PROJECT}/locations/{LOCATION}/services/nlp:analyzeEntities'\n",
+ "NLP_SERVICE=f'projects/{PROJECT}/locations/{LOCATION}/services/nlp'\n",
+ "GCS_BUCKET=PROJECT"
+ ],
+ "metadata": {
+ "id": "s9lhe5CZ5F3o"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**BigQuery Setup**\n",
+ "\n",
+ "We will be using BigQuery to warehouse the structured data revealed in the output of the Healthcare NLP API. For this purpose, we create 3 tables to organize the data"
+ ],
+ "metadata": {
+ "id": "DI_Qkyn75LO-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.cloud import bigquery\n",
+ "\n",
+ "# Construct a BigQuery client object.\n",
+ "\n",
+ "TABLE_ENTITY=\"entity\"\n",
+ "TABLE_REL=\"relations\"\n",
+ "TABLE_ENTITYMENTIONS=\"entitymentions\"\n",
+ "\n",
+ "schemaEntity = [\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"preferredTerm\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"vocabularyCodes\", \"STRING\", mode=\"REPEATED\"),\n",
+ "]\n",
+ "\n",
+ "schemaRelations = [\n",
+ " bigquery.SchemaField(\"subjectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"objectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\"),\n",
+ "]\n",
+ "\n",
+ "schemaEntityMentions = [\n",
+ " bigquery.SchemaField(\"mentionId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"type\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\n",
+ " \"text\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"content\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"beginOffset\", \"INTEGER\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"linkedEntities\",\n",
+ " \"RECORD\",\n",
+ " mode=\"REPEATED\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"temporalAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"certaintyAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"subject\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\")\n",
+ "]\n",
+ "\n",
+ "client = bigquery.Client()\n",
+ "\n",
+ "# Create Table IDs\n",
+ "table_ent = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITY\n",
+ "table_rel = PROJECT+\".\"+DATASET+\".\"+TABLE_REL\n",
+ "table_mentions = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITYMENTIONS\n",
+ "\n",
+ "# If table exists, delete the tables.\n",
+ "client.delete_table(table_ent, not_found_ok=True)\n",
+ "client.delete_table(table_rel, not_found_ok=True)\n",
+ "client.delete_table(table_mentions, not_found_ok=True)\n",
+ "\n",
+ "# Create tables\n",
+ "\n",
+ "table = bigquery.Table(table_ent, schema=schemaEntity)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "\n",
+ "table = bigquery.Table(table_rel, schema=schemaRelations)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "table = bigquery.Table(table_mentions, schema=schemaEntityMentions)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")"
+ ],
+ "metadata": {
+ "id": "bZDqtFVE5Wd_"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Pipeline Setup**\n",
+ "\n",
+ "For the purpose of experimenting, I have setup an interactiveRunner. But this should be changed to a DatafowRunner once you are comfortable with it"
+ ],
+ "metadata": {
+ "id": "jc_iS_BP5aS4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Python's regular expression library\n",
+ "import re\n",
+ "from sys import argv\n",
+ "# Beam and interactive Beam imports\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.runners.interactive.interactive_runner import InteractiveRunner\n",
+ "import apache_beam.runners.interactive.interactive_beam as ib\n",
+ "\n",
+ "#Reference https://cloud.google.com/dataflow/docs/guides/specifying-exec-params#python_1\n",
+ "from apache_beam.options.pipeline_options import PipelineOptions\n",
+ "if debug:\n",
+ " runnertype = \"InteractiveRunner\"\n",
+ "else:\n",
+ " runnertype = \"DataflowRunner\"\n",
+ "\n",
+ "options = PipelineOptions(\n",
+ " flags=argv,\n",
+ " runner=runnertype,\n",
+ " project=PROJECT,\n",
+ " job_name=\"my-healthcare-nlp-job\",\n",
+ " temp_location=TEMP_LOCATION,\n",
+ " region=LOCATION)"
+ ],
+ "metadata": {
+ "id": "07ct6kf55ihP"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following defines a `PTransform` named `ReadLinesFromText`, that extracts lines from a file."
+ ],
+ "metadata": {
+ "id": "dO1A9_WK5lb4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class ReadLinesFromText(beam.PTransform):\n",
+ "\n",
+ " def __init__(self, file_pattern):\n",
+ " self._file_pattern = file_pattern\n",
+ "\n",
+ " def expand(self, pcoll):\n",
+ " return (pcoll.pipeline\n",
+ " | beam.io.ReadFromText(self._file_pattern))"
+ ],
+ "metadata": {
+ "id": "t5iDRKMK5n_B"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following sets up an Apache Beam pipeline with the *Interactive Runner*. The *Interactive Runner* is the runner suitable for running in notebooks. A runner is an execution engine for Apache Beam pipelines."
+ ],
+ "metadata": {
+ "id": "HI_HVB185sMQ"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.auth import default\n",
+ "\n",
+ "credentials = default()"
+ ],
+ "metadata": {
+ "id": "dMc10Dlgtp1c"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "p = beam.Pipeline(options = options)"
+ ],
+ "metadata": {
+ "id": "7osCZ1om5ql0"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following sets up a PTransform that extracts words from a Google Cloud Storage file that contains lines with each line containing a In our example, each line is a medical notes excerpt that will be passed through the Healthcare NLP API\n",
+ "\n",
+ "| is an overloaded operator that applies a PTransform to a PCollection to produce a new PCollection. Together with |, >> allows you to optionally name a PTransform.\n",
+ "\n",
+ "Usage: [PCollection] | [PTransform] or [PCollection] | [name] >> [PTransform]"
+ ],
+ "metadata": {
+ "id": "EaF8NfC_521y"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "lines = p | 'read' >> ReadLinesFromText(GCS_BUCKET + \"nlpsample500.csv\")"
+ ],
+ "metadata": {
+ "id": "2APAh6XQ6NYd"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "if debug:\n",
+ " ib.show(lines)"
+ ],
+ "metadata": {
+ "id": "NH_04EXw6P8F"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "if debug:\n",
Review Comment:
There's a lot of consecutive code blocks here with not much explanation as to what they're doing, I think dropping some text snippets in here would help.
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
+ ],
+ "metadata": {
+ "id": "ZLBB0PTG5CHw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pip install apache-beam[gcp]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "O7hq2sse8K4u",
+ "outputId": "c6f559a6-1419-43f2-b88d-49fedca1c686"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Requirement already satisfied: apache-beam[gcp] in /usr/local/lib/python3.10/dist-packages (2.49.0)\n",
+ "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.7)\n",
+ "Requirement already satisfied: orjson<4.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.9.2)\n",
+ "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.3.1.1)\n",
+ "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.2.1)\n",
+ "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.0)\n",
+ "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.18)\n",
+ "Requirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.56.0)\n",
+ "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.7.0)\n",
+ "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.4)\n",
+ "Requirement already satisfied: objsize<0.7.0,>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.6.1)\n",
+ "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.4.1)\n",
+ "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.3)\n",
+ "Requirement already satisfied: protobuf<4.24.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.20.3)\n",
+ "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.4.2)\n",
+ "Requirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.8.2)\n",
+ "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.7.1)\n",
+ "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.10.31)\n",
+ "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.27.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.7.1)\n",
+ "Requirement already satisfied: zstandard<1,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: pyarrow<12.0.0,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (9.0.0)\n",
+ "Requirement already satisfied: cachetools<6,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (5.3.1)\n",
+ "Requirement already satisfied: google-apitools<0.5.32,>=0.5.31 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.5.31)\n",
+ "Requirement already satisfied: google-auth<3,>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.17.3)\n",
+ "Requirement already satisfied: google-auth-httplib2<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.1.0)\n",
+ "Requirement already satisfied: google-cloud-datastore<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.15.2)\n",
+ "Requirement already satisfied: google-cloud-pubsub<3,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.18.0)\n",
+ "Requirement already satisfied: google-cloud-pubsublite<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.3)\n",
+ "Requirement already satisfied: google-cloud-bigquery<4,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.10.0)\n",
+ "Requirement already satisfied: google-cloud-bigquery-storage<3,>=2.6.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.22.0)\n",
+ "Requirement already satisfied: google-cloud-core<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.3.3)\n",
+ "Requirement already satisfied: google-cloud-bigtable<3,>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.19.0)\n",
+ "Requirement already satisfied: google-cloud-spanner<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.36.0)\n",
+ "Requirement already satisfied: google-cloud-dlp<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.12.2)\n",
+ "Requirement already satisfied: google-cloud-language<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.9.1)\n",
+ "Requirement already satisfied: google-cloud-videointelligence<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.11.3)\n",
+ "Requirement already satisfied: google-cloud-vision<4,>=2 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.4.4)\n",
+ "Requirement already satisfied: google-cloud-recommendations-ai<0.11.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.10.4)\n",
+ "Requirement already satisfied: google-cloud-aiplatform<2.0,>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.28.0)\n",
+ "Requirement already satisfied: oauth2client>=1.4.12 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (4.1.3)\n",
+ "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (1.16.0)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (4.9)\n",
+ "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.11.1)\n",
+ "Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (23.1)\n",
+ "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.8.0)\n",
+ "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.10.2)\n",
+ "Requirement already satisfied: shapely<2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.8.5.post1)\n",
+ "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (2.5.0)\n",
+ "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigtable<3,>=2.19.0->apache-beam[gcp]) (0.12.6)\n",
+ "Requirement already satisfied: grpcio-status>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsub<3,>=2.1.0->apache-beam[gcp]) (1.48.2)\n",
+ "Requirement already satisfied: overrides<7.0.0,>=6.0.1 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsublite<2,>=1.2.0->apache-beam[gcp]) (6.5.0)\n",
+ "Requirement already satisfied: sqlparse>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-spanner<4,>=3.0.0->apache-beam[gcp]) (0.4.4)\n",
+ "Requirement already satisfied: docopt in /usr/local/lib/python3.10/dist-packages (from hdfs<3.0.0,>=2.1.0->apache-beam[gcp]) (0.6.2)\n",
+ "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.10/dist-packages (from httplib2<0.23.0,>=0.8->apache-beam[gcp]) (3.1.0)\n",
+ "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (2.4.0)\n",
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2023.5.7)\n",
+ "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2.0.12)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (3.4)\n",
+ "Requirement already satisfied: httpcore>=0.17.3 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.17.3)\n",
+ "Requirement already satisfied: sniffio<2.0,>=1.1 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.3.0)\n",
+ "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.59.1)\n",
+ "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (1.5.0)\n",
+ "Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from oauth2client>=1.4.12->google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (0.5.0)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.14.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (3.7.1)\n",
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.1.2)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Note that below, **us-central1** is hardcoded as the location. This is because of the limited number of [locations](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) the API currently supports."
+ ],
+ "metadata": {
+ "id": "D7lJqW2PRFcN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Change this variable to True if you want to debug the Interactive Runner Pipeline else it uses Dataflow\n",
+ "debug = False\n",
+ "DATASET=\"<YOUR_DATASET_NAME>\"\n",
+ "TEMP_LOCATION=\"<YOUR_TEMP_LOCATION>\"\n",
+ "PROJECT='<YOUR_PROJECT_ID>'\n",
+ "LOCATION='us-central1'\n",
+ "URL=f'https://healthcare.googleapis.com/v1/projects/{PROJECT}/locations/{LOCATION}/services/nlp:analyzeEntities'\n",
+ "NLP_SERVICE=f'projects/{PROJECT}/locations/{LOCATION}/services/nlp'\n",
+ "GCS_BUCKET=PROJECT"
+ ],
+ "metadata": {
+ "id": "s9lhe5CZ5F3o"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**BigQuery Setup**\n",
+ "\n",
+ "We will be using BigQuery to warehouse the structured data revealed in the output of the Healthcare NLP API. For this purpose, we create 3 tables to organize the data"
+ ],
+ "metadata": {
+ "id": "DI_Qkyn75LO-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.cloud import bigquery\n",
+ "\n",
+ "# Construct a BigQuery client object.\n",
+ "\n",
+ "TABLE_ENTITY=\"entity\"\n",
+ "TABLE_REL=\"relations\"\n",
+ "TABLE_ENTITYMENTIONS=\"entitymentions\"\n",
+ "\n",
+ "schemaEntity = [\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"preferredTerm\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"vocabularyCodes\", \"STRING\", mode=\"REPEATED\"),\n",
+ "]\n",
+ "\n",
+ "schemaRelations = [\n",
+ " bigquery.SchemaField(\"subjectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"objectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\"),\n",
+ "]\n",
+ "\n",
+ "schemaEntityMentions = [\n",
+ " bigquery.SchemaField(\"mentionId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"type\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\n",
+ " \"text\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"content\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"beginOffset\", \"INTEGER\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"linkedEntities\",\n",
+ " \"RECORD\",\n",
+ " mode=\"REPEATED\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"temporalAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"certaintyAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"subject\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\")\n",
+ "]\n",
+ "\n",
+ "client = bigquery.Client()\n",
+ "\n",
+ "# Create Table IDs\n",
+ "table_ent = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITY\n",
+ "table_rel = PROJECT+\".\"+DATASET+\".\"+TABLE_REL\n",
+ "table_mentions = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITYMENTIONS\n",
+ "\n",
+ "# If table exists, delete the tables.\n",
+ "client.delete_table(table_ent, not_found_ok=True)\n",
+ "client.delete_table(table_rel, not_found_ok=True)\n",
+ "client.delete_table(table_mentions, not_found_ok=True)\n",
+ "\n",
+ "# Create tables\n",
+ "\n",
+ "table = bigquery.Table(table_ent, schema=schemaEntity)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "\n",
+ "table = bigquery.Table(table_rel, schema=schemaRelations)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "table = bigquery.Table(table_mentions, schema=schemaEntityMentions)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")"
+ ],
+ "metadata": {
+ "id": "bZDqtFVE5Wd_"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Pipeline Setup**\n",
+ "\n",
+ "For the purpose of experimenting, I have setup an interactiveRunner. But this should be changed to a DatafowRunner once you are comfortable with it"
+ ],
+ "metadata": {
+ "id": "jc_iS_BP5aS4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# Python's regular expression library\n",
+ "import re\n",
+ "from sys import argv\n",
+ "# Beam and interactive Beam imports\n",
+ "import apache_beam as beam\n",
+ "from apache_beam.runners.interactive.interactive_runner import InteractiveRunner\n",
+ "import apache_beam.runners.interactive.interactive_beam as ib\n",
+ "\n",
+ "#Reference https://cloud.google.com/dataflow/docs/guides/specifying-exec-params#python_1\n",
+ "from apache_beam.options.pipeline_options import PipelineOptions\n",
+ "if debug:\n",
+ " runnertype = \"InteractiveRunner\"\n",
+ "else:\n",
+ " runnertype = \"DataflowRunner\"\n",
+ "\n",
+ "options = PipelineOptions(\n",
+ " flags=argv,\n",
+ " runner=runnertype,\n",
+ " project=PROJECT,\n",
+ " job_name=\"my-healthcare-nlp-job\",\n",
+ " temp_location=TEMP_LOCATION,\n",
+ " region=LOCATION)"
+ ],
+ "metadata": {
+ "id": "07ct6kf55ihP"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following defines a `PTransform` named `ReadLinesFromText`, that extracts lines from a file."
+ ],
+ "metadata": {
+ "id": "dO1A9_WK5lb4"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "class ReadLinesFromText(beam.PTransform):\n",
+ "\n",
+ " def __init__(self, file_pattern):\n",
+ " self._file_pattern = file_pattern\n",
+ "\n",
+ " def expand(self, pcoll):\n",
+ " return (pcoll.pipeline\n",
+ " | beam.io.ReadFromText(self._file_pattern))"
+ ],
+ "metadata": {
+ "id": "t5iDRKMK5n_B"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following sets up an Apache Beam pipeline with the *Interactive Runner*. The *Interactive Runner* is the runner suitable for running in notebooks. A runner is an execution engine for Apache Beam pipelines."
+ ],
+ "metadata": {
+ "id": "HI_HVB185sMQ"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.auth import default\n",
+ "\n",
+ "credentials = default()"
+ ],
+ "metadata": {
+ "id": "dMc10Dlgtp1c"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "p = beam.Pipeline(options = options)"
+ ],
+ "metadata": {
+ "id": "7osCZ1om5ql0"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "The following sets up a PTransform that extracts words from a Google Cloud Storage file that contains lines with each line containing a In our example, each line is a medical notes excerpt that will be passed through the Healthcare NLP API\n",
+ "\n",
+ "| is an overloaded operator that applies a PTransform to a PCollection to produce a new PCollection. Together with |, >> allows you to optionally name a PTransform.\n",
+ "\n",
+ "Usage: [PCollection] | [PTransform] or [PCollection] | [name] >> [PTransform]"
Review Comment:
Could we separate these examples onto 2 different lines (and maybe bold "or")? This was confusing on my first readthrough because I thought you were saying
`[PCollection] | (either [PTransform] or [PCollection]) | [name] >> [PTransform]"`
and that didn't make sense to me
##########
examples/notebooks/healthcare/beam_nlp.ipynb:
##########
@@ -0,0 +1,968 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "<a href=\"https://colab.research.google.com/github/apache/beam/blob/healthcarenlp/examples/notebooks/healthcare/beam_nlp.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "# @title ###### Licensed to the Apache Software Foundation (ASF), Version 2.0 (the \"License\")\n",
+ "\n",
+ "# Licensed to the Apache Software Foundation (ASF) under one\n",
+ "# or more contributor license agreements. See the NOTICE file\n",
+ "# distributed with this work for additional information\n",
+ "# regarding copyright ownership. The ASF licenses this file\n",
+ "# to you under the Apache License, Version 2.0 (the\n",
+ "# \"License\"); you may not use this file except in compliance\n",
+ "# with the License. You may obtain a copy of the License at\n",
+ "#\n",
+ "# http://www.apache.org/licenses/LICENSE-2.0\n",
+ "#\n",
+ "# Unless required by applicable law or agreed to in writing,\n",
+ "# software distributed under the License is distributed on an\n",
+ "# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n",
+ "# KIND, either express or implied. See the License for the\n",
+ "# specific language governing permissions and limitations\n",
+ "# under the License"
+ ],
+ "metadata": {
+ "id": "lBuUTzxD2mvJ"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# **Natural Language Processing Pipeline**\n",
+ "\n",
+ "**Note**: This example is used from [here](https://github.com/rasalt/healthcarenlp/blob/main/nlp_public.ipynb).\n",
+ "\n",
+ "\n",
+ "\n",
+ "This example demonstrates how to set up an Apache Beam pipeline that reads a file from [Google Cloud Storage](https://https://cloud.google.com/storage), and calls the [Google Cloud Healthcare NLP API](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) to extract information from unstructured data. This application can be used in contexts such as reading scanned clinical documents and extracting structure from it.\n",
+ "\n",
+ "An Apache Beam pipeline is a pipeline that reads input data, transforms that data, and writes output data. It consists of PTransforms and PCollections. A PCollection represents a distributed data set that your Beam pipeline operates on. A PTransform represents a data processing operation, or a step, in your pipeline. It takes one or more PCollections as input, performs a processing function that you provide on the elements of that PCollection, and produces zero or more output PCollection objects.\n",
+ "\n",
+ "For details about Apache Beam pipelines, including PTransforms and PCollections, visit the [Beam Programming Guide](https://beam.apache.org/documentation/programming-guide/).\n",
+ "\n",
+ "You'll be able to use this notebook to explore the data in each PCollection."
+ ],
+ "metadata": {
+ "id": "nEUAYCTx4Ijj"
+ }
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Instructions\n",
+ "1. Set the variables in the next cell based upon your project and preferences\n",
+ "2. The files referred to in this notebook nlpsample*.csv are in the format with one\n",
+ "blurb of clinical note."
+ ],
+ "metadata": {
+ "id": "ZLBB0PTG5CHw"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "pip install apache-beam[gcp]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "O7hq2sse8K4u",
+ "outputId": "c6f559a6-1419-43f2-b88d-49fedca1c686"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Requirement already satisfied: apache-beam[gcp] in /usr/local/lib/python3.10/dist-packages (2.49.0)\n",
+ "Requirement already satisfied: crcmod<2.0,>=1.7 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.7)\n",
+ "Requirement already satisfied: orjson<4.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.9.2)\n",
+ "Requirement already satisfied: dill<0.3.2,>=0.3.1.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.3.1.1)\n",
+ "Requirement already satisfied: cloudpickle~=2.2.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.2.1)\n",
+ "Requirement already satisfied: fastavro<2,>=0.23.6 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.0)\n",
+ "Requirement already satisfied: fasteners<1.0,>=0.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.18)\n",
+ "Requirement already satisfied: grpcio!=1.48.0,<2,>=1.33.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.56.0)\n",
+ "Requirement already satisfied: hdfs<3.0.0,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.7.0)\n",
+ "Requirement already satisfied: httplib2<0.23.0,>=0.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: numpy<1.25.0,>=1.14.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.4)\n",
+ "Requirement already satisfied: objsize<0.7.0,>=0.6.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.6.1)\n",
+ "Requirement already satisfied: pymongo<5.0.0,>=3.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.4.1)\n",
+ "Requirement already satisfied: proto-plus<2,>=1.7.1 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.22.3)\n",
+ "Requirement already satisfied: protobuf<4.24.0,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.20.3)\n",
+ "Requirement already satisfied: pydot<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.4.2)\n",
+ "Requirement already satisfied: python-dateutil<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.8.2)\n",
+ "Requirement already satisfied: pytz>=2018.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.7.1)\n",
+ "Requirement already satisfied: regex>=2020.6.8 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2022.10.31)\n",
+ "Requirement already satisfied: requests<3.0.0,>=2.24.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.27.1)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (4.7.1)\n",
+ "Requirement already satisfied: zstandard<1,>=0.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.21.0)\n",
+ "Requirement already satisfied: pyarrow<12.0.0,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (9.0.0)\n",
+ "Requirement already satisfied: cachetools<6,>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (5.3.1)\n",
+ "Requirement already satisfied: google-apitools<0.5.32,>=0.5.31 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.5.31)\n",
+ "Requirement already satisfied: google-auth<3,>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.17.3)\n",
+ "Requirement already satisfied: google-auth-httplib2<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.1.0)\n",
+ "Requirement already satisfied: google-cloud-datastore<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.15.2)\n",
+ "Requirement already satisfied: google-cloud-pubsub<3,>=2.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.18.0)\n",
+ "Requirement already satisfied: google-cloud-pubsublite<2,>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.8.3)\n",
+ "Requirement already satisfied: google-cloud-bigquery<4,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.10.0)\n",
+ "Requirement already satisfied: google-cloud-bigquery-storage<3,>=2.6.3 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.22.0)\n",
+ "Requirement already satisfied: google-cloud-core<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.3.3)\n",
+ "Requirement already satisfied: google-cloud-bigtable<3,>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.19.0)\n",
+ "Requirement already satisfied: google-cloud-spanner<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.36.0)\n",
+ "Requirement already satisfied: google-cloud-dlp<4,>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.12.2)\n",
+ "Requirement already satisfied: google-cloud-language<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.9.1)\n",
+ "Requirement already satisfied: google-cloud-videointelligence<3,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (2.11.3)\n",
+ "Requirement already satisfied: google-cloud-vision<4,>=2 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (3.4.4)\n",
+ "Requirement already satisfied: google-cloud-recommendations-ai<0.11.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (0.10.4)\n",
+ "Requirement already satisfied: google-cloud-aiplatform<2.0,>=1.26.0 in /usr/local/lib/python3.10/dist-packages (from apache-beam[gcp]) (1.28.0)\n",
+ "Requirement already satisfied: oauth2client>=1.4.12 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (4.1.3)\n",
+ "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (1.16.0)\n",
+ "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (0.3.0)\n",
+ "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.18.0->apache-beam[gcp]) (4.9)\n",
+ "Requirement already satisfied: google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.11.1)\n",
+ "Requirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (23.1)\n",
+ "Requirement already satisfied: google-cloud-storage<3.0.0dev,>=1.32.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (2.8.0)\n",
+ "Requirement already satisfied: google-cloud-resource-manager<3.0.0dev,>=1.3.3 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.10.2)\n",
+ "Requirement already satisfied: shapely<2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.8.5.post1)\n",
+ "Requirement already satisfied: google-resumable-media<3.0dev,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (2.5.0)\n",
+ "Requirement already satisfied: grpc-google-iam-v1<1.0.0dev,>=0.12.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-bigtable<3,>=2.19.0->apache-beam[gcp]) (0.12.6)\n",
+ "Requirement already satisfied: grpcio-status>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsub<3,>=2.1.0->apache-beam[gcp]) (1.48.2)\n",
+ "Requirement already satisfied: overrides<7.0.0,>=6.0.1 in /usr/local/lib/python3.10/dist-packages (from google-cloud-pubsublite<2,>=1.2.0->apache-beam[gcp]) (6.5.0)\n",
+ "Requirement already satisfied: sqlparse>=0.4.4 in /usr/local/lib/python3.10/dist-packages (from google-cloud-spanner<4,>=3.0.0->apache-beam[gcp]) (0.4.4)\n",
+ "Requirement already satisfied: docopt in /usr/local/lib/python3.10/dist-packages (from hdfs<3.0.0,>=2.1.0->apache-beam[gcp]) (0.6.2)\n",
+ "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /usr/local/lib/python3.10/dist-packages (from httplib2<0.23.0,>=0.8->apache-beam[gcp]) (3.1.0)\n",
+ "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (2.4.0)\n",
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2023.5.7)\n",
+ "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (2.0.12)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0,>=2.24.0->apache-beam[gcp]) (3.4)\n",
+ "Requirement already satisfied: httpcore>=0.17.3 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.17.3)\n",
+ "Requirement already satisfied: sniffio<2.0,>=1.1 in /usr/local/lib/python3.10/dist-packages (from dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.3.0)\n",
+ "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,<3.0.0dev,>=1.32.0->google-cloud-aiplatform<2.0,>=1.26.0->apache-beam[gcp]) (1.59.1)\n",
+ "Requirement already satisfied: google-crc32c<2.0dev,>=1.0 in /usr/local/lib/python3.10/dist-packages (from google-resumable-media<3.0dev,>=0.6.0->google-cloud-bigquery<4,>=2.0.0->apache-beam[gcp]) (1.5.0)\n",
+ "Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.10/dist-packages (from oauth2client>=1.4.12->google-apitools<0.5.32,>=0.5.31->apache-beam[gcp]) (0.5.0)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (0.14.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (3.7.1)\n",
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->httpcore>=0.17.3->dnspython<3.0.0,>=1.16.0->pymongo<5.0.0,>=3.8.0->apache-beam[gcp]) (1.1.2)\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "Note that below, **us-central1** is hardcoded as the location. This is because of the limited number of [locations](https://cloud.google.com/healthcare-api/docs/how-tos/nlp) the API currently supports."
+ ],
+ "metadata": {
+ "id": "D7lJqW2PRFcN"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "#Change this variable to True if you want to debug the Interactive Runner Pipeline else it uses Dataflow\n",
+ "debug = False\n",
+ "DATASET=\"<YOUR_DATASET_NAME>\"\n",
+ "TEMP_LOCATION=\"<YOUR_TEMP_LOCATION>\"\n",
+ "PROJECT='<YOUR_PROJECT_ID>'\n",
+ "LOCATION='us-central1'\n",
+ "URL=f'https://healthcare.googleapis.com/v1/projects/{PROJECT}/locations/{LOCATION}/services/nlp:analyzeEntities'\n",
+ "NLP_SERVICE=f'projects/{PROJECT}/locations/{LOCATION}/services/nlp'\n",
+ "GCS_BUCKET=PROJECT"
+ ],
+ "metadata": {
+ "id": "s9lhe5CZ5F3o"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**BigQuery Setup**\n",
+ "\n",
+ "We will be using BigQuery to warehouse the structured data revealed in the output of the Healthcare NLP API. For this purpose, we create 3 tables to organize the data"
+ ],
+ "metadata": {
+ "id": "DI_Qkyn75LO-"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from google.cloud import bigquery\n",
+ "\n",
+ "# Construct a BigQuery client object.\n",
+ "\n",
+ "TABLE_ENTITY=\"entity\"\n",
+ "TABLE_REL=\"relations\"\n",
+ "TABLE_ENTITYMENTIONS=\"entitymentions\"\n",
+ "\n",
+ "schemaEntity = [\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"preferredTerm\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"vocabularyCodes\", \"STRING\", mode=\"REPEATED\"),\n",
+ "]\n",
+ "\n",
+ "schemaRelations = [\n",
+ " bigquery.SchemaField(\"subjectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"objectId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\"),\n",
+ "]\n",
+ "\n",
+ "schemaEntityMentions = [\n",
+ " bigquery.SchemaField(\"mentionId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"type\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\n",
+ " \"text\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"content\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"beginOffset\", \"INTEGER\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"linkedEntities\",\n",
+ " \"RECORD\",\n",
+ " mode=\"REPEATED\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"entityId\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"temporalAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"certaintyAssessment\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\n",
+ " \"subject\",\n",
+ " \"RECORD\",\n",
+ " mode=\"NULLABLE\",\n",
+ " fields=[\n",
+ " bigquery.SchemaField(\"value\", \"STRING\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " ],\n",
+ " ),\n",
+ " bigquery.SchemaField(\"confidence\", \"FLOAT64\", mode=\"NULLABLE\"),\n",
+ " bigquery.SchemaField(\"id\", \"STRING\", mode=\"NULLABLE\")\n",
+ "]\n",
+ "\n",
+ "client = bigquery.Client()\n",
+ "\n",
+ "# Create Table IDs\n",
+ "table_ent = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITY\n",
+ "table_rel = PROJECT+\".\"+DATASET+\".\"+TABLE_REL\n",
+ "table_mentions = PROJECT+\".\"+DATASET+\".\"+TABLE_ENTITYMENTIONS\n",
+ "\n",
+ "# If table exists, delete the tables.\n",
+ "client.delete_table(table_ent, not_found_ok=True)\n",
+ "client.delete_table(table_rel, not_found_ok=True)\n",
+ "client.delete_table(table_mentions, not_found_ok=True)\n",
+ "\n",
+ "# Create tables\n",
+ "\n",
+ "table = bigquery.Table(table_ent, schema=schemaEntity)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "\n",
+ "table = bigquery.Table(table_rel, schema=schemaRelations)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")\n",
+ "table = bigquery.Table(table_mentions, schema=schemaEntityMentions)\n",
+ "table = client.create_table(table) # Make an API request.\n",
+ "print(\n",
+ " \"Created table {}.{}.{}\".format(table.project, table.dataset_id, table.table_id)\n",
+ ")"
+ ],
+ "metadata": {
+ "id": "bZDqtFVE5Wd_"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "**Pipeline Setup**\n",
+ "\n",
+ "For the purpose of experimenting, I have setup an interactiveRunner. But this should be changed to a DatafowRunner once you are comfortable with it"
Review Comment:
Why recommend Dataflow here? For notebooks the interactive runner seems like the best choice.
also "DatafowRunner" -> "DataflowRunner"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org