rahul7star commited on
Commit
06bbca2
·
verified ·
1 Parent(s): 0cc0a38

Update app_4k.py

Browse files
Files changed (1) hide show
  1. app_4k.py +56 -12
app_4k.py CHANGED
@@ -17,6 +17,42 @@ snapshot_download(repo_id="APRIL-AIGC/UltraWan", repo_type="model", local_dir="u
17
 
18
 
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  # LIGHT WEIGHT 1.3b
@@ -143,6 +179,22 @@ MAX_FRAMES_MODEL = 81
143
  default_prompt_t2v = "cinematic footage, group of pedestrians dancing in the streets of NYC, high quality breakdance, 4K, tiktok video, intricate details, instagram feel, dynamic camera, smooth dance motion, dimly lit, stylish, beautiful faces, smiling, music video"
144
  default_negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards, watermark, text, signature"
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def get_duration(prompt, height, width,
147
  negative_prompt, duration_seconds,
148
  guidance_scale, steps,
@@ -170,18 +222,10 @@ def generate_video(prompt, height, width,
170
 
171
  # Decide whether to use UltraWan or regular model
172
  if use_ultrawan_4k:
173
- # Override with 4K resolution
174
- target_h, target_w = 2160, 3840
175
- steps = max(steps, 10)
176
- guidance_scale = max(guidance_scale, 7.5)
177
-
178
- # ✅ Lazy-load UltraWan model if not already loaded
179
- global ultrawan_pipe
180
- if "ultrawan_pipe" not in globals() or ultrawan_pipe is None:
181
- from transformers import pipeline # or appropriate loader
182
- ultrawan_pipe = load_model_from_path("ultrawan_weights/UltraWan")
183
-
184
- generator_pipe = ultrawan_pipe
185
  else:
186
  # Clamp values in demo mode
187
  if IS_ORIGINAL_SPACE:
 
17
 
18
 
19
 
20
+ import subprocess
21
+ import os
22
+ import uuid
23
+
24
+ def generate_4k_ultrawan_video(prompt: str, seed: int = 42, out_dir: str = "/tmp/output"):
25
+ output_name = f"ultrawan_{uuid.uuid4().hex[:8]}"
26
+ output_path = os.path.join(out_dir, output_name)
27
+ os.makedirs(out_dir, exist_ok=True)
28
+
29
+ cmd = [
30
+ "python", "infer.py",
31
+ "--model_dir", "ultrawan_weights/Wan2.1-T2V-1.3B",
32
+ "--model_path", "ultrawan_weights/UltraWan/ultrawan-4k.ckpt",
33
+ "--mode", "lora",
34
+ "--lora_alpha", "0.5",
35
+ "--usp", "0",
36
+ "--height", "2160",
37
+ "--width", "3840",
38
+ "--num_frames", "33",
39
+ "--prompt", prompt,
40
+ "--seed", str(seed),
41
+ "--out_dir", output_path
42
+ ]
43
+
44
+ env = os.environ.copy()
45
+ env["CUDA_VISIBLE_DEVICES"] = "0"
46
+
47
+ subprocess.run(cmd, env=env, check=True)
48
+
49
+ # Assume the output is a .mp4 inside output_path
50
+ generated_files = [f for f in os.listdir(output_path) if f.endswith(".mp4")]
51
+ if not generated_files:
52
+ raise FileNotFoundError("No output video found.")
53
+
54
+ return os.path.join(output_path, generated_files[0])
55
+
56
 
57
 
58
  # LIGHT WEIGHT 1.3b
 
179
  default_prompt_t2v = "cinematic footage, group of pedestrians dancing in the streets of NYC, high quality breakdance, 4K, tiktok video, intricate details, instagram feel, dynamic camera, smooth dance motion, dimly lit, stylish, beautiful faces, smiling, music video"
180
  default_negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards, watermark, text, signature"
181
 
182
+
183
+
184
+ def load_model_from_path(model_path: str):
185
+ """
186
+ Loads a diffusion pipeline from a local directory.
187
+ The model is automatically loaded to CUDA with float16.
188
+ """
189
+ pipe = DiffusionPipeline.from_pretrained(
190
+ model_path,
191
+ torch_dtype=torch.float16,
192
+ variant="fp16" if os.path.exists(os.path.join(model_path, "model.fp16.safetensors")) else None
193
+ ).to("cuda")
194
+ pipe.enable_model_cpu_offload() # Optional: for large models
195
+ return pipe
196
+
197
+
198
  def get_duration(prompt, height, width,
199
  negative_prompt, duration_seconds,
200
  guidance_scale, steps,
 
222
 
223
  # Decide whether to use UltraWan or regular model
224
  if use_ultrawan_4k:
225
+ video_path = generate_4k_ultrawan_video(prompt=prompt, seed=current_seed)
226
+ return video_path, current_seed
227
+
228
+
 
 
 
 
 
 
 
 
229
  else:
230
  # Clamp values in demo mode
231
  if IS_ORIGINAL_SPACE: