Munaf1987 commited on
Commit
7f65796
Β·
verified Β·
1 Parent(s): 4c3888a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -37
app.py CHANGED
@@ -17,13 +17,27 @@ from huggingface_hub import hf_hub_download
17
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
18
  from diffusers import (
19
  FluxPipeline,
20
- FluxControlNetPipeline,
21
  DDIMScheduler,
22
  DPMSolverMultistepScheduler
23
  )
24
  import soundfile as sf
25
  import requests
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  class ProfessionalCartoonFilmGenerator:
28
  def __init__(self):
29
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -76,11 +90,43 @@ class ProfessionalCartoonFilmGenerator:
76
  self.flux_pipe.enable_vae_slicing()
77
  self.flux_pipe.enable_vae_tiling()
78
 
 
 
 
 
 
 
 
 
 
 
79
  print("βœ… FLUX pipeline loaded successfully")
80
 
81
  except Exception as e:
82
  print(f"❌ FLUX pipeline failed: {e}")
83
- self.flux_pipe = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  try:
86
  # 2. Advanced script generation model
@@ -356,17 +402,20 @@ class ProfessionalCartoonFilmGenerator:
356
  character_images = {}
357
 
358
  if not self.flux_pipe:
359
- print("❌ FLUX pipeline not available")
360
  return character_images
361
 
362
  for character in characters:
363
  try:
364
  print(f"🎭 Generating professional character: {character['name']}")
365
 
366
- # Load appropriate LoRA based on character type
367
- if "anime" in character.get("animation_style", "").lower():
368
  if hasattr(self, 'cartoon_lora'):
369
- self.flux_pipe.load_lora_weights(self.cartoon_lora)
 
 
 
370
 
371
  # Professional character prompt
372
  prompt = f"""
@@ -382,15 +431,28 @@ class ProfessionalCartoonFilmGenerator:
382
  inconsistent, amateur, simple, crude, manga, sketch
383
  """
384
 
385
- image = self.flux_pipe(
386
- prompt=prompt,
387
- negative_prompt=negative_prompt,
388
- num_inference_steps=25, # High quality steps
389
- guidance_scale=3.5,
390
- height=1024, # High resolution
391
- width=1024,
392
- max_sequence_length=256
393
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
394
 
395
  char_path = f"{self.temp_dir}/character_{character['name'].replace(' ', '_')}.png"
396
  image.save(char_path)
@@ -411,7 +473,7 @@ class ProfessionalCartoonFilmGenerator:
411
  background_images = {}
412
 
413
  if not self.flux_pipe:
414
- print("❌ FLUX pipeline not available")
415
  return background_images
416
 
417
  for scene in scenes:
@@ -432,15 +494,28 @@ class ProfessionalCartoonFilmGenerator:
432
  blurry, simple, amateur, 3D render
433
  """
434
 
435
- image = self.flux_pipe(
436
- prompt=prompt,
437
- negative_prompt=negative_prompt,
438
- num_inference_steps=20,
439
- guidance_scale=3.0,
440
- height=768, # 4:3 aspect ratio for traditional animation
441
- width=1024,
442
- max_sequence_length=256
443
- ).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
444
 
445
  bg_path = f"{self.temp_dir}/background_scene_{scene['scene_number']}.png"
446
  image.save(bg_path)
@@ -459,21 +534,36 @@ class ProfessionalCartoonFilmGenerator:
459
  try:
460
  print("🎬 Setting up Open-Sora 2.0 for video generation...")
461
 
462
- # Clone Open-Sora repository
463
- if not os.path.exists("Open-Sora"):
 
 
 
 
 
464
  subprocess.run([
465
  "git", "clone", "https://github.com/hpcaitech/Open-Sora.git"
466
  ], check=True, capture_output=True)
467
 
468
- os.chdir("Open-Sora")
469
-
470
- # Download model weights
471
- print("πŸ“₯ Downloading Open-Sora 2.0 model...")
472
- subprocess.run([
473
- "huggingface-cli", "download", "hpcai-tech/Open-Sora-v2",
474
- "--local-dir", "./ckpts"
475
- ], check=True, capture_output=True)
476
-
 
 
 
 
 
 
 
 
 
 
477
  return True
478
 
479
  except Exception as e:
@@ -526,6 +616,14 @@ class ProfessionalCartoonFilmGenerator:
526
 
527
  video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
528
 
 
 
 
 
 
 
 
 
529
  # Run Open-Sora inference
530
  cmd = [
531
  "torchrun", "--nproc_per_node", "1", "--standalone",
@@ -538,7 +636,7 @@ class ProfessionalCartoonFilmGenerator:
538
  "--motion-score", "6" # High motion for dynamic scenes
539
  ]
540
 
541
- result = subprocess.run(cmd, capture_output=True, text=True, cwd="Open-Sora")
542
 
543
  if result.returncode == 0:
544
  # Find generated video file
 
17
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
18
  from diffusers import (
19
  FluxPipeline,
 
20
  DDIMScheduler,
21
  DPMSolverMultistepScheduler
22
  )
23
  import soundfile as sf
24
  import requests
25
 
26
+ # Optional imports for enhanced performance
27
+ try:
28
+ import flash_attn
29
+ FLASH_ATTN_AVAILABLE = True
30
+ except ImportError:
31
+ FLASH_ATTN_AVAILABLE = False
32
+ print("⚠️ Flash Attention not available - using standard attention")
33
+
34
+ try:
35
+ import triton
36
+ TRITON_AVAILABLE = True
37
+ except ImportError:
38
+ TRITON_AVAILABLE = False
39
+ print("⚠️ Triton not available - using standard operations")
40
+
41
  class ProfessionalCartoonFilmGenerator:
42
  def __init__(self):
43
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
 
90
  self.flux_pipe.enable_vae_slicing()
91
  self.flux_pipe.enable_vae_tiling()
92
 
93
+ # Enable flash attention if available
94
+ if FLASH_ATTN_AVAILABLE:
95
+ try:
96
+ self.flux_pipe.enable_xformers_memory_efficient_attention()
97
+ print("βœ… Flash attention enabled for better performance")
98
+ except Exception as e:
99
+ print(f"⚠️ Flash attention failed: {e}")
100
+ else:
101
+ print("ℹ️ Using standard attention (flash attention not available)")
102
+
103
  print("βœ… FLUX pipeline loaded successfully")
104
 
105
  except Exception as e:
106
  print(f"❌ FLUX pipeline failed: {e}")
107
+ print("πŸ”„ Falling back to Stable Diffusion...")
108
+
109
+ # Fallback to Stable Diffusion
110
+ try:
111
+ from diffusers import StableDiffusionPipeline
112
+ self.flux_pipe = StableDiffusionPipeline.from_pretrained(
113
+ "runwayml/stable-diffusion-v1-5",
114
+ torch_dtype=torch.float16,
115
+ use_safetensors=True,
116
+ safety_checker=None,
117
+ requires_safety_checker=False
118
+ ).to(self.device)
119
+
120
+ # Enable memory optimizations
121
+ self.flux_pipe.enable_vae_slicing()
122
+ if hasattr(self.flux_pipe, 'enable_vae_tiling'):
123
+ self.flux_pipe.enable_vae_tiling()
124
+
125
+ print("βœ… Stable Diffusion fallback loaded successfully")
126
+
127
+ except Exception as e2:
128
+ print(f"❌ Stable Diffusion fallback also failed: {e2}")
129
+ self.flux_pipe = None
130
 
131
  try:
132
  # 2. Advanced script generation model
 
402
  character_images = {}
403
 
404
  if not self.flux_pipe:
405
+ print("❌ No image generation pipeline available")
406
  return character_images
407
 
408
  for character in characters:
409
  try:
410
  print(f"🎭 Generating professional character: {character['name']}")
411
 
412
+ # Load appropriate LoRA based on character type (only for FLUX)
413
+ if hasattr(self.flux_pipe, 'load_lora_weights') and "anime" in character.get("animation_style", "").lower():
414
  if hasattr(self, 'cartoon_lora'):
415
+ try:
416
+ self.flux_pipe.load_lora_weights(self.cartoon_lora)
417
+ except Exception as e:
418
+ print(f"⚠️ LoRA loading failed: {e}")
419
 
420
  # Professional character prompt
421
  prompt = f"""
 
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)
 
