ginipick commited on
Commit
29549c3
·
verified ·
1 Parent(s): 6312ef8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -21
app.py CHANGED
@@ -43,20 +43,19 @@ default_negative_prompt = "static, still, no motion, frozen"
43
 
44
  # Initialize once on startup
45
  video_pipe = None
 
46
 
47
  def initialize_video_pipeline():
48
- global video_pipe
49
- if video_pipe is None:
50
  try:
 
 
51
  # Install PyTorch 2.8 (if needed)
52
  os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 "torch<2.9" spaces')
53
 
54
- # Import optimization module
55
- try:
56
- from optimization import optimize_pipeline_
57
- except ImportError:
58
- print("Warning: optimization module not found, skipping optimization")
59
- optimize_pipeline_ = None
60
 
61
  video_pipe = WanImageToVideoPipeline.from_pretrained(VIDEO_MODEL_ID,
62
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
@@ -72,14 +71,30 @@ def initialize_video_pipeline():
72
  torch_dtype=torch.bfloat16,
73
  ).to('cuda')
74
 
75
- # Clear memory
76
- for i in range(3):
77
- gc.collect()
78
- torch.cuda.synchronize()
79
- torch.cuda.empty_cache()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- # Optimize pipeline if module available
82
- if optimize_pipeline_ is not None:
 
 
83
  optimize_pipeline_(video_pipe,
84
  image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
85
  prompt='prompt',
@@ -87,11 +102,19 @@ def initialize_video_pipeline():
87
  width=LANDSCAPE_WIDTH,
88
  num_frames=MAX_FRAMES_MODEL,
89
  )
 
 
 
 
 
90
 
 
91
  print("Video pipeline initialized successfully!")
 
92
  except Exception as e:
93
  print(f"Error initializing video pipeline: {e}")
94
  video_pipe = None
 
95
 
96
  # ===========================
97
  # Image Processing Functions
@@ -647,14 +670,13 @@ with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
647
 
648
  # Launch
649
  if __name__ == "__main__":
650
- # Try to initialize video pipeline on startup
651
- try:
652
- initialize_video_pipeline()
653
- except:
654
- print("Video pipeline initialization deferred to first use")
655
 
656
  demo.launch(
657
  share=True,
658
  server_name="0.0.0.0",
659
- server_port=7860
 
660
  )
 
43
 
44
  # Initialize once on startup
45
  video_pipe = None
46
+ video_pipeline_ready = False
47
 
48
  def initialize_video_pipeline():
49
+ global video_pipe, video_pipeline_ready
50
+ if video_pipe is None and not video_pipeline_ready:
51
  try:
52
+ print("Starting video pipeline initialization...")
53
+
54
  # Install PyTorch 2.8 (if needed)
55
  os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 "torch<2.9" spaces')
56
 
57
+ # Import LoRA loading utilities
58
+ from peft import LoraConfig, get_peft_model, TaskType
 
 
 
 
59
 
60
  video_pipe = WanImageToVideoPipeline.from_pretrained(VIDEO_MODEL_ID,
61
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
 
71
  torch_dtype=torch.bfloat16,
72
  ).to('cuda')
73
 
74
+ # Clear memory after loading
75
+ gc.collect()
76
+ torch.cuda.empty_cache()
77
+
78
+ # Load Lightning LoRA
79
+ try:
80
+ print("Loading Lightning LoRA adapter...")
81
+ video_pipe.transformer.load_adapter("Lightx2v/lightx2v_I2V_14B_480p_cfg_step_4", adapter_name="lightx2v")
82
+ video_pipe.transformer_2.load_adapter("Lightx2v/lightx2v_I2V_14B_480p_cfg_step_4", adapter_name="lightx2v_2")
83
+ video_pipe.transformer.set_adapters(["lightx2v"], adapter_weights=[1.0])
84
+ video_pipe.transformer_2.set_adapters(["lightx2v_2"], adapter_weights=[1.0])
85
+ print("Lightning LoRA loaded successfully")
86
+ except Exception as e:
87
+ print(f"Warning: Could not load Lightning LoRA: {e}")
88
+ # Continue without LoRA
89
+
90
+ # Clear memory again
91
+ gc.collect()
92
+ torch.cuda.empty_cache()
93
 
94
+ # Try to optimize if module available
95
+ try:
96
+ from optimization import optimize_pipeline_
97
+ print("Optimizing pipeline...")
98
  optimize_pipeline_(video_pipe,
99
  image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
100
  prompt='prompt',
 
102
  width=LANDSCAPE_WIDTH,
103
  num_frames=MAX_FRAMES_MODEL,
104
  )
105
+ print("Pipeline optimization complete")
106
+ except ImportError:
107
+ print("Optimization module not found, running without optimization")
108
+ except Exception as e:
109
+ print(f"Warning: Optimization failed: {e}")
110
 
111
+ video_pipeline_ready = True
112
  print("Video pipeline initialized successfully!")
113
+
114
  except Exception as e:
115
  print(f"Error initializing video pipeline: {e}")
116
  video_pipe = None
117
+ video_pipeline_ready = False
118
 
119
  # ===========================
120
  # Image Processing Functions
 
670
 
671
  # Launch
672
  if __name__ == "__main__":
673
+ # Don't initialize video pipeline on startup to avoid blocking
674
+ print("Starting application...")
675
+ print("Note: Video pipeline will initialize on first use")
 
 
676
 
677
  demo.launch(
678
  share=True,
679
  server_name="0.0.0.0",
680
+ server_port=7860,
681
+ show_error=True
682
  )