Skip to content
Python

Merge remote-tracking branch 'upstream/main' into queues

Chris Malonebb3f99012fa7
diff --cc gradio_server.py
index 7848b39,674d6df..d7037e4
--- a/gradio_server.py
+++ b/gradio_server.py
@@@ -1219,161 -1154,188 +1225,161 @@@ def generate_video
if seed == None or seed <0:
seed = random.randint(0, 999999999)
- file_list = []
- state["file_list"] = file_list
+ global file_list
+ state["file_list"] = file_list
global save_path
os.makedirs(save_path, exist_ok=True)
- video_no = 0
- total_video = repeat_generation * len(prompts)
- state["total_video"] = total_video
- extra_generation = 0
abort = False
- start_time = time.time()
- state["prompts_max"] = len(prompts)
- for no, prompt in enumerate(prompts):
- state["prompt"] = prompt
- repeat_no = 0
- state["prompt_no"] = no
- extra_generation = 0
- yield f"Prompt No{no}"
- while True:
- extra_orders = state.get("extra_orders",0)
- state["extra_orders"] = 0
- extra_generation += extra_orders
- state["total_generation"] = repeat_generation + extra_generation
- # total_video += (len(prompts)- no) * extra_orders
- total_video += extra_orders
- if abort or repeat_no >= (repeat_generation + extra_generation):
- break
-
- if trans.enable_teacache:
- trans.teacache_counter = 0
- trans.num_steps = num_inference_steps
- trans.teacache_skipped_steps = 0
- trans.previous_residual_uncond = None
- trans.previous_residual_cond = None
-
- video_no += 1
- status = f"Video {video_no}/{total_video}"
- state["video_no"] = video_no
- state["progress_status"] = status
- state["progress_phase"] = (" - Encoding Prompt", -1 )
- progress(0, desc=status + " - Encoding Prompt" )
- callback = build_callback(state, trans, progress, status, num_inference_steps)
- offload.shared_state["callback"] = callback
-
-
- gc.collect()
- torch.cuda.empty_cache()
- wan_model._interrupt = False
- try:
- if image2video:
- samples = wan_model.generate(
- prompt,
- image_to_continue[no].convert('RGB'),
- image_to_end[no].convert('RGB') if image_to_end != None else None,
- frame_num=(video_length // 4)* 4 + 1,
- max_area=MAX_AREA_CONFIGS[resolution],
- shift=flow_shift,
- sampling_steps=num_inference_steps,
- guide_scale=guidance_scale,
- n_prompt=negative_prompt,
- seed=seed,
- offload_model=False,
- callback=callback,
- enable_RIFLEx = enable_RIFLEx,
- VAE_tile_size = VAE_tile_size,
- joint_pass = joint_pass,
- slg_layers = slg_layers,
- slg_start = slg_start/100,
- slg_end = slg_end/100,
- )
+ with tracker_lock:
+ progress_tracker[task_id] = {
+ 'current_step': 0,
+ 'total_steps': num_inference_steps,
+ 'start_time': time.time(),
+ 'last_update': time.time()
+ }
+ if trans.enable_teacache:
+ trans.teacache_counter = 0
+ trans.num_steps = num_inference_steps
+ trans.teacache_skipped_steps = 0
+ trans.previous_residual_uncond = None
+ trans.previous_residual_cond = None
+ callback = build_callback(task_id, num_inference_steps)
+ offload.shared_state["callback"] = callback
+ gc.collect()
+ torch.cuda.empty_cache()
+ wan_model._interrupt = False
+ state["progress_status"] = "Starting"
+ try:
+ if image2video:
+ samples = wan_model.generate(
+ prompt,
+ image_to_continue.convert('RGB'),
+ image_to_end.convert('RGB') if image_to_end != None else None,
+ frame_num=(video_length // 4)* 4 + 1,
+ max_area=MAX_AREA_CONFIGS[resolution],
+ shift=flow_shift,
+ sampling_steps=num_inference_steps,
+ guide_scale=guidance_scale,
+ n_prompt=negative_prompt,
+ seed=seed,
+ offload_model=False,
+ callback=callback,
+ enable_RIFLEx = enable_RIFLEx,
+ VAE_tile_size = VAE_tile_size,
+ joint_pass = joint_pass,
+ slg_layers = slg_layers,
+ slg_start = slg_start/100,
+ slg_end = slg_end/100,
+ )
- else:
- samples = wan_model.generate(
- prompt,
- frame_num=(video_length // 4)* 4 + 1,
- size=(width, height),
- shift=flow_shift,
- sampling_steps=num_inference_steps,
- guide_scale=guidance_scale,
- n_prompt=negative_prompt,
- seed=seed,
- offload_model=False,
- callback=callback,
- enable_RIFLEx = enable_RIFLEx,
- VAE_tile_size = VAE_tile_size,
- joint_pass = joint_pass,
- slg_layers = slg_layers,
- slg_start = slg_start/100,
- slg_end = slg_end/100,
- )
- except Exception as e:
- gen_in_progress = False
- if temp_filename!= None and os.path.isfile(temp_filename):
- os.remove(temp_filename)
- offload.last_offload_obj.unload_all()
- offload.unload_loras_from_model(trans)
- # if compile:
- # cache_size = torch._dynamo.config.cache_size_limit
- # torch.compiler.reset()
- # torch._dynamo.config.cache_size_limit = cache_size
-
- gc.collect()
- torch.cuda.empty_cache()
- s = str(e)
- keyword_list = ["vram", "VRAM", "memory","allocat"]
- VRAM_crash= False
- if any( keyword in s for keyword in keyword_list):
+ else:
+ samples = wan_model.generate(
+ prompt,
+ frame_num=(video_length // 4)* 4 + 1,
+ size=(width, height),
+ shift=flow_shift,
+ sampling_steps=num_inference_steps,
+ guide_scale=guidance_scale,
+ n_prompt=negative_prompt,
+ seed=seed,
+ offload_model=False,
+ callback=callback,
+ enable_RIFLEx = enable_RIFLEx,
+ VAE_tile_size = VAE_tile_size,
+ joint_pass = joint_pass,
+ slg_layers = slg_layers,
+ slg_start = slg_start/100,
+ slg_end = slg_end/100,
+ )
+ except Exception as e:
+ gen_in_progress = False
+ if temp_filename!= None and os.path.isfile(temp_filename):
+ os.remove(temp_filename)
+ offload.last_offload_obj.unload_all()
+ offload.unload_loras_from_model(trans)
+ # if compile:
+ # cache_size = torch._dynamo.config.cache_size_limit
+ # torch.compiler.reset()
+ # torch._dynamo.config.cache_size_limit = cache_size
+
+ gc.collect()
+ torch.cuda.empty_cache()
+ s = str(e)
+ keyword_list = ["vram", "VRAM", "memory","allocat"]
+ VRAM_crash= False
+ if any( keyword in s for keyword in keyword_list):
+ VRAM_crash = True
+ else:
+ stack = traceback.extract_stack(f=None, limit=5)
+ for frame in stack:
+ if any( keyword in frame.name for keyword in keyword_list):
VRAM_crash = True
- else:
- stack = traceback.extract_stack(f=None, limit=5)
- for frame in stack:
- if any( keyword in frame.name for keyword in keyword_list):
- VRAM_crash = True
- break
- state["prompt"] = ""
- if VRAM_crash:
- raise gr.Error("The generation of the video has encountered an error: it is likely that you have unsufficient VRAM and you should therefore reduce the video resolution or its number of frames.")
- else:
- raise gr.Error(f"The generation of the video has encountered an error, please check your terminal for more information. '{s}'")
-
- if trans.enable_teacache:
- print(f"Teacache Skipped Steps:{trans.teacache_skipped_steps}/{num_inference_steps}" )
- trans.previous_residual_uncond = None
- trans.previous_residual_cond = None
-
- if samples != None:
- samples = samples.to("cpu")
- offload.last_offload_obj.unload_all()
- gc.collect()
- torch.cuda.empty_cache()
-
- if samples == None:
- end_time = time.time()
- abort = True
- state["prompt"] = ""
- yield f"Video generation was aborted. Total Generation Time: {end_time-start_time:.1f}s"
- else:
- sample = samples.cpu()
- # video = rearrange(sample.cpu().numpy(), "c t h w -> t h w c")
+ break
+ state["prompt"] = ""
+ if VRAM_crash:
+ raise gr.Error("The generation of the video has encountered an error: it is likely that you have unsufficient VRAM and you should therefore reduce the video resolution or its number of frames.")
+ else:
+ raise gr.Error(f"The generation of the video has encountered an error, please check your terminal for more information. '{s}'")
+ finally:
+ with tracker_lock:
+ if task_id in progress_tracker:
+ del progress_tracker[task_id]
- time_flag = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%Hh%Mm%Ss")
- if os.name == 'nt':
- file_name = f"{time_flag}_seed{seed}_{sanitize_file_name(prompt[:50]).strip()}.mp4"
- else:
- file_name = f"{time_flag}_seed{seed}_{sanitize_file_name(prompt[:100]).strip()}.mp4"
- video_path = os.path.join(save_path, file_name)
- cache_video(
- tensor=sample[None],
- save_file=video_path,
- fps=16,
- nrow=1,
- normalize=True,
- value_range=(-1, 1))
-
- configs = {
- 'prompt': prompt,
- 'negative_prompt': negative_prompt,
- 'resolution': resolution,
- 'video_length': video_length,
- 'seed': seed,
- 'num_inference_steps': num_inference_steps,
- }
-
- metadata_choice = server_config.get("metadata_choice","metadata")
- if metadata_choice == "json":
- with open(video_path.replace('.mp4', '.json'), 'w') as f:
- json.dump(configs, f, indent=4)
- elif metadata_choice == "metadata":
- from mutagen.mp4 import MP4
- file = MP4(video_path)
- file.tags['©cmt'] = [json.dumps(configs)]
- file.save()
-
- print(f"New video saved to Path: "+video_path)
- file_list.append(video_path)
- if video_no < total_video:
- yield status
- else:
- end_time = time.time()
- state["prompt"] = ""
- yield f"Total Generation Time: {end_time-start_time:.1f}s"
- seed += 1
- repeat_no += 1
+ if trans.enable_teacache:
+ print(f"Teacache Skipped Steps:{trans.teacache_skipped_steps}/{num_inference_steps}" )
+ trans.previous_residual_uncond = None
+ trans.previous_residual_cond = None
+
+ if samples != None:
+ samples = samples.to("cpu")
+ offload.last_offload_obj.unload_all()
+ gc.collect()
+ torch.cuda.empty_cache()
+
+ if samples == None:
+ end_time = time.time()
+ abort = True
+ state["prompt"] = ""
+ else:
+ sample = samples.cpu()
+ # video = rearrange(sample.cpu().numpy(), "c t h w -> t h w c")
+
+ time_flag = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%Hh%Mm%Ss")
+ if os.name == 'nt':
+ file_name = f"{time_flag}_seed{seed}_{sanitize_file_name(prompt[:50]).strip()}.mp4"
+ else:
+ file_name = f"{time_flag}_seed{seed}_{sanitize_file_name(prompt[:100]).strip()}.mp4"
+ video_path = os.path.join(save_path, file_name)
+ cache_video(
+ tensor=sample[None],
+ save_file=video_path,
+ fps=16,
+ nrow=1,
+ normalize=True,
+ value_range=(-1, 1))
+
+ configs = {
+ 'prompt': prompt,
+ 'negative_prompt': negative_prompt,
+ 'resolution': resolution,
+ 'video_length': video_length,
+ 'seed': seed,
+ 'num_inference_steps': num_inference_steps,
+ }
+
- metadata_choice = server_config["metadata_choice"]
++ metadata_choice = server_config.get("metadata_choice","metadata")
+ if metadata_choice == "json":
+ with open(video_path.replace('.mp4', '.json'), 'w') as f:
+ json.dump(configs, f, indent=4)
+ elif metadata_choice == "metadata":
+ from mutagen.mp4 import MP4
+ file = MP4(video_path)
+ file.tags['©cmt'] = [json.dumps(configs)]
+ file.save()
+
+ print(f"New video saved to Path: "+video_path)
+ file_list.append(video_path)
+ seed += 1
if temp_filename!= None and os.path.isfile(temp_filename):
os.remove(temp_filename)