Skip to content

Commit 00d4be8

Browse files
chore(functions): delete region tag and update sample for Python 3.9+ (#13240)
* chore(functions): update dependencies to support Python 3.13 * chore(functions): delete unneeded space and move function 'validate_message' for clearity * chore(functions): fix unmatched quotes
1 parent 3006265 commit 00d4be8

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

functions/ocr/app/main.py

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
project_id = os.environ["GCP_PROJECT"]
3232
# [END functions_ocr_setup]
3333

34+
T = TypeVar("T")
35+
36+
37+
def validate_message(message: Dict[str, T], param: str) -> T:
38+
"""
39+
Placeholder function for validating message parts.
40+
41+
Args:
42+
message: message to be validated.
43+
param: name of the message parameter to be validated.
44+
45+
Returns:
46+
The value of message['param'] if it's valid. Throws ValueError
47+
if it's not valid.
48+
"""
49+
var = message.get(param)
50+
if not var:
51+
raise ValueError(
52+
f"{param} is not provided. Make sure you have "
53+
f"property {param} in the request"
54+
)
55+
return var
56+
3457

3558
# [START functions_ocr_detect]
3659
def detect_text(bucket: str, filename: str) -> None:
@@ -57,10 +80,12 @@ def detect_text(bucket: str, filename: str) -> None:
5780
)
5881
text_detection_response = vision_client.text_detection(image=image)
5982
annotations = text_detection_response.text_annotations
83+
6084
if len(annotations) > 0:
6185
text = annotations[0].description
6286
else:
6387
text = ""
88+
6489
print(f"Extracted text {text} from image ({len(text)} chars).")
6590

6691
detect_language_response = translate_client.detect_language(text)
@@ -85,39 +110,8 @@ def detect_text(bucket: str, filename: str) -> None:
85110
futures.append(future)
86111
for future in futures:
87112
future.result()
88-
89-
90113
# [END functions_ocr_detect]
91114

92-
T = TypeVar("T")
93-
94-
95-
# [START message_validatation_helper]
96-
def validate_message(message: Dict[str, T], param: str) -> T:
97-
"""
98-
Placeholder function for validating message parts.
99-
100-
Args:
101-
message: message to be validated.
102-
param: name of the message parameter to be validated.
103-
104-
Returns:
105-
The value of message['param'] if it's valid. Throws ValueError
106-
if it's not valid.
107-
"""
108-
var = message.get(param)
109-
if not var:
110-
raise ValueError(
111-
"{} is not provided. Make sure you have \
112-
property {} in the request".format(
113-
param, param
114-
)
115-
)
116-
return var
117-
118-
119-
# [END message_validatation_helper]
120-
121115

122116
# [START functions_ocr_process]
123117
def process_image(file_info: dict, context: dict) -> None:
@@ -136,16 +130,13 @@ def process_image(file_info: dict, context: dict) -> None:
136130

137131
detect_text(bucket, name)
138132

139-
print("File {} processed.".format(file_info["name"]))
140-
141-
133+
print(f"File '{file_info['name']}' processed.")
142134
# [END functions_ocr_process]
143135

144136

145137
# [START functions_ocr_translate]
146138
def translate_text(event: dict, context: dict) -> None:
147-
"""
148-
Cloud Function triggered by PubSub when a message is received from
139+
"""Cloud Function triggered by PubSub when a message is received from
149140
a subscription.
150141
151142
Translates the text in the message from the specified source language
@@ -184,8 +175,6 @@ def translate_text(event: dict, context: dict) -> None:
184175
topic_path = publisher.topic_path(project_id, topic_name)
185176
future = publisher.publish(topic_path, data=encoded_message)
186177
future.result()
187-
188-
189178
# [END functions_ocr_translate]
190179

191180

@@ -224,6 +213,4 @@ def save_result(event: dict, context: dict) -> None:
224213
blob.upload_from_string(text)
225214

226215
print("File saved.")
227-
228-
229216
# [END functions_ocr_save]

functions/ocr/app/noxfile_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
# google-cloud-translate==3.12.1 is incompatible with Python 12.
26-
# Staying with 3.11 testing for now.
27-
"ignored_versions": ["2.7", "3.7", "3.9", "3.10", "3.12", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8"],
2826
# Declare optional test sessions you want to opt-in. Currently we
2927
# have the following optional test sessions:
3028
# 'cloud_run' # Test session for Cloud Run application.

functions/ocr/app/requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google-cloud-pubsub==2.28.0
2-
google-cloud-storage==2.9.0
3-
google-cloud-translate==3.18.0
4-
google-cloud-vision==3.8.1
2+
google-cloud-storage==3.1.0
3+
google-cloud-translate==3.20.2
4+
google-cloud-vision==3.10.1

0 commit comments

Comments
 (0)