import argparse import copy import datetime import json import os import random import sys import tempfile import time import zipfile from io import BytesIO from typing import Any, Dict, List, Literal, Optional, Tuple import torch from cosmos_transfer1.checkpoints import ( BASE_7B_CHECKPOINT_AV_SAMPLE_PATH, BASE_7B_CHECKPOINT_PATH, EDGE2WORLD_CONTROLNET_DISTILLED_CHECKPOINT_PATH, ) from cosmos_transfer1.diffusion.inference.inference_utils import ( valid_hint_keys, validate_controlnet_specs, ) from cosmos_transfer1.diffusion.inference.preprocessors import Preprocessors from cosmos_transfer1.diffusion.inference.world_generation_pipeline import ( DiffusionControl2WorldGenerationPipeline, DistilledControl2WorldGenerationPipeline, ) from cosmos_transfer1.utils import log, misc from cosmos_transfer1.utils.io import read_prompts_from_file, save_video from gpu_info import stop_watcher, watch_gpu_memory PWD = os.path.dirname(__file__) LOG_DIR = os.path.join(PWD, "logs") os.makedirs(LOG_DIR, exist_ok=True) os.environ["TOKENIZERS_PARALLELISM"] = "false" # Workaround to suppress MP warning torch.enable_grad(False) torch.serialization.add_safe_globals([BytesIO]) def load_controlnet_specs(controlnet_specs_in: dict) -> Dict[str, Any]: controlnet_specs = {} args = {} for hint_key, config in controlnet_specs_in.items(): if hint_key in valid_hint_keys: controlnet_specs[hint_key] = config else: if isinstance(config, dict): raise ValueError(f"Invalid hint_key: {hint_key}. Must be one of {valid_hint_keys}") else: args[hint_key] = config continue return controlnet_specs, args def parse_arguments( controlnet_specs_in: dict, prompt: str = "The video captures a stunning, photorealistic scene with remarkable attention to detail, giving it a lifelike appearance that is almost indistinguishable from reality. It appears to be from a high-budget 4K movie, showcasing ultra-high-definition quality with impeccable resolution.", # noqa: E501 negative_prompt: str = "The video captures a game playing, with bad crappy graphics and cartoonish frames. It represents a recording of old outdated games. The lighting looks very fake. The textures are very raw and basic. The geometries are very primitive. The images are very pixelated and of poor CG quality. There are many subtitles in the footage. Overall, the video is unrealistic at all.", # noqa: E501 input_video_path: str = "", num_input_frames: int = 1, sigma_max: float = 70.0, blur_strength: Literal["very_low", "low", "medium", "high", "very_high"] = "medium", canny_threshold: Literal["very_low", "low", "medium", "high", "very_high"] = "medium", is_av_sample: bool = False, checkpoint_dir: str = "checkpoints", tokenizer_dir: str = "Cosmos-Tokenize1-CV8x8x8-720p", video_save_name: str = "output", video_save_folder: str = "outputs/", batch_input_path: Optional[str] = None, batch_size: int = 1, num_steps: int = 35, guidance: float = 5, fps: int = 24, seed: int = 1, num_gpus: Literal[1] = 1, offload_diffusion_transformer: bool = False, offload_text_encoder_model: bool = False, offload_guardrail_models: bool = False, upsample_prompt: bool = False, offload_prompt_upsampler: bool = False, use_distilled: bool = False, ) -> argparse.Namespace: """ Parse input of control to world generation :param str controlnet_specs_in: multicontrolnet configurations dict :param str prompt: prompt which the sampled video condition on :param str negative_prompt: negative prompt which the sampled video condition on :param str input_video_path: Optional input RGB video path :param int num_input_frames: Number of conditional frames for long video generation :param float sigma_max: sigma_max for partial denoising :param str blur_strength: blur strength :param str canny_threshold: blur strength of canny threshold applied to input. Lower means less blur or more detected edges, which means higher fidelity to input :param bool is_av_sample: Whether the model is an driving post-training model :param str checkpoint_dir: Base directory containing model checkpoints :param str tokenizer_dir: Tokenizer weights directory relative to checkpoint_dir :param str video_save_name: Output filename for generating a single video :param str video_save_folder: Output folder for generating a batch of videos :param str batch_input_path: Path to a JSONL file of input prompts for generating a batch of videos :param int batch_size: Batch size :param int num_steps: Number of diffusion sampling steps :param float guidance: Classifier-free guidance scale value :param int fps: FPS of the output video :param int seed: Random seed :param int num_gpus: Number of GPUs used to run inference in parallel :param bool offload_diffusion_transformer: Offload DiT after inference :param bool offload_text_encoder_model: Offload text encoder model after inference :param bool offload_guardrail_models: Offload guardrail models after inference :param bool upsample_prompt: Upsample prompt using Pixtral upsampler model :param bool offload_prompt_upsampler: Offload prompt upsampler model after inference :param bool use_distilled: Use distilled ControlNet model variant """ cmd_args = argparse.Namespace( prompt=prompt, negative_prompt=negative_prompt, input_video_path=input_video_path, num_input_frames=num_input_frames, sigma_max=sigma_max, blur_strength=blur_strength, canny_threshold=canny_threshold, is_av_sample=is_av_sample, checkpoint_dir=checkpoint_dir, tokenizer_dir=tokenizer_dir, video_save_name=video_save_name, video_save_folder=video_save_folder, batch_input_path=batch_input_path, batch_size=batch_size, num_steps=num_steps, guidance=guidance, fps=fps, seed=seed, num_gpus=num_gpus, offload_diffusion_transformer=offload_diffusion_transformer, offload_text_encoder_model=offload_text_encoder_model, offload_guardrail_models=offload_guardrail_models, upsample_prompt=upsample_prompt, offload_prompt_upsampler=offload_prompt_upsampler, use_distilled=use_distilled, ) # Load and parse JSON input control_inputs, json_args = load_controlnet_specs(controlnet_specs_in) # if parameters not set on command line, use the ones from the controlnet_specs # if both not set use command line defaults for key in json_args: if f"--{key}" not in sys.argv: setattr(cmd_args, key, json_args[key]) return cmd_args, control_inputs def inference(cfg, control_inputs, chunking) -> Tuple[List[str], List[str]]: video_paths = [] prompt_paths = [] control_inputs = validate_controlnet_specs(cfg, control_inputs) misc.set_random_seed(cfg.seed) device_rank = 0 process_group = None if cfg.num_gpus > 1: from megatron.core import ( parallel_state, # pyright: ignore[reportMissingImports] ) from cosmos_transfer1.utils import distributed distributed.init() parallel_state.initialize_model_parallel(context_parallel_size=cfg.num_gpus) process_group = parallel_state.get_context_parallel_group() device_rank = distributed.get_rank(process_group) preprocessors = Preprocessors() if cfg.use_distilled: assert not cfg.is_av_sample checkpoint = EDGE2WORLD_CONTROLNET_DISTILLED_CHECKPOINT_PATH pipeline = DistilledControl2WorldGenerationPipeline( checkpoint_dir=cfg.checkpoint_dir, checkpoint_name=checkpoint, offload_network=cfg.offload_diffusion_transformer, offload_text_encoder_model=cfg.offload_text_encoder_model, offload_guardrail_models=cfg.offload_guardrail_models, guidance=cfg.guidance, num_steps=cfg.num_steps, fps=cfg.fps, seed=cfg.seed, num_input_frames=cfg.num_input_frames, control_inputs=control_inputs, sigma_max=cfg.sigma_max, blur_strength=cfg.blur_strength, canny_threshold=cfg.canny_threshold, upsample_prompt=cfg.upsample_prompt, offload_prompt_upsampler=cfg.offload_prompt_upsampler, process_group=process_group, ) else: checkpoint = BASE_7B_CHECKPOINT_AV_SAMPLE_PATH if cfg.is_av_sample else BASE_7B_CHECKPOINT_PATH # Initialize transfer generation model pipeline pipeline = DiffusionControl2WorldGenerationPipeline( checkpoint_dir=cfg.checkpoint_dir, checkpoint_name=checkpoint, offload_network=cfg.offload_diffusion_transformer, offload_text_encoder_model=cfg.offload_text_encoder_model, offload_guardrail_models=cfg.offload_guardrail_models, guidance=cfg.guidance, num_steps=cfg.num_steps, fps=cfg.fps, seed=cfg.seed, num_input_frames=cfg.num_input_frames, control_inputs=control_inputs, sigma_max=cfg.sigma_max, blur_strength=cfg.blur_strength, canny_threshold=cfg.canny_threshold, upsample_prompt=cfg.upsample_prompt, offload_prompt_upsampler=cfg.offload_prompt_upsampler, process_group=process_group, chunking=chunking, ) if cfg.batch_input_path: log.info(f"Reading batch inputs from path: {cfg.batch_input_path}") prompts = read_prompts_from_file(cfg.batch_input_path) else: # Single prompt case prompts = [{"prompt": cfg.prompt, "visual_input": cfg.input_video_path}] batch_size = cfg.batch_size if hasattr(cfg, "batch_size") else 1 if any("upscale" in control_input for control_input in control_inputs) and batch_size > 1: batch_size = 1 log.info("Setting batch_size=1 as upscale does not support batch generation") os.makedirs(cfg.video_save_folder, exist_ok=True) for batch_start in range(0, len(prompts), batch_size): # Get current batch batch_prompts = prompts[batch_start : batch_start + batch_size] actual_batch_size = len(batch_prompts) # Extract batch data batch_prompt_texts = [p.get("prompt", None) for p in batch_prompts] batch_video_paths = [p.get("visual_input", None) for p in batch_prompts] batch_control_inputs = [] for i, input_dict in enumerate(batch_prompts): current_prompt = input_dict.get("prompt", None) current_video_path = input_dict.get("visual_input", None) if cfg.batch_input_path: video_save_subfolder = os.path.join(cfg.video_save_folder, f"video_{batch_start+i}") os.makedirs(video_save_subfolder, exist_ok=True) else: video_save_subfolder = cfg.video_save_folder current_control_inputs = copy.deepcopy(control_inputs) if "control_overrides" in input_dict: for hint_key, override in input_dict["control_overrides"].items(): if hint_key in current_control_inputs: current_control_inputs[hint_key].update(override) else: log.warning(f"Ignoring unknown control key in override: {hint_key}") # if control inputs are not provided, run respective preprocessor (for seg and depth) log.info("running preprocessor") preprocessors( current_video_path, current_prompt, current_control_inputs, video_save_subfolder, cfg.regional_prompts if hasattr(cfg, "regional_prompts") else None, ) batch_control_inputs.append(current_control_inputs) regional_prompts = [] region_definitions = [] if hasattr(cfg, "regional_prompts") and cfg.regional_prompts: log.info(f"regional_prompts: {cfg.regional_prompts}") for regional_prompt in cfg.regional_prompts: regional_prompts.append(regional_prompt["prompt"]) if "region_definitions_path" in regional_prompt: log.info(f"region_definitions_path: {regional_prompt['region_definitions_path']}") region_definition_path = regional_prompt["region_definitions_path"] if isinstance(region_definition_path, str) and region_definition_path.endswith(".json"): with open(region_definition_path, "r") as f: region_definitions_json = json.load(f) region_definitions.extend(region_definitions_json) else: region_definitions.append(region_definition_path) if hasattr(pipeline, "regional_prompts"): pipeline.regional_prompts = regional_prompts if hasattr(pipeline, "region_definitions"): pipeline.region_definitions = region_definitions # Generate videos in batch batch_outputs = pipeline.generate( prompt=batch_prompt_texts, video_path=batch_video_paths, negative_prompt=cfg.negative_prompt, control_inputs=batch_control_inputs, save_folder=video_save_subfolder, batch_size=actual_batch_size, ) if batch_outputs is None: log.critical("Guardrail blocked generation for entire batch.") continue videos, final_prompts = batch_outputs for i, (video, prompt) in enumerate(zip(videos, final_prompts)): if cfg.batch_input_path: video_save_subfolder = os.path.join(cfg.video_save_folder, f"video_{batch_start+i}") video_save_path = os.path.join(video_save_subfolder, "output.mp4") prompt_save_path = os.path.join(video_save_subfolder, "prompt.txt") else: video_save_path = os.path.join(cfg.video_save_folder, f"{cfg.video_save_name}.mp4") prompt_save_path = os.path.join(cfg.video_save_folder, f"{cfg.video_save_name}.txt") # Save video and prompt if device_rank == 0: os.makedirs(os.path.dirname(video_save_path), exist_ok=True) save_video( video=video, fps=cfg.fps, H=video.shape[1], W=video.shape[2], video_save_quality=5, video_save_path=video_save_path, ) video_paths.append(video_save_path) # Save prompt to text file alongside video with open(prompt_save_path, "wb") as f: f.write(prompt.encode("utf-8")) prompt_paths.append(prompt_save_path) log.info(f"Saved video to {video_save_path}") log.info(f"Saved prompt to {prompt_save_path}") # clean up properly if cfg.num_gpus > 1: parallel_state.destroy_model_parallel() import torch.distributed as dist dist.destroy_process_group() return video_paths, prompt_paths def create_zip_for_download(filename, files_to_zip): temp_dir = tempfile.mkdtemp() zip_path = os.path.join(temp_dir, f"{os.path.splitext(filename)[0]}.zip") with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: for file_path in files_to_zip: arcname = os.path.basename(file_path) zipf.write(file_path, arcname) return zip_path import gradio as gr def generate_video_fun(checkpoints_path: str): def generate_video( rgb_video_path, hdmap_video_input, lidar_video_input, 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.", # noqa: E501 seed=42, randomize_seed=False, chunking=None, progress=gr.Progress(track_tqdm=True), ): _dt = datetime.datetime.now(tz=datetime.timezone(datetime.timedelta(hours=8))).strftime("%Y-%m-%d_%H.%M.%S") logfile_path = os.path.join(LOG_DIR, f"{_dt}.log") log_handler = log.init_dev_loguru_file(logfile_path) if randomize_seed: actual_seed = random.randint(0, 1000000) else: actual_seed = seed log.info(f"actual_seed: {actual_seed}") log.info(f"chunking size: {chunking}") try: if rgb_video_path is None or not os.path.isfile(rgb_video_path): log.warning(f"File `{rgb_video_path}` does not exist") rgb_video_path = "" # add timer to calculate the generation time start_time = time.time() # parse generation configs args, control_inputs = parse_arguments( controlnet_specs_in={ "hdmap": {"control_weight": 0.3, "input_control": hdmap_video_input}, "lidar": {"control_weight": 0.7, "input_control": lidar_video_input}, }, input_video_path=rgb_video_path, checkpoint_dir=checkpoints_path, prompt=prompt, negative_prompt=negative_prompt, sigma_max=80, offload_text_encoder_model=True, is_av_sample=True, num_gpus=1, seed=seed, ) # watch gpu memory watcher = watch_gpu_memory(10, lambda x: log.debug(f"GPU memory (used, total): {x} (MiB)")) # start inference if chunking <= 0: chunking = None videos, prompts = inference(args, control_inputs, chunking) # print the generation time end_time = time.time() log.info(f"Time taken: {end_time - start_time} s") # stop the watcher stop_watcher() video = videos[0] log.logger.remove(log_handler) return video, create_zip_for_download(filename=logfile_path, files_to_zip=[video, logfile_path]), actual_seed except Exception as e: log.logger.remove(log_handler) log.exception(e) return "", logfile_path, actual_seed return generate_video