Skip to content

Commit 7dcfea2

Browse files
autoerrkweinmeisterengelke
authored
Update BigQuery remote function for vision AI to VPCSC compliant and latest version (#8883)
Co-authored-by: Karl Weinmeister <[email protected]> Co-authored-by: Charles Engelke <[email protected]>
1 parent 4f0c43f commit 7dcfea2

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Flask==2.2.2
2-
functions-framework==3.2.0
3-
google-cloud-vision==3.1.2
4-
pytest==7.1.3
2+
functions-framework==3.3.0
3+
google-cloud-vision==3.2.0
4+
pytest==7.2.0
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==2.2.2
2-
functions-framework==3.2.0
3-
google-cloud-vision==3.1.2
2+
functions-framework==3.3.0
3+
google-cloud-vision==3.2.0

bigquery/remote-function/vision/vision_function.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
# limitations under the License.
1414

1515
# [START bigquery_remote_function_vision]
16+
import urllib.request
17+
1618
import flask
1719
import functions_framework
18-
from google.cloud import vision_v1
20+
from google.cloud import vision
1921

2022

2123
@functions_framework.http
@@ -29,13 +31,13 @@ def label_detection(request: flask.Request) -> flask.Response:
2931
https://cloud.google.com/bigquery/docs/reference/standard-sql/remote-functions#output_format
3032
"""
3133
try:
32-
client = vision_v1.ImageAnnotatorClient()
34+
client = vision.ImageAnnotatorClient()
3335
calls = request.get_json()['calls']
3436
replies = []
3537
for call in calls:
36-
results = client.label_detection(
37-
{'source': {'image_uri': call[0]}})
38-
replies.append(vision_v1.AnnotateImageResponse.to_dict(results))
38+
content = urllib.request.urlopen(call[0]).read()
39+
results = client.label_detection({'content': content})
40+
replies.append(vision.AnnotateImageResponse.to_dict(results))
3941
return flask.make_response(flask.jsonify({'replies': replies}))
4042
except Exception as e:
4143
return flask.make_response(flask.jsonify({'errorMessage': str(e)}), 400)

bigquery/remote-function/vision/vision_function_test.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from unittest import mock
1616

1717
import flask
18-
from google.cloud import vision_v1
18+
from google.cloud import vision
1919
import pytest
2020

2121
import vision_function
@@ -27,16 +27,19 @@ def app() -> flask.Flask:
2727
return flask.Flask(__name__)
2828

2929

30-
@mock.patch('vision_function.vision_v1')
31-
def test_vision_function(mock_vision_v1: object, app: flask.Flask) -> None:
30+
@mock.patch('vision_function.urllib.request')
31+
@mock.patch('vision_function.vision')
32+
def test_vision_function(mock_vision: object, mock_request: object,
33+
app: flask.Flask) -> None:
34+
mock_request.urlopen = mock.Mock(read=mock.Mock(return_value=b'filedata'))
3235
label_detection_mock = mock.Mock(side_effect=[
33-
vision_v1.AnnotateImageResponse(
36+
vision.AnnotateImageResponse(
3437
{'label_annotations': [{'description': 'apple'}]}),
35-
vision_v1.AnnotateImageResponse(
38+
vision.AnnotateImageResponse(
3639
{'label_annotations': [{'description': 'banana'}]})])
37-
mock_vision_v1.ImageAnnotatorClient = mock.Mock(
40+
mock_vision.ImageAnnotatorClient = mock.Mock(
3841
return_value=mock.Mock(label_detection=label_detection_mock))
39-
mock_vision_v1.AnnotateImageResponse = vision_v1.AnnotateImageResponse
42+
mock_vision.AnnotateImageResponse = vision.AnnotateImageResponse
4043
with app.test_request_context(
4144
json={'calls': [['https://storage.googleapis.com/bucket/apple'],
4245
['https://storage.googleapis.com/bucket/banana']]}):
@@ -47,11 +50,13 @@ def test_vision_function(mock_vision_v1: object, app: flask.Flask) -> None:
4750
assert 'banana' in str(response.get_json()['replies'][1])
4851

4952

50-
@mock.patch('vision_function.vision_v1')
51-
def test_vision_function_error(
52-
mock_vision_v1: object, app: flask.Flask) -> None:
53+
@mock.patch('vision_function.urllib.request')
54+
@mock.patch('vision_function.vision')
55+
def test_vision_function_error(mock_vision: object, mock_request: object,
56+
app: flask.Flask) -> None:
57+
mock_request.urlopen = mock.Mock(read=mock.Mock(return_value=b'filedata'))
5358
label_detection_mock = mock.Mock(side_effect=Exception('API error'))
54-
mock_vision_v1.ImageAnnotatorClient = mock.Mock(
59+
mock_vision.ImageAnnotatorClient = mock.Mock(
5560
return_value=mock.Mock(label_detection=label_detection_mock))
5661
with app.test_request_context(
5762
json={'calls': [['https://storage.googleapis.com/bucket/apple'],

0 commit comments

Comments
 (0)