@@ -187,6 +187,23 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
187187 bq_client , * args , ** kwargs )
188188 return bigquery
189189
190+ # Monkey patch classes that use the init method
191+ # eg
192+ # from google.cloud import aiplatform
193+ # aiplatform.init(args)
194+ def monkeypatch_init (client_klass , kaggle_kernel_credentials ):
195+ client_init = client_klass .init
196+ def patched_init (self , * args , ** kwargs ):
197+ specified_credentials = kwargs .get ('credentials' )
198+ if specified_credentials is None :
199+ Log .info ("No credentials specified, using KaggleKernelCredentials." )
200+ kwargs ['credentials' ] = kaggle_kernel_credentials
201+ return client_init (self , * args , ** kwargs )
202+
203+ if (not has_been_monkeypatched (client_klass .init )):
204+ client_klass .init = patched_init
205+ Log .info (f"Client patched: { client_klass } " )
206+
190207def monkeypatch_client (client_klass , kaggle_kernel_credentials ):
191208 client_init = client_klass .__init__
192209 def patched_init (self , * args , ** kwargs ):
@@ -309,6 +326,22 @@ def init_natural_language():
309326 monkeypatch_client (language .LanguageServiceAsyncClient , kernel_credentials )
310327 return language
311328
329+ def init_ucaip ():
330+ from google .cloud import aiplatform
331+ if not is_user_secrets_token_set ():
332+ return
333+
334+ from kaggle_gcp import get_integrations
335+ if not get_integrations ().has_cloudai ():
336+ return
337+
338+ from kaggle_secrets import GcpTarget
339+ from kaggle_gcp import KaggleKernelCredentials
340+ kaggle_kernel_credentials = KaggleKernelCredentials (target = GcpTarget .CLOUDAI )
341+
342+ # Patch the ucaip init method, this flows down to all ucaip services
343+ monkeypatch_init (aiplatform .initializer .global_config , kaggle_kernel_credentials )
344+
312345def init_video_intelligence ():
313346 from google .cloud import videointelligence
314347 if not is_user_secrets_token_set ():
@@ -352,6 +385,7 @@ def init():
352385 init_natural_language ()
353386 init_video_intelligence ()
354387 init_vision ()
388+ init_ucaip ()
355389
356390# We need to initialize the monkeypatching of the client libraries
357391# here since there is a circular dependency between our import hook version
0 commit comments