Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from diffusers import StableDiffusionPipeline,
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import requests
|
| 7 |
import os
|
| 8 |
from datetime import datetime
|
| 9 |
import re
|
| 10 |
-
import tempfile
|
| 11 |
import time
|
| 12 |
-
import
|
| 13 |
-
import json
|
| 14 |
-
from typing import Dict, List, Tuple, Optional
|
| 15 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 16 |
from pydantic import BaseModel
|
| 17 |
-
import random
|
| 18 |
import gc
|
| 19 |
import psutil
|
| 20 |
import threading
|
| 21 |
-
from
|
| 22 |
|
| 23 |
# External OCI API URL
|
| 24 |
OCI_API_BASE_URL = "https://yukee1992-oci-story-book.hf.space"
|
|
@@ -49,28 +45,38 @@ class StorybookRequest(BaseModel):
|
|
| 49 |
story_title: str
|
| 50 |
scenes: List[StoryScene]
|
| 51 |
characters: List[CharacterDescription] = []
|
| 52 |
-
model_choice: str = "
|
| 53 |
style: str = "childrens_book"
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# MODEL SELECTION
|
| 56 |
MODEL_CHOICES = {
|
| 57 |
-
"sdxl": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 58 |
-
"sdxl-turbo": "stabilityai/sdxl-turbo",
|
| 59 |
"dreamshaper-8": "lykon/dreamshaper-8",
|
| 60 |
"realistic-vision": "SG161222/Realistic_Vision_V5.1",
|
| 61 |
}
|
| 62 |
|
| 63 |
-
# GLOBAL MODEL CACHE
|
| 64 |
model_cache = {}
|
| 65 |
current_model_name = None
|
| 66 |
current_pipe = None
|
| 67 |
model_lock = threading.Lock()
|
| 68 |
|
| 69 |
# Character consistency tracking
|
| 70 |
-
character_descriptions = {}
|
| 71 |
character_seeds = {}
|
| 72 |
|
| 73 |
-
#
|
|
|
|
|
|
|
| 74 |
def monitor_memory():
|
| 75 |
try:
|
| 76 |
process = psutil.Process()
|
|
@@ -83,138 +89,112 @@ def cleanup_memory():
|
|
| 83 |
if torch.cuda.is_available():
|
| 84 |
torch.cuda.empty_cache()
|
| 85 |
|
| 86 |
-
def load_model(model_name="
|
| 87 |
-
"""Thread-safe model loading
|
| 88 |
global model_cache, current_model_name, current_pipe
|
| 89 |
|
| 90 |
with model_lock:
|
| 91 |
if model_name in model_cache:
|
| 92 |
-
print(f"โ
Using cached model: {model_name}")
|
| 93 |
current_pipe = model_cache[model_name]
|
| 94 |
current_model_name = model_name
|
| 95 |
return current_pipe
|
| 96 |
|
| 97 |
print(f"๐ Loading model: {model_name}")
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
model_id = MODEL_CHOICES[model_name]
|
| 101 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 102 |
-
model_id,
|
| 103 |
-
torch_dtype=torch.float32,
|
| 104 |
-
use_safetensors=True,
|
| 105 |
-
safety_checker=None,
|
| 106 |
-
requires_safety_checker=False
|
| 107 |
-
)
|
| 108 |
-
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 109 |
-
else:
|
| 110 |
-
model_id = MODEL_CHOICES.get(model_name, "lykon/dreamshaper-8")
|
| 111 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 112 |
-
model_id,
|
| 113 |
-
torch_dtype=torch.float32,
|
| 114 |
-
safety_checker=None,
|
| 115 |
-
requires_safety_checker=False
|
| 116 |
-
)
|
| 117 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
pipe = pipe.to("cpu")
|
|
|
|
| 120 |
model_cache[model_name] = pipe
|
| 121 |
current_pipe = pipe
|
| 122 |
current_model_name = model_name
|
| 123 |
|
| 124 |
-
print(f"โ
Model loaded
|
| 125 |
return pipe
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
print(f"โ Model loading failed: {e}")
|
| 129 |
-
|
| 130 |
-
"runwayml/stable-diffusion-v1-5",
|
| 131 |
-
torch_dtype=torch.float32
|
| 132 |
-
).to("cpu")
|
| 133 |
-
model_cache[model_name] = pipe
|
| 134 |
-
return pipe
|
| 135 |
|
| 136 |
# Initialize default model
|
| 137 |
print("๐ Initializing Storybook Generator...")
|
| 138 |
-
|
| 139 |
print("โ
Model loaded and ready!")
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return text
|
| 149 |
-
|
| 150 |
-
# Keep first 40 words (most important part) and key descriptors
|
| 151 |
-
compressed = ' '.join(words[:40])
|
| 152 |
-
|
| 153 |
-
# Add style context
|
| 154 |
-
style_context = {
|
| 155 |
-
"childrens_book": "children's book style",
|
| 156 |
-
"realistic": "realistic style",
|
| 157 |
-
"fantasy": "fantasy style",
|
| 158 |
-
"anime": "anime style"
|
| 159 |
-
}
|
| 160 |
-
|
| 161 |
-
return f"{compressed}... {style_context.get(style, '')} masterpiece 4K"
|
| 162 |
-
|
| 163 |
-
def create_optimized_prompt(scene_visual, characters, style="childrens_book", page_number=1):
|
| 164 |
-
"""Create optimized prompt within token limits"""
|
| 165 |
-
# Compress the scene visual
|
| 166 |
-
scene_compressed = compress_prompt(scene_visual, style)
|
| 167 |
-
|
| 168 |
-
# Extract character essentials
|
| 169 |
-
char_descriptors = []
|
| 170 |
if characters:
|
|
|
|
| 171 |
for char in characters:
|
| 172 |
-
if
|
| 173 |
-
name = char.name
|
| 174 |
-
desc = char.description
|
| 175 |
-
elif isinstance(char, dict):
|
| 176 |
-
name = char.get('name', 'Unknown')
|
| 177 |
-
desc = char.get('description', '')
|
| 178 |
-
else:
|
| 179 |
-
continue
|
| 180 |
|
| 181 |
-
# Extract key
|
| 182 |
import re
|
| 183 |
-
# Get species/type
|
| 184 |
species_match = re.search(r'(rabbit|hedgehog|bird|dog|cat|fox|bear|dragon|human|girl|boy)', desc, re.IGNORECASE)
|
| 185 |
species = species_match.group(1) if species_match else "character"
|
| 186 |
|
| 187 |
-
# Get color
|
| 188 |
color_match = re.search(r'(white|black|brown|blue|red|green|yellow|golden|pink)', desc, re.IGNORECASE)
|
| 189 |
color = color_match.group(1) if color_match else ""
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# Ensure it's under 60 words
|
| 200 |
words = final_prompt.split()
|
| 201 |
if len(words) > 60:
|
| 202 |
final_prompt = ' '.join(words[:60])
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
return final_prompt
|
| 205 |
|
| 206 |
def enhance_prompt(scene_visual, characters, style="childrens_book", page_number=1):
|
| 207 |
-
"""Create optimized prompt"""
|
| 208 |
-
main_prompt =
|
| 209 |
-
|
| 210 |
-
print(f"๐ Optimized prompt: {main_prompt}")
|
| 211 |
-
print(f"๐ Length: {len(main_prompt.split())} words")
|
| 212 |
|
| 213 |
-
# Negative prompt
|
| 214 |
negative_prompt = (
|
| 215 |
"blurry, low quality, ugly, deformed, bad anatomy, "
|
| 216 |
-
"watermark,
|
| 217 |
-
"inconsistent features, low resolution"
|
| 218 |
)
|
| 219 |
|
| 220 |
return main_prompt, negative_prompt
|
|
@@ -263,169 +243,143 @@ def get_character_seed(story_title, character_name, page_number):
|
|
| 263 |
|
| 264 |
return character_seeds[story_title][seed_key]
|
| 265 |
|
| 266 |
-
def
|
| 267 |
-
"""Generate a single page -
|
| 268 |
-
global current_pipe, current_model_name
|
| 269 |
-
|
| 270 |
try:
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
else:
|
| 276 |
-
print(f"โ
Using already loaded model: {model_choice}")
|
| 277 |
|
| 278 |
enhanced_prompt, negative_prompt = enhance_prompt(
|
| 279 |
scene_visual, characters, style, sequence_number
|
| 280 |
)
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
if characters:
|
| 285 |
-
char_names = []
|
| 286 |
-
for char in characters:
|
| 287 |
-
if hasattr(char, 'name'):
|
| 288 |
-
char_names.append(char.name)
|
| 289 |
-
elif isinstance(char, dict):
|
| 290 |
-
char_names.append(char.get('name', 'unknown'))
|
| 291 |
-
print(f"๐ค Characters: {char_names}")
|
| 292 |
-
|
| 293 |
-
generator = torch.Generator(device="cpu")
|
| 294 |
-
|
| 295 |
if characters:
|
| 296 |
first_char = characters[0]
|
| 297 |
-
|
| 298 |
-
main_char_seed = get_character_seed(story_title, char_name, sequence_number)
|
| 299 |
-
generator.manual_seed(main_char_seed)
|
| 300 |
-
print(f"๐ฑ Using seed {main_char_seed} for {char_name}")
|
| 301 |
-
else:
|
| 302 |
-
scene_seed = hash(f"{story_title}_{sequence_number}") % 1000000
|
| 303 |
-
generator.manual_seed(scene_seed)
|
| 304 |
|
| 305 |
-
#
|
| 306 |
-
|
| 307 |
-
|
|
|
|
| 308 |
|
|
|
|
|
|
|
| 309 |
image = current_pipe(
|
| 310 |
prompt=enhanced_prompt,
|
| 311 |
negative_prompt=negative_prompt,
|
| 312 |
-
num_inference_steps=
|
| 313 |
guidance_scale=7.0,
|
| 314 |
-
width=512, #
|
| 315 |
height=512,
|
| 316 |
generator=generator
|
| 317 |
).images[0]
|
| 318 |
|
| 319 |
-
gen_time = time.time() - start_time
|
| 320 |
-
print(f"โ
Image generated in {gen_time:.1f} seconds")
|
| 321 |
-
|
| 322 |
save_status = save_complete_storybook_page(image, story_title, sequence_number, scene_text)
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
except Exception as e:
|
| 326 |
-
return
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
results = []
|
| 333 |
-
status_messages = []
|
| 334 |
-
|
| 335 |
-
print(f"๐ Starting OPTIMIZED batch generation: {story_title}")
|
| 336 |
-
print(f"๐ Pages: {len(scenes_data)}")
|
| 337 |
-
print(f"๐ค Characters: {len(characters)}")
|
| 338 |
-
|
| 339 |
-
# Load model ONCE at the beginning
|
| 340 |
-
print(f"๐ง Loading model once for entire batch...")
|
| 341 |
-
current_pipe = load_model(model_choice)
|
| 342 |
-
batch_start_time = time.time()
|
| 343 |
-
|
| 344 |
-
for i, scene_data in enumerate(scenes_data, 1):
|
| 345 |
-
try:
|
| 346 |
-
scene_visual = scene_data.get('visual', '')
|
| 347 |
-
scene_text = scene_data.get('text', '')
|
| 348 |
-
|
| 349 |
-
print(f"๐ Generating page {i}/{len(scenes_data)}...")
|
| 350 |
-
page_start_time = time.time()
|
| 351 |
-
|
| 352 |
-
image, status = generate_storybook_page(
|
| 353 |
-
scene_visual, story_title, i, scene_text, characters, model_choice, style
|
| 354 |
-
)
|
| 355 |
-
|
| 356 |
-
page_time = time.time() - page_start_time
|
| 357 |
-
print(f"โฐ Page {i} completed in {page_time:.1f} seconds")
|
| 358 |
-
|
| 359 |
-
if image:
|
| 360 |
-
results.append((f"Page {i}", image, scene_text))
|
| 361 |
-
status_messages.append(f"Page {i}: {status}")
|
| 362 |
-
|
| 363 |
-
# Clean memory every 3 pages
|
| 364 |
-
if i % 3 == 0:
|
| 365 |
-
cleanup_memory()
|
| 366 |
-
|
| 367 |
-
except Exception as e:
|
| 368 |
-
error_msg = f"โ Failed page {i}: {str(e)}"
|
| 369 |
-
print(error_msg)
|
| 370 |
-
status_messages.append(error_msg)
|
| 371 |
-
|
| 372 |
-
total_time = time.time() - batch_start_time
|
| 373 |
-
print(f"โ
Batch completed in {total_time:.2f} seconds")
|
| 374 |
-
print(f"๐ Average: {total_time/len(scenes_data):.1f} seconds per page")
|
| 375 |
-
|
| 376 |
-
return results, "\n".join(status_messages)
|
| 377 |
|
| 378 |
-
# FastAPI endpoint
|
| 379 |
-
@app.post("/api/generate-storybook")
|
| 380 |
async def api_generate_storybook(request: StorybookRequest):
|
|
|
|
| 381 |
try:
|
| 382 |
print(f"๐ Received request: {request.story_title}")
|
| 383 |
print(f"๐ Pages: {len(request.scenes)}")
|
| 384 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
start_time = time.time()
|
| 386 |
-
scenes_data = [{"visual": scene.visual, "text": scene.text} for scene in request.scenes]
|
| 387 |
|
| 388 |
-
#
|
|
|
|
|
|
|
|
|
|
| 389 |
characters_dict = []
|
| 390 |
for char in request.characters:
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
"description": getattr(char, 'description', '')})
|
| 396 |
|
| 397 |
-
results
|
| 398 |
-
|
| 399 |
-
scenes_data,
|
| 400 |
-
characters_dict,
|
| 401 |
-
request.model_choice,
|
| 402 |
-
request.style
|
| 403 |
-
)
|
| 404 |
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
"story_title": request.story_title,
|
| 410 |
-
"total_pages": len(request.scenes),
|
| 411 |
-
"characters_used": len(request.characters),
|
| 412 |
-
"generated_pages": len(results),
|
| 413 |
-
"generation_time": round(generation_time, 2),
|
| 414 |
-
"message": status,
|
| 415 |
-
"folder_path": f"storybook-library/stories/{request.story_title.replace(' ', '_')}/",
|
| 416 |
-
"pages": [
|
| 417 |
-
{
|
| 418 |
-
"page_number": i+1,
|
| 419 |
-
"image_file": f"page_{i+1:03d}_{request.story_title.replace(' ', '_')}.png",
|
| 420 |
-
"text_file": f"page_{i+1:03d}_{request.story_title.replace(' ', '_')}.txt"
|
| 421 |
-
} for i in range(len(request.scenes))
|
| 422 |
-
]
|
| 423 |
-
}
|
| 424 |
|
| 425 |
except Exception as e:
|
| 426 |
-
|
| 427 |
-
print(f"โ {error_msg}")
|
| 428 |
-
raise HTTPException(status_code=500, detail=error_msg)
|
| 429 |
|
| 430 |
@app.get("/api/health")
|
| 431 |
async def health_check():
|
|
@@ -438,49 +392,23 @@ async def health_check():
|
|
| 438 |
"current_model": current_model_name
|
| 439 |
}
|
| 440 |
|
| 441 |
-
# Gradio
|
| 442 |
-
def generate_single_page(prompt, story_title, scene_text, model_choice, style):
|
| 443 |
-
if not prompt or not story_title:
|
| 444 |
-
return None, "โ Please enter both scene description and story title"
|
| 445 |
-
|
| 446 |
-
global current_pipe
|
| 447 |
-
if current_model_name != model_choice:
|
| 448 |
-
current_pipe = load_model(model_choice)
|
| 449 |
-
|
| 450 |
-
image, status = generate_storybook_page(
|
| 451 |
-
prompt, story_title, 1, scene_text or "", [], model_choice, style
|
| 452 |
-
)
|
| 453 |
-
return image, status
|
| 454 |
-
|
| 455 |
with gr.Blocks(title="Storybook Generator", theme="soft") as demo:
|
| 456 |
gr.Markdown("# ๐ Storybook Generator")
|
| 457 |
-
gr.Markdown("Create beautiful storybooks with consistent characters")
|
| 458 |
|
| 459 |
with gr.Row():
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
value="sdxl"
|
| 466 |
-
)
|
| 467 |
-
style_choice = gr.Dropdown(
|
| 468 |
-
label="Art Style",
|
| 469 |
-
choices=["childrens_book", "realistic", "fantasy", "anime"],
|
| 470 |
-
value="childrens_book"
|
| 471 |
-
)
|
| 472 |
-
|
| 473 |
-
with gr.Column(scale=2):
|
| 474 |
-
prompt_input = gr.Textbox(label="Visual Description", lines=5)
|
| 475 |
-
text_input = gr.Textbox(label="Story Text (Optional)", lines=2)
|
| 476 |
-
generate_btn = gr.Button("โจ Generate Single Page", variant="primary")
|
| 477 |
-
image_output = gr.Image(label="Generated Page", height=400)
|
| 478 |
-
status_output = gr.Textbox(label="Status", interactive=False)
|
| 479 |
|
| 480 |
generate_btn.click(
|
| 481 |
-
fn=generate_single_page
|
| 482 |
-
|
| 483 |
-
|
|
|
|
|
|
|
| 484 |
)
|
| 485 |
|
| 486 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import requests
|
| 7 |
import os
|
| 8 |
from datetime import datetime
|
| 9 |
import re
|
|
|
|
| 10 |
import time
|
| 11 |
+
from typing import List, Optional
|
|
|
|
|
|
|
| 12 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 13 |
from pydantic import BaseModel
|
|
|
|
| 14 |
import gc
|
| 15 |
import psutil
|
| 16 |
import threading
|
| 17 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 18 |
|
| 19 |
# External OCI API URL
|
| 20 |
OCI_API_BASE_URL = "https://yukee1992-oci-story-book.hf.space"
|
|
|
|
| 45 |
story_title: str
|
| 46 |
scenes: List[StoryScene]
|
| 47 |
characters: List[CharacterDescription] = []
|
| 48 |
+
model_choice: str = "dreamshaper-8"
|
| 49 |
style: str = "childrens_book"
|
| 50 |
|
| 51 |
+
class StorybookResponse(BaseModel):
|
| 52 |
+
status: str
|
| 53 |
+
story_title: str
|
| 54 |
+
total_pages: int
|
| 55 |
+
characters_used: int
|
| 56 |
+
generated_pages: int
|
| 57 |
+
generation_time: float
|
| 58 |
+
message: str
|
| 59 |
+
folder_path: str
|
| 60 |
+
pages: List[dict]
|
| 61 |
+
|
| 62 |
# MODEL SELECTION
|
| 63 |
MODEL_CHOICES = {
|
|
|
|
|
|
|
| 64 |
"dreamshaper-8": "lykon/dreamshaper-8",
|
| 65 |
"realistic-vision": "SG161222/Realistic_Vision_V5.1",
|
| 66 |
}
|
| 67 |
|
| 68 |
+
# GLOBAL MODEL CACHE
|
| 69 |
model_cache = {}
|
| 70 |
current_model_name = None
|
| 71 |
current_pipe = None
|
| 72 |
model_lock = threading.Lock()
|
| 73 |
|
| 74 |
# Character consistency tracking
|
|
|
|
| 75 |
character_seeds = {}
|
| 76 |
|
| 77 |
+
# Thread pool for parallel processing
|
| 78 |
+
executor = ThreadPoolExecutor(max_workers=2)
|
| 79 |
+
|
| 80 |
def monitor_memory():
|
| 81 |
try:
|
| 82 |
process = psutil.Process()
|
|
|
|
| 89 |
if torch.cuda.is_available():
|
| 90 |
torch.cuda.empty_cache()
|
| 91 |
|
| 92 |
+
def load_model(model_name="dreamshaper-8"):
|
| 93 |
+
"""Thread-safe model loading"""
|
| 94 |
global model_cache, current_model_name, current_pipe
|
| 95 |
|
| 96 |
with model_lock:
|
| 97 |
if model_name in model_cache:
|
|
|
|
| 98 |
current_pipe = model_cache[model_name]
|
| 99 |
current_model_name = model_name
|
| 100 |
return current_pipe
|
| 101 |
|
| 102 |
print(f"๐ Loading model: {model_name}")
|
| 103 |
try:
|
| 104 |
+
model_id = MODEL_CHOICES.get(model_name, "lykon/dreamshaper-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 107 |
+
model_id,
|
| 108 |
+
torch_dtype=torch.float32,
|
| 109 |
+
safety_checker=None,
|
| 110 |
+
requires_safety_checker=False
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 114 |
pipe = pipe.to("cpu")
|
| 115 |
+
|
| 116 |
model_cache[model_name] = pipe
|
| 117 |
current_pipe = pipe
|
| 118 |
current_model_name = model_name
|
| 119 |
|
| 120 |
+
print(f"โ
Model loaded: {model_name}")
|
| 121 |
return pipe
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
print(f"โ Model loading failed: {e}")
|
| 125 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# Initialize default model
|
| 128 |
print("๐ Initializing Storybook Generator...")
|
| 129 |
+
load_model("dreamshaper-8")
|
| 130 |
print("โ
Model loaded and ready!")
|
| 131 |
|
| 132 |
+
# CRITICAL: PROMPT OPTIMIZATION THAT ACTUALLY WORKS
|
| 133 |
+
def optimize_prompt(scene_visual, characters, style="childrens_book", page_number=1):
|
| 134 |
+
"""
|
| 135 |
+
Create a prompt that FITS within 77 tokens while preserving the ESSENCE
|
| 136 |
+
"""
|
| 137 |
+
# Extract ONLY the most critical information
|
| 138 |
+
character_essence = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
if characters:
|
| 140 |
+
char_descriptors = []
|
| 141 |
for char in characters:
|
| 142 |
+
desc = char.get('description', '') if isinstance(char, dict) else getattr(char, 'description', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Extract ONLY: species + color + 1 key feature
|
| 145 |
import re
|
|
|
|
| 146 |
species_match = re.search(r'(rabbit|hedgehog|bird|dog|cat|fox|bear|dragon|human|girl|boy)', desc, re.IGNORECASE)
|
| 147 |
species = species_match.group(1) if species_match else "character"
|
| 148 |
|
|
|
|
| 149 |
color_match = re.search(r'(white|black|brown|blue|red|green|yellow|golden|pink)', desc, re.IGNORECASE)
|
| 150 |
color = color_match.group(1) if color_match else ""
|
| 151 |
|
| 152 |
+
# Find one key feature
|
| 153 |
+
key_feature = ""
|
| 154 |
+
if 'glasses' in desc.lower(): key_feature = "with glasses"
|
| 155 |
+
elif 'dress' in desc.lower(): key_feature = "in dress"
|
| 156 |
+
elif 'hat' in desc.lower(): key_feature = "with hat"
|
| 157 |
+
|
| 158 |
+
char_descriptors.append(f"{color} {species} {key_feature}".strip())
|
| 159 |
+
|
| 160 |
+
character_essence = f"Features: {', '.join(char_descriptors)}. "
|
| 161 |
|
| 162 |
+
# Compress scene description to MAX 40 words
|
| 163 |
+
scene_words = scene_visual.split()
|
| 164 |
+
if len(scene_words) > 40:
|
| 165 |
+
scene_compressed = ' '.join(scene_words[:40])
|
| 166 |
+
else:
|
| 167 |
+
scene_compressed = scene_visual
|
| 168 |
|
| 169 |
+
# Style context (very brief)
|
| 170 |
+
style_context = {
|
| 171 |
+
"childrens_book": "children's book illustration",
|
| 172 |
+
"realistic": "photorealistic",
|
| 173 |
+
"fantasy": "fantasy art",
|
| 174 |
+
"anime": "anime style"
|
| 175 |
+
}.get(style, "children's book illustration")
|
| 176 |
+
|
| 177 |
+
# Build the final prompt (GUARANTEED to fit 77 tokens)
|
| 178 |
+
continuity = f"Scene {page_number}: " if page_number > 1 else ""
|
| 179 |
+
final_prompt = f"{continuity}{scene_compressed}. {character_essence}{style_context}. masterpiece, best quality"
|
| 180 |
|
| 181 |
# Ensure it's under 60 words
|
| 182 |
words = final_prompt.split()
|
| 183 |
if len(words) > 60:
|
| 184 |
final_prompt = ' '.join(words[:60])
|
| 185 |
|
| 186 |
+
print(f"๐ Optimized prompt: {final_prompt}")
|
| 187 |
+
print(f"๐ Length: {len(final_prompt.split())} words")
|
| 188 |
+
|
| 189 |
return final_prompt
|
| 190 |
|
| 191 |
def enhance_prompt(scene_visual, characters, style="childrens_book", page_number=1):
|
| 192 |
+
"""Create optimized prompt that WILL work"""
|
| 193 |
+
main_prompt = optimize_prompt(scene_visual, characters, style, page_number)
|
|
|
|
|
|
|
|
|
|
| 194 |
|
|
|
|
| 195 |
negative_prompt = (
|
| 196 |
"blurry, low quality, ugly, deformed, bad anatomy, "
|
| 197 |
+
"watermark, text, username, multiple people, inconsistent"
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
return main_prompt, negative_prompt
|
|
|
|
| 243 |
|
| 244 |
return character_seeds[story_title][seed_key]
|
| 245 |
|
| 246 |
+
def generate_single_page(scene_data, story_title, sequence_number, characters, model_choice, style):
|
| 247 |
+
"""Generate a single page - isolated for error handling"""
|
|
|
|
|
|
|
| 248 |
try:
|
| 249 |
+
scene_visual = scene_data.get('visual', '')
|
| 250 |
+
scene_text = scene_data.get('text', '')
|
| 251 |
+
|
| 252 |
+
print(f"๐ Generating page {sequence_number}...")
|
|
|
|
|
|
|
| 253 |
|
| 254 |
enhanced_prompt, negative_prompt = enhance_prompt(
|
| 255 |
scene_visual, characters, style, sequence_number
|
| 256 |
)
|
| 257 |
|
| 258 |
+
# Get character name for seed
|
| 259 |
+
main_char_name = "default"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
if characters:
|
| 261 |
first_char = characters[0]
|
| 262 |
+
main_char_name = first_char.get('name', 'default') if isinstance(first_char, dict) else getattr(first_char, 'name', 'default')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
# Use consistent seed
|
| 265 |
+
generator = torch.Generator(device="cpu")
|
| 266 |
+
main_char_seed = get_character_seed(story_title, main_char_name, sequence_number)
|
| 267 |
+
generator.manual_seed(main_char_seed)
|
| 268 |
|
| 269 |
+
# Generate with current pipe (already loaded)
|
| 270 |
+
global current_pipe
|
| 271 |
image = current_pipe(
|
| 272 |
prompt=enhanced_prompt,
|
| 273 |
negative_prompt=negative_prompt,
|
| 274 |
+
num_inference_steps=20, # Faster generation
|
| 275 |
guidance_scale=7.0,
|
| 276 |
+
width=512, # Smaller for speed
|
| 277 |
height=512,
|
| 278 |
generator=generator
|
| 279 |
).images[0]
|
| 280 |
|
|
|
|
|
|
|
|
|
|
| 281 |
save_status = save_complete_storybook_page(image, story_title, sequence_number, scene_text)
|
| 282 |
+
|
| 283 |
+
return {
|
| 284 |
+
"success": True,
|
| 285 |
+
"page_number": sequence_number,
|
| 286 |
+
"image": image,
|
| 287 |
+
"status": save_status
|
| 288 |
+
}
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
+
return {
|
| 292 |
+
"success": False,
|
| 293 |
+
"page_number": sequence_number,
|
| 294 |
+
"error": f"Generation failed: {str(e)}"
|
| 295 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
+
# FastAPI endpoint - OPTIMIZED
|
| 298 |
+
@app.post("/api/generate-storybook", response_model=StorybookResponse)
|
| 299 |
async def api_generate_storybook(request: StorybookRequest):
|
| 300 |
+
"""API endpoint that WON'T timeout"""
|
| 301 |
try:
|
| 302 |
print(f"๐ Received request: {request.story_title}")
|
| 303 |
print(f"๐ Pages: {len(request.scenes)}")
|
| 304 |
|
| 305 |
+
# IMMEDIATE response to n8n to prevent timeout
|
| 306 |
+
response_data = {
|
| 307 |
+
"status": "processing",
|
| 308 |
+
"story_title": request.story_title,
|
| 309 |
+
"total_pages": len(request.scenes),
|
| 310 |
+
"characters_used": len(request.characters),
|
| 311 |
+
"generated_pages": 0,
|
| 312 |
+
"generation_time": 0,
|
| 313 |
+
"message": "Processing started in background",
|
| 314 |
+
"folder_path": f"storybook-library/stories/{request.story_title.replace(' ', '_')}/",
|
| 315 |
+
"pages": []
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
# Start background processing
|
| 319 |
+
background_tasks = BackgroundTasks()
|
| 320 |
+
background_tasks.add_task(process_storybook_background, request)
|
| 321 |
+
|
| 322 |
+
return response_data
|
| 323 |
+
|
| 324 |
+
except Exception as e:
|
| 325 |
+
raise HTTPException(status_code=500, detail=f"Request failed: {str(e)}")
|
| 326 |
+
|
| 327 |
+
def process_storybook_background(request):
|
| 328 |
+
"""Background processing to avoid timeouts"""
|
| 329 |
+
try:
|
| 330 |
start_time = time.time()
|
|
|
|
| 331 |
|
| 332 |
+
# Load model ONCE
|
| 333 |
+
load_model(request.model_choice)
|
| 334 |
+
|
| 335 |
+
# Convert characters to dict
|
| 336 |
characters_dict = []
|
| 337 |
for char in request.characters:
|
| 338 |
+
characters_dict.append({
|
| 339 |
+
"name": char.name,
|
| 340 |
+
"description": char.description
|
| 341 |
+
})
|
|
|
|
| 342 |
|
| 343 |
+
results = []
|
| 344 |
+
status_messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
+
# Process each page SEQUENTIALLY (better for memory)
|
| 347 |
+
for i, scene in enumerate(request.scenes, 1):
|
| 348 |
+
try:
|
| 349 |
+
result = generate_single_page(
|
| 350 |
+
{"visual": scene.visual, "text": scene.text},
|
| 351 |
+
request.story_title,
|
| 352 |
+
i,
|
| 353 |
+
characters_dict,
|
| 354 |
+
request.model_choice,
|
| 355 |
+
request.style
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
if result["success"]:
|
| 359 |
+
results.append(result)
|
| 360 |
+
status_messages.append(f"Page {i}: {result['status']}")
|
| 361 |
+
print(f"โ
Page {i} completed successfully")
|
| 362 |
+
else:
|
| 363 |
+
status_messages.append(f"Page {i}: {result['error']}")
|
| 364 |
+
print(f"โ Page {i} failed: {result['error']}")
|
| 365 |
+
|
| 366 |
+
# Clean memory after each page
|
| 367 |
+
cleanup_memory()
|
| 368 |
+
|
| 369 |
+
# Add small delay to prevent resource exhaustion
|
| 370 |
+
if i < len(request.scenes):
|
| 371 |
+
time.sleep(2)
|
| 372 |
+
|
| 373 |
+
except Exception as e:
|
| 374 |
+
error_msg = f"Page {i} failed: {str(e)}"
|
| 375 |
+
status_messages.append(error_msg)
|
| 376 |
+
print(f"โ {error_msg}")
|
| 377 |
|
| 378 |
+
total_time = time.time() - start_time
|
| 379 |
+
print(f"โ
Background processing completed in {total_time:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
except Exception as e:
|
| 382 |
+
print(f"โ Background processing failed: {str(e)}")
|
|
|
|
|
|
|
| 383 |
|
| 384 |
@app.get("/api/health")
|
| 385 |
async def health_check():
|
|
|
|
| 392 |
"current_model": current_model_name
|
| 393 |
}
|
| 394 |
|
| 395 |
+
# Simple Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
with gr.Blocks(title="Storybook Generator", theme="soft") as demo:
|
| 397 |
gr.Markdown("# ๐ Storybook Generator")
|
|
|
|
| 398 |
|
| 399 |
with gr.Row():
|
| 400 |
+
story_title = gr.Textbox(label="Story Title")
|
| 401 |
+
prompt_input = gr.Textbox(label="Scene Description", lines=3)
|
| 402 |
+
generate_btn = gr.Button("Generate")
|
| 403 |
+
output_image = gr.Image()
|
| 404 |
+
status = gr.Textbox()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
|
| 406 |
generate_btn.click(
|
| 407 |
+
fn=lambda p, t: generate_single_page(
|
| 408 |
+
{"visual": p, "text": ""}, t, 1, [], "dreamshaper-8", "childrens_book"
|
| 409 |
+
),
|
| 410 |
+
inputs=[prompt_input, story_title],
|
| 411 |
+
outputs=[output_image, status]
|
| 412 |
)
|
| 413 |
|
| 414 |
app = gr.mount_gradio_app(app, demo, path="/")
|