import gradio as gr import os import torch import argparse import spaces import torchvision from pipelines.pipeline_videogen import VideoGenPipeline from diffusers.schedulers import DDIMScheduler from diffusers.models import AutoencoderKL from diffusers.models import AutoencoderKLTemporalDecoder from transformers import CLIPTokenizer, CLIPTextModel from omegaconf import OmegaConf import os, sys sys.path.append(os.path.split(sys.path[0])[0]) from models import get_models import imageio from PIL import Image import numpy as np from datasets import video_transforms from torchvision import transforms from einops import rearrange, repeat from utils import dct_low_pass_filter, exchanged_mixed_dct_freq from copy import deepcopy import requests from datetime import datetime import random parser = argparse.ArgumentParser() parser.add_argument("--config", type=str, default="./configs/sample.yaml") args = parser.parse_args() args = OmegaConf.load(args.config) torch.set_grad_enabled(False) device = "cuda" if torch.cuda.is_available() else "cpu" dtype = torch.float16 # torch.float16 unet = get_models(args).to(device, dtype=dtype) if args.enable_vae_temporal_decoder: if args.use_dct: vae_for_base_content = AutoencoderKLTemporalDecoder.from_pretrained(args.pretrained_model_path, subfolder="vae_temporal_decoder", torch_dtype=torch.float64).to(device) else: vae_for_base_content = AutoencoderKLTemporalDecoder.from_pretrained(args.pretrained_model_path, subfolder="vae_temporal_decoder", torch_dtype=torch.float16).to(device) vae = deepcopy(vae_for_base_content).to(dtype=dtype) else: vae_for_base_content = AutoencoderKL.from_pretrained(args.pretrained_model_path, subfolder="vae",).to(device, dtype=torch.float64) vae = deepcopy(vae_for_base_content).to(dtype=dtype) tokenizer = CLIPTokenizer.from_pretrained(args.pretrained_model_path, subfolder="tokenizer") text_encoder = CLIPTextModel.from_pretrained(args.pretrained_model_path, subfolder="text_encoder", torch_dtype=dtype).to(device) # huge # set eval mode unet.eval() vae.eval() text_encoder.eval() basedir = os.getcwd() savedir = os.path.join(basedir, "samples/Gradio", datetime.now().strftime("%Y-%m-%dT%H-%M-%S")) savedir_sample = os.path.join(savedir, "sample") os.makedirs(savedir, exist_ok=True) def update_and_resize_image(input_image_path, height_slider, width_slider): if input_image_path.startswith("http://") or input_image_path.startswith("https://"): pil_image = Image.open(requests.get(input_image_path, stream=True).raw).convert('RGB') else: pil_image = Image.open(input_image_path).convert('RGB') original_width, original_height = pil_image.size if original_height == height_slider and original_width == width_slider: return gr.Image(value=np.array(pil_image)) ratio1 = height_slider / original_height ratio2 = width_slider / original_width if ratio1 > ratio2: new_width = int(original_width * ratio1) new_height = int(original_height * ratio1) else: new_width = int(original_width * ratio2) new_height = int(original_height * ratio2) pil_image = pil_image.resize((new_width, new_height), Image.LANCZOS) left = (new_width - width_slider) / 2 top = (new_height - height_slider) / 2 right = left + width_slider bottom = top + height_slider pil_image = pil_image.crop((left, top, right, bottom)) return gr.Image(value=np.array(pil_image)) def update_textbox_and_save_image(input_image, height_slider, width_slider): pil_image = Image.fromarray(input_image.astype(np.uint8)).convert("RGB") original_width, original_height = pil_image.size ratio1 = height_slider / original_height ratio2 = width_slider / original_width if ratio1 > ratio2: new_width = int(original_width * ratio1) new_height = int(original_height * ratio1) else: new_width = int(original_width * ratio2) new_height = int(original_height * ratio2) pil_image = pil_image.resize((new_width, new_height), Image.LANCZOS) left = (new_width - width_slider) / 2 top = (new_height - height_slider) / 2 right = left + width_slider bottom = top + height_slider pil_image = pil_image.crop((left, top, right, bottom)) img_path = os.path.join(savedir, "input_image.png") pil_image.save(img_path) return gr.Textbox(value=img_path), gr.Image(value=np.array(pil_image)) def prepare_image(image, vae, transform_video, device, dtype=torch.float16): image = torch.as_tensor(np.array(image, dtype=np.uint8, copy=True)).unsqueeze(0).permute(0, 3, 1, 2) image = transform_video(image) image = vae.encode(image.to(dtype=dtype, device=device)).latent_dist.sample().mul_(vae.config.scaling_factor) image = image.unsqueeze(2) return image @spaces.GPU def gen_video(input_image, prompt, negative_prompt, diffusion_step, height, width, scfg_scale, use_dctinit, dct_coefficients, noise_level, motion_bucket_id, randomize_seed, seed): if randomize_seed: seed = random.randint(1, int(1e8)) torch.manual_seed(seed) scheduler = DDIMScheduler.from_pretrained(args.pretrained_model_path, subfolder="scheduler", beta_start=args.beta_start, beta_end=args.beta_end, beta_schedule=args.beta_schedule) videogen_pipeline = VideoGenPipeline(vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, scheduler=scheduler, unet=unet).to(device) # videogen_pipeline.enable_xformers_memory_efficient_attention() transform_video = transforms.Compose([ video_transforms.ToTensorVideo(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True), ]) if args.use_dct: base_content = prepare_image(input_image, vae_for_base_content, transform_video, device, dtype=torch.float64).to(device) else: base_content = prepare_image(input_image, vae_for_base_content, transform_video, device, dtype=torch.float16).to(device) if use_dctinit: # filter params print("Using DCT!") base_content_repeat = repeat(base_content, 'b c f h w -> b c (f r) h w', r=15).contiguous() # define filter freq_filter = dct_low_pass_filter(dct_coefficients=base_content, percentage=dct_coefficients) noise = torch.randn(1, 4, 15, 40, 64).to(device) # add noise to base_content diffuse_timesteps = torch.full((1,),int(noise_level)) diffuse_timesteps = diffuse_timesteps.long() # 3d content base_content_noise = scheduler.add_noise( original_samples=base_content_repeat.to(device), noise=noise, timesteps=diffuse_timesteps.to(device)) # 3d content latents = exchanged_mixed_dct_freq(noise=noise, base_content=base_content_noise, LPF_3d=freq_filter).to(dtype=torch.float16) base_content = base_content.to(dtype=torch.float16) videos = videogen_pipeline(prompt, negative_prompt=negative_prompt, latents=latents if use_dctinit else None, base_content=base_content, video_length=15, height=height, width=width, num_inference_steps=diffusion_step, guidance_scale=scfg_scale, motion_bucket_id=100-motion_bucket_id, enable_vae_temporal_decoder=args.enable_vae_temporal_decoder).video save_path = args.save_img_path + 'temp' + '.mp4' # torchvision.io.write_video(save_path, videos[0], fps=8, video_codec='h264', options={'crf': '10'}) imageio.mimwrite(save_path, videos[0], fps=8, quality=7) return save_path if not os.path.exists(args.save_img_path): os.makedirs(args.save_img_path) with gr.Blocks() as demo: gr.Markdown("