mik-laj commented on a change in pull request #5720: [AIRFLOW-5099] Implement Google Cloud AutoML operators URL: https://github.com/apache/airflow/pull/5720#discussion_r317270546
########## File path: airflow/gcp/hooks/automl.py ########## @@ -0,0 +1,733 @@ +# -*- coding: utf-8 -*- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +""" +This module contains a Google AutoML hook. +""" +from typing import Dict, Sequence, Tuple, Union, List +from cached_property import cached_property + +from google.api_core.retry import Retry +from google.cloud.automl_v1beta1 import PredictionServiceClient, AutoMlClient +from google.cloud.automl_v1beta1.types import ( + BatchPredictInputConfig, + BatchPredictOutputConfig, + Model, + ExamplePayload, + Dataset, + FieldMask, + ImageObjectDetectionModelDeploymentMetadata, + PredictResponse, + ColumnSpec, + Operation, + TableSpec, + InputConfig, +) + +from airflow.contrib.hooks.gcp_api_base_hook import GoogleCloudBaseHook + + +class CloudAutoMLHook(GoogleCloudBaseHook): + """ + Google Cloud AutoML hook. + + All the methods in the hook where project_id is used must be called with + keyword arguments rather than positional. + """ + + def __init__(self, gcp_conn_id: str = "google_cloud_default", delegate_to=None): + super().__init__(gcp_conn_id, delegate_to) + self._client = None + + @staticmethod + def extract_object_id(obj: dict) -> str: + """ + Returns unique id of the object. + """ + return obj["name"].rpartition("/")[-1] + + def get_conn(self) -> AutoMlClient: + """ + Retrieves connection to AutoML. + + :return: Google Cloud AutoML client object. + :rtype: google.cloud.automl_v1beta1.AutoMlClient + """ + if self._client is None: + self._client = AutoMlClient( + credentials=self._get_credentials(), + client_info=self.client_info + ) + return self._client + + @cached_property + def prediction_client(self) -> PredictionServiceClient: + """ + Creates PredictionServiceClient. + + :return: Google Cloud AutoML PredictionServiceClient client object. + :rtype: google.cloud.automl_v1beta1.PredictionServiceClient + """ + return PredictionServiceClient( + credentials=self._get_credentials(), + client_info=self.client_info + ) + + @GoogleCloudBaseHook.catch_http_exception + @GoogleCloudBaseHook.fallback_to_default_project_id + def create_model( + self, + model: Union[dict, Model], + location: str, + project_id: str = None, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = None, + retry: Retry = None, + ) -> Operation: Review comment: ```suggestion ) -> Operation: ``` I think, hook should wait for the result of the operation. Let's talk about it on Monday. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
