Update app.py
Browse files
app.py
CHANGED
@@ -152,6 +152,27 @@ class ProfessionalCartoonFilmGenerator:
|
|
152 |
torch.cuda.empty_cache()
|
153 |
gc.collect()
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
def generate_professional_script(self, user_input: str) -> Dict[str, Any]:
|
156 |
"""Generate a professional cartoon script with detailed character development"""
|
157 |
|
@@ -417,42 +438,71 @@ class ProfessionalCartoonFilmGenerator:
|
|
417 |
except Exception as e:
|
418 |
print(f"⚠️ LoRA loading failed: {e}")
|
419 |
|
420 |
-
# Professional character prompt
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
|
429 |
negative_prompt = """
|
430 |
realistic, 3D render, dark, scary, inappropriate, low quality, blurry,
|
431 |
inconsistent, amateur, simple, crude, manga, sketch
|
432 |
"""
|
433 |
|
434 |
-
# Handle different pipeline types
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
456 |
|
457 |
char_path = f"{self.temp_dir}/character_{character['name'].replace(' ', '_')}.png"
|
458 |
image.save(char_path)
|
@@ -480,42 +530,73 @@ class ProfessionalCartoonFilmGenerator:
|
|
480 |
try:
|
481 |
print(f"🏞️ Creating cinematic background for scene {scene['scene_number']}")
|
482 |
|
483 |
-
prompt
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
{
|
490 |
-
|
|
|
|
|
491 |
|
492 |
negative_prompt = """
|
493 |
characters, people, animals, realistic, dark, scary, low quality,
|
494 |
blurry, simple, amateur, 3D render
|
495 |
"""
|
496 |
|
497 |
-
# Handle different pipeline types for backgrounds
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
519 |
|
520 |
bg_path = f"{self.temp_dir}/background_scene_{scene['scene_number']}.png"
|
521 |
image.save(bg_path)
|
@@ -637,14 +718,17 @@ class ProfessionalCartoonFilmGenerator:
|
|
637 |
try:
|
638 |
characters_text = ", ".join(scene['characters_present'])
|
639 |
|
640 |
-
# Professional prompt for Open-Sora
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
""
|
|
|
|
|
|
|
648 |
|
649 |
video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
|
650 |
|
|
|
152 |
torch.cuda.empty_cache()
|
153 |
gc.collect()
|
154 |
|
155 |
+
def optimize_prompt_for_clip(self, prompt: str, max_tokens: int = 70) -> str:
|
156 |
+
"""Optimize prompt to fit within CLIP token limit"""
|
157 |
+
try:
|
158 |
+
# Simple word-based token estimation (CLIP uses ~1.3 words per token)
|
159 |
+
words = prompt.split()
|
160 |
+
if len(words) <= max_tokens:
|
161 |
+
return prompt
|
162 |
+
|
163 |
+
# Truncate to fit within token limit
|
164 |
+
optimized_words = words[:max_tokens]
|
165 |
+
optimized_prompt = " ".join(optimized_words)
|
166 |
+
|
167 |
+
print(f"📝 Prompt optimized: {len(words)} words → {len(optimized_words)} words")
|
168 |
+
return optimized_prompt
|
169 |
+
|
170 |
+
except Exception as e:
|
171 |
+
print(f"⚠️ Prompt optimization failed: {e}")
|
172 |
+
# Fallback: return first 50 words
|
173 |
+
words = prompt.split()
|
174 |
+
return " ".join(words[:50])
|
175 |
+
|
176 |
def generate_professional_script(self, user_input: str) -> Dict[str, Any]:
|
177 |
"""Generate a professional cartoon script with detailed character development"""
|
178 |
|
|
|
438 |
except Exception as e:
|
439 |
print(f"⚠️ LoRA loading failed: {e}")
|
440 |
|
441 |
+
# Professional character prompt (optimized for CLIP token limit)
|
442 |
+
character_desc = character['description'][:100] # Limit description length
|
443 |
+
animation_style = character.get('animation_style', 'high-quality character design')[:50]
|
444 |
+
|
445 |
+
prompt = f"anime style, professional cartoon character, {character_desc}, character sheet, clean background, 2D animation, Disney quality, detailed, {animation_style}"
|
446 |
+
|
447 |
+
# Use the optimization function to ensure CLIP compatibility
|
448 |
+
prompt = self.optimize_prompt_for_clip(prompt)
|
449 |
|
450 |
negative_prompt = """
|
451 |
realistic, 3D render, dark, scary, inappropriate, low quality, blurry,
|
452 |
inconsistent, amateur, simple, crude, manga, sketch
|
453 |
"""
|
454 |
|
455 |
+
# Handle different pipeline types with CLIP token error handling
|
456 |
+
try:
|
457 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
458 |
+
# FLUX pipeline
|
459 |
+
image = self.flux_pipe(
|
460 |
+
prompt=prompt,
|
461 |
+
negative_prompt=negative_prompt,
|
462 |
+
num_inference_steps=25, # High quality steps
|
463 |
+
guidance_scale=3.5,
|
464 |
+
height=1024, # High resolution
|
465 |
+
width=1024,
|
466 |
+
max_sequence_length=256
|
467 |
+
).images[0]
|
468 |
+
else:
|
469 |
+
# Stable Diffusion pipeline
|
470 |
+
image = self.flux_pipe(
|
471 |
+
prompt=prompt,
|
472 |
+
negative_prompt=negative_prompt,
|
473 |
+
num_inference_steps=25, # High quality steps
|
474 |
+
guidance_scale=7.5,
|
475 |
+
height=1024, # High resolution
|
476 |
+
width=1024
|
477 |
+
).images[0]
|
478 |
+
except Exception as e:
|
479 |
+
if "CLIP" in str(e) and "token" in str(e).lower():
|
480 |
+
print(f"⚠️ CLIP token error detected, using simplified prompt...")
|
481 |
+
# Fallback to very simple prompt
|
482 |
+
simple_prompt = f"anime character, {character['name']}, clean background"
|
483 |
+
simple_prompt = self.optimize_prompt_for_clip(simple_prompt, max_tokens=30)
|
484 |
+
|
485 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
486 |
+
image = self.flux_pipe(
|
487 |
+
prompt=simple_prompt,
|
488 |
+
negative_prompt="low quality, blurry",
|
489 |
+
num_inference_steps=20,
|
490 |
+
guidance_scale=3.0,
|
491 |
+
height=1024,
|
492 |
+
width=1024,
|
493 |
+
max_sequence_length=128
|
494 |
+
).images[0]
|
495 |
+
else:
|
496 |
+
image = self.flux_pipe(
|
497 |
+
prompt=simple_prompt,
|
498 |
+
negative_prompt="low quality, blurry",
|
499 |
+
num_inference_steps=20,
|
500 |
+
guidance_scale=7.0,
|
501 |
+
height=1024,
|
502 |
+
width=1024
|
503 |
+
).images[0]
|
504 |
+
else:
|
505 |
+
raise e
|
506 |
|
507 |
char_path = f"{self.temp_dir}/character_{character['name'].replace(' ', '_')}.png"
|
508 |
image.save(char_path)
|
|
|
530 |
try:
|
531 |
print(f"🏞️ Creating cinematic background for scene {scene['scene_number']}")
|
532 |
|
533 |
+
# Professional background prompt (optimized for CLIP token limit)
|
534 |
+
background_desc = scene['background'][:80] # Limit background description
|
535 |
+
mood = scene['mood'][:30]
|
536 |
+
shot_type = scene.get('shot_type', 'medium shot')[:20]
|
537 |
+
animation_notes = scene.get('animation_notes', 'professional background art')[:40]
|
538 |
+
|
539 |
+
prompt = f"Professional cartoon background, {background_desc}, {mood} atmosphere, {color_palette} colors, {shot_type}, no characters, detailed environment, Disney quality, {animation_notes}"
|
540 |
+
|
541 |
+
# Use the optimization function to ensure CLIP compatibility
|
542 |
+
prompt = self.optimize_prompt_for_clip(prompt)
|
543 |
|
544 |
negative_prompt = """
|
545 |
characters, people, animals, realistic, dark, scary, low quality,
|
546 |
blurry, simple, amateur, 3D render
|
547 |
"""
|
548 |
|
549 |
+
# Handle different pipeline types for backgrounds with CLIP token error handling
|
550 |
+
try:
|
551 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
552 |
+
# FLUX pipeline
|
553 |
+
image = self.flux_pipe(
|
554 |
+
prompt=prompt,
|
555 |
+
negative_prompt=negative_prompt,
|
556 |
+
num_inference_steps=20,
|
557 |
+
guidance_scale=3.0,
|
558 |
+
height=768, # 4:3 aspect ratio for traditional animation
|
559 |
+
width=1024,
|
560 |
+
max_sequence_length=256
|
561 |
+
).images[0]
|
562 |
+
else:
|
563 |
+
# Stable Diffusion pipeline
|
564 |
+
image = self.flux_pipe(
|
565 |
+
prompt=prompt,
|
566 |
+
negative_prompt=negative_prompt,
|
567 |
+
num_inference_steps=20,
|
568 |
+
guidance_scale=7.0,
|
569 |
+
height=768, # 4:3 aspect ratio for traditional animation
|
570 |
+
width=1024
|
571 |
+
).images[0]
|
572 |
+
except Exception as e:
|
573 |
+
if "CLIP" in str(e) and "token" in str(e).lower():
|
574 |
+
print(f"⚠️ CLIP token error detected for background, using simplified prompt...")
|
575 |
+
# Fallback to very simple prompt
|
576 |
+
simple_prompt = f"cartoon background, {scene['background'][:40]}, clean"
|
577 |
+
simple_prompt = self.optimize_prompt_for_clip(simple_prompt, max_tokens=25)
|
578 |
+
|
579 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
580 |
+
image = self.flux_pipe(
|
581 |
+
prompt=simple_prompt,
|
582 |
+
negative_prompt="characters, low quality",
|
583 |
+
num_inference_steps=15,
|
584 |
+
guidance_scale=3.0,
|
585 |
+
height=768,
|
586 |
+
width=1024,
|
587 |
+
max_sequence_length=128
|
588 |
+
).images[0]
|
589 |
+
else:
|
590 |
+
image = self.flux_pipe(
|
591 |
+
prompt=simple_prompt,
|
592 |
+
negative_prompt="characters, low quality",
|
593 |
+
num_inference_steps=15,
|
594 |
+
guidance_scale=7.0,
|
595 |
+
height=768,
|
596 |
+
width=1024
|
597 |
+
).images[0]
|
598 |
+
else:
|
599 |
+
raise e
|
600 |
|
601 |
bg_path = f"{self.temp_dir}/background_scene_{scene['scene_number']}.png"
|
602 |
image.save(bg_path)
|
|
|
718 |
try:
|
719 |
characters_text = ", ".join(scene['characters_present'])
|
720 |
|
721 |
+
# Professional prompt for Open-Sora (optimized for CLIP token limit)
|
722 |
+
characters_text = characters_text[:60] # Limit character text
|
723 |
+
background_desc = scene['background'][:60]
|
724 |
+
mood = scene['mood'][:20]
|
725 |
+
shot_type = scene.get('shot_type', 'medium shot')[:15]
|
726 |
+
animation_notes = scene.get('animation_notes', 'high-quality animation')[:30]
|
727 |
+
|
728 |
+
prompt = f"Professional 2D cartoon animation, {characters_text} in {background_desc}, {mood} mood, {shot_type}, smooth animation, Disney quality, cinematic lighting, {animation_notes}"
|
729 |
+
|
730 |
+
# Use the optimization function to ensure CLIP compatibility
|
731 |
+
prompt = self.optimize_prompt_for_clip(prompt)
|
732 |
|
733 |
video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
|
734 |
|