Spaces:
Build error
Build error
Julian Bilcke commited on
Commit ·
ca9df37
1
Parent(s): 2f28683
improving the preview_tab
Browse filesFiles changed (2)
84 unmodified lines8586878889909188899091929394959697989910010148 unmodified lines15015115215314615415514815615715015115815916016116291 unmodified lines254255256249250251252253257258259260261262263264265266267114 unmodified lines382383384385386374375376387388389390391392393394108 unmodified lines50350450550650749149249350850951051151251351451584 unmodified lines # Return updated log string for UI updates return "\n".join(log_messages) # Find latest LoRA weights if lora_weight > 0 lora_path = None using_lora = lora_weight > 0 # Find latest LoRA weights lora_path = self.find_latest_lora_weights() if not lora_path: return None, "Error: No LoRA weights found", log("Error: No LoRA weights found in output directory") if using_lora: lora_path = self.find_latest_lora_weights() if not lora_path: return None, "Error: No LoRA weights found", log("Error: No LoRA weights found in output directory") log(f"Using LoRA weights with weight {lora_weight}") else: log("Using original model without LoRA weights") # Add prefix to prompt if prompt_prefix and not prompt.startswith(prompt_prefix):48 unmodified lines log(f"Generating video with model type: {internal_model_type}") log(f"Using model version: {model_version}") if using_lora and lora_path: log(f"Using LoRA weights from: {lora_path}") log(f"Using LoRA weights from: {lora_path}") log(f"Resolution: {width}x{height}, Frames: {num_frames}, FPS: {fps}") log(f"Guidance Scale: {guidance_scale}, Flow Shift: {flow_shift}, LoRA Weight: {lora_weight}") log(f"Guidance Scale: {guidance_scale}, Flow Shift: {flow_shift}, LoRA Weight: {lora_weight if using_lora else 0}") log(f"Generation Seed: {seed}") log(f"Prompt: {full_prompt}") log(f"Negative Prompt: {negative_prompt}") #log(f"Prompt: {full_prompt}") #log(f"Negative Prompt: {negative_prompt}") # Import required components based on model type if internal_model_type == "wan":91 unmodified lines log_fn("Enabling model CPU offload...") pipe.enable_model_cpu_offload() log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) # TODO: Set the lora scale directly instead of using fuse_lora #pipe._lora_scale = lora_weight # Apply LoRA weights if using them if lora_weight > 0 and lora_path: log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) # TODO: Set the lora scale directly instead of using fuse_lora #pipe._lora_scale = lora_weight else: log_fn("Using base model without LoRA weights") # Create temporary file for the output with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:114 unmodified lines log_fn("Enabling model CPU offload...") pipe.enable_model_cpu_offload() # Apply LoRA weights if using them if lora_weight > 0 and lora_path: log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) pipe.fuse_lora(lora_weight) log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) pipe.fuse_lora(lora_weight) else: log_fn("Using base model without LoRA weights") # Create temporary file for the output with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:108 unmodified lines log_fn("Enabling model CPU offload...") pipe.enable_model_cpu_offload() # Apply LoRA weights if using them if lora_weight > 0 and lora_path: log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) pipe.fuse_lora(lora_weight) log_fn(f"Loading LoRA weights from {lora_path} with weight {lora_weight}...") pipe.load_lora_weights(lora_path) pipe.fuse_lora(lora_weight) else: log_fn("Using base model without LoRA weights") # Create temporary file for the output with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:51 unmodified lines5253545556575859606161626364656616 unmodified lines838485868788899091929394959697989910010110210310410510661 unmodified lines1681691701511711721731741751 unmodified line17717817915918018118218318416418518618718816 unmodified lines205206207187208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241131 unmodified lines37337437537637737837938038138238338438512 unmodified lines3983994003464014024034044054061 unmodified line40840941035441141241341420 unmodified lines43543643738143843944044144244359 unmodified lines5035045055065075085095105115125135145155165175185195205215225235245255265275287 unmodified lines5365375384605395405415425435443 unmodified lines54854955047055155255355455555648 unmodified lines60560660752560860961061161261321 unmodified lines6356366375536386396406416426437 unmodified lines6516526536546556566576586596606616626635 unmodified lines66967067157967267367467558367667767867968051 unmodified lines value=DEFAULT_PROMPT_PREFIX )
# Ensure seed is interactive with a slider self.components["seed"] = gr.Slider( label="Generation Seed (-1 for random)", minimum=-1, maximum=2147483647, # 2^31 - 1 step=1, value=-1, info="Set to -1 for random seed or specific value for reproducible results" info="Set to -1 for random seed or specific value for reproducible results", interactive=True ) with gr.Row():16 unmodified lines value=self.get_default_model_version(default_model) ) # Add dropdown to choose between LoRA and original model has_lora = self.check_lora_model_exists() lora_choices = [] default_lora_choice = ""
if has_lora: lora_choices = ["Use LoRA model", "Use original model"] default_lora_choice = "Use LoRA model" else: lora_choices = ["Cannot find LoRA model", "Use original model"] default_lora_choice = "Use original model"
self.components["use_lora"] = gr.Dropdown( choices=lora_choices, label="Model Selection", value=default_lora_choice ) # Add image input for image-to-video models self.components["conditioning_image"] = gr.Image( label="Conditioning Image (for Image-to-Video models)",61 unmodified lines minimum=0.0, maximum=1.0, step=0.01, value=0.7 value=0.7, visible=has_lora # Only visible if using LoRA ) self.components["inference_steps"] = gr.Slider(1 unmodified line minimum=1, maximum=100, step=1, value=30 value=20 ) self.components["enable_cpu_offload"] = gr.Checkbox( label="Enable Model CPU Offload (for low-VRAM GPUs)", value=True value=False # let's assume user is using a video model training rig with a good GPU ) self.components["generate_btn"] = gr.Button(16 unmodified lines self.components["log"] = gr.TextArea( label="Generation Log", interactive=False, lines=15 lines=20 ) return tab
def check_lora_model_exists(self) -> bool: """Check if any LoRA model files exist in the output directory""" # Look for the standard LoRA weights file lora_path = OUTPUT_PATH / "pytorch_lora_weights.safetensors" if lora_path.exists(): return True # If not found in the expected location, try to find in checkpoints checkpoints = list(OUTPUT_PATH.glob("checkpoint-*")) if not checkpoints: return False for checkpoint in checkpoints: lora_path = checkpoint / "pytorch_lora_weights.safetensors" if lora_path.exists(): return True return False def update_lora_ui(self, use_lora_value: str) -> Dict[str, Any]: """Update UI based on LoRA selection""" is_using_lora = "Use LoRA model" in use_lora_value return { self.components["lora_weight"]: gr.Slider(visible=is_using_lora) } def get_model_version_choices(self, model_type: str) -> List[str]: """Get model version choices based on model type"""131 unmodified lines ] ) # Connect LoRA selection dropdown to update LoRA weight visibility self.components["use_lora"].change( fn=self.update_lora_ui, inputs=[self.components["use_lora"]], outputs=[self.components["lora_weight"]] ) # Load preview UI state when the tab is selected if hasattr(self.app, 'tabs_component') and self.app.tabs_component is not None: self.app.tabs_component.select(12 unmodified lines self.components["lora_weight"], self.components["inference_steps"], self.components["enable_cpu_offload"], self.components["model_version"] self.components["model_version"], self.components["seed"], self.components["use_lora"] ] ) 1 unmodified line for component_name in [ "prompt", "negative_prompt", "prompt_prefix", "model_version", "resolution_preset", "width", "height", "num_frames", "fps", "guidance_scale", "flow_shift", "lora_weight", "inference_steps", "enable_cpu_offload" "lora_weight", "inference_steps", "enable_cpu_offload", "seed", "use_lora" ]: if component_name in self.components: self.components[component_name].change(20 unmodified lines self.components["inference_steps"], self.components["enable_cpu_offload"], self.components["fps"], self.components["conditioning_image"] self.components["conditioning_image"], self.components["seed"], self.components["use_lora"] ], outputs=[ self.components["preview_video"],59 unmodified lines if model_version not in model_version_choices and model_version_choices: model_version = model_version_choices[0] # Check if LoRA exists and set appropriate dropdown options has_lora = self.check_lora_model_exists() use_lora = preview_state.get("use_lora", "") # Validate use_lora value against current state if has_lora: valid_choices = ["Use LoRA model", "Use original model"] if use_lora not in valid_choices: use_lora = "Use LoRA model" # Default when LoRA exists else: valid_choices = ["Cannot find LoRA model", "Use original model"] if use_lora not in valid_choices: use_lora = "Use original model" # Default when no LoRA # Update the dropdown choices in the UI try: self.components["use_lora"].choices = valid_choices except Exception as e: logger.error(f"Failed to update use_lora choices: {e}") return ( preview_state.get("prompt", ""), preview_state.get("negative_prompt", "worst quality, low quality, blurry, jittery, distorted, ugly, deformed, disfigured, messy background"),7 unmodified lines preview_state.get("lora_weight", 0.7), preview_state.get("inference_steps", 30), preview_state.get("enable_cpu_offload", True), model_version model_version, preview_state.get("seed", -1), use_lora ) except Exception as e: logger.error(f"Error loading preview state: {e}")3 unmodified lines "worst quality, low quality, blurry, jittery, distorted, ugly, deformed, disfigured, messy background", DEFAULT_PROMPT_PREFIX, 832, 480, 49, 16, 5.0, 3.0, 0.7, 30, True, self.get_default_model_version(self.get_default_model_type()) self.get_default_model_version(self.get_default_model_type()), -1, "Use original model" if not self.check_lora_model_exists() else "Use LoRA model" ) def save_preview_state_value(self, value: Any) -> None:48 unmodified lines inference_steps: int, enable_cpu_offload: bool, fps: int, conditioning_image: Optional[str] = None conditioning_image: Optional[str] = None, seed: int = -1, use_lora: str = "Use LoRA model" ) -> Tuple[Optional[str], str, str]: """Handler for generate button click, delegates to preview service""" # Save all the parameters to preview state before generating21 unmodified lines "flow_shift": flow_shift, "lora_weight": lora_weight, "inference_steps": inference_steps, "enable_cpu_offload": enable_cpu_offload "enable_cpu_offload": enable_cpu_offload, "seed": seed, "use_lora": use_lora } state["preview"] = preview_state7 unmodified lines # Initial UI update video_path, status, log = None, "Initializing generation...", "Starting video generation process..." # Set lora_path to None if not using LoRA use_lora_model = use_lora == "Use LoRA model" # Start actual generation # If not using LoRA, set lora_weight to 0 to disable it effective_lora_weight = lora_weight if use_lora_model else 0.0 result = self.app.previewing.generate_video( model_type=model_type, model_version=model_version_id,5 unmodified lines num_frames=num_frames, guidance_scale=guidance_scale, flow_shift=flow_shift, lora_weight=lora_weight, lora_weight=effective_lora_weight, # Use 0.0 if not using LoRA inference_steps=inference_steps, enable_cpu_offload=enable_cpu_offload, fps=fps, conditioning_image=conditioning_image conditioning_image=conditioning_image, seed=seed ) # Return final resultvms/ui/project/services/previewing.py+38-19
vms/ui/project/tabs/preview_tab.py+108-14