-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathkaggle_gcp.py
395 lines (333 loc) · 16.1 KB
/
kaggle_gcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import os
import inspect
from google.auth import credentials, environment_vars
from google.auth.exceptions import RefreshError
from google.api_core.gapic_v1.client_info import ClientInfo
from google.cloud import bigquery
from google.cloud.exceptions import Forbidden
from google.cloud.bigquery._http import Connection
from kaggle_secrets import GcpTarget, UserSecretsClient
from log import Log
KAGGLE_GCP_CLIENT_USER_AGENT="kaggle-gcp-client/1.0"
def get_integrations():
kernel_integrations_var = os.getenv("KAGGLE_KERNEL_INTEGRATIONS")
kernel_integrations = KernelIntegrations()
if kernel_integrations_var is None:
return kernel_integrations
for integration in kernel_integrations_var.split(':'):
try:
target = GcpTarget[integration.upper()]
kernel_integrations.add_integration(target)
except KeyError as e:
Log.error(f"Unknown integration target: {integration.upper()}")
return kernel_integrations
class KernelIntegrations():
def __init__(self):
self.integrations = {}
def add_integration(self, target):
self.integrations[target] = True
def has_integration(self, target):
return target in self.integrations
def has_bigquery(self):
return GcpTarget.BIGQUERY in self.integrations
def has_gcs(self):
return GcpTarget.GCS in self.integrations
def has_cloudai(self):
return GcpTarget.CLOUDAI in self.integrations or \
GcpTarget.AUTOML in self.integrations
class KaggleKernelCredentials(credentials.Credentials):
"""Custom Credentials used to authenticate using the Kernel's connected OAuth account.
Example usage:
client = bigquery.Client(project='ANOTHER_PROJECT',
credentials=KaggleKernelCredentials())
"""
def __init__(self, target=GcpTarget.BIGQUERY):
super().__init__()
self.target = target
def refresh(self, request):
try:
client = UserSecretsClient()
if self.target == GcpTarget.BIGQUERY:
self.token, self.expiry = client.get_bigquery_access_token()
elif self.target == GcpTarget.GCS:
self.token, self.expiry = client._get_gcs_access_token()
elif self.target == GcpTarget.CLOUDAI:
self.token, self.expiry = client._get_cloudai_access_token()
except ConnectionError as e:
Log.error(f"Connection error trying to refresh access token: {e}")
print("There was a connection error trying to fetch the access token. "
f"Please ensure internet is on in order to use the {self.target.service} Integration.")
raise RefreshError('Unable to refresh access token due to connection error.') from e
except Exception as e:
Log.error(f"Error trying to refresh access token: {e}")
if (not get_integrations().has_integration(self.target)):
Log.error(f"No {self.target.service} integration found.")
print(
f"Please ensure you have selected a {self.target.service} account in the Notebook Add-ons menu.")
raise RefreshError('Unable to refresh access token.') from e
class KaggleKernelWithProjetCredentials(KaggleKernelCredentials):
""" Wrapper Kaggle Credentials with quota_project_id.
"""
def __init__(self, parentCredential=None, quota_project_id=None):
super().__init__(target=parentCredential.target)
self._quota_project_id=quota_project_id
class _DataProxyConnection(Connection):
"""Custom Connection class used to proxy the BigQuery client to Kaggle's data proxy."""
def __init__(self, client, **kwargs):
super().__init__(client, **kwargs)
self.extra_headers["X-KAGGLE-PROXY-DATA"] = os.getenv(
"KAGGLE_DATA_PROXY_TOKEN")
def api_request(self, *args, **kwargs):
"""Wrap Connection.api_request in order to handle errors gracefully.
"""
try:
return super().api_request(*args, **kwargs)
except Forbidden as e:
msg = ("Permission denied using Kaggle's public BigQuery integration. "
"Did you mean to select a BigQuery account in the Notebook Add-ons menu?")
print(msg)
Log.info(msg)
raise e
class PublicBigqueryClient(bigquery.client.Client):
"""A modified BigQuery client that routes requests using Kaggle's Data Proxy to provide free access to Public Datasets.
Example usage:
from kaggle import PublicBigqueryClient
client = PublicBigqueryClient()
"""
def __init__(self, *args, **kwargs):
data_proxy_project = os.getenv("KAGGLE_DATA_PROXY_PROJECT")
default_api_endpoint = os.getenv("KAGGLE_DATA_PROXY_URL")
anon_credentials = credentials.AnonymousCredentials()
anon_credentials.refresh = lambda *args: None
super().__init__(
project=data_proxy_project, credentials=anon_credentials, *args, **kwargs
)
# TODO: Remove this once https://github.com/googleapis/google-cloud-python/issues/7122 is implemented.
self._connection = _DataProxyConnection(self, api_endpoint=default_api_endpoint)
def has_been_monkeypatched(method):
return "kaggle_gcp" in inspect.getsourcefile(method)
def is_user_secrets_token_set():
return "KAGGLE_USER_SECRETS_TOKEN" in os.environ
def is_proxy_token_set():
return "KAGGLE_DATA_PROXY_TOKEN" in os.environ
def init_bigquery():
from google.cloud import bigquery
if not (is_proxy_token_set() or is_user_secrets_token_set()):
return bigquery
# If this Notebook has bigquery integration on startup, preload the Kaggle Credentials
# object for magics to work.
if get_integrations().has_bigquery():
from google.cloud.bigquery import magics
magics.context.credentials = KaggleKernelCredentials()
def monkeypatch_bq(bq_client, *args, **kwargs):
from kaggle_gcp import get_integrations, PublicBigqueryClient, KaggleKernelCredentials
specified_credentials = kwargs.get('credentials')
has_bigquery = get_integrations().has_bigquery()
# Prioritize passed in project id, but if it is missing look for env var.
arg_project = kwargs.get('project')
explicit_project_id = arg_project or os.environ.get(environment_vars.PROJECT)
# This is a hack to get around the bug in google-cloud library.
# Remove these two lines once this is resolved:
# https://github.com/googleapis/google-cloud-python/issues/8108
if explicit_project_id:
Log.info(f"Explicit project set to {explicit_project_id}")
kwargs['project'] = explicit_project_id
if explicit_project_id is None and specified_credentials is None and not has_bigquery:
msg = "Using Kaggle's public dataset BigQuery integration."
Log.info(msg)
print(msg)
return PublicBigqueryClient(*args, **kwargs)
else:
if specified_credentials is None:
Log.info("No credentials specified, using KaggleKernelCredentials.")
kwargs['credentials'] = KaggleKernelCredentials()
if (not has_bigquery):
Log.info("No bigquery integration found, creating client anyways.")
print('Please ensure you have selected a BigQuery '
'account in the Notebook Add-ons menu.')
if explicit_project_id is None:
Log.info("No project specified while using the unmodified client.")
print('Please ensure you specify a project id when creating the client'
' in order to use your BigQuery account.')
kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info'))
return bq_client(*args, **kwargs)
# Monkey patches BigQuery client creation to use proxy or user-connected GCP account.
# Deprecated in favor of Kaggle.DataProxyClient().
# TODO: Remove this once uses have migrated to that new interface.
bq_client = bigquery.Client
if (not has_been_monkeypatched(bigquery.Client)):
bigquery.Client = lambda *args, **kwargs: monkeypatch_bq(
bq_client, *args, **kwargs)
return bigquery
# Monkey patch for aiplatform init
# eg
# from google.cloud import aiplatform
# aiplatform.init(args)
def monkeypatch_aiplatform_init(aiplatform_klass, kaggle_kernel_credentials):
aiplatform_init = aiplatform_klass.init
def patched_init(*args, **kwargs):
specified_credentials = kwargs.get('credentials')
if specified_credentials is None:
Log.info("No credentials specified, using KaggleKernelCredentials.")
kwargs['credentials'] = kaggle_kernel_credentials
return aiplatform_init(*args, **kwargs)
if (not has_been_monkeypatched(aiplatform_klass.init)):
aiplatform_klass.init = patched_init
Log.info("aiplatform.init patched")
def monkeypatch_client(client_klass, kaggle_kernel_credentials):
client_init = client_klass.__init__
def patched_init(self, *args, **kwargs):
specified_credentials = kwargs.get('credentials')
if specified_credentials is None:
Log.info("No credentials specified, using KaggleKernelCredentials.")
# Some GCP services demand the billing and target project must be the same.
# To avoid using default service account based credential as caller credential
# user need to provide ClientOptions with quota_project_id:
# srv.Client(client_options=client_options.ClientOptions(quota_project_id="YOUR PROJECT"))
client_options=kwargs.get('client_options')
if client_options != None and client_options.quota_project_id != None:
kwargs['credentials'] = KaggleKernelWithProjetCredentials(
parentCredential = kaggle_kernel_credentials,
quota_project_id = client_options.quota_project_id)
else:
kwargs['credentials'] = kaggle_kernel_credentials
kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info'))
return client_init(self, *args, **kwargs)
if (not has_been_monkeypatched(client_klass.__init__)):
client_klass.__init__ = patched_init
Log.info(f"Client patched: {client_klass}")
def set_kaggle_user_agent(client_info: ClientInfo):
# Add kaggle client user agent in order to attribute usage.
if client_info is None:
client_info = ClientInfo(user_agent=KAGGLE_GCP_CLIENT_USER_AGENT)
else:
client_info.user_agent = KAGGLE_GCP_CLIENT_USER_AGENT
return client_info
def init_gcs():
from google.cloud import storage
if not is_user_secrets_token_set():
return storage
from kaggle_gcp import get_integrations
if not get_integrations().has_gcs():
return storage
from kaggle_secrets import GcpTarget
from kaggle_gcp import KaggleKernelCredentials
monkeypatch_client(
storage.Client,
KaggleKernelCredentials(target=GcpTarget.GCS))
return storage
def init_automl():
from google.cloud import automl, automl_v1beta1
if not is_user_secrets_token_set():
return
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return
from kaggle_secrets import GcpTarget
from kaggle_gcp import KaggleKernelCredentials
kaggle_kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
# Patch the 2 GA clients: AutoMlClient and PreditionServiceClient
monkeypatch_client(automl.AutoMlClient, kaggle_kernel_credentials)
monkeypatch_client(automl.PredictionServiceClient, kaggle_kernel_credentials)
# The AutoML client library exposes 3 different client classes (AutoMlClient,
# TablesClient, PredictionServiceClient), so patch each of them.
# The same KaggleKernelCredentials are passed to all of them.
# The GcsClient class is only used internally by TablesClient.
# The beta version of the clients that are now GA are included here for now.
# They are deprecated and will be removed by 1 May 2020.
monkeypatch_client(automl_v1beta1.AutoMlClient, kaggle_kernel_credentials)
monkeypatch_client(automl_v1beta1.PredictionServiceClient, kaggle_kernel_credentials)
# The TablesClient is still in beta, so this will not be deprecated until
# the TablesClient is GA.
monkeypatch_client(automl_v1beta1.TablesClient, kaggle_kernel_credentials)
def init_translation_v2():
from google.cloud import translate_v2
if not is_user_secrets_token_set():
return translate_v2
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return translate_v2
from kaggle_secrets import GcpTarget
kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
monkeypatch_client(translate_v2.Client, kernel_credentials)
return translate_v2
def init_translation_v3():
# Translate v3 exposes different client than translate v2.
from google.cloud import translate_v3
if not is_user_secrets_token_set():
return translate_v3
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return translate_v3
from kaggle_secrets import GcpTarget
kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
monkeypatch_client(translate_v3.TranslationServiceClient, kernel_credentials)
return translate_v3
def init_natural_language():
from google.cloud import language
if not is_user_secrets_token_set():
return language
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return language
from kaggle_secrets import GcpTarget
kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
monkeypatch_client(language.LanguageServiceClient, kernel_credentials)
monkeypatch_client(language.LanguageServiceAsyncClient, kernel_credentials)
return language
def init_ucaip():
from google.cloud import aiplatform
if not is_user_secrets_token_set():
return
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return
from kaggle_secrets import GcpTarget
from kaggle_gcp import KaggleKernelCredentials
kaggle_kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
# Patch the ucaip init method, this flows down to all ucaip services
monkeypatch_aiplatform_init(aiplatform, kaggle_kernel_credentials)
def init_video_intelligence():
from google.cloud import videointelligence
if not is_user_secrets_token_set():
return videointelligence
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return videointelligence
from kaggle_secrets import GcpTarget
kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
monkeypatch_client(
videointelligence.VideoIntelligenceServiceClient,
kernel_credentials)
monkeypatch_client(
videointelligence.VideoIntelligenceServiceAsyncClient,
kernel_credentials)
return videointelligence
def init_vision():
from google.cloud import vision
if not is_user_secrets_token_set():
return vision
from kaggle_gcp import get_integrations
if not get_integrations().has_cloudai():
return vision
from kaggle_secrets import GcpTarget
kernel_credentials = KaggleKernelCredentials(target=GcpTarget.CLOUDAI)
monkeypatch_client(vision.ImageAnnotatorClient, kernel_credentials)
monkeypatch_client(vision.ImageAnnotatorAsyncClient, kernel_credentials)
return vision
def init():
init_bigquery()
init_gcs()
init_automl()
init_translation_v2()
init_translation_v3()
init_natural_language()
init_video_intelligence()
init_vision()
init_ucaip()
# We need to initialize the monkeypatching of the client libraries
# here since there is a circular dependency between our import hook version
# google.cloud.* and kaggle_gcp. By calling init here, we guarantee
# that regardless of the original import that caused google.cloud.* to be
# loaded, the monkeypatching will be done.
init()