-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
entities_to_dict()
and entities_to_bigquery()
to `Docum…
…ent` wrapper (#50) * feat: Add `entities_to_dict()` and `entities_to_bigquery()` to Document wrapper - Uploads entities to an existing dataset, creates new table if it doesn't already exist. ## Example Output Table | supplier_iban | purchase_order | supplier_email | freight_amount | supplier_address | receiver_address | total_amount | supplier_name | total_tax_amount | payment_terms | line_item | receiver_name | receiver_email | due_date | invoice_date | invoice_id | currency | receiver_tax_id | net_amount | vat | |---------------|----------------|---------------------|----------------|------------------------------|------------------------------|--------------|---------------|------------------|------------------|-------------------------------------------------------------------------------|---------------|-------------------|------------|--------------|------------|----------|-----------------|------------|-----| | 50 | 1 | user@companyabc.com | 600 | 111 Main Street Anytown, USA | 222 Main Street Anytown, USA | 2140 | Company ABC | 140 | 6 month contract | [Tool A 500 1.00 500.00,Service B 1 900.00 900.00,Resource C 50 12.00 600.00] | John Doe | johndoe@email.com | 2025-01-01 | 1970-01-01 | NO. 001 | $ | 1 | 2000 | 140 | * Removed unneeded test code * Added bigquery library to setup.py * Updated Docstrings * Fixed Test import linter error * Added bigQuery Library to Testing Constraints * Added handling of Nested Entities (properties) * Dependency Update for Tests * Update Dependencies * Fixed Test Output * Updated DatasetReference based on Deprecation Warning * samples: Added Entities to BigQuery Sample Code * Added Required tag to `entities_to_bigquery()` arguments * Fixed Issues from merge conflict * Fixed numpy import --------- Co-authored-by: Gal Zahavi <38544478+galz10@users.noreply.github.com>
- Loading branch information
1 parent
8359911
commit 494fa86
Showing
15 changed files
with
255 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
|
||
# [START documentai_toolbox_entities_to_bigquery] | ||
|
||
from google.cloud.documentai_toolbox import document | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# Given a document.proto or sharded document.proto in path gs://bucket/path/to/folder | ||
# gcs_bucket_name = "bucket" | ||
# gcs_prefix = "path/to/folder" | ||
# dataset_name = "test_dataset" | ||
# table_name = "test_table" | ||
# project_id = "YOUR_PROJECT_ID" | ||
|
||
|
||
def entities_to_bigquery_sample( | ||
gcs_bucket_name: str, | ||
gcs_prefix: str, | ||
dataset_name: str, | ||
table_name: str, | ||
project_id: str, | ||
) -> None: | ||
wrapped_document = document.Document.from_gcs( | ||
gcs_bucket_name=gcs_bucket_name, gcs_prefix=gcs_prefix | ||
) | ||
|
||
job = wrapped_document.entities_to_bigquery( | ||
dataset_name=dataset_name, table_name=table_name, project_id=project_id | ||
) | ||
|
||
print("Document entities loaded into BigQuery") | ||
print(f"Job ID: {job.job_id}") | ||
print(f"Table: {job.destination.path}") | ||
|
||
|
||
# [END documentai_toolbox_entities_to_bigquery] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pytest==7.2.1 | ||
mock==5.0.1 | ||
mock==5.0.1 | ||
google-cloud-bigquery==3.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
google-cloud-bigquery==3.5.0 | ||
google-cloud-documentai==2.12.0 | ||
google-cloud-storage==2.7.0 | ||
google-cloud-documentai-toolbox==0.1.1a0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import os | ||
import uuid | ||
|
||
import pytest | ||
from samples.snippets import entities_to_bigquery_sample | ||
|
||
from google.cloud import bigquery | ||
|
||
location = "us" | ||
project_id = os.environ["GOOGLE_CLOUD_PROJECT"] | ||
gcs_bucket_name = "documentai_toolbox_samples" | ||
gcs_input_uri = "output/123456789/0" | ||
dataset_name = f"document_ai_toolbox_test_{uuid.uuid4().hex}" | ||
table_name = f"test_table_{uuid.uuid4().hex}" | ||
|
||
|
||
def test_entities_to_bigquery_sample(capsys: pytest.CaptureFixture) -> None: | ||
client = bigquery.Client(project=project_id) | ||
dataset = bigquery.Dataset(f"{project_id}.{dataset_name}") | ||
dataset.location = "US" | ||
dataset = client.create_dataset(dataset, timeout=30, exists_ok=True) | ||
|
||
entities_to_bigquery_sample.entities_to_bigquery_sample( | ||
gcs_bucket_name=gcs_bucket_name, | ||
gcs_prefix=gcs_input_uri, | ||
dataset_name=dataset_name, | ||
table_name=table_name, | ||
project_id=project_id, | ||
) | ||
out, _ = capsys.readouterr() | ||
|
||
assert "Document entities loaded into BigQuery" in out | ||
assert "Job ID:" in out | ||
assert ( | ||
f"Table: /projects/{project_id}/datasets/{dataset_name}/tables/{table_name}" | ||
in out | ||
) | ||
|
||
client.delete_dataset(dataset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.