Skip to content

Commit 0e874c0

Browse files
rhaertel80busunkim96Alec Glassford
authored
Update predict.py (GoogleCloudPlatform#4897)
* Update predict.py Calling the discovery API for every prediction is bad practice, leads to high latency, and also users will very quickly hit the quota imposed on the CRUD server. We've had multiple customers hit this issue, and I can only assume that they are copying and pasting this code. I think a class would be more elegant here, but this quick fix should at least get us started along a better path. * Add extra line break before function (for linter) Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Co-authored-by: Alec Glassford <alecglassford@google.com>
1 parent 8d4ad77 commit 0e874c0

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

ml_engine/online_prediction/predict.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424

2525
# [START predict_json]
26+
# Create the AI Platform service object.
27+
# To authenticate set the environment variable
28+
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
29+
service = googleapiclient.discovery.build('ml', 'v1')
30+
31+
2632
def predict_json(project, model, instances, version=None):
2733
"""Send json data to a deployed model for prediction.
2834
@@ -38,10 +44,6 @@ def predict_json(project, model, instances, version=None):
3844
Mapping[str: any]: dictionary of prediction results defined by the
3945
model.
4046
"""
41-
# Create the AI Platform service object.
42-
# To authenticate set the environment variable
43-
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
44-
service = googleapiclient.discovery.build('ml', 'v1')
4547
name = 'projects/{}/models/{}'.format(project, model)
4648

4749
if version is not None:

0 commit comments

Comments
 (0)