# flake8: noqa import subprocess import sys import os env = os.environ.copy() env["FLASH_ATTENTION_SKIP_CUDA_BUILD"] = "TRUE" subprocess.check_call([sys.executable, "-m", "pip", "install", "wheel"], env=env) subprocess.check_call([ sys.executable, "-m", "pip", "install", "flash-attn", "--no-build-isolation" ], env=env) import gradio as gr import spaces import torch from diffusers import Cosmos2VideoToWorldPipeline from diffusers.utils import export_to_video from transformers import AutoModelForCausalLM, SiglipProcessor import random import math try: from huggingface_hub import login # Try to login with token from environment variable hf_token = os.environ["HF_TOKEN"] print(hf_token) if hf_token: login(token=hf_token) print("✅ Authenticated with Hugging Face") else: print("No HF_TOKEN found, trying without authentication...") except Exception as e: print(f"Authentication failed: {e}") # Add flash_attention_2 to the safeguard model def patch_from_pretrained(cls): orig_method = cls.from_pretrained def new_from_pretrained(*args, **kwargs): kwargs.setdefault("attn_implementation", "flash_attention_2") kwargs.setdefault("torch_dtype", torch.bfloat16) return orig_method(*args, **kwargs) cls.from_pretrained = new_from_pretrained patch_from_pretrained(AutoModelForCausalLM) # Add a `use_fast` to the safeguard image processor def patch_processor_fast(cls): orig_method = cls.from_pretrained def new_from_pretrained(*args, **kwargs): kwargs.setdefault("use_fast", True) return orig_method(*args, **kwargs) cls.from_pretrained = new_from_pretrained patch_processor_fast(SiglipProcessor) model_14b_id = "nvidia/Cosmos-Predict2-14B-Video2World" pipe_14b = Cosmos2VideoToWorldPipeline.from_pretrained(model_14b_id, torch_dtype=torch.bfloat16, use_auth_token=True) pipe_14b.to("cuda") @spaces.GPU(duration=140) def generate_video( image, prompt, negative_prompt="The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality.", seed=42, randomize_seed=False, model_choice="14B", progress=gr.Progress(track_tqdm=True), ): width, height = image.size def resize_keep_ratio(w, h, target_pixels=901120): scale = math.sqrt(target_pixels / (w * h)) new_w = int(w * scale) new_h = int(h * scale) # 四捨五入為 16 的倍數 def round16(x): return int(round(x / 16)) * 16 return round16(new_w), round16(new_h) width, height =resize_keep_ratio(width, height) if randomize_seed: actual_seed = random.randint(0, 1000000) else: actual_seed = seed generator = torch.Generator().manual_seed(actual_seed) video = pipe_14b(image=image, prompt=prompt, negative_prompt=negative_prompt, generator=generator, width=width, height=height, num_inference_steps=35).frames[0] output = export_to_video(video, "output.mp4", fps=16) return output, output, actual_seed # Define the Gradio Blocks interface with gr.Blocks() as demo: gr.Markdown( """ # Cosmos-Predict2 14B Video2World """ ) with gr.Row(): with gr.Column(): image_input = gr.Image(label="Input Image", type="pil") prompt_input = gr.Textbox( label="Prompt", lines=5, value="A close-up shot captures a vibrant yellow scrubber vigorously working on a grimy plate, its bristles moving in circular motions to lift stubborn grease and food residue. The dish, once covered in remnants of a hearty meal, gradually reveals its original glossy surface. Suds form and bubble around the scrubber, creating a satisfying visual of cleanliness in progress. The sound of scrubbing fills the air, accompanied by the gentle clinking of the dish against the sink. As the scrubber continues its task, the dish transforms, gleaming under the bright kitchen lights, symbolizing the triumph of cleanliness over mess.", placeholder="Enter your descriptive prompt here...", ) negative_prompt_input = gr.Textbox( label="Negative Prompt", lines=3, value="The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality.", placeholder="Enter what you DON'T want to see in the image...", ) with gr.Row(): randomize_seed_checkbox = gr.Checkbox(label="Randomize Seed", value=True) seed_input = gr.Slider(minimum=0, maximum=1000000, value=1, step=1, label="Seed") generate_button = gr.Button("Generate Image") with gr.Column(): output_video = gr.Video(label="Generated Video", format="mp4") output_file = gr.File(label="Download Video") generate_button.click( fn=generate_video, inputs=[image_input, prompt_input, negative_prompt_input, seed_input, randomize_seed_checkbox], outputs=[output_video, output_file, seed_input], ) if __name__ == "__main__": demo.launch()