Spaces:
Sleeping
Sleeping
Commit ·
c1254e9
1
Parent(s): 5ea4286
comic grading updated code
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
"""Driver File."""
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import core
|
| 4 |
import parameters
|
|
@@ -14,6 +15,7 @@ with gr.Blocks() as demo:
|
|
| 14 |
character_data = gr.State({})
|
| 15 |
current_frame_data = gr.State(None)
|
| 16 |
details = gr.State({})
|
|
|
|
| 17 |
|
| 18 |
with gr.Row():
|
| 19 |
with gr.Column():
|
|
@@ -74,33 +76,43 @@ with gr.Blocks() as demo:
|
|
| 74 |
gr.Markdown("## Composition #4")
|
| 75 |
prompt_4 = gr.TextArea(label="Image Prompt")
|
| 76 |
seed_4 = gr.Textbox(label="Generation Seed")
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
with gr.Column():
|
| 80 |
-
negative_prompt = gr.TextArea(
|
|
|
|
|
|
|
|
|
|
| 81 |
chars = gr.Textbox(value="[]", label="Related Characters")
|
| 82 |
with gr.Row():
|
| 83 |
height = gr.Textbox(value="1024", label="Image Height")
|
| 84 |
width = gr.Textbox(value="1024", label="Image Width")
|
| 85 |
visual_style = gr.Dropdown(
|
| 86 |
-
choices=parameters.VISUAL_CHOICES,
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
-
|
| 89 |
with gr.Row():
|
| 90 |
regenerate_btn = gr.Button("Regenerate")
|
| 91 |
save_btn = gr.Button("Save")
|
| 92 |
|
| 93 |
-
############################################ EVENTS ############################################
|
| 94 |
load_metadata.click(
|
| 95 |
core.load_metadata_fn,
|
| 96 |
inputs=[comic_id],
|
| 97 |
outputs=[
|
| 98 |
episode_dropdown,
|
| 99 |
frame_dropdown,
|
|
|
|
|
|
|
| 100 |
episodes_data,
|
| 101 |
character_data,
|
| 102 |
details,
|
| 103 |
-
developer
|
| 104 |
],
|
| 105 |
)
|
| 106 |
|
|
@@ -229,30 +241,43 @@ with gr.Blocks() as demo:
|
|
| 229 |
|
| 230 |
developer.change(
|
| 231 |
core.toggle_developer_options,
|
| 232 |
-
inputs=[developer],
|
| 233 |
-
outputs=[developer_options],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
regenerate_comps_btn.click(
|
| 237 |
core.regenerate_composition_data,
|
| 238 |
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
current_episode,
|
| 240 |
-
current_scene,
|
| 241 |
current_frame,
|
| 242 |
episodes_data,
|
| 243 |
-
details,
|
| 244 |
-
comic_id
|
| 245 |
],
|
| 246 |
outputs=[
|
| 247 |
prompt_1,
|
| 248 |
seed_1,
|
| 249 |
-
prompt_2,
|
| 250 |
seed_2,
|
| 251 |
prompt_3,
|
| 252 |
seed_3,
|
| 253 |
prompt_4,
|
| 254 |
-
seed_4
|
| 255 |
-
]
|
| 256 |
)
|
| 257 |
regenerate_btn.click(
|
| 258 |
core.regenerate_data,
|
|
@@ -265,10 +290,42 @@ with gr.Blocks() as demo:
|
|
| 265 |
character_data,
|
| 266 |
visual_style,
|
| 267 |
height,
|
| 268 |
-
width
|
| 269 |
],
|
| 270 |
-
outputs=[images]
|
| 271 |
)
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""Driver File."""
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import core
|
| 5 |
import parameters
|
|
|
|
| 15 |
character_data = gr.State({})
|
| 16 |
current_frame_data = gr.State(None)
|
| 17 |
details = gr.State({})
|
| 18 |
+
image_data_b64 = gr.State([])
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
with gr.Column():
|
|
|
|
| 76 |
gr.Markdown("## Composition #4")
|
| 77 |
prompt_4 = gr.TextArea(label="Image Prompt")
|
| 78 |
seed_4 = gr.Textbox(label="Generation Seed")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
regenerate_comps_btn = gr.Button(value="Regenerate Compositions")
|
| 82 |
+
save_components_btn = gr.Button(value="Save Components")
|
| 83 |
|
| 84 |
with gr.Column():
|
| 85 |
+
negative_prompt = gr.TextArea(
|
| 86 |
+
value="",
|
| 87 |
+
label="Negative Prompt",
|
| 88 |
+
)
|
| 89 |
chars = gr.Textbox(value="[]", label="Related Characters")
|
| 90 |
with gr.Row():
|
| 91 |
height = gr.Textbox(value="1024", label="Image Height")
|
| 92 |
width = gr.Textbox(value="1024", label="Image Width")
|
| 93 |
visual_style = gr.Dropdown(
|
| 94 |
+
choices=parameters.VISUAL_CHOICES,
|
| 95 |
+
label="Current Visual Style",
|
| 96 |
+
interactive=True,
|
| 97 |
)
|
| 98 |
+
|
| 99 |
with gr.Row():
|
| 100 |
regenerate_btn = gr.Button("Regenerate")
|
| 101 |
save_btn = gr.Button("Save")
|
| 102 |
|
| 103 |
+
############################################ EVENTS ############################################
|
| 104 |
load_metadata.click(
|
| 105 |
core.load_metadata_fn,
|
| 106 |
inputs=[comic_id],
|
| 107 |
outputs=[
|
| 108 |
episode_dropdown,
|
| 109 |
frame_dropdown,
|
| 110 |
+
current_episode,
|
| 111 |
+
current_frame,
|
| 112 |
episodes_data,
|
| 113 |
character_data,
|
| 114 |
details,
|
| 115 |
+
developer,
|
| 116 |
],
|
| 117 |
)
|
| 118 |
|
|
|
|
| 241 |
|
| 242 |
developer.change(
|
| 243 |
core.toggle_developer_options,
|
| 244 |
+
inputs=[developer, prompt_1, prompt_2, prompt_3, prompt_4, setting],
|
| 245 |
+
outputs=[developer_options, prompt_1, prompt_2, prompt_3, prompt_4, setting],
|
| 246 |
+
).then(
|
| 247 |
+
lambda: (
|
| 248 |
+
gr.update(interactive=True),
|
| 249 |
+
gr.update(interactive=True),
|
| 250 |
+
gr.update(interactive=True),
|
| 251 |
+
gr.update(interactive=True),
|
| 252 |
+
gr.update(interactive=True),
|
| 253 |
+
),
|
| 254 |
+
outputs=[prompt_1, prompt_2, prompt_3, prompt_4, setting],
|
| 255 |
)
|
| 256 |
|
| 257 |
regenerate_comps_btn.click(
|
| 258 |
core.regenerate_composition_data,
|
| 259 |
inputs=[
|
| 260 |
+
image_description,
|
| 261 |
+
narration,
|
| 262 |
+
character,
|
| 263 |
+
dialouge,
|
| 264 |
+
location,
|
| 265 |
+
setting,
|
| 266 |
+
chars,
|
| 267 |
current_episode,
|
|
|
|
| 268 |
current_frame,
|
| 269 |
episodes_data,
|
|
|
|
|
|
|
| 270 |
],
|
| 271 |
outputs=[
|
| 272 |
prompt_1,
|
| 273 |
seed_1,
|
| 274 |
+
prompt_2,
|
| 275 |
seed_2,
|
| 276 |
prompt_3,
|
| 277 |
seed_3,
|
| 278 |
prompt_4,
|
| 279 |
+
seed_4,
|
| 280 |
+
],
|
| 281 |
)
|
| 282 |
regenerate_btn.click(
|
| 283 |
core.regenerate_data,
|
|
|
|
| 290 |
character_data,
|
| 291 |
visual_style,
|
| 292 |
height,
|
| 293 |
+
width,
|
| 294 |
],
|
| 295 |
+
outputs=[images, image_data_b64],
|
| 296 |
)
|
| 297 |
|
| 298 |
+
save_components_btn.click(
|
| 299 |
+
core.save_image_compositions,
|
| 300 |
+
inputs=[
|
| 301 |
+
current_episode,
|
| 302 |
+
current_frame,
|
| 303 |
+
details,
|
| 304 |
+
comic_id,
|
| 305 |
+
image_description,
|
| 306 |
+
narration,
|
| 307 |
+
character,
|
| 308 |
+
dialouge,
|
| 309 |
+
location,
|
| 310 |
+
setting,
|
| 311 |
+
chars,
|
| 312 |
+
prompt_1,
|
| 313 |
+
prompt_2,
|
| 314 |
+
prompt_3,
|
| 315 |
+
prompt_4,
|
| 316 |
+
],
|
| 317 |
+
outputs=[current_scene],
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
save_btn.click(
|
| 321 |
+
core.save_images,
|
| 322 |
+
inputs=[
|
| 323 |
+
image_data_b64,
|
| 324 |
+
current_episode,
|
| 325 |
+
current_frame,
|
| 326 |
+
current_scene,
|
| 327 |
+
comic_id,
|
| 328 |
+
],
|
| 329 |
+
)
|
| 330 |
+
# demo.launch(auth=("admin", "Qrt@12*34#immersfy"), share=True, ssr_mode=False, debug=True)
|
| 331 |
+
demo.launch(share=True, ssr_mode=False, debug=True)
|
core.py
CHANGED
|
@@ -158,6 +158,8 @@ def load_metadata_fn(comic_id: str):
|
|
| 158 |
gr.update(
|
| 159 |
choices=range(len(episodes_data[current_episode])), value=current_frame
|
| 160 |
),
|
|
|
|
|
|
|
| 161 |
episodes_data,
|
| 162 |
character_data,
|
| 163 |
details,
|
|
@@ -240,91 +242,96 @@ def save_image(selected_image, comic_id: str, current_episode: int, current_fram
|
|
| 240 |
gr.Info("Saved Image successfully!")
|
| 241 |
|
| 242 |
|
| 243 |
-
def toggle_developer_options(
|
|
|
|
|
|
|
| 244 |
if is_developer:
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
def regenerate_composition_data(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
current_episode: int,
|
| 251 |
-
current_scene: int,
|
| 252 |
current_frame: int,
|
| 253 |
episodes_data: dict,
|
| 254 |
-
details: dict,
|
| 255 |
-
comic_id: str,
|
| 256 |
):
|
| 257 |
try:
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
scene_num, frame_num_in_scene = None, None
|
| 263 |
-
prev_frame_count = 0
|
| 264 |
-
|
| 265 |
-
for scene_idx, cumulative_frame_count in episode_details.items():
|
| 266 |
-
if current_frame < cumulative_frame_count:
|
| 267 |
-
scene_num = scene_idx
|
| 268 |
-
frame_num_in_scene = current_frame - prev_frame_count
|
| 269 |
-
break
|
| 270 |
-
prev_frame_count = cumulative_frame_count
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
"all_characters": frame.all_characters,
|
| 291 |
}
|
| 292 |
-
}
|
| 293 |
-
),
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
compositions = llm.generate_valid_json_response(prompt_dict)
|
| 297 |
-
frame.compositions = [
|
| 298 |
-
Composition(
|
| 299 |
-
**comp,
|
| 300 |
-
seed=(
|
| 301 |
-
frame.compositions[idx].seed
|
| 302 |
-
if idx < len(frame.compositions)
|
| 303 |
-
else ""
|
| 304 |
),
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
),
|
| 310 |
-
)
|
| 311 |
-
for idx, comp in enumerate(compositions["compositions"])
|
| 312 |
-
]
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
|
|
|
| 328 |
return [
|
| 329 |
frame.compositions[0].prompt,
|
| 330 |
frame.compositions[0].seed,
|
|
@@ -336,6 +343,7 @@ def regenerate_composition_data(
|
|
| 336 |
frame.compositions[3].seed,
|
| 337 |
]
|
| 338 |
except Exception as e:
|
|
|
|
| 339 |
return [""] * 8
|
| 340 |
|
| 341 |
|
|
@@ -351,35 +359,169 @@ def regenerate_data(
|
|
| 351 |
width,
|
| 352 |
):
|
| 353 |
images = []
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
gr.update(
|
| 159 |
choices=range(len(episodes_data[current_episode])), value=current_frame
|
| 160 |
),
|
| 161 |
+
current_episode,
|
| 162 |
+
current_frame,
|
| 163 |
episodes_data,
|
| 164 |
character_data,
|
| 165 |
details,
|
|
|
|
| 242 |
gr.Info("Saved Image successfully!")
|
| 243 |
|
| 244 |
|
| 245 |
+
def toggle_developer_options(
|
| 246 |
+
is_developer: bool, prompt_1, prompt_2, prompt_3, prompt_4, setting
|
| 247 |
+
):
|
| 248 |
if is_developer:
|
| 249 |
+
# Return visibility updates for the developer options along with the values
|
| 250 |
+
return gr.update(visible=True), prompt_1, prompt_2, prompt_3, prompt_4, setting
|
| 251 |
+
else:
|
| 252 |
+
# Hide the developer options and return only the updated visibility
|
| 253 |
+
return gr.update(visible=False), prompt_1, prompt_2, prompt_3, prompt_4, setting
|
| 254 |
|
| 255 |
|
| 256 |
def regenerate_composition_data(
|
| 257 |
+
image_description,
|
| 258 |
+
narration,
|
| 259 |
+
character,
|
| 260 |
+
dialouge,
|
| 261 |
+
location,
|
| 262 |
+
setting,
|
| 263 |
+
chars,
|
| 264 |
current_episode: int,
|
|
|
|
| 265 |
current_frame: int,
|
| 266 |
episodes_data: dict,
|
|
|
|
|
|
|
| 267 |
):
|
| 268 |
try:
|
| 269 |
+
print(
|
| 270 |
+
f"Regenerating composition data for episode {current_episode}, frame {current_frame}"
|
| 271 |
+
)
|
| 272 |
+
frame = episodes_data[current_episode][current_frame]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
try:
|
| 275 |
+
print("Creating prompt template for composition generation")
|
| 276 |
+
prompt_template = jinja2.Template(
|
| 277 |
+
script_gen.generate_image_compositions_user_prompt
|
| 278 |
+
)
|
| 279 |
+
except Exception as e:
|
| 280 |
+
print(f"Error creating prompt template: {e}")
|
| 281 |
+
raise
|
| 282 |
|
| 283 |
+
try:
|
| 284 |
+
print("Rendering prompt with frame details")
|
| 285 |
+
prompt_dict = {
|
| 286 |
+
"system": script_gen.generate_image_compositions_instruction,
|
| 287 |
+
"user": prompt_template.render(
|
| 288 |
+
{
|
| 289 |
+
"FRAME": {
|
| 290 |
+
"description": image_description,
|
| 291 |
+
"narration": narration,
|
| 292 |
+
"character_dilouge": dialouge,
|
| 293 |
+
"character": character,
|
| 294 |
+
"location": location,
|
| 295 |
+
"setting": setting,
|
| 296 |
+
"all_characters": chars,
|
| 297 |
+
}
|
|
|
|
| 298 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
),
|
| 300 |
+
}
|
| 301 |
+
except Exception as e:
|
| 302 |
+
print(f"Error rendering prompt: {e}")
|
| 303 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
+
try:
|
| 306 |
+
print("Generating compositions using LLM")
|
| 307 |
+
compositions = llm.generate_valid_json_response(prompt_dict)
|
| 308 |
+
except Exception as e:
|
| 309 |
+
print(f"Error generating compositions: {e}")
|
| 310 |
+
raise
|
| 311 |
|
| 312 |
+
try:
|
| 313 |
+
print("Updating frame compositions")
|
| 314 |
+
frame.compositions = [
|
| 315 |
+
Composition(
|
| 316 |
+
**comp,
|
| 317 |
+
seed=(
|
| 318 |
+
frame.compositions[idx].seed
|
| 319 |
+
if idx < len(frame.compositions)
|
| 320 |
+
else ""
|
| 321 |
+
),
|
| 322 |
+
image=(
|
| 323 |
+
frame.compositions[idx].image
|
| 324 |
+
if idx < len(frame.compositions)
|
| 325 |
+
else ""
|
| 326 |
+
),
|
| 327 |
+
)
|
| 328 |
+
for idx, comp in enumerate(compositions["compositions"])
|
| 329 |
+
]
|
| 330 |
+
except Exception as e:
|
| 331 |
+
print(f"Error updating frame compositions: {e}")
|
| 332 |
+
raise
|
| 333 |
|
| 334 |
+
print("Composition data regenerated successfully.")
|
| 335 |
return [
|
| 336 |
frame.compositions[0].prompt,
|
| 337 |
frame.compositions[0].seed,
|
|
|
|
| 343 |
frame.compositions[3].seed,
|
| 344 |
]
|
| 345 |
except Exception as e:
|
| 346 |
+
print(f"Error in regenerate_composition_data: {e}")
|
| 347 |
return [""] * 8
|
| 348 |
|
| 349 |
|
|
|
|
| 359 |
width,
|
| 360 |
):
|
| 361 |
images = []
|
| 362 |
+
image_data_b64 = []
|
| 363 |
+
try:
|
| 364 |
+
print(
|
| 365 |
+
f"Regenerating data for episode {current_episode}, scene {current_scene}, frame {current_frame}"
|
| 366 |
+
)
|
| 367 |
+
frame = episodes_data[current_episode][current_frame]
|
| 368 |
+
related_chars = [character_data[ch] for ch in frame.all_characters]
|
| 369 |
+
|
| 370 |
+
for i, composition in enumerate(frame.compositions):
|
| 371 |
+
try:
|
| 372 |
+
print(f"Generating image for composition {i}")
|
| 373 |
+
payload = {
|
| 374 |
+
"prompt": composition.prompt,
|
| 375 |
+
"characters": related_chars,
|
| 376 |
+
"parameters": {
|
| 377 |
+
"height": height,
|
| 378 |
+
"width": width,
|
| 379 |
+
"visual_style": visual_style,
|
| 380 |
+
"seed": composition.seed,
|
| 381 |
+
},
|
| 382 |
+
}
|
| 383 |
|
| 384 |
+
try:
|
| 385 |
+
print(f"Sending request to generate image for composition {i}")
|
| 386 |
+
data = iowrapper.get_valid_post_response(
|
| 387 |
+
url=f"{parameters.MODEL_SERVER_URL}generate_image",
|
| 388 |
+
payload=payload,
|
| 389 |
+
)
|
| 390 |
+
print(f"Image generated for composition {i}. Decoding image data.")
|
| 391 |
+
image_data = io.BytesIO(base64.b64decode(data["image"]))
|
| 392 |
+
image_data_b64.append(image_data)
|
| 393 |
+
images.append(Image.open(image_data))
|
| 394 |
+
except Exception as e:
|
| 395 |
+
print(f"Error generating image for composition {i}: {e}")
|
| 396 |
+
continue
|
| 397 |
+
except Exception as e:
|
| 398 |
+
print(f"Error processing composition {i}: {e}")
|
| 399 |
+
continue
|
| 400 |
+
|
| 401 |
+
print("Data regeneration completed.")
|
| 402 |
+
return images, image_data_b64
|
| 403 |
+
except Exception as e:
|
| 404 |
+
print(f"Error in regenerate_data: {e}")
|
| 405 |
+
return [], []
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def save_image_compositions(
|
| 409 |
+
current_episode: int,
|
| 410 |
+
current_frame: int,
|
| 411 |
+
details: dict,
|
| 412 |
+
comic_id: str,
|
| 413 |
+
image_description,
|
| 414 |
+
narration,
|
| 415 |
+
character,
|
| 416 |
+
dialogue, # Fixed typo from 'dialouge' to 'dialogue'
|
| 417 |
+
location,
|
| 418 |
+
setting,
|
| 419 |
+
chars,
|
| 420 |
+
prompt_1,
|
| 421 |
+
prompt_2,
|
| 422 |
+
prompt_3,
|
| 423 |
+
prompt_4,
|
| 424 |
+
):
|
| 425 |
+
try:
|
| 426 |
+
print(
|
| 427 |
+
f"Saving image components for episode {current_episode}, frame {current_frame}"
|
| 428 |
+
)
|
| 429 |
|
| 430 |
+
# Fetch episode details early and return if not found
|
| 431 |
+
episode_details = details.get(current_episode)
|
| 432 |
+
if not episode_details:
|
| 433 |
+
print(f"Episode {current_episode} not found!")
|
| 434 |
+
return None
|
| 435 |
+
|
| 436 |
+
# Determine scene number and frame number within the scene
|
| 437 |
+
scene_num, frame_num_in_scene = None, 0
|
| 438 |
+
for scene_idx, cumulative_frame_count in enumerate(episode_details.items()):
|
| 439 |
+
if current_frame < cumulative_frame_count[1]:
|
| 440 |
+
scene_num = cumulative_frame_count[0]
|
| 441 |
+
frame_num_in_scene = current_frame - (
|
| 442 |
+
episode_details.get(scene_num - 1, 0)
|
| 443 |
+
)
|
| 444 |
+
break
|
| 445 |
+
|
| 446 |
+
if scene_num is None:
|
| 447 |
+
print(f"Scene not found for frame {current_frame}.")
|
| 448 |
+
return None
|
| 449 |
+
|
| 450 |
+
# Fetch episode data from S3
|
| 451 |
+
episode_path = f"s3://blix-demo-v0/{comic_id}/episodes/episode-{current_episode}/episode.json"
|
| 452 |
+
print(f"Fetching episode from S3: {episode_path}")
|
| 453 |
+
episode_json = aws_utils.fetch_from_s3(episode_path).decode("utf-8")
|
| 454 |
+
episode = json.loads(episode_json)
|
| 455 |
+
|
| 456 |
+
frame_data = episode["scenes"][scene_num]["frames"][frame_num_in_scene]
|
| 457 |
+
print(
|
| 458 |
+
f"Updating compositions for scene {scene_num}, frame {frame_num_in_scene}"
|
| 459 |
+
)
|
| 460 |
+
|
| 461 |
+
# Update compositions with prompts
|
| 462 |
+
prompts_list = [prompt_1, prompt_2, prompt_3, prompt_4]
|
| 463 |
+
frame_data["compositions"] = [
|
| 464 |
+
{
|
| 465 |
+
"prompt": prompts_list[i],
|
| 466 |
+
"shot_type": comp["shot_type"],
|
| 467 |
+
"seed": comp["seed"],
|
| 468 |
+
"image": comp["image"],
|
| 469 |
+
}
|
| 470 |
+
for i, comp in enumerate(frame_data["compositions"])
|
| 471 |
+
]
|
| 472 |
+
|
| 473 |
+
# Batch update frame data
|
| 474 |
+
frame_data.update(
|
| 475 |
+
{
|
| 476 |
+
"description": image_description,
|
| 477 |
+
"narration": narration,
|
| 478 |
+
"audio_cue_text": dialogue,
|
| 479 |
+
"location": location,
|
| 480 |
+
"setting": setting,
|
| 481 |
+
"audio_cue_character": character,
|
| 482 |
+
}
|
| 483 |
+
)
|
| 484 |
+
|
| 485 |
+
# Save the updated episode back to S3
|
| 486 |
+
print(f"Saving updated episode to S3 at {episode_path}")
|
| 487 |
+
aws_utils.save_to_s3(
|
| 488 |
+
bucket_name=parameters.AWS_BUCKET,
|
| 489 |
+
folder_name=f"{comic_id}/episodes/episode-{current_episode}",
|
| 490 |
+
content=json.dumps(episode),
|
| 491 |
+
file_name="episode.json",
|
| 492 |
+
)
|
| 493 |
+
|
| 494 |
+
gr.Info("Components saved successfully!")
|
| 495 |
+
return scene_num
|
| 496 |
+
|
| 497 |
+
except Exception as e:
|
| 498 |
+
print(f"Error in save_image_compositions: {e}")
|
| 499 |
+
return None
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def save_images(
|
| 503 |
+
image_data_b64,
|
| 504 |
+
current_episode,
|
| 505 |
+
current_frame,
|
| 506 |
+
current_scene,
|
| 507 |
+
comic_id,
|
| 508 |
+
):
|
| 509 |
+
try:
|
| 510 |
+
print(
|
| 511 |
+
f"Saving images for scene {current_scene}, episode {current_episode}, frame {current_frame}."
|
| 512 |
+
)
|
| 513 |
+
for i, image_data in enumerate(image_data_b64):
|
| 514 |
+
try:
|
| 515 |
+
print(f"Saving image {i} to S3")
|
| 516 |
+
aws_utils.save_to_s3(
|
| 517 |
+
parameters.AWS_BUCKET,
|
| 518 |
+
f"{comic_id}/episodes/episode-{current_episode}/compositions/scene-{current_scene}/frame-{current_frame}",
|
| 519 |
+
image_data,
|
| 520 |
+
f"{i}.jpg",
|
| 521 |
+
)
|
| 522 |
+
except Exception as e:
|
| 523 |
+
print(f"Error saving image {i} to S3: {e}")
|
| 524 |
+
continue
|
| 525 |
+
gr.Info("All Images saved successfully!")
|
| 526 |
+
except Exception as e:
|
| 527 |
+
print(f"Error in save_images: {e}")
|