Spaces:
Sleeping
Sleeping
Commit ·
0abb49c
1
Parent(s): 1fbff61
Completed regeneration flow
Browse files
app.py
CHANGED
|
@@ -279,21 +279,6 @@ with gr.Blocks() as demo:
|
|
| 279 |
seed_4,
|
| 280 |
],
|
| 281 |
)
|
| 282 |
-
regenerate_btn.click(
|
| 283 |
-
core.regenerate_data,
|
| 284 |
-
inputs=[
|
| 285 |
-
comic_id,
|
| 286 |
-
current_episode,
|
| 287 |
-
current_scene,
|
| 288 |
-
current_frame,
|
| 289 |
-
episodes_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,
|
|
@@ -316,6 +301,21 @@ with gr.Blocks() as demo:
|
|
| 316 |
],
|
| 317 |
outputs=[current_scene],
|
| 318 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
save_btn.click(
|
| 321 |
core.save_images,
|
|
@@ -323,8 +323,8 @@ with gr.Blocks() as demo:
|
|
| 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)
|
|
|
|
| 279 |
seed_4,
|
| 280 |
],
|
| 281 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
save_components_btn.click(
|
| 284 |
core.save_image_compositions,
|
|
|
|
| 301 |
],
|
| 302 |
outputs=[current_scene],
|
| 303 |
)
|
| 304 |
+
regenerate_btn.click(
|
| 305 |
+
core.regenerate_data,
|
| 306 |
+
inputs=[
|
| 307 |
+
comic_id,
|
| 308 |
+
current_episode,
|
| 309 |
+
current_frame,
|
| 310 |
+
episodes_data,
|
| 311 |
+
character_data,
|
| 312 |
+
visual_style,
|
| 313 |
+
height,
|
| 314 |
+
width,
|
| 315 |
+
details
|
| 316 |
+
],
|
| 317 |
+
outputs=[images, image_data_b64],
|
| 318 |
+
)
|
| 319 |
|
| 320 |
save_btn.click(
|
| 321 |
core.save_images,
|
|
|
|
| 323 |
image_data_b64,
|
| 324 |
current_episode,
|
| 325 |
current_frame,
|
|
|
|
| 326 |
comic_id,
|
| 327 |
+
details
|
| 328 |
],
|
| 329 |
)
|
| 330 |
demo.launch(auth=("admin", "Qrt@12*34#immersfy"), share=True, ssr_mode=False, debug=True)
|
core.py
CHANGED
|
@@ -12,6 +12,8 @@ import inout as iowrapper
|
|
| 12 |
import openai_wrapper
|
| 13 |
import json
|
| 14 |
from dataclasses import asdict
|
|
|
|
|
|
|
| 15 |
|
| 16 |
AWS_BUCKET = parameters.AWS_BUCKET
|
| 17 |
llm = openai_wrapper.GPT_4O_MINI
|
|
@@ -220,6 +222,22 @@ def load_from_dropdown(
|
|
| 220 |
)
|
| 221 |
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
def load_dropdown_fn(selected_episode):
|
| 224 |
return (gr.update(value=selected_episode), gr.update(value=0), selected_episode, 0)
|
| 225 |
|
|
@@ -350,19 +368,22 @@ def regenerate_composition_data(
|
|
| 350 |
def regenerate_data(
|
| 351 |
comic_id,
|
| 352 |
current_episode,
|
| 353 |
-
current_scene,
|
| 354 |
current_frame,
|
| 355 |
episodes_data,
|
| 356 |
character_data,
|
| 357 |
visual_style,
|
| 358 |
height,
|
| 359 |
width,
|
|
|
|
| 360 |
):
|
| 361 |
images = []
|
| 362 |
image_data_b64 = []
|
| 363 |
try:
|
|
|
|
|
|
|
|
|
|
| 364 |
print(
|
| 365 |
-
f"Regenerating data for episode {current_episode},
|
| 366 |
)
|
| 367 |
frame = episodes_data[current_episode][current_frame]
|
| 368 |
related_chars = [character_data[ch] for ch in frame.all_characters]
|
|
@@ -389,7 +410,7 @@ def regenerate_data(
|
|
| 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(
|
| 393 |
images.append(Image.open(image_data))
|
| 394 |
except Exception as e:
|
| 395 |
print(f"Error generating image for composition {i}: {e}")
|
|
@@ -423,39 +444,22 @@ def save_image_compositions(
|
|
| 423 |
prompt_4,
|
| 424 |
):
|
| 425 |
try:
|
|
|
|
|
|
|
|
|
|
| 426 |
print(
|
| 427 |
-
f"Saving image components for episode {current_episode}, 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"][
|
| 457 |
print(
|
| 458 |
-
f"Updating compositions for scene {
|
| 459 |
)
|
| 460 |
|
| 461 |
# Update compositions with prompts
|
|
@@ -492,36 +496,34 @@ def save_image_compositions(
|
|
| 492 |
)
|
| 493 |
|
| 494 |
gr.Info("Components saved successfully!")
|
| 495 |
-
|
|
|
|
| 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
|
| 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-{
|
| 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 |
-
|
| 526 |
except Exception as e:
|
| 527 |
print(f"Error in save_images: {e}")
|
|
|
|
| 12 |
import openai_wrapper
|
| 13 |
import json
|
| 14 |
from dataclasses import asdict
|
| 15 |
+
import base64
|
| 16 |
+
import io
|
| 17 |
|
| 18 |
AWS_BUCKET = parameters.AWS_BUCKET
|
| 19 |
llm = openai_wrapper.GPT_4O_MINI
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
|
| 225 |
+
def get_scene_number(details, current_episode, current_frame):
|
| 226 |
+
episode_details = details.get(current_episode)
|
| 227 |
+
if not episode_details:
|
| 228 |
+
print(f"Episode {current_episode} not found!")
|
| 229 |
+
return None
|
| 230 |
+
|
| 231 |
+
# Determine scene number and frame number within the scene
|
| 232 |
+
scene_num, frame_num_in_scene = None, 0
|
| 233 |
+
for scene_idx, cumulative_frame_count in enumerate(episode_details.items()):
|
| 234 |
+
if current_frame < cumulative_frame_count[1]:
|
| 235 |
+
scene_num = cumulative_frame_count[0]
|
| 236 |
+
frame_num_in_scene = current_frame - (episode_details.get(scene_num - 1, 0))
|
| 237 |
+
break
|
| 238 |
+
return scene_num, frame_num_in_scene
|
| 239 |
+
|
| 240 |
+
|
| 241 |
def load_dropdown_fn(selected_episode):
|
| 242 |
return (gr.update(value=selected_episode), gr.update(value=0), selected_episode, 0)
|
| 243 |
|
|
|
|
| 368 |
def regenerate_data(
|
| 369 |
comic_id,
|
| 370 |
current_episode,
|
|
|
|
| 371 |
current_frame,
|
| 372 |
episodes_data,
|
| 373 |
character_data,
|
| 374 |
visual_style,
|
| 375 |
height,
|
| 376 |
width,
|
| 377 |
+
details,
|
| 378 |
):
|
| 379 |
images = []
|
| 380 |
image_data_b64 = []
|
| 381 |
try:
|
| 382 |
+
current_scene, frame_num_in_scene = get_scene_number(
|
| 383 |
+
details, current_episode, current_frame
|
| 384 |
+
)
|
| 385 |
print(
|
| 386 |
+
f"Regenerating data for episode {current_episode}, Scene: {current_scene}, frame {frame_num_in_scene}"
|
| 387 |
)
|
| 388 |
frame = episodes_data[current_episode][current_frame]
|
| 389 |
related_chars = [character_data[ch] for ch in frame.all_characters]
|
|
|
|
| 410 |
)
|
| 411 |
print(f"Image generated for composition {i}. Decoding image data.")
|
| 412 |
image_data = io.BytesIO(base64.b64decode(data["image"]))
|
| 413 |
+
image_data_b64.append(data["image"])
|
| 414 |
images.append(Image.open(image_data))
|
| 415 |
except Exception as e:
|
| 416 |
print(f"Error generating image for composition {i}: {e}")
|
|
|
|
| 444 |
prompt_4,
|
| 445 |
):
|
| 446 |
try:
|
| 447 |
+
current_scene, frame_num_in_scene = get_scene_number(
|
| 448 |
+
details, current_episode, current_frame
|
| 449 |
+
)
|
| 450 |
print(
|
| 451 |
+
f"Saving image components for episode {current_episode}, frame {frame_num_in_scene}"
|
| 452 |
)
|
| 453 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
# Fetch episode data from S3
|
| 455 |
episode_path = f"s3://blix-demo-v0/{comic_id}/episodes/episode-{current_episode}/episode.json"
|
| 456 |
print(f"Fetching episode from S3: {episode_path}")
|
| 457 |
episode_json = aws_utils.fetch_from_s3(episode_path).decode("utf-8")
|
| 458 |
episode = json.loads(episode_json)
|
| 459 |
|
| 460 |
+
frame_data = episode["scenes"][current_scene]["frames"][frame_num_in_scene]
|
| 461 |
print(
|
| 462 |
+
f"Updating compositions for scene {current_scene}, frame {frame_num_in_scene}"
|
| 463 |
)
|
| 464 |
|
| 465 |
# Update compositions with prompts
|
|
|
|
| 496 |
)
|
| 497 |
|
| 498 |
gr.Info("Components saved successfully!")
|
| 499 |
+
print(current_scene)
|
| 500 |
+
return current_scene
|
| 501 |
|
| 502 |
except Exception as e:
|
| 503 |
print(f"Error in save_image_compositions: {e}")
|
| 504 |
return None
|
| 505 |
|
| 506 |
|
| 507 |
+
def save_images(image_data_b64, current_episode, current_frame, comic_id, details):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
try:
|
| 509 |
+
current_scene, frame_num_in_scene = get_scene_number(
|
| 510 |
+
details, current_episode, current_frame
|
| 511 |
+
)
|
| 512 |
print(
|
| 513 |
+
f"Saving images for episode {current_episode}, Scene {current_scene} ,frame {frame_num_in_scene}."
|
| 514 |
)
|
| 515 |
for i, image_data in enumerate(image_data_b64):
|
| 516 |
try:
|
| 517 |
print(f"Saving image {i} to S3")
|
| 518 |
aws_utils.save_to_s3(
|
| 519 |
parameters.AWS_BUCKET,
|
| 520 |
+
f"{comic_id}/episodes/episode-{current_episode}/compositions/scene-{current_scene}/frame-{frame_num_in_scene}",
|
| 521 |
+
io.BytesIO(base64.b64decode(image_data)),
|
| 522 |
f"{i}.jpg",
|
| 523 |
)
|
| 524 |
except Exception as e:
|
| 525 |
print(f"Error saving image {i} to S3: {e}")
|
| 526 |
continue
|
| 527 |
+
print("All images saved successfully!")
|
| 528 |
except Exception as e:
|
| 529 |
print(f"Error in save_images: {e}")
|