@@ -130,7 +130,7 @@ def init_bigquery():
130130 return bigquery
131131
132132 # If this Kernel has bigquery integration on startup, preload the Kaggle Credentials
133- # object for magics to work.
133+ # object for magics to work.
134134 if get_integrations ().has_bigquery ():
135135 from google .cloud .bigquery import magics
136136 magics .context .credentials = KaggleKernelCredentials ()
@@ -139,7 +139,7 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
139139 from kaggle_gcp import get_integrations , PublicBigqueryClient , KaggleKernelCredentials
140140 specified_credentials = kwargs .get ('credentials' )
141141 has_bigquery = get_integrations ().has_bigquery ()
142- # Prioritize passed in project id, but if it is missing look for env var.
142+ # Prioritize passed in project id, but if it is missing look for env var.
143143 arg_project = kwargs .get ('project' )
144144 explicit_project_id = arg_project or os .environ .get (environment_vars .PROJECT )
145145 # This is a hack to get around the bug in google-cloud library.
@@ -200,9 +200,70 @@ def monkeypatch_gcs(self, *args, **kwargs):
200200 storage .Client .__init__ = monkeypatch_gcs
201201 return storage
202202
203+ def init_automl ():
204+ is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os .environ
205+ from google .cloud import automl_v1beta1 as automl
206+ if not is_user_secrets_token_set :
207+ return automl
208+
209+ from kaggle_gcp import get_integrations
210+ if not get_integrations ().has_automl ():
211+ return automl
212+
213+ from kaggle_secrets import GcpTarget
214+ from kaggle_gcp import KaggleKernelCredentials
215+ kaggle_kernel_credentials = KaggleKernelCredentials (target = GcpTarget .AUTOML )
216+
217+ # The AutoML client library exposes 4 different client classes (AutoMlClient,
218+ # TablesClient, PredictionServiceClient and GcsClient), so patch each of them.
219+ # The same KaggleKernelCredentials are passed to all of them.
220+
221+ automl_client_init = automl .AutoMlClient .__init__
222+ def monkeypatch_automl (self , * args , ** kwargs ):
223+ specified_credentials = kwargs .get ('credentials' )
224+ if specified_credentials is None :
225+ Log .info ("No credentials specified, using KaggleKernelCredentials." )
226+ kwargs ['credentials' ] = kaggle_kernel_credentials
227+ # Note: This is only here so that unit tests can check whether
228+ # credentials were set properly.
229+ self ._kaggle_credentials = kwargs ['credentials' ]
230+ return automl_client_init (self , * args , ** kwargs )
231+
232+ if (not has_been_monkeypatched (automl .AutoMlClient .__init__ )):
233+ automl .AutoMlClient .__init__ = monkeypatch_automl
234+
235+
236+ automl_tablesclient_init = automl .TablesClient .__init__
237+ def monkeypatch_tablesclient (self , * args , ** kwargs ):
238+ specified_credentials = kwargs .get ('credentials' )
239+ if specified_credentials is None :
240+ Log .info ("No credentials specified, using KaggleKernelCredentials." )
241+ kwargs ['credentials' ] = kaggle_kernel_credentials
242+ self ._kaggle_credentials = kwargs ['credentials' ]
243+ return automl_tablesclient_init (self , * args , ** kwargs )
244+
245+ if (not has_been_monkeypatched (automl .TablesClient .__init__ )):
246+ automl .TablesClient .__init__ = monkeypatch_tablesclient
247+
248+
249+ automl_predictionclient_init = automl .PredictionServiceClient .__init__
250+ def monkeypatch_predictionclient (self , * args , ** kwargs ):
251+ specified_credentials = kwargs .get ('credentials' )
252+ if specified_credentials is None :
253+ Log .info ("No credentials specified, using KaggleKernelCredentials." )
254+ kwargs ['credentials' ] = kaggle_kernel_credentials
255+ self ._kaggle_credentials = kwargs ['credentials' ]
256+ return automl_predictionclient_init (self , * args , ** kwargs )
257+
258+ if (not has_been_monkeypatched (automl .PredictionServiceClient .__init__ )):
259+ automl .PredictionServiceClient .__init__ = monkeypatch_predictionclient
260+
261+ return automl
262+
203263def init ():
204264 init_bigquery ()
205265 init_gcs ()
266+ init_automl ()
206267
207268# We need to initialize the monkeypatching of the client libraries
208269# here since there is a circular dependency between our import hook version
0 commit comments