Munaf1987 commited on
Commit
7499b76
·
verified ·
1 Parent(s): 9ed4fc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -68
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
- prompt = f"""
422
- anime style, professional cartoon character design, {character['description']},
423
- character sheet style, multiple poses reference, clean white background,
424
- 2D animation model sheet, Disney-Pixar quality, highly detailed,
425
- consistent character design, expressive face, perfect for animation,
426
- {character.get('animation_style', 'high-quality character design')}
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
- if hasattr(self.flux_pipe, 'max_sequence_length'):
436
- # FLUX pipeline
437
- image = self.flux_pipe(
438
- prompt=prompt,
439
- negative_prompt=negative_prompt,
440
- num_inference_steps=25, # High quality steps
441
- guidance_scale=3.5,
442
- height=1024, # High resolution
443
- width=1024,
444
- max_sequence_length=256
445
- ).images[0]
446
- else:
447
- # Stable Diffusion pipeline
448
- image = self.flux_pipe(
449
- prompt=prompt,
450
- negative_prompt=negative_prompt,
451
- num_inference_steps=25, # High quality steps
452
- guidance_scale=7.5,
453
- height=1024, # High resolution
454
- width=1024
455
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = f"""
484
- Professional cartoon background art, {scene['background']},
485
- {scene['mood']} atmosphere, {color_palette} color palette,
486
- cinematic composition, {scene.get('shot_type', 'medium shot')},
487
- no characters, detailed environment art, Disney-Pixar quality backgrounds,
488
- 2D animation background, highly detailed, perfect lighting,
489
- {scene.get('animation_notes', 'professional background art')}
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
- if hasattr(self.flux_pipe, 'max_sequence_length'):
499
- # FLUX pipeline
500
- image = self.flux_pipe(
501
- prompt=prompt,
502
- negative_prompt=negative_prompt,
503
- num_inference_steps=20,
504
- guidance_scale=3.0,
505
- height=768, # 4:3 aspect ratio for traditional animation
506
- width=1024,
507
- max_sequence_length=256
508
- ).images[0]
509
- else:
510
- # Stable Diffusion pipeline
511
- image = self.flux_pipe(
512
- prompt=prompt,
513
- negative_prompt=negative_prompt,
514
- num_inference_steps=20,
515
- guidance_scale=7.0,
516
- height=768, # 4:3 aspect ratio for traditional animation
517
- width=1024
518
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- prompt = f"""
642
- Professional 2D cartoon animation, {characters_text} in {scene['background']},
643
- {scene['mood']} mood, {scene.get('shot_type', 'medium shot')},
644
- smooth character animation, Disney-Pixar quality, cinematic lighting,
645
- expressive character movement, detailed background art, family-friendly,
646
- {scene.get('animation_notes', 'high-quality animation')}
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