rahul7star commited on
Commit
2b78ef9
·
verified ·
1 Parent(s): 49930a4

Update app_4k.py

Browse files
Files changed (1) hide show
  1. app_4k.py +53 -28
app_4k.py CHANGED
@@ -165,6 +165,32 @@ default_prompt_t2v = "cinematic footage, group of pedestrians dancing in the str
165
  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"
166
 
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  def load_model_from_path(model_path: str):
170
  """
@@ -194,54 +220,53 @@ def get_duration(prompt, height, width,
194
 
195
  @spaces.GPU(duration=get_duration)
196
  def generate_video(prompt, height, width,
197
- negative_prompt=default_negative_prompt, duration_seconds=2,
198
- guidance_scale=1, steps=4,
199
- seed=42, randomize_seed=False,
200
- use_ultrawan_4k=False, # ✅ New toggle argument
201
  progress=gr.Progress(track_tqdm=True)):
202
-
203
  if not prompt or prompt.strip() == "":
204
  raise gr.Error("Please enter a text prompt. Try to use long and precise descriptions.")
205
 
206
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
207
 
208
- # Decide whether to use UltraWan or regular model
209
- if use_ultrawan_4k:
210
- video_path = generate_4k_ultrawan_video(prompt=prompt, seed=current_seed)
211
- return video_path, current_seed
212
-
213
-
214
- else:
215
- # Clamp values in demo mode
216
- if IS_ORIGINAL_SPACE:
217
- height = min(height, LIMITED_MAX_RESOLUTION)
218
- width = min(width, LIMITED_MAX_RESOLUTION)
219
- duration_seconds = min(duration_seconds, LIMITED_MAX_DURATION)
220
- steps = min(steps, LIMITED_MAX_STEPS)
221
 
222
- # Ensure height/width are valid
223
- target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
224
- target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
225
 
226
- generator_pipe = pipe # use your existing model
227
 
228
  num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
229
 
230
- # Run inference
231
  with torch.inference_mode():
232
  output_frames_list = generator_pipe(
233
- prompt=prompt, negative_prompt=negative_prompt,
234
- height=target_h, width=target_w, num_frames=num_frames,
235
- guidance_scale=float(guidance_scale), num_inference_steps=int(steps),
 
 
 
 
236
  generator=torch.Generator(device="cuda").manual_seed(current_seed)
237
  ).frames[0]
238
 
239
- # Save video
240
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
241
  video_path = tmpfile.name
 
242
  export_to_video(output_frames_list, video_path, fps=FIXED_OUTPUT_FPS)
243
- upscale_to_4k(video_path, os.path.join(temp_dir, "upscaled_4k.mp4"))
244
 
 
 
 
245
 
246
  return video_path, current_seed
247
 
 
165
  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"
166
 
167
 
168
+ import os
169
+ import tempfile
170
+ import random
171
+ import numpy as np
172
+ import torch
173
+ import gradio as gr
174
+ import subprocess
175
+ import shutil
176
+
177
+ def upscale_to_4k_and_replace(input_video_path):
178
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp_upscaled:
179
+ upscaled_path = tmp_upscaled.name
180
+
181
+ cmd = [
182
+ "ffmpeg",
183
+ "-i", input_video_path,
184
+ "-vf", "scale=3840:2160:flags=lanczos",
185
+ "-c:v", "libx264",
186
+ "-crf", "18",
187
+ "-preset", "slow",
188
+ "-y",
189
+ upscaled_path,
190
+ ]
191
+ subprocess.run(cmd, check=True)
192
+
193
+ shutil.move(upscaled_path, input_video_path)
194
 
195
  def load_model_from_path(model_path: str):
196
  """
 
220
 
221
  @spaces.GPU(duration=get_duration)
222
  def generate_video(prompt, height, width,
223
+ negative_prompt=default_negative_prompt,
224
+ duration_seconds=2, guidance_scale=1,
225
+ steps=4, seed=42, randomize_seed=False,
226
+ use_ultrawan_4k=False,
227
  progress=gr.Progress(track_tqdm=True)):
228
+
229
  if not prompt or prompt.strip() == "":
230
  raise gr.Error("Please enter a text prompt. Try to use long and precise descriptions.")
231
 
232
  current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
233
 
234
+ # Clamp values in demo mode
235
+ if IS_ORIGINAL_SPACE:
236
+ height = min(height, LIMITED_MAX_RESOLUTION)
237
+ width = min(width, LIMITED_MAX_RESOLUTION)
238
+ duration_seconds = min(duration_seconds, LIMITED_MAX_DURATION)
239
+ steps = min(steps, LIMITED_MAX_STEPS)
 
 
 
 
 
 
 
240
 
241
+ # Ensure dimensions are multiples of MOD_VALUE
242
+ target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
243
+ target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
244
 
245
+ generator_pipe = pipe # your model pipeline
246
 
247
  num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
248
 
 
249
  with torch.inference_mode():
250
  output_frames_list = generator_pipe(
251
+ prompt=prompt,
252
+ negative_prompt=negative_prompt,
253
+ height=target_h,
254
+ width=target_w,
255
+ num_frames=num_frames,
256
+ guidance_scale=float(guidance_scale),
257
+ num_inference_steps=int(steps),
258
  generator=torch.Generator(device="cuda").manual_seed(current_seed)
259
  ).frames[0]
260
 
261
+ # Export video
262
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
263
  video_path = tmpfile.name
264
+
265
  export_to_video(output_frames_list, video_path, fps=FIXED_OUTPUT_FPS)
 
266
 
267
+ # Optional 4K upscale
268
+ if use_ultrawan_4k:
269
+ upscale_to_4k_and_replace(video_path)
270
 
271
  return video_path, current_seed
272