apache-airflow-providers-google 10.18.0rc1__py3-none-any.whl → 10.18.0rc2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,8 +21,10 @@ from __future__ import annotations
21
21
 
22
22
  import ast
23
23
  import warnings
24
+ from functools import cached_property
24
25
  from typing import TYPE_CHECKING, Sequence, Tuple
25
26
 
27
+ from deprecated import deprecated
26
28
  from google.api_core.gapic_v1.method import DEFAULT, _MethodDefault
27
29
  from google.cloud.automl_v1beta1 import (
28
30
  BatchPredictResult,
@@ -33,8 +35,9 @@ from google.cloud.automl_v1beta1 import (
33
35
  TableSpec,
34
36
  )
35
37
 
36
- from airflow.exceptions import AirflowProviderDeprecationWarning
38
+ from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
37
39
  from airflow.providers.google.cloud.hooks.automl import CloudAutoMLHook
40
+ from airflow.providers.google.cloud.hooks.vertex_ai.prediction_service import PredictionServiceHook
38
41
  from airflow.providers.google.cloud.links.automl import (
39
42
  AutoMLDatasetLink,
40
43
  AutoMLDatasetListLink,
@@ -53,12 +56,36 @@ if TYPE_CHECKING:
53
56
  MetaData = Sequence[Tuple[str, str]]
54
57
 
55
58
 
59
+ def _raise_exception_for_deprecated_operator(
60
+ deprecated_class_name: str, alternative_class_names: str | list[str]
61
+ ):
62
+ if isinstance(alternative_class_names, str):
63
+ alternative_class_name_str = alternative_class_names
64
+ elif len(alternative_class_names) == 1:
65
+ alternative_class_name_str = alternative_class_names[0]
66
+ else:
67
+ alternative_class_name_str = ", ".join(f"`{cls_name}`" for cls_name in alternative_class_names[:-1])
68
+ alternative_class_name_str += f" or `{alternative_class_names[-1]}`"
69
+
70
+ raise AirflowException(
71
+ f"{deprecated_class_name} for text, image, and video prediction has been "
72
+ f"deprecated and no longer available. All the functionality of "
73
+ f"legacy AutoML Natural Language, Vision, Video Intelligence and Tables "
74
+ f"and new features are available on the Vertex AI platform. "
75
+ f"Please use {alternative_class_name_str} from Vertex AI."
76
+ )
77
+
78
+
56
79
  class AutoMLTrainModelOperator(GoogleCloudBaseOperator):
57
80
  """
58
81
  Creates Google Cloud AutoML model.
59
82
 
60
- AutoMLTrainModelOperator for text prediction is deprecated. Please use
61
- :class:`airflow.providers.google.cloud.operators.vertex_ai.auto_ml.CreateAutoMLTextTrainingJobOperator`
83
+ AutoMLTrainModelOperator for tables, video intelligence, vision and natural language has been deprecated
84
+ and no longer available. Please use
85
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.auto_ml.CreateAutoMLTabularTrainingJobOperator`,
86
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.auto_ml.CreateAutoMLVideoTrainingJobOperator`,
87
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.auto_ml.CreateAutoMLImageTrainingJobOperator`,
88
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.auto_ml.CreateAutoMLTextTrainingJobOperator`,
62
89
  instead.
63
90
 
64
91
  .. seealso::
@@ -120,17 +147,16 @@ class AutoMLTrainModelOperator(GoogleCloudBaseOperator):
120
147
  self.impersonation_chain = impersonation_chain
121
148
 
122
149
  def execute(self, context: Context):
123
- # Output warning if running not AutoML Translation prediction job
150
+ # Raise exception if running not AutoML Translation prediction job
124
151
  if "translation_model_metadata" not in self.model:
125
- warnings.warn(
126
- "AutoMLTrainModelOperator for text, image and video prediction is deprecated. "
127
- "All the functionality of legacy "
128
- "AutoML Natural Language, Vision and Video Intelligence and new features are available "
129
- "on the Vertex AI platform. "
130
- "Please use `CreateAutoMLTextTrainingJobOperator`, `CreateAutoMLImageTrainingJobOperator` or"
131
- " `CreateAutoMLVideoTrainingJobOperator` from VertexAI.",
132
- AirflowProviderDeprecationWarning,
133
- stacklevel=3,
152
+ _raise_exception_for_deprecated_operator(
153
+ self.__class__.__name__,
154
+ [
155
+ "CreateAutoMLTabularTrainingJobOperator",
156
+ "CreateAutoMLVideoTrainingJobOperator",
157
+ "CreateAutoMLImageTrainingJobOperator",
158
+ "CreateAutoMLTextTrainingJobOperator",
159
+ ],
134
160
  )
135
161
  hook = CloudAutoMLHook(
136
162
  gcp_conn_id=self.gcp_conn_id,
@@ -174,7 +200,8 @@ class AutoMLPredictOperator(GoogleCloudBaseOperator):
174
200
  :ref:`howto/operator:AutoMLPredictOperator`
175
201
 
176
202
  :param model_id: Name of the model requested to serve the batch prediction.
177
- :param payload: Name od the model used for the prediction.
203
+ :param endpoint_id: Name of the endpoint used for the prediction.
204
+ :param payload: Name of the model used for the prediction.
178
205
  :param project_id: ID of the Google Cloud project where model is located if None then
179
206
  default project_id is used.
180
207
  :param location: The location of the project.
@@ -206,10 +233,12 @@ class AutoMLPredictOperator(GoogleCloudBaseOperator):
206
233
  def __init__(
207
234
  self,
208
235
  *,
209
- model_id: str,
236
+ model_id: str | None = None,
237
+ endpoint_id: str | None = None,
210
238
  location: str,
211
239
  payload: dict,
212
240
  operation_params: dict[str, str] | None = None,
241
+ instances: list[str] | None = None,
213
242
  project_id: str = PROVIDE_PROJECT_ID,
214
243
  metadata: MetaData = (),
215
244
  timeout: float | None = None,
@@ -221,7 +250,9 @@ class AutoMLPredictOperator(GoogleCloudBaseOperator):
221
250
  super().__init__(**kwargs)
222
251
 
223
252
  self.model_id = model_id
253
+ self.endpoint_id = endpoint_id
224
254
  self.operation_params = operation_params # type: ignore
255
+ self.instances = instances
225
256
  self.location = location
226
257
  self.project_id = project_id
227
258
  self.metadata = metadata
@@ -231,23 +262,69 @@ class AutoMLPredictOperator(GoogleCloudBaseOperator):
231
262
  self.gcp_conn_id = gcp_conn_id
232
263
  self.impersonation_chain = impersonation_chain
233
264
 
234
- def execute(self, context: Context):
235
- hook = CloudAutoMLHook(
236
- gcp_conn_id=self.gcp_conn_id,
237
- impersonation_chain=self.impersonation_chain,
238
- )
239
- result = hook.predict(
265
+ @cached_property
266
+ def hook(self) -> CloudAutoMLHook | PredictionServiceHook:
267
+ if self.model_id:
268
+ return CloudAutoMLHook(
269
+ gcp_conn_id=self.gcp_conn_id,
270
+ impersonation_chain=self.impersonation_chain,
271
+ )
272
+ else: # endpoint_id defined
273
+ return PredictionServiceHook(
274
+ gcp_conn_id=self.gcp_conn_id,
275
+ impersonation_chain=self.impersonation_chain,
276
+ )
277
+
278
+ def _check_model_type(self):
279
+ hook = self.hook
280
+ model = hook.get_model(
240
281
  model_id=self.model_id,
241
- payload=self.payload,
242
282
  location=self.location,
243
283
  project_id=self.project_id,
244
- params=self.operation_params,
245
284
  retry=self.retry,
246
285
  timeout=self.timeout,
247
286
  metadata=self.metadata,
248
287
  )
288
+ if not hasattr(model, "translation_model_metadata"):
289
+ raise AirflowException(
290
+ "AutoMLPredictOperator for text, image, and video prediction has been deprecated. "
291
+ "Please use endpoint_id param instead of model_id param."
292
+ )
293
+
294
+ def execute(self, context: Context):
295
+ if self.model_id is None and self.endpoint_id is None:
296
+ raise AirflowException("You must specify model_id or endpoint_id!")
297
+
298
+ if self.model_id:
299
+ self._check_model_type()
300
+
301
+ hook = self.hook
302
+ if self.model_id:
303
+ result = hook.predict(
304
+ model_id=self.model_id,
305
+ payload=self.payload,
306
+ location=self.location,
307
+ project_id=self.project_id,
308
+ params=self.operation_params,
309
+ retry=self.retry,
310
+ timeout=self.timeout,
311
+ metadata=self.metadata,
312
+ )
313
+ else: # self.endpoint_id is defined
314
+ result = hook.predict(
315
+ endpoint_id=self.endpoint_id,
316
+ instances=self.instances,
317
+ payload=self.payload,
318
+ location=self.location,
319
+ project_id=self.project_id,
320
+ parameters=self.operation_params,
321
+ retry=self.retry,
322
+ timeout=self.timeout,
323
+ metadata=self.metadata,
324
+ )
325
+
249
326
  project_id = self.project_id or hook.project_id
250
- if project_id:
327
+ if project_id and self.model_id:
251
328
  AutoMLModelPredictLink.persist(
252
329
  context=context,
253
330
  task_instance=self,
@@ -261,6 +338,14 @@ class AutoMLBatchPredictOperator(GoogleCloudBaseOperator):
261
338
  """
262
339
  Perform a batch prediction on Google Cloud AutoML.
263
340
 
341
+ AutoMLBatchPredictOperator for tables, video intelligence, vision and natural language has been deprecated
342
+ and no longer available. Please use
343
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.batch_prediction_job.CreateBatchPredictionJobOperator`,
344
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.batch_prediction_job.GetBatchPredictionJobOperator`,
345
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.batch_prediction_job.ListBatchPredictionJobsOperator`,
346
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.batch_prediction_job.DeleteBatchPredictionJobOperator`,
347
+ instead.
348
+
264
349
  .. seealso::
265
350
  For more information on how to use this operator, take a look at the guide:
266
351
  :ref:`howto/operator:AutoMLBatchPredictOperator`
@@ -341,6 +426,25 @@ class AutoMLBatchPredictOperator(GoogleCloudBaseOperator):
341
426
  gcp_conn_id=self.gcp_conn_id,
342
427
  impersonation_chain=self.impersonation_chain,
343
428
  )
429
+ model: Model = hook.get_model(
430
+ model_id=self.model_id,
431
+ location=self.location,
432
+ project_id=self.project_id,
433
+ retry=self.retry,
434
+ timeout=self.timeout,
435
+ metadata=self.metadata,
436
+ )
437
+
438
+ if not hasattr(model, "translation_model_metadata"):
439
+ _raise_exception_for_deprecated_operator(
440
+ self.__class__.__name__,
441
+ [
442
+ "CreateBatchPredictionJobOperator",
443
+ "GetBatchPredictionJobOperator",
444
+ "ListBatchPredictionJobsOperator",
445
+ "DeleteBatchPredictionJobOperator",
446
+ ],
447
+ )
344
448
  self.log.info("Fetch batch prediction.")
345
449
  operation = hook.batch_predict(
346
450
  model_id=self.model_id,
@@ -371,6 +475,10 @@ class AutoMLCreateDatasetOperator(GoogleCloudBaseOperator):
371
475
  """
372
476
  Creates a Google Cloud AutoML dataset.
373
477
 
478
+ AutoMLCreateDatasetOperator for tables, video intelligence, vision and natural language has been
479
+ deprecated and no longer available. Please use
480
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.dataset.CreateDatasetOperator` instead.
481
+
374
482
  .. seealso::
375
483
  For more information on how to use this operator, take a look at the guide:
376
484
  :ref:`howto/operator:AutoMLCreateDatasetOperator`
@@ -430,6 +538,8 @@ class AutoMLCreateDatasetOperator(GoogleCloudBaseOperator):
430
538
  self.impersonation_chain = impersonation_chain
431
539
 
432
540
  def execute(self, context: Context):
541
+ if "translation_dataset_metadata" not in self.dataset:
542
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "CreateDatasetOperator")
433
543
  hook = CloudAutoMLHook(
434
544
  gcp_conn_id=self.gcp_conn_id,
435
545
  impersonation_chain=self.impersonation_chain,
@@ -463,6 +573,10 @@ class AutoMLImportDataOperator(GoogleCloudBaseOperator):
463
573
  """
464
574
  Imports data to a Google Cloud AutoML dataset.
465
575
 
576
+ AutoMLImportDataOperator for tables, video intelligence, vision and natural language has been deprecated
577
+ and no longer available. Please use
578
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.dataset.ImportDataOperator` instead.
579
+
466
580
  .. seealso::
467
581
  For more information on how to use this operator, take a look at the guide:
468
582
  :ref:`howto/operator:AutoMLImportDataOperator`
@@ -530,6 +644,16 @@ class AutoMLImportDataOperator(GoogleCloudBaseOperator):
530
644
  gcp_conn_id=self.gcp_conn_id,
531
645
  impersonation_chain=self.impersonation_chain,
532
646
  )
647
+ dataset: Dataset = hook.get_dataset(
648
+ dataset_id=self.dataset_id,
649
+ location=self.location,
650
+ project_id=self.project_id,
651
+ retry=self.retry,
652
+ timeout=self.timeout,
653
+ metadata=self.metadata,
654
+ )
655
+ if not hasattr(dataset, "translation_dataset_metadata"):
656
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "ImportDataOperator")
533
657
  self.log.info("Importing data to dataset...")
534
658
  operation = hook.import_data(
535
659
  dataset_id=self.dataset_id,
@@ -662,10 +786,22 @@ class AutoMLTablesListColumnSpecsOperator(GoogleCloudBaseOperator):
662
786
  return result
663
787
 
664
788
 
789
+ @deprecated(
790
+ reason=(
791
+ "Class `AutoMLTablesUpdateDatasetOperator` has been deprecated and no longer available. "
792
+ "Please use `UpdateDatasetOperator` instead"
793
+ ),
794
+ category=AirflowProviderDeprecationWarning,
795
+ action="error",
796
+ )
665
797
  class AutoMLTablesUpdateDatasetOperator(GoogleCloudBaseOperator):
666
798
  """
667
799
  Updates a dataset.
668
800
 
801
+ AutoMLTablesUpdateDatasetOperator has been deprecated and no longer available. Please use
802
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.dataset.UpdateDatasetOperator`
803
+ instead.
804
+
669
805
  .. seealso::
670
806
  For more information on how to use this operator, take a look at the guide:
671
807
  :ref:`howto/operator:AutoMLTablesUpdateDatasetOperator`
@@ -753,6 +889,10 @@ class AutoMLGetModelOperator(GoogleCloudBaseOperator):
753
889
  """
754
890
  Get Google Cloud AutoML model.
755
891
 
892
+ AutoMLGetModelOperator for tables, video intelligence, vision and natural language has been deprecated
893
+ and no longer available. Please use
894
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.model_service.GetModelOperator` instead.
895
+
756
896
  .. seealso::
757
897
  For more information on how to use this operator, take a look at the guide:
758
898
  :ref:`howto/operator:AutoMLGetModelOperator`
@@ -823,6 +963,8 @@ class AutoMLGetModelOperator(GoogleCloudBaseOperator):
823
963
  timeout=self.timeout,
824
964
  metadata=self.metadata,
825
965
  )
966
+ if not hasattr(result, "translation_model_metadata"):
967
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "GetModelOperator")
826
968
  model = Model.to_dict(result)
827
969
  project_id = self.project_id or hook.project_id
828
970
  if project_id:
@@ -840,6 +982,10 @@ class AutoMLDeleteModelOperator(GoogleCloudBaseOperator):
840
982
  """
841
983
  Delete Google Cloud AutoML model.
842
984
 
985
+ AutoMLDeleteModelOperator for tables, video intelligence, vision and natural language has been deprecated
986
+ and no longer available. Please use
987
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.model_service.DeleteModelOperator` instead.
988
+
843
989
  .. seealso::
844
990
  For more information on how to use this operator, take a look at the guide:
845
991
  :ref:`howto/operator:AutoMLDeleteModelOperator`
@@ -901,6 +1047,16 @@ class AutoMLDeleteModelOperator(GoogleCloudBaseOperator):
901
1047
  gcp_conn_id=self.gcp_conn_id,
902
1048
  impersonation_chain=self.impersonation_chain,
903
1049
  )
1050
+ model: Model = hook.get_model(
1051
+ model_id=self.model_id,
1052
+ location=self.location,
1053
+ project_id=self.project_id,
1054
+ retry=self.retry,
1055
+ timeout=self.timeout,
1056
+ metadata=self.metadata,
1057
+ )
1058
+ if not hasattr(model, "translation_model_metadata"):
1059
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "DeleteModelOperator")
904
1060
  operation = hook.delete_model(
905
1061
  model_id=self.model_id,
906
1062
  location=self.location,
@@ -913,6 +1069,14 @@ class AutoMLDeleteModelOperator(GoogleCloudBaseOperator):
913
1069
  self.log.info("Deletion is completed")
914
1070
 
915
1071
 
1072
+ @deprecated(
1073
+ reason=(
1074
+ "Class `AutoMLDeployModelOperator` has been deprecated and no longer available. Please use "
1075
+ "`DeployModelOperator` instead"
1076
+ ),
1077
+ category=AirflowProviderDeprecationWarning,
1078
+ action="error",
1079
+ )
916
1080
  class AutoMLDeployModelOperator(GoogleCloudBaseOperator):
917
1081
  """
918
1082
  Deploys a model; if a model is already deployed, deploying it with the same parameters has no effect.
@@ -923,6 +1087,10 @@ class AutoMLDeployModelOperator(GoogleCloudBaseOperator):
923
1087
  Only applicable for Text Classification, Image Object Detection and Tables; all other
924
1088
  domains manage deployment automatically.
925
1089
 
1090
+ AutoMLDeployModelOperator has been deprecated and no longer available. Please use
1091
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.endpoint_service.DeployModelOperator`
1092
+ instead.
1093
+
926
1094
  .. seealso::
927
1095
  For more information on how to use this operator, take a look at the guide:
928
1096
  :ref:`howto/operator:AutoMLDeployModelOperator`
@@ -989,6 +1157,16 @@ class AutoMLDeployModelOperator(GoogleCloudBaseOperator):
989
1157
  gcp_conn_id=self.gcp_conn_id,
990
1158
  impersonation_chain=self.impersonation_chain,
991
1159
  )
1160
+ model = hook.get_model(
1161
+ model_id=self.model_id,
1162
+ location=self.location,
1163
+ project_id=self.project_id,
1164
+ retry=self.retry,
1165
+ timeout=self.timeout,
1166
+ metadata=self.metadata,
1167
+ )
1168
+ if not hasattr(model, "translation_model_metadata"):
1169
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "DeployModelOperator")
992
1170
  self.log.info("Deploying model_id %s", self.model_id)
993
1171
 
994
1172
  operation = hook.deploy_model(
@@ -1108,6 +1286,10 @@ class AutoMLListDatasetOperator(GoogleCloudBaseOperator):
1108
1286
  """
1109
1287
  Lists AutoML Datasets in project.
1110
1288
 
1289
+ AutoMLListDatasetOperator for tables, video intelligence, vision and natural language has been deprecated
1290
+ and no longer available. Please use
1291
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.dataset.ListDatasetsOperator` instead.
1292
+
1111
1293
  .. seealso::
1112
1294
  For more information on how to use this operator, take a look at the guide:
1113
1295
  :ref:`howto/operator:AutoMLListDatasetOperator`
@@ -1172,7 +1354,16 @@ class AutoMLListDatasetOperator(GoogleCloudBaseOperator):
1172
1354
  timeout=self.timeout,
1173
1355
  metadata=self.metadata,
1174
1356
  )
1175
- result = [Dataset.to_dict(dataset) for dataset in page_iterator]
1357
+ result = []
1358
+ for dataset in page_iterator:
1359
+ if not hasattr(dataset, "translation_dataset_metadata"):
1360
+ warnings.warn(
1361
+ "Class `AutoMLListDatasetOperator` has been deprecated and no longer available. "
1362
+ "Please use `ListDatasetsOperator` instead.",
1363
+ stacklevel=2,
1364
+ )
1365
+ else:
1366
+ result.append(Dataset.to_dict(dataset))
1176
1367
  self.log.info("Datasets obtained.")
1177
1368
 
1178
1369
  self.xcom_push(
@@ -1190,6 +1381,10 @@ class AutoMLDeleteDatasetOperator(GoogleCloudBaseOperator):
1190
1381
  """
1191
1382
  Deletes a dataset and all of its contents.
1192
1383
 
1384
+ AutoMLDeleteDatasetOperator for tables, video intelligence, vision and natural language has been
1385
+ deprecated and no longer available. Please use
1386
+ :class:`airflow.providers.google.cloud.operators.vertex_ai.dataset.DeleteDatasetOperator` instead.
1387
+
1193
1388
  .. seealso::
1194
1389
  For more information on how to use this operator, take a look at the guide:
1195
1390
  :ref:`howto/operator:AutoMLDeleteDatasetOperator`
@@ -1260,6 +1455,16 @@ class AutoMLDeleteDatasetOperator(GoogleCloudBaseOperator):
1260
1455
  gcp_conn_id=self.gcp_conn_id,
1261
1456
  impersonation_chain=self.impersonation_chain,
1262
1457
  )
1458
+ dataset: Dataset = hook.get_dataset(
1459
+ dataset_id=self.dataset_id,
1460
+ location=self.location,
1461
+ project_id=self.project_id,
1462
+ retry=self.retry,
1463
+ timeout=self.timeout,
1464
+ metadata=self.metadata,
1465
+ )
1466
+ if not hasattr(dataset, "translation_dataset_metadata"):
1467
+ _raise_exception_for_deprecated_operator(self.__class__.__name__, "DeleteDatasetOperator")
1263
1468
  dataset_id_list = self._parse_dataset_id(self.dataset_id)
1264
1469
  for dataset_id in dataset_id_list:
1265
1470
  self.log.info("Deleting dataset %s", dataset_id)