-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components - Added AutoML Tables components and tests (#2174)
* Components - Added AutoML Tables components * Added the sample - AutoML Tables - Retail product stockout prediction * Replaced the project ID with dummy placeholder * Fixed the description parameter passing * Replaced pip with pip3 and changed quotes * Added licenses * Updated the component links * Revert "Replaced pip with pip3" This reverts commit 65ed0a7. (part of it) Here, `pip` is not the name of executable. It's the module name which is just `pip`, not `pip3`. * Changed quotes to single quotes * Moved the components to the gcp folder * Switched container images to python:3.7 * Updated component versions in sample
- Loading branch information
1 parent
33bbe54
commit 4339e70
Showing
13 changed files
with
1,261 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
components/gcp/automl/create_dataset_for_tables/component.py
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,58 @@ | ||
# Copyright 2019 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. | ||
|
||
from typing import NamedTuple | ||
|
||
|
||
def automl_create_dataset_for_tables( | ||
gcp_project_id: str, | ||
gcp_region: str, | ||
display_name: str, | ||
description: str = None, | ||
tables_dataset_metadata: dict = {}, | ||
retry=None, #=google.api_core.gapic_v1.method.DEFAULT, | ||
timeout: float = None, #=google.api_core.gapic_v1.method.DEFAULT, | ||
metadata: dict = None, | ||
) -> NamedTuple('Outputs', [('dataset_path', str), ('create_time', str), ('dataset_id', str)]): | ||
'''automl_create_dataset_for_tables creates an empty Dataset for AutoML tables | ||
''' | ||
import sys | ||
import subprocess | ||
subprocess.run([sys.executable, '-m', 'pip', 'install', 'google-cloud-automl==0.4.0', '--quiet', '--no-warn-script-location'], env={'PIP_DISABLE_PIP_VERSION_CHECK': '1'}, check=True) | ||
|
||
import google | ||
from google.cloud import automl | ||
client = automl.AutoMlClient() | ||
|
||
location_path = client.location_path(gcp_project_id, gcp_region) | ||
dataset_dict = { | ||
'display_name': display_name, | ||
'description': description, | ||
'tables_dataset_metadata': tables_dataset_metadata, | ||
} | ||
dataset = client.create_dataset( | ||
location_path, | ||
dataset_dict, | ||
retry or google.api_core.gapic_v1.method.DEFAULT, | ||
timeout or google.api_core.gapic_v1.method.DEFAULT, | ||
metadata, | ||
) | ||
print(dataset) | ||
dataset_id = dataset.name.rsplit('/', 1)[-1] | ||
return (dataset.name, dataset.create_time, dataset_id) | ||
|
||
|
||
if __name__ == '__main__': | ||
import kfp | ||
kfp.components.func_to_container_op(automl_create_dataset_for_tables, output_component_file='component.yaml', base_image='python:3.7') |
149 changes: 149 additions & 0 deletions
149
components/gcp/automl/create_dataset_for_tables/component.yaml
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,149 @@ | ||
description: | | ||
automl_create_dataset_for_tables creates an empty Dataset for AutoML tables | ||
implementation: | ||
container: | ||
args: | ||
- --gcp-project-id | ||
- inputValue: gcp_project_id | ||
- --gcp-region | ||
- inputValue: gcp_region | ||
- --display-name | ||
- inputValue: display_name | ||
- if: | ||
cond: | ||
isPresent: description | ||
then: | ||
- --description | ||
- inputValue: description | ||
- if: | ||
cond: | ||
isPresent: tables_dataset_metadata | ||
then: | ||
- --tables-dataset-metadata | ||
- inputValue: tables_dataset_metadata | ||
- if: | ||
cond: | ||
isPresent: retry | ||
then: | ||
- --retry | ||
- inputValue: retry | ||
- if: | ||
cond: | ||
isPresent: timeout | ||
then: | ||
- --timeout | ||
- inputValue: timeout | ||
- if: | ||
cond: | ||
isPresent: metadata | ||
then: | ||
- --metadata | ||
- inputValue: metadata | ||
- '----output-paths' | ||
- outputPath: dataset_path | ||
- outputPath: create_time | ||
- outputPath: dataset_id | ||
command: | ||
- python3 | ||
- -u | ||
- -c | ||
- | | ||
from typing import NamedTuple | ||
def automl_create_dataset_for_tables( | ||
gcp_project_id: str, | ||
gcp_region: str, | ||
display_name: str, | ||
description: str = None, | ||
tables_dataset_metadata: dict = {}, | ||
retry=None, #=google.api_core.gapic_v1.method.DEFAULT, | ||
timeout: float = None, #=google.api_core.gapic_v1.method.DEFAULT, | ||
metadata: dict = None, | ||
) -> NamedTuple('Outputs', [('dataset_path', str), ('create_time', str), ('dataset_id', str)]): | ||
'''automl_create_dataset_for_tables creates an empty Dataset for AutoML tables | ||
''' | ||
import sys | ||
import subprocess | ||
subprocess.run([sys.executable, '-m', 'pip', 'install', 'google-cloud-automl==0.4.0', '--quiet', '--no-warn-script-location'], env={'PIP_DISABLE_PIP_VERSION_CHECK': '1'}, check=True) | ||
import google | ||
from google.cloud import automl | ||
client = automl.AutoMlClient() | ||
location_path = client.location_path(gcp_project_id, gcp_region) | ||
dataset_dict = { | ||
'display_name': display_name, | ||
'description': description, | ||
'tables_dataset_metadata': tables_dataset_metadata, | ||
} | ||
dataset = client.create_dataset( | ||
location_path, | ||
dataset_dict, | ||
retry or google.api_core.gapic_v1.method.DEFAULT, | ||
timeout or google.api_core.gapic_v1.method.DEFAULT, | ||
metadata, | ||
) | ||
print(dataset) | ||
dataset_id = dataset.name.rsplit('/', 1)[-1] | ||
return (dataset.name, dataset.create_time, dataset_id) | ||
import json | ||
import argparse | ||
_missing_arg = object() | ||
_parser = argparse.ArgumentParser(prog='Automl create dataset for tables', description='automl_create_dataset_for_tables creates an empty Dataset for AutoML tables\n') | ||
_parser.add_argument("--gcp-project-id", dest="gcp_project_id", type=str, required=True, default=_missing_arg) | ||
_parser.add_argument("--gcp-region", dest="gcp_region", type=str, required=True, default=_missing_arg) | ||
_parser.add_argument("--display-name", dest="display_name", type=str, required=True, default=_missing_arg) | ||
_parser.add_argument("--description", dest="description", type=str, required=False, default=_missing_arg) | ||
_parser.add_argument("--tables-dataset-metadata", dest="tables_dataset_metadata", type=json.loads, required=False, default=_missing_arg) | ||
_parser.add_argument("--retry", dest="retry", type=str, required=False, default=_missing_arg) | ||
_parser.add_argument("--timeout", dest="timeout", type=float, required=False, default=_missing_arg) | ||
_parser.add_argument("--metadata", dest="metadata", type=json.loads, required=False, default=_missing_arg) | ||
_parser.add_argument("----output-paths", dest="_output_paths", type=str, nargs=3) | ||
_parsed_args = {k: v for k, v in vars(_parser.parse_args()).items() if v is not _missing_arg} | ||
_output_files = _parsed_args.pop("_output_paths", []) | ||
_outputs = automl_create_dataset_for_tables(**_parsed_args) | ||
if not hasattr(_outputs, '__getitem__') or isinstance(_outputs, str): | ||
_outputs = [_outputs] | ||
import os | ||
for idx, output_file in enumerate(_output_files): | ||
try: | ||
os.makedirs(os.path.dirname(output_file)) | ||
except OSError: | ||
pass | ||
with open(output_file, 'w') as f: | ||
f.write(str(_outputs[idx])) | ||
image: python:3.7 | ||
inputs: | ||
- name: gcp_project_id | ||
type: String | ||
- name: gcp_region | ||
type: String | ||
- name: display_name | ||
type: String | ||
- name: description | ||
optional: true | ||
type: String | ||
- default: '{}' | ||
name: tables_dataset_metadata | ||
optional: true | ||
type: JsonObject | ||
- name: retry | ||
optional: true | ||
- name: timeout | ||
optional: true | ||
type: Float | ||
- name: metadata | ||
optional: true | ||
type: JsonObject | ||
name: Automl create dataset for tables | ||
outputs: | ||
- name: dataset_path | ||
type: String | ||
- name: create_time | ||
type: String | ||
- name: dataset_id | ||
type: String |
58 changes: 58 additions & 0 deletions
58
components/gcp/automl/create_model_for_tables/component.py
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,58 @@ | ||
# Copyright 2019 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. | ||
|
||
from typing import NamedTuple | ||
|
||
|
||
def automl_create_model_for_tables( | ||
gcp_project_id: str, | ||
gcp_region: str, | ||
display_name: str, | ||
dataset_id: str, | ||
target_column_path: str = None, | ||
input_feature_column_paths: list = None, | ||
optimization_objective: str = 'MAXIMIZE_AU_PRC', | ||
train_budget_milli_node_hours: int = 1000, | ||
) -> NamedTuple('Outputs', [('model_path', str), ('model_id', str)]): | ||
import sys | ||
import subprocess | ||
subprocess.run([sys.executable, '-m', 'pip', 'install', 'google-cloud-automl==0.4.0', '--quiet', '--no-warn-script-location'], env={'PIP_DISABLE_PIP_VERSION_CHECK': '1'}, check=True) | ||
|
||
from google.cloud import automl | ||
client = automl.AutoMlClient() | ||
|
||
location_path = client.location_path(gcp_project_id, gcp_region) | ||
model_dict = { | ||
'display_name': display_name, | ||
'dataset_id': dataset_id, | ||
'tables_model_metadata': { | ||
'target_column_spec': automl.types.ColumnSpec(name=target_column_path), | ||
'input_feature_column_specs': [automl.types.ColumnSpec(name=path) for path in input_feature_column_paths] if input_feature_column_paths else None, | ||
'optimization_objective': optimization_objective, | ||
'train_budget_milli_node_hours': train_budget_milli_node_hours, | ||
}, | ||
} | ||
|
||
create_model_response = client.create_model(location_path, model_dict) | ||
print('Create model operation: {}'.format(create_model_response.operation)) | ||
result = create_model_response.result() | ||
print(result) | ||
model_name = result.name | ||
model_id = model_name.rsplit('/', 1)[-1] | ||
return (model_name, model_id) | ||
|
||
|
||
if __name__ == '__main__': | ||
import kfp | ||
kfp.components.func_to_container_op(automl_create_model_for_tables, output_component_file='component.yaml', base_image='python:3.7') |
Oops, something went wrong.