forked from googleapis/python-bigquery-dataframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_functions.py
More file actions
396 lines (293 loc) · 10.9 KB
/
_functions.py
File metadata and controls
396 lines (293 loc) · 10.9 KB
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
396
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass
import inspect
from typing import Callable, Iterable
import google.cloud.bigquery as bigquery
import bigframes.session
import bigframes.session._io.bigquery as bf_io_bigquery
_PYTHON_TO_BQ_TYPES = {int: "INT64", float: "FLOAT64", str: "STRING", bytes: "BYTES"}
@dataclass(frozen=True)
class FunctionDef:
"""Definition of a Python UDF."""
func: Callable # function body
requirements: Iterable[str] # required packages
# TODO(garrettwu): migrate to bigframes UDF when it is available
class TransformFunction:
"""Simple transform function class to deal with Python UDF."""
def __init__(
self,
func_def: FunctionDef,
session: bigframes.session.Session,
connection: str,
max_batching_rows: int,
):
self._func = func_def.func
self._requirements = func_def.requirements
self._session = session
self._connection = connection
self._max_batching_rows = max_batching_rows
def _input_bq_signature(self):
sig = inspect.signature(self._func)
inputs = []
for k, v in sig.parameters.items():
inputs.append(f"{k} {_PYTHON_TO_BQ_TYPES[v.annotation]}")
return ", ".join(inputs)
def _output_bq_type(self):
sig = inspect.signature(self._func)
return _PYTHON_TO_BQ_TYPES[sig.return_annotation]
def _create_udf(self):
"""Create Python UDF in BQ. Return name of the UDF."""
udf_name = str(self._session._loader._storage_manager._random_table())
func_body = inspect.getsource(self._func)
func_name = self._func.__name__
packages = str(list(self._requirements))
sql = f"""
CREATE OR REPLACE FUNCTION `{udf_name}`({self._input_bq_signature()})
RETURNS {self._output_bq_type()} LANGUAGE python
WITH CONNECTION `{self._connection}`
OPTIONS (entry_point='{func_name}', runtime_version='python-3.11', packages={packages}, max_batching_rows={self._max_batching_rows})
AS r\"\"\"
{func_body}
\"\"\"
"""
bf_io_bigquery.start_query_with_client(
self._session.bqclient,
sql,
job_config=bigquery.QueryJobConfig(),
metrics=self._session._metrics,
)
return udf_name
def udf(self):
"""Create and return the UDF object."""
udf_name = self._create_udf()
return self._session.read_gbq_function(udf_name)
# Blur images. Takes ObjectRefRuntime as JSON string. Outputs ObjectRefRuntime JSON string.
def image_blur_func(
src_obj_ref_rt: str, dst_obj_ref_rt: str, ksize_x: int, ksize_y: int
) -> str:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_blurred = cv.blur(img, ksize=(ksize_x, ksize_y))
bts = cv.imencode(".jpeg", img_blurred)[1].tobytes()
requests.put(
url=dst_url,
data=bts,
headers={
"Content-Type": "image/jpeg",
},
)
return dst_obj_ref_rt
image_blur_def = FunctionDef(image_blur_func, ["opencv-python", "numpy", "requests"])
def image_blur_to_bytes_func(src_obj_ref_rt: str, ksize_x: int, ksize_y: int) -> bytes:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_blurred = cv.blur(img, ksize=(ksize_x, ksize_y))
bts = cv.imencode(".jpeg", img_blurred)[1].tobytes()
return bts
image_blur_to_bytes_def = FunctionDef(
image_blur_to_bytes_func, ["opencv-python", "numpy", "requests"]
)
def image_resize_func(
src_obj_ref_rt: str,
dst_obj_ref_rt: str,
dsize_x: int,
dsize_y: int,
fx: float,
fy: float,
) -> str:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_resized = cv.resize(img, dsize=(dsize_x, dsize_y), fx=fx, fy=fy)
bts = cv.imencode(".jpeg", img_resized)[1].tobytes()
requests.put(
url=dst_url,
data=bts,
headers={
"Content-Type": "image/jpeg",
},
)
return dst_obj_ref_rt
image_resize_def = FunctionDef(
image_resize_func, ["opencv-python", "numpy", "requests"]
)
def image_resize_to_bytes_func(
src_obj_ref_rt: str,
dsize_x: int,
dsize_y: int,
fx: float,
fy: float,
) -> bytes:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_resized = cv.resize(img, dsize=(dsize_x, dsize_y), fx=fx, fy=fy)
bts = cv.imencode(".jpeg", img_resized)[1].tobytes()
return bts
image_resize_to_bytes_def = FunctionDef(
image_resize_to_bytes_func, ["opencv-python", "numpy", "requests"]
)
def image_normalize_func(
src_obj_ref_rt: str, dst_obj_ref_rt: str, alpha: float, beta: float, norm_type: str
) -> str:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
norm_type_mapping = {
"inf": cv.NORM_INF,
"l1": cv.NORM_L1,
"l2": cv.NORM_L2,
"minmax": cv.NORM_MINMAX,
}
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
dst_obj_ref_rt_json = json.loads(dst_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
dst_url = dst_obj_ref_rt_json["access_urls"]["write_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_normalized = cv.normalize(
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
)
bts = cv.imencode(".jpeg", img_normalized)[1].tobytes()
requests.put(
url=dst_url,
data=bts,
headers={
"Content-Type": "image/jpeg",
},
)
return dst_obj_ref_rt
image_normalize_def = FunctionDef(
image_normalize_func, ["opencv-python", "numpy", "requests"]
)
def image_normalize_to_bytes_func(
src_obj_ref_rt: str, alpha: float, beta: float, norm_type: str
) -> bytes:
import json
import cv2 as cv # type: ignore
import numpy as np
import requests
norm_type_mapping = {
"inf": cv.NORM_INF,
"l1": cv.NORM_L1,
"l2": cv.NORM_L2,
"minmax": cv.NORM_MINMAX,
}
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
response = requests.get(src_url)
bts = response.content
nparr = np.frombuffer(bts, np.uint8)
img = cv.imdecode(nparr, cv.IMREAD_UNCHANGED)
img_normalized = cv.normalize(
img, None, alpha=alpha, beta=beta, norm_type=norm_type_mapping[norm_type]
)
bts = cv.imencode(".jpeg", img_normalized)[1].tobytes()
return bts
image_normalize_to_bytes_def = FunctionDef(
image_normalize_to_bytes_func, ["opencv-python", "numpy", "requests"]
)
# Extracts all text from a PDF url
def pdf_extract_func(src_obj_ref_rt: str) -> str:
import io
import json
from pypdf import PdfReader # type: ignore
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
response = requests.get(src_url, stream=True)
response.raise_for_status()
pdf_bytes = response.content
pdf_file = io.BytesIO(pdf_bytes)
reader = PdfReader(pdf_file, strict=False)
all_text = ""
for page in reader.pages:
page_extract_text = page.extract_text()
if page_extract_text:
all_text += page_extract_text
return all_text
pdf_extract_def = FunctionDef(pdf_extract_func, ["pypdf", "requests"])
# Extracts text from a PDF url and chunks it simultaneously
def pdf_chunk_func(src_obj_ref_rt: str, chunk_size: int, overlap_size: int) -> str:
import io
import json
from pypdf import PdfReader # type: ignore
import requests
src_obj_ref_rt_json = json.loads(src_obj_ref_rt)
src_url = src_obj_ref_rt_json["access_urls"]["read_url"]
response = requests.get(src_url, stream=True)
response.raise_for_status()
pdf_bytes = response.content
pdf_file = io.BytesIO(pdf_bytes)
reader = PdfReader(pdf_file, strict=False)
# extract and chunk text simultaneously
all_text_chunks = []
curr_chunk = ""
for page in reader.pages:
page_text = page.extract_text()
if page_text:
curr_chunk += page_text
# split the accumulated text into chunks of a specific size with overlaop
# this loop implements a sliding window approach to create chunks
while len(curr_chunk) >= chunk_size:
split_idx = curr_chunk.rfind(" ", 0, chunk_size)
if split_idx == -1:
split_idx = chunk_size
actual_chunk = curr_chunk[:split_idx]
all_text_chunks.append(actual_chunk)
overlap = curr_chunk[split_idx + 1 : split_idx + 1 + overlap_size]
curr_chunk = overlap + curr_chunk[split_idx + 1 + overlap_size :]
if curr_chunk:
all_text_chunks.append(curr_chunk)
all_text_json_string = json.dumps(all_text_chunks)
return all_text_json_string
pdf_chunk_def = FunctionDef(pdf_chunk_func, ["pypdf", "requests"])