Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added batch prediction tabular forecasting sample #156

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,28 @@
def make_parent(parent: str) -> str:
parent = parent

return parent


def make_batch_prediction_job(
display_name: str,
model_name: str,
gcs_source_uri: str,
gcs_destination_output_uri_prefix: str,
predictions_format: str,
) -> google.cloud.aiplatform_v1beta1.types.batch_prediction_job.BatchPredictionJob:
batch_prediction_job = {
"display_name": display_name,
# Format: 'projects/{project}/locations/{location}/models/{model_id}'
"model": model_name,
"input_config": {
"instances_format": predictions_format,
"gcs_source": {"uris": [gcs_source_uri]},
},
"output_config": {
"predictions_format": predictions_format,
"gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix},
},
}
return batch_prediction_job

@@ -0,0 +1,54 @@
# Copyright 2020 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
#
# https://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 aiplatform_create_batch_prediction_job_tabular_forecasting_sample]
from google.cloud import aiplatform


def create_batch_prediction_job_tabular_forecasting_sample(
project: str,
display_name: str,
model_name: str,
gcs_source_uri: str,
gcs_destination_output_uri_prefix: str,
predictions_format: str,
location: str = "us-central1",
api_endpoint: str = "us-central1-aiplatform.googleapis.com",
):
# The AI Platform services require regional API endpoints.
client_options = {"api_endpoint": api_endpoint}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.JobServiceClient(client_options=client_options)
batch_prediction_job = {
"display_name": display_name,
# Format: 'projects/{project}/locations/{location}/models/{model_id}'
"model": model_name,
"input_config": {
"instances_format": predictions_format,
"gcs_source": {"uris": [gcs_source_uri]},
},
"output_config": {
"predictions_format": predictions_format,
"gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix},
},
}
parent = f"projects/{project}/locations/{location}"
response = client.create_batch_prediction_job(
parent=parent, batch_prediction_job=batch_prediction_job
)
print("response:", response)


# [END aiplatform_create_batch_prediction_job_tabular_forecasting_sample]
@@ -0,0 +1,54 @@
# Copyright 2020 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
#
# https://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
from uuid import uuid4

import pytest

import create_batch_prediction_job_tabular_forecasting_sample
import helpers

PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT")
LOCATION = "us-central1"
MODEL_ID = "8531330622239539200" # Permanent restaurant rating model
DISPLAY_NAME = f"temp_create_batch_prediction_tabular_forecasting_test_{uuid4()}"
GCS_SOURCE_URI = "gs://cloud-samples-data/ai-platform/covid/bigquery-public-covid-nyt-us-counties-train.csv"
GCS_OUTPUT_URI = "gs://ucaip-samples-test-output/"
PREDICTIONS_FORMAT = "csv"


@pytest.fixture(scope="function", autouse=True)
def teardown(teardown_batch_prediction_job):
yield


# Creating AutoML Tabular Forecasting Classification batch prediction job
def test_create_batch_prediction_job_tabular_forecasting_sample(capsys, shared_state):

model_name = f"projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}"

create_batch_prediction_job_tabular_forecasting_sample.create_batch_prediction_job_tabular_forecasting_sample(
project=PROJECT_ID,
display_name=DISPLAY_NAME,
model_name=model_name,
gcs_source_uri=GCS_SOURCE_URI,
gcs_destination_output_uri_prefix=GCS_OUTPUT_URI,
predictions_format=PREDICTIONS_FORMAT,
)

out, _ = capsys.readouterr()

# Save resource name of the newly created batch prediction job
shared_state["batch_prediction_job_name"] = helpers.get_name(out)