473
  background_images = {}
474
 
475
  if not self.flux_pipe:
476
+ print("❌ No image generation pipeline available")
477
  return background_images
478
 
479
  for scene in scenes:
 
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)
 
534
  try:
535
  print("🎬 Setting up Open-Sora 2.0 for video generation...")
536
 
537
+ # Check if we're already in the right directory
538
+ current_dir = os.getcwd()
539
+ opensora_dir = os.path.join(current_dir, "Open-Sora")
540
+
541
+ # Clone Open-Sora repository if it doesn't exist
542
+ if not os.path.exists(opensora_dir):
543
+ print("πŸ“₯ Cloning Open-Sora repository...")
544
  subprocess.run([
545
  "git", "clone", "https://github.com/hpcaitech/Open-Sora.git"
546
  ], check=True, capture_output=True)
547
 
548
+ # Check if the repository was cloned successfully
549
+ if not os.path.exists(opensora_dir):
550
+ print("❌ Failed to clone Open-Sora repository")
551
+ return False
552
+
553
+ # Check if model weights exist
554
+ ckpts_dir = os.path.join(opensora_dir, "ckpts")
555
+ if not os.path.exists(ckpts_dir):
556
+ print("πŸ“₯ Downloading Open-Sora 2.0 model...")
557
+ try:
558
+ subprocess.run([
559
+ "huggingface-cli", "download", "hpcai-tech/Open-Sora-v2",
560
+ "--local-dir", ckpts_dir
561
+ ], check=True, capture_output=True)
562
+ except Exception as e:
563
+ print(f"❌ Model download failed: {e}")
564
+ return False
565
+
566
+ print("βœ… Open-Sora setup completed")
567
  return True
568
 
569
  except Exception as e:
 
616
 
617
  video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
618
 
619
+ # Get the correct Open-Sora directory
620
+ current_dir = os.getcwd()
621
+ opensora_dir = os.path.join(current_dir, "Open-Sora")
622
+
623
+ if not os.path.exists(opensora_dir):
624
+ print("❌ Open-Sora directory not found")
625
+ return None
626
+
627
  # Run Open-Sora inference
628
  cmd = [
629
  "torchrun", "--nproc_per_node", "1", "--standalone",
 
636
  "--motion-score", "6" # High motion for dynamic scenes
637
  ]
638
 
639
+ result = subprocess.run(cmd, capture_output=True, text=True, cwd=opensora_dir)
640
 
641
  if result.returncode == 0:
642
  # Find generated video file