Skip to content

Commit d31a27f

Browse files
committed
Add integrations for ucaip
Allow users to use all the aiplatform services
1 parent d4c5a86 commit d31a27f

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ RUN pip install --upgrade cython && \
273273
pip install google-cloud-language==2.* && \
274274
pip install google-cloud-videointelligence==2.* && \
275275
pip install google-cloud-vision==2.* && \
276+
# After launch this should be installed from pip
277+
pip install git+https://github.com/googleapis/python-aiplatform.git@mb-release && \
276278
pip install ortools && \
277279
pip install scattertext && \
278280
# Pandas data reader

patches/kaggle_gcp.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
190207
def 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+
312345
def 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

test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ readonly PATTERN
7070

7171
set -x
7272
docker run --rm --net=none -v /tmp/python-build:/tmp/python-build "$IMAGE_TAG" rm -rf /tmp/python-build/*
73-
7473
docker rm jupyter_test || true
7574
mkdir -p /tmp/python-build/tmp
7675
mkdir -p /tmp/python-build/devshm

tests/test_ucaip.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from unittest.mock import Mock
4+
5+
from kaggle_gcp import KaggleKernelCredentials, init_ucaip
6+
from test.support import EnvironmentVarGuard
7+
8+
def _make_credentials():
9+
import google.auth.credentials
10+
return Mock(spec=google.auth.credentials.Credentials)
11+
12+
class TestUcaip(unittest.TestCase):
13+
14+
def test_user_provided_credentials(self):
15+
credentials = _make_credentials()
16+
env = EnvironmentVarGuard()
17+
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
18+
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI')
19+
with env:
20+
from google.cloud import aiplatform
21+
init_ucaip()
22+
aiplatform.init(credentials=credentials)
23+
self.assertNotIsInstance(aiplatform.initializer.global_config.credentials, KaggleKernelCredentials)
24+
self.assertIsNotNone(aiplatform.initializer.global_config.credentials)

0 commit comments

Comments
 (0)