-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneration.py
321 lines (279 loc) · 9.81 KB
/
generation.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
import boto3
from botocore.config import Config
import base64
import time
import os
import json
import string
import random
import re
from PIL import Image
import io
import sys
import concurrent.futures
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config import (
SYSTEM_TEXT_ONLY,
SYSTEM_IMAGE_TEXT,
SYSTEM_CANVAS,
MODEL_OPTIONS,
DEFAULT_BUCKET,
DEFAULT_GUIDELINE,
GENERATED_VIDEOS_DIR,
CANVAS_SIZE)
from utils import (
random_string_name,
load_guideline,
parse_prompt,
process_image,
download_video
)
# Initialize AWS clients
session = boto3.session.Session(region_name='us-east-1')
client = session.client(service_name='bedrock-runtime',
config=Config(connect_timeout=1000, read_timeout=1000))
bedrock_runtime = session.client("bedrock-runtime")
# Constants
TARGET_WIDTH = 1280
TARGET_HEIGHT = 720
MAX_PROMPT_LENGTH = 512
def optimize_prompt(prompt, guideline_path, model_name=MODEL_OPTIONS["Nova Pro"], image=None):
if image is not None:
image = process_image(image)
doc_bytes = load_guideline(guideline_path)
model_id = MODEL_OPTIONS[model_name]
if image is None:
# Text-only prompt optimization
system = [{"text": SYSTEM_TEXT_ONLY}]
messages = [
{
"role": "user",
"content": [
{
"document": {
"format": "pdf",
"name": "DocumentPDFmessages",
"source": {"bytes": doc_bytes}
}
},
{"text": f"Please optimize: {prompt}"},
],
}
]
else:
# Image + text prompt optimization
system = [{"text": SYSTEM_IMAGE_TEXT}]
# Convert image to bytes
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_bytes = img_byte_arr.getvalue()
messages = [
{
"role": "user",
"content": [
{
"document": {
"format": "pdf",
"name": "DocumentPDFmessages",
"source": {"bytes": doc_bytes}
}
},
{"image": {"format": "png", "source": {"bytes": img_bytes}}},
{"text": f"Please optimize: {prompt}"},
],
}
]
# Configure inference parameters
inf_params = {"maxTokens": 512, "topP": 0.9, "temperature": 0.8}
# Get response
response = client.converse_stream(
modelId=model_id,
messages=messages,
system=system,
inferenceConfig=inf_params
)
# Collect response text
text = ""
stream = response.get("stream")
if stream:
for event in stream:
if "contentBlockDelta" in event:
text += event["contentBlockDelta"]["delta"]["text"]
optimized = parse_prompt(text)
length = len(optimized)
return optimized, f"{length} chars" + (" (Too Long!)" if length > MAX_PROMPT_LENGTH else "")
def optimize_canvas_prompt(prompt, model_name="Nova Pro"):
"""Optimize prompt for image generation using Canvas model"""
system = [{"text": SYSTEM_CANVAS}]
messages = [
{
"role": "user",
"content": [{"text": f"Please optimize: {prompt}"}],
}
]
# Configure inference parameters
inf_params = {"maxTokens": 512, "topP": 0.9, "temperature": 0.8}
# Get response
response = client.converse_stream(
modelId=MODEL_OPTIONS[model_name],
messages=messages,
system=system,
inferenceConfig=inf_params
)
# Collect response text
text = ""
stream = response.get("stream")
if stream:
for event in stream:
if "contentBlockDelta" in event:
text += event["contentBlockDelta"]["delta"]["text"]
# Extract both prompt and negative prompt
prompt = parse_prompt(text, r"<prompt>(.*?)</prompt>")
try:
negative_prompt = parse_prompt(text, r"<negative_prompt>(.*?)</negative_prompt>")
except ValueError:
negative_prompt = ""
return prompt, negative_prompt
def generate_image_pair(original_prompt, optimized_prompt, negative_prompt="", quality="standard", num_images=1, height=720, width=1280, seed=0, cfg_scale=6.5):
"""Generate images in parallel for both original and optimized prompts"""
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
# Submit both image generation tasks
future_original = executor.submit(
generate_single_image,
original_prompt,
negative_prompt,
quality,
num_images,
height,
width,
seed,
cfg_scale
)
future_optimized = executor.submit(
generate_single_image,
optimized_prompt,
negative_prompt,
quality,
num_images,
height,
width,
seed,
cfg_scale
)
# Wait for both tasks to complete
original_images = future_original.result()
optimized_images = future_optimized.result()
# Combine results
all_images = []
if original_images:
all_images.extend(original_images)
if optimized_images:
all_images.extend(optimized_images)
return all_images
def generate_single_image(prompt, negative_prompt="", quality="standard", num_images=1, height=720, width=1280, seed=0, cfg_scale=6.5):
"""Generate image using Nova Canvas model"""
body = json.dumps({
"taskType": "TEXT_IMAGE",
"textToImageParams": {
"text": prompt,
"negativeText": negative_prompt
} if negative_prompt else {
"text": prompt
},
"imageGenerationConfig": {
"numberOfImages": int(num_images), # Ensure integer
"height": int(height),
"width": int(width),
"cfgScale": float(cfg_scale),
"seed": random.randint(0,858993459) if int(seed) == -1 else int(seed),
"quality": quality
}
})
try:
response = bedrock_runtime.invoke_model(
body=body,
modelId='amazon.nova-canvas-v1:0',
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get("body").read())
# Create temporary directory for saving images
local_dir = './generated_images'
os.makedirs(local_dir, exist_ok=True)
image_paths = []
# Save each image to a temporary file and collect paths
for i, base64_image in enumerate(response_body.get("images", [])):
image_bytes = base64.b64decode(base64_image)
image = Image.open(io.BytesIO(image_bytes))
rand_name = random_string_name()
path = f"{local_dir}/generated_{rand_name}.png"
image.save(path)
image_paths.append(path)
return image_paths if image_paths else None
except Exception as e:
print(f"Error generating image: {str(e)}")
return None
except Exception as e:
print(f"Error downloading video: {str(e)}")
return None
def generate_video(prompt, bucket, image=None, seed=0):
if image is not None:
image = process_image(image)
# Prepare model input
model_input = {
"taskType": "TEXT_VIDEO",
"textToVideoParams": {
"text": prompt,
},
"videoGenerationConfig": {
"durationSeconds": 6,
"fps": 24,
"dimension": "1280x720",
"seed": random.randint(0,858993459) if int(seed) == -1 else int(seed),
},
}
# Add image if provided
if image is not None:
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_bytes = img_byte_arr.getvalue()
img_base64 = base64.b64encode(img_bytes).decode("utf-8")
model_input['textToVideoParams']['images'] = [{
"format": "png",
"source": {
"bytes": img_base64
}
}]
# Start async video generation
invocation = bedrock_runtime.start_async_invoke(
modelId="amazon.nova-reel-v1:0",
modelInput=model_input,
outputDataConfig={
"s3OutputDataConfig": {
"s3Uri": bucket
}
}
)
# Wait for completion
while True:
response = bedrock_runtime.get_async_invoke(
invocationArn=invocation['invocationArn']
)
status = response["status"]
if status != 'InProgress':
break
time.sleep(10)
# Download video
output_uri = f"{response['outputDataConfig']['s3OutputDataConfig']['s3Uri']}/output.mp4"
local_path = download_video(output_uri,GENERATED_VIDEOS_DIR)
return local_path
def generate_comparison_videos(original_prompt, optimized_prompt, bucket, image=None, seed=0):
# Create a thread pool executor
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
# Submit both video generation tasks
future_original = executor.submit(generate_video, original_prompt, bucket, image, seed)
future_optimized = executor.submit(generate_video, optimized_prompt, bucket, image, seed)
# Wait for both tasks to complete
original_video = future_original.result()
optimized_video = future_optimized.result()
return original_video, optimized_video