forked from GoogleCloudPlatform/genai-for-marketing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_image.py
548 lines (472 loc) · 18 KB
/
utils_image.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# Copyright 2023 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
#
# https://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.
"""
Utility module to:
- Resize image bytes
- Generate an image with Imagen
- Edit an image with Imagen
- Render the image generation and editing UI
"""
import io
import base64
import math
import tomllib
import utils_edit_image
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value
from PIL import Image
import streamlit as st
from typing import List
# Load configuration file
with open("./app_config.toml", "rb") as f:
data = tomllib.load(f)
# Confoguration variables for Vertex AI
PROJECT_ID = data["global"]["project_id"]
LOCATION = data["global"]["location"]
MODEL_NAME = data["models"]["image"]["image_model_name"]
IMAGEN_API_ENDPOINT = f'{LOCATION}-aiplatform.googleapis.com'
IMAGEN_ENDPOINT = f'projects/{PROJECT_ID}/locations/{LOCATION}/publishers/google/models/{MODEL_NAME}'
IMAGE_UPLOAD_BYTES_LIMIT = 10 ** 7
def resize_image_bytes(
bytes_data: bytes,
bytes_limit: int=IMAGE_UPLOAD_BYTES_LIMIT) -> bytes:
"""Resizes an image to a specified byte limit.
Args:
bytes_data:
The image data in bytes. (bytes)
bytes_limit:
The maximum byte size of the resized image. (int)
Returns:
The resized image data in bytes.
Raises:
Image.ImageTooBigError: If the image is larger than the bytes_limit.
"""
with io.BytesIO(bytes_data) as buffer_in:
img_to_resize = Image.open(buffer_in)
width = img_to_resize.size[0]
aspect = img_to_resize.size[0] / img_to_resize.size[1]
bytes_size = len(bytes_data)
while bytes_size > bytes_limit :
resize_factor = bytes_size / (bytes_limit*0.9)
width = width / math.sqrt(resize_factor)
height = width / aspect
# resize from img_orig to not lose quality
img = img_to_resize.resize((int(width), int(height)))
with io.BytesIO() as buffer_out:
img.save(buffer_out, format="PNG")
bytes_data = buffer_out.getvalue()
bytes_size = len(bytes_data)
return bytes_data
def predict_large_language_model_sample(
api_endpoint: str,
endpoint: str,
input: dict,
parameters: dict
):
"""Predicts the output of a large language model on a given input.
Args:
api_endpoint:
The API endpoint of the AI Platform Prediction service. (str)
endpoint:
The name of the endpoint to use for predictions. (str)
input:
The input to the large language model. (str)
parameters:
The parameters for the prediction. (str)
Returns:
A list of strings containing the predictions.
Raises:
aiplatform.exceptions.NotFoundError: If the endpoint does not exist.
aiplatform.exceptions.BadRequestError: If the input is invalid.
aiplatform.exceptions.InternalServerError: If an internal error occurred.
"""
# The AI Platform services require regional API endpoints.
client_options = {"api_endpoint": api_endpoint}
# Initialize client that will be used to create and send requests.
# This client only needs to be created once, and can be reused for multiple requests.
client = aiplatform.gapic.PredictionServiceClient(
client_options=client_options
)
instance_dict = input
instance = json_format.ParseDict(instance_dict, Value())
instances = [instance]
parameters_dict = parameters
parameters = json_format.ParseDict(parameters_dict, Value())
response = client.predict(
endpoint=endpoint, instances=instances, parameters=parameters
)
return response.predictions
def image_generation(
prompt:str,
sample_count:int,
sample_image_size: int,
aspect_ratio: str,
state_key: str):
"""Generates an image from a prompt.
Args:
prompt:
The prompt to use to generate the image.
sample_count:
The number of images to generate.
sample_image_size:
The size of the generated images.
aspect_ratio:
The aspect ratio of the generated images.
state_key:
The key to use to store the generated images in the session state.
Returns:
None.
"""
st.session_state[state_key] = predict_large_language_model_sample(
api_endpoint=IMAGEN_API_ENDPOINT,
endpoint=IMAGEN_ENDPOINT,
input={
"prompt": prompt
},
parameters={
'sampleCount':sample_count,
'sampleImageSize':sample_image_size,
'aspectRatio':aspect_ratio
}
)
def edit_image_generation(
prompt:str,
sample_count:int,
bytes_data:bytes,
state_key: str,
mask_bytes_data: bytes=b""):
"""Generates an edited image from a prompt and a base image.
Args:
prompt:
A string that describes the desired edit to the image.
sample_count:
The number of edited images to generate.
bytes_data:
The image data in bytes.
state_key:
The key to store the generated images in the session state.
mask_bytes_data:
The mask data in bytes.
Returns:
None.
"""
input_dict = {
'prompt': prompt,
'image': {
'bytesBase64Encoded': base64.b64encode(bytes_data).decode('utf-8')
}
}
if mask_bytes_data:
input_dict["mask"] = {
"image": {
"bytesBase64Encoded": base64.b64encode(
mask_bytes_data).decode('utf-8')
}
}
st.session_state[state_key] = predict_large_language_model_sample(
api_endpoint=IMAGEN_API_ENDPOINT,
endpoint=IMAGEN_ENDPOINT,
input=input_dict,
parameters={
'sampleCount':sample_count
}
)
def render_one_image(
images_key: str,
image_position: int,
select_button: bool=False,
selected_image_key: str="",
edit_button: bool=False,
image_to_edit_key: str="",
download_button: bool=True):
"""
Renders one image from a list of images.
Args:
images_key:
The key in the session state that stores the list of images.
image_position:
The index of the image to render.
select_button:
Whether to show a button that allows the user to select the image.
selected_image_key:
The key in the session state to store the selected image.
edit_button:
Whether to show a button that allows the user to edit the image.
image_to_edit_key:
The key in the session state to store the edited image.
download_button:
Whether to show a button that allows the user to download the image.
Returns:
None.
"""
image = io.BytesIO(
base64.b64decode(
st.session_state[images_key][image_position]["bytesBase64Encoded"])
)
st.image(image)
if download_button:
st.download_button(
label='Download',
data=image,
file_name='image.png',
)
if select_button and selected_image_key:
if st.button(
"Select", key=f"_btn_select_{images_key}_{image_position}"):
st.session_state[selected_image_key] = image
if edit_button and image_to_edit_key:
if st.button("Edit", key=f"_btn_edit_{images_key}_{image_position}"):
st.session_state[image_to_edit_key] = image.getvalue()
def generate_image_columns(
images_key: str,
select_button: bool=False,
selected_image_key: str="",
edit_button: bool=False,
image_to_edit_key: str="",
download_button: bool=True):
"""Generates a grid of image columns.
Args:
images_key (str):
The key in the session state that stores the images.
select_button (bool, optional):
Whether to show a button to select the image. Defaults to False.
selected_image_key (str, optional):
The key in the session state that stores the selected image. Defaults to an empty string.
edit_button (bool, optional):
Whether to show a button to edit the image. Defaults to False.
image_to_edit_key (str, optional):
The key in the session state that stores the image to edit. Defaults to an empty string.
download_button (bool, optional):
Whether to show a button to download the image. Defaults to True.
Returns:
None.
"""
image_count = len(st.session_state[images_key])
counter = 0
while image_count > 0:
cols = st.columns([25,25,25,25])
for i, col in enumerate(cols):
with col:
try:
render_one_image(
images_key,
i+counter,
select_button,
selected_image_key,
edit_button,
image_to_edit_key,
download_button)
except:
continue
counter+=4
image_count-=4
def render_image_generation_ui(
image_text_prompt_key: str,
generated_images_key: str,
pre_populated_prompts: List[str] = ["an image of a cat"],
select_button: bool=False,
selected_image_key: str='',
edit_button: bool=False,
title: str="Generate Images",
image_to_edit_key: str='',
download_button: bool=True,
auto_submit_first_pre_populated: bool=False):
"""Renders a user interface for generating images.
Args:
image_text_prompt_key:
The key used to store the user's text prompt in the session state.
generated_images_key:
The key used to store the generated images in the session state.
pre_populated_prompts:
A list of pre-populated prompts.
select_button:
Whether to show a button to select a pre-populated prompt.
selected_image_key:
The key used to store the selected image in the session state.
edit_button:
Whether to show a button to edit the selected image.
title:
The title of the user interface.
image_to_edit_key:
The key used to store the image to edit in the session state.
download_button:
Whether to show a button to download the generated images.
auto_submit_first_pre_populated:
Whether to automatically submit the form with the first pre-populated prompt.
Returns:
None.
"""
SAMPLE_COUNT = [8, 4, 2, 1]
SAMPLE_IMAGE_SIZE = [256, 64, 512, 1024]
# ASPECT_RATIO = ['1:1', '5:4', '3:2', '7:4', '4:3', '16:9', '9:16']
ASPECT_RATIO = ['1:1']
if image_text_prompt_key in st.session_state:
st.session_state[
f"{image_text_prompt_key}_text_area"] = st.session_state[
image_text_prompt_key]
if auto_submit_first_pre_populated:
if generated_images_key not in st.session_state:
with st.spinner('Generating images ...'):
image_generation(
pre_populated_prompts[0],
SAMPLE_COUNT[0],
SAMPLE_IMAGE_SIZE[0],
ASPECT_RATIO[0],
generated_images_key)
if generated_images_key in st.session_state:
generate_image_columns(
generated_images_key,
select_button,
selected_image_key,
edit_button,
image_to_edit_key,
download_button)
def render_image_edit_prompt(
edit_image_prompt_key: str,
edited_images_key: str,
upload_file: bool=True,
image_to_edit_key: str="",
mask_image: bool=False,
mask_image_key: str="",
select_button: bool=False,
selected_image_key: str="",
download_button: bool=True,
file_uploader_key: str=""):
"""
Renders a prompt for editing an image.
Args:
edit_image_prompt_key:
The key to store the edit image prompt in the session state.
edited_images_key:
The key to store the edited images in the session state.
upload_file:
Whether to allow users to upload an image to edit.
image_to_edit_key:
The key to store the image to edit in the session state.
mask_image:
Whether to allow users to mask the image to edit.
mask_image_key:
The key to store the mask image in the session state.
select_button:
Whether to show a button to select an image to edit.
selected_image_key:
The key to store the selected image in the session state.
download_button:
Whether to show a button to download the edited images.
file_uploader_key:
The key to store the file uploader in the session state.
Returns:
None.
"""
SAMPLE_COUNT = [8, 4, 2, 1]
def submitted():
st.session_state[edit_image_prompt_key] = st.session_state[
f"{edit_image_prompt_key}_text_area"]
if edit_image_prompt_key in st.session_state:
st.session_state[
f"{edit_image_prompt_key}_text_area"] = st.session_state[
edit_image_prompt_key]
if upload_file:
with st.form(f"{file_uploader_key}_form", clear_on_submit=True):
uploaded_file = st.file_uploader(
'Upload your image here. It MUST be in PNG or JPEG format.',
type=['png', 'jpg'],
key=file_uploader_key)
submit_button_uploader = st.form_submit_button('Upload Image')
if submit_button_uploader:
if uploaded_file is not None:
if edited_images_key in st.session_state:
del st.session_state[edited_images_key]
if selected_image_key in st.session_state:
del st.session_state[selected_image_key]
st.session_state[image_to_edit_key] = uploaded_file.getvalue()
if mask_image and mask_image_key in st.session_state:
del st.session_state[mask_image_key]
if image_to_edit_key in st.session_state:
if image_to_edit_key in st.session_state and mask_image:
with st.expander(
"**[Optional] Paint where to edit in the image**", expanded=True):
utils_edit_image.edit_image_canvas(
mask_image_key,
resize_image_bytes(st.session_state[image_to_edit_key]))
else:
st.image(st.session_state[image_to_edit_key])
with st.form(f'{edited_images_key}_edit_image'):
st.write('**Edit the image with a prompt**')
edit_image_prompt = st.text_input(
'Provide a prompt using natural language to edit the image',
key=f"{edit_image_prompt_key}_text_area")
submit_button = st.form_submit_button('Edit Image', on_click=submitted)
if submit_button:
bytes_data = st.session_state[image_to_edit_key]
if bytes_data:
if len(bytes_data) > IMAGE_UPLOAD_BYTES_LIMIT:
bytes_data = resize_image_bytes(bytes_data)
if not edit_image_prompt:
st.error("Provide a prompt for editing the image")
else:
st.session_state[edit_image_prompt_key] = edit_image_prompt
with st.spinner('Generating Edited images ...'):
edit_image_generation(
st.session_state[edit_image_prompt_key],
8,
bytes_data,
edited_images_key,
st.session_state.get(mask_image_key, b"") if mask_image and mask_image_key else b"")
else:
st.error("No image found to edit")
if edited_images_key in st.session_state:
generate_image_columns(
edited_images_key,
select_button,
selected_image_key,
download_button=download_button)
def render_image_generation_and_edition_ui(
image_text_prompt_key: str,
generated_images_key: str,
edit_image_prompt_key: str,
pre_populated_prompts: List[str]=["an image of a cat"],
select_button: bool=False,
selected_image_key: str="",
edit_button: bool=False,
title: str="Generate Images",
image_to_edit_key: str="",
edit_with_mask: bool=False,
mask_image_key: str="",
edited_images_key: str="",
download_button: bool=False,
auto_submit_first_pre_populated=False):
render_image_generation_ui(
image_text_prompt_key,
generated_images_key,
pre_populated_prompts,
select_button,
selected_image_key,
edit_button,
title,
image_to_edit_key,
download_button,
auto_submit_first_pre_populated)
if image_to_edit_key in st.session_state:
render_image_edit_prompt(
edit_image_prompt_key,
edited_images_key,
False,
image_to_edit_key,
edit_with_mask,
mask_image_key,
select_button,
selected_image_key,
download_button)