Spaces:
Runtime error
Runtime error
Update app.py
Browse files10 unmodified lines1112131415161771 unmodified lines89909192939495969798991001012 unmodified lines1041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351362 unmodified lines13914014114214314414538 unmodified lines1841851861871881891901 unmodified line19219319419519619719819920020120220320420520623 unmodified lines2302312322332342352368 unmodified lines24524624724824925025145 unmodified lines2972982993003013025 unmodified lines3083093103113123138 unmodified lines32232332432532632732818 unmodified lines34734834935035135235335435535635735835936036136236336436536636736836937037137210 unmodified linesfrom PIL import Imagefrom huggingface_hub import hf_hub_downloadimport shutilimport math # For math.round, though built-in round works for floatsfrom inference import (create_ltx_video_pipeline,71 unmodified linestarget_inference_device = "cuda"print(f"Target inference device: {target_inference_device}")pipeline_instance.to(target_inference_device)if latent_upsampler_instance: # Check if it was created before movinglatent_upsampler_instance.to(target_inference_device)@spaces.GPUdef generate(prompt, negative_prompt, input_image_filepath, input_video_filepath,height_ui, width_ui, mode,ui_steps, duration_ui, # << CHANGED from num_frames_uiui_frames_to_use,seed_ui, randomize_seed, ui_guidance_scale, improve_texture_flag,progress=gr.Progress(track_tqdm=True)):2 unmodified linesseed_ui = random.randint(0, 2**32 - 1)seed_everething(int(seed_ui))# Convert duration_ui (seconds) to actual_num_frames (N*8+1 format)target_frames_ideal = duration_ui * FPStarget_frames_rounded = round(target_frames_ideal)if target_frames_rounded < 1: # ensure positive for calculationtarget_frames_rounded = 1# Calculate N for N*8+1, ensuring it's rounded to the nearest integer# (target_frames_rounded - 1) could be float if target_frames_rounded is floatn_val = round((float(target_frames_rounded) - 1.0) / 8.0)actual_num_frames = int(n_val * 8 + 1)# Clamp to the allowed min (9) and max (MAX_NUM_FRAMES) N*8+1 valuesactual_num_frames = max(9, actual_num_frames)actual_num_frames = min(MAX_NUM_FRAMES, actual_num_frames)actual_height = int(height_ui)actual_width = int(width_ui)# actual_num_frames is now calculated aboveheight_padded = ((actual_height - 1) // 32 + 1) * 32width_padded = ((actual_width - 1) // 32 + 1) * 32# This padding ensures the model gets a frame count that is N*8+1# Since actual_num_frames is already N*8+1, this should preserve it.num_frames_padded = ((actual_num_frames - 2) // 8 + 1) * 8 + 1if num_frames_padded != actual_num_frames:print(f"Warning: actual_num_frames ({actual_num_frames}) and num_frames_padded ({num_frames_padded}) differ. Using num_frames_padded for pipeline.")# This case should ideally not happen if actual_num_frames is correctly N*8+1 and >= 9.padding_values = calculate_padding(actual_height, actual_width, height_padded, width_padded)2 unmodified lines"negative_prompt": negative_prompt,"height": height_padded,"width": width_padded,"num_frames": num_frames_padded, # Use the padded value for the model"frame_rate": int(FPS),"generator": torch.Generator(device=target_inference_device).manual_seed(int(seed_ui)),"output_type": "pt",38 unmodified linesmedia_path=input_video_filepath,height=actual_height,width=actual_width,max_frames=int(ui_frames_to_use), # This is from a separate slider for V2Vpadding=padding_values).to(target_inference_device)except Exception as e:1 unmodified lineraise gr.Error(f"Could not load video: {e}")print(f"Moving models to {target_inference_device} for inference (if not already there)...")# Models are moved globally once, no need to move per call unless strategy changes.# pipeline_instance.to(target_inference_device)# if latent_upsampler_instance:# latent_upsampler_instance.to(target_inference_device)active_latent_upsampler = Noneif improve_texture_flag and latent_upsampler_instance:active_latent_upsampler = latent_upsampler_instance#print("Models moved.")result_images_tensor = Noneif improve_texture_flag:23 unmodified linessingle_pass_call_kwargs = call_kwargs.copy()single_pass_call_kwargs["guidance_scale"] = float(ui_guidance_scale)single_pass_call_kwargs["num_inference_steps"] = int(ui_steps)# These keys might not exist if improve_texture_flag is false from the start of call_kwargssingle_pass_call_kwargs.pop("first_pass", None)single_pass_call_kwargs.pop("second_pass", None)single_pass_call_kwargs.pop("downscale_factor", None)8 unmodified linesslice_h_end = -pad_bottom if pad_bottom > 0 else Noneslice_w_end = -pad_right if pad_right > 0 else None# Crop to actual_num_frames, which is the desired output lengthresult_images_tensor = result_images_tensor[:, :, :actual_num_frames, pad_top:slice_h_end, pad_left:slice_w_end]45 unmodified linesreturn output_video_path# --- Gradio UI Definition ---css="""#col-container {5 unmodified lineswith gr.Blocks(css=css) as demo:gr.Markdown("# LTX Video 0.9.7 Distilled")gr.Markdown("Fast high quality video generation. [Model](https://huggingface.co/Lightricks/LTX-Video/blob/main/ltxv-2b-0.9.6-distilled-04-25.safetensors) [GitHub](https://github.com/Lightricks/LTX-Video) [Diffusers](#)")with gr.Row():with gr.Column():with gr.Tab("image-to-video") as image_tab:8 unmodified linest2v_button = gr.Button("Generate Text-to-Video", variant="primary")with gr.Tab("video-to-video") as video_tab:image_v_hidden = gr.Textbox(label="image_v", visible=False, value=None)video_v2v = gr.Video(label="Input Video", sources=["upload", "webcam"])frames_to_use = gr.Slider(label="Frames to use from input video", minimum=9, maximum=MAX_NUM_FRAMES, value=9, step=8, info="Number of initial frames to use for conditioning/transformation. Must be N*8+1.")v2v_prompt = gr.Textbox(label="Prompt", value="Change the style to cinematic anime", lines=3)v2v_button = gr.Button("Generate Video-to-Video", variant="primary")18 unmodified linesrandomize_seed_input = gr.Checkbox(label="Randomize Seed", value=False)with gr.Row():guidance_scale_input = gr.Slider(label="Guidance Scale (CFG)", minimum=1.0, maximum=10.0, value=PIPELINE_CONFIG_YAML.get("first_pass", {}).get("guidance_scale", 1.0), step=0.1, info="Controls how much the prompt influences the output. Higher values = stronger influence.")default_steps = len(PIPELINE_CONFIG_YAML.get("first_pass", {}).get("timesteps", [1]*7)) # Default to 7 if not foundsteps_input = gr.Slider(label="Inference Steps (for first pass if multi-scale)", minimum=1, maximum=30, value=default_steps, step=1, info="Number of denoising steps. More steps can improve quality but increase time. If YAML defines 'timesteps' for a pass, this UI value is ignored for that pass.")with gr.Row():height_input = gr.Slider(label="Height", value=512, step=32, minimum=256, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")width_input = gr.Slider(label="Width", value=704, step=32, minimum=256, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")# --- UPDATED INPUT LISTS ---t2v_inputs = [t2v_prompt, negative_prompt_input, image_n_hidden, video_n_hidden,height_input, width_input, gr.State("text-to-video"),steps_input, duration_input, gr.State(0), # Replaced num_frames_input with duration_inputseed_input, randomize_seed_input, guidance_scale_input, improve_texture]i2v_inputs = [i2v_prompt, negative_prompt_input, image_i2v, video_i_hidden,height_input, width_input, gr.State("image-to-video"),steps_input, duration_input, gr.State(0), # Replaced num_frames_input with duration_inputseed_input, randomize_seed_input, guidance_scale_input, improve_texture]v2v_inputs = [v2v_prompt, negative_prompt_input, image_v_hidden, video_v2v,height_input, width_input, gr.State("video-to-video"),steps_input, duration_input, frames_to_use, # Replaced num_frames_input with duration_inputseed_input, randomize_seed_input, guidance_scale_input, improve_texture]t2v_button.click(fn=generate, inputs=t2v_inputs, outputs=[output_video], api_name="text_to_video")10 unmodified lines11121314151671 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421432 unmodified lines1461471481491501511521531541551561571581591601611621631641651661671681691702 unmodified lines17317417517617717817938 unmodified lines2182192202212222232241 unmodified line22622722822923023123223323423523 unmodified lines2592602612622632648 unmodified lines27327427527627727845 unmodified lines3243253263273283293305 unmodified lines3363373383393403413428 unmodified lines35135235335435535635718 unmodified lines37637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446510 unmodified linesfrom PIL import Imagefrom huggingface_hub import hf_hub_downloadimport shutilfrom inference import (create_ltx_video_pipeline,71 unmodified linestarget_inference_device = "cuda"print(f"Target inference device: {target_inference_device}")pipeline_instance.to(target_inference_device)if latent_upsampler_instance:latent_upsampler_instance.to(target_inference_device)# --- Helper function for dimension calculation ---MIN_DIM_SLIDER = 256 # As defined in the sliders minimum attributeTARGET_FIXED_SIDE = 512 # Desired fixed side length as per requirementdef calculate_new_dimensions(orig_w, orig_h):"""Calculates new dimensions for height and width sliders based on original media dimensions.Ensures one side is TARGET_FIXED_SIDE, the other is scaled proportionally,both are multiples of 32, and within [MIN_DIM_SLIDER, MAX_IMAGE_SIZE]."""if orig_w == 0 or orig_h == 0:# Default to TARGET_FIXED_SIDE square if original dimensions are invalidreturn int(TARGET_FIXED_SIDE), int(TARGET_FIXED_SIDE)if orig_w >= orig_h: # Landscape or squarenew_h = TARGET_FIXED_SIDEaspect_ratio = orig_w / orig_hnew_w_ideal = new_h * aspect_ratio# Round to nearest multiple of 32new_w = round(new_w_ideal / 32) * 32# Clamp to [MIN_DIM_SLIDER, MAX_IMAGE_SIZE]new_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))# Ensure new_h is also clamped (TARGET_FIXED_SIDE should be within these bounds if configured correctly)new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))else: # Portraitnew_w = TARGET_FIXED_SIDEaspect_ratio = orig_h / orig_w # Use H/W ratio for portrait scalingnew_h_ideal = new_w * aspect_ratio# Round to nearest multiple of 32new_h = round(new_h_ideal / 32) * 32# Clamp to [MIN_DIM_SLIDER, MAX_IMAGE_SIZE]new_h = max(MIN_DIM_SLIDER, min(new_h, MAX_IMAGE_SIZE))# Ensure new_w is also clampednew_w = max(MIN_DIM_SLIDER, min(new_w, MAX_IMAGE_SIZE))return int(new_h), int(new_w)@spaces.GPUdef generate(prompt, negative_prompt, input_image_filepath, input_video_filepath,height_ui, width_ui, mode,ui_steps, duration_ui,ui_frames_to_use,seed_ui, randomize_seed, ui_guidance_scale, improve_texture_flag,progress=gr.Progress(track_tqdm=True)):2 unmodified linesseed_ui = random.randint(0, 2**32 - 1)seed_everething(int(seed_ui))target_frames_ideal = duration_ui * FPStarget_frames_rounded = round(target_frames_ideal)if target_frames_rounded < 1:target_frames_rounded = 1n_val = round((float(target_frames_rounded) - 1.0) / 8.0)actual_num_frames = int(n_val * 8 + 1)actual_num_frames = max(9, actual_num_frames)actual_num_frames = min(MAX_NUM_FRAMES, actual_num_frames)actual_height = int(height_ui)actual_width = int(width_ui)height_padded = ((actual_height - 1) // 32 + 1) * 32width_padded = ((actual_width - 1) // 32 + 1) * 32num_frames_padded = ((actual_num_frames - 2) // 8 + 1) * 8 + 1if num_frames_padded != actual_num_frames:print(f"Warning: actual_num_frames ({actual_num_frames}) and num_frames_padded ({num_frames_padded}) differ. Using num_frames_padded for pipeline.")padding_values = calculate_padding(actual_height, actual_width, height_padded, width_padded)2 unmodified lines"negative_prompt": negative_prompt,"height": height_padded,"width": width_padded,"num_frames": num_frames_padded,"frame_rate": int(FPS),"generator": torch.Generator(device=target_inference_device).manual_seed(int(seed_ui)),"output_type": "pt",38 unmodified linesmedia_path=input_video_filepath,height=actual_height,width=actual_width,max_frames=int(ui_frames_to_use),padding=padding_values).to(target_inference_device)except Exception as e:1 unmodified lineraise gr.Error(f"Could not load video: {e}")print(f"Moving models to {target_inference_device} for inference (if not already there)...")active_latent_upsampler = Noneif improve_texture_flag and latent_upsampler_instance:active_latent_upsampler = latent_upsampler_instanceresult_images_tensor = Noneif improve_texture_flag:23 unmodified linessingle_pass_call_kwargs = call_kwargs.copy()single_pass_call_kwargs["guidance_scale"] = float(ui_guidance_scale)single_pass_call_kwargs["num_inference_steps"] = int(ui_steps)single_pass_call_kwargs.pop("first_pass", None)single_pass_call_kwargs.pop("second_pass", None)single_pass_call_kwargs.pop("downscale_factor", None)8 unmodified linesslice_h_end = -pad_bottom if pad_bottom > 0 else Noneslice_w_end = -pad_right if pad_right > 0 else Noneresult_images_tensor = result_images_tensor[:, :, :actual_num_frames, pad_top:slice_h_end, pad_left:slice_w_end]45 unmodified linesreturn output_video_path# --- Gradio UI Definition ---css="""#col-container {5 unmodified lineswith gr.Blocks(css=css) as demo:gr.Markdown("# LTX Video 0.9.7 Distilled")gr.Markdown("Fast high quality video generation. [Model](https://huggingface.co/Lightricks/LTX-Video/blob/main/ltxv-2b-0.9.6-distilled-04-25.safetensors) [GitHub](https://github.com/Lightricks/LTX-Video) [Diffusers](#)")with gr.Row():with gr.Column():with gr.Tab("image-to-video") as image_tab:8 unmodified linest2v_button = gr.Button("Generate Text-to-Video", variant="primary")with gr.Tab("video-to-video") as video_tab:image_v_hidden = gr.Textbox(label="image_v", visible=False, value=None)video_v2v = gr.Video(label="Input Video", sources=["upload", "webcam"]) # type defaults to filepathframes_to_use = gr.Slider(label="Frames to use from input video", minimum=9, maximum=MAX_NUM_FRAMES, value=9, step=8, info="Number of initial frames to use for conditioning/transformation. Must be N*8+1.")v2v_prompt = gr.Textbox(label="Prompt", value="Change the style to cinematic anime", lines=3)v2v_button = gr.Button("Generate Video-to-Video", variant="primary")18 unmodified linesrandomize_seed_input = gr.Checkbox(label="Randomize Seed", value=False)with gr.Row():guidance_scale_input = gr.Slider(label="Guidance Scale (CFG)", minimum=1.0, maximum=10.0, value=PIPELINE_CONFIG_YAML.get("first_pass", {}).get("guidance_scale", 1.0), step=0.1, info="Controls how much the prompt influences the output. Higher values = stronger influence.")default_steps = len(PIPELINE_CONFIG_YAML.get("first_pass", {}).get("timesteps", [1]*7))steps_input = gr.Slider(label="Inference Steps (for first pass if multi-scale)", minimum=1, maximum=30, value=default_steps, step=1, info="Number of denoising steps. More steps can improve quality but increase time. If YAML defines 'timesteps' for a pass, this UI value is ignored for that pass.")with gr.Row():height_input = gr.Slider(label="Height", value=512, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")width_input = gr.Slider(label="Width", value=704, step=32, minimum=MIN_DIM_SLIDER, maximum=MAX_IMAGE_SIZE, info="Must be divisible by 32.")# --- Event handlers for updating dimensions on upload ---def handle_image_upload_for_dims(image_filepath, current_h, current_w):if not image_filepath: # Image cleared or no image initially# Keep current slider values if image is cleared or no inputreturn gr.update(value=current_h), gr.update(value=current_w)try:img = Image.open(image_filepath)orig_w, orig_h = img.sizenew_h, new_w = calculate_new_dimensions(orig_w, orig_h)return gr.update(value=new_h), gr.update(value=new_w)except Exception as e:print(f"Error processing image for dimension update: {e}")# Keep current slider values on errorreturn gr.update(value=current_h), gr.update(value=current_w)def handle_video_upload_for_dims(video_filepath, current_h, current_w):if not video_filepath: # Video cleared or no video initiallyreturn gr.update(value=current_h), gr.update(value=current_w)try:# Ensure video_filepath is a string for os.path.exists and imageiovideo_filepath_str = str(video_filepath)if not os.path.exists(video_filepath_str):print(f"Video file path does not exist for dimension update: {video_filepath_str}")return gr.update(value=current_h), gr.update(value=current_w)orig_w, orig_h = -1, -1with imageio.get_reader(video_filepath_str) as reader:meta = reader.get_meta_data()if 'size' in meta:orig_w, orig_h = meta['size']else:# Fallback: read first frame if 'size' not in metadatatry:first_frame = reader.get_data(0)# Shape is (h, w, c) for framesorig_h, orig_w = first_frame.shape[0], first_frame.shape[1]except Exception as e_frame:print(f"Could not get video size from metadata or first frame: {e_frame}")return gr.update(value=current_h), gr.update(value=current_w)if orig_w == -1 or orig_h == -1: # If dimensions couldn't be determinedprint(f"Could not determine dimensions for video: {video_filepath_str}")return gr.update(value=current_h), gr.update(value=current_w)new_h, new_w = calculate_new_dimensions(orig_w, orig_h)return gr.update(value=new_h), gr.update(value=new_w)except Exception as e:# Log type of video_filepath for debugging if it's not a path-like stringprint(f"Error processing video for dimension update: {e} (Path: {video_filepath}, Type: {type(video_filepath)})")return gr.update(value=current_h), gr.update(value=current_w)# Attach upload handlersimage_i2v.upload(fn=handle_image_upload_for_dims,inputs=[image_i2v, height_input, width_input],outputs=[height_input, width_input])video_v2v.upload(fn=handle_video_upload_for_dims,inputs=[video_v2v, height_input, width_input],outputs=[height_input, width_input])# --- INPUT LISTS (remain the same structurally) ---t2v_inputs = [t2v_prompt, negative_prompt_input, image_n_hidden, video_n_hidden,height_input, width_input, gr.State("text-to-video"),steps_input, duration_input, gr.State(0),seed_input, randomize_seed_input, guidance_scale_input, improve_texture]i2v_inputs = [i2v_prompt, negative_prompt_input, image_i2v, video_i_hidden,height_input, width_input, gr.State("image-to-video"),steps_input, duration_input, gr.State(0),seed_input, randomize_seed_input, guidance_scale_input, improve_texture]v2v_inputs = [v2v_prompt, negative_prompt_input, image_v_hidden, video_v2v,height_input, width_input, gr.State("video-to-video"),steps_input, duration_input, frames_to_use,seed_input, randomize_seed_input, guidance_scale_input, improve_texture]t2v_button.click(fn=generate, inputs=t2v_inputs, outputs=[output_video], api_name="text_to_video")
app.py+122-29