| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -273,6 +273,8 @@ RUN pip install --upgrade cython && \ | |||
| 273 | 273 | pip install google-cloud-language==2.* && \ | |
| 274 | 274 | pip install google-cloud-videointelligence==2.* && \ | |
| 275 | 275 | 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 && \ | ||
| 276 | 278 | pip install ortools && \ | |
| 277 | 279 | pip install scattertext && \ | |
| 278 | 280 | # Pandas data reader | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,6 +187,23 @@ def monkeypatch_bq(bq_client, *args, **kwargs): | |||
| 187 | 187 | bq_client, *args, **kwargs) | |
| 188 | 188 | return bigquery | |
| 189 | 189 | ||
| 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 | + | ||
| 190 | 207 | def monkeypatch_client(client_klass, kaggle_kernel_credentials): | |
| 191 | 208 | client_init = client_klass.__init__ | |
| 192 | 209 | def patched_init(self, *args, **kwargs): | |
@@ -309,6 +326,22 @@ def init_natural_language(): | |||
| 309 | 326 | monkeypatch_client(language.LanguageServiceAsyncClient, kernel_credentials) | |
| 310 | 327 | return language | |
| 311 | 328 | ||
| 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 | + | ||
| 312 | 345 | def init_video_intelligence(): | |
| 313 | 346 | from google.cloud import videointelligence | |
| 314 | 347 | if not is_user_secrets_token_set(): | |
@@ -352,6 +385,7 @@ def init(): | |||
| 352 | 385 | init_natural_language() | |
| 353 | 386 | init_video_intelligence() | |
| 354 | 387 | init_vision() | |
| 388 | + init_ucaip() | ||
| 355 | 389 | ||
| 356 | 390 | # We need to initialize the monkeypatching of the client libraries | |
| 357 | 391 | # here since there is a circular dependency between our import hook version | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,7 +70,6 @@ readonly PATTERN | |||
| 70 | 70 | ||
| 71 | 71 | set -x | |
| 72 | 72 | docker run --rm --net=none -v /tmp/python-build:/tmp/python-build "$IMAGE_TAG" rm -rf /tmp/python-build/* | |
| 73 | - | ||
| 74 | 73 | docker rm jupyter_test || true | |
| 75 | 74 | mkdir -p /tmp/python-build/tmp | |
| 76 | 75 | mkdir -p /tmp/python-build/devshm | |
| Original file line number | Diff line number | Diff 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) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments