import gradio as gr from image_to_video import model_i2v_fun, get_input, auto_inpainting, setup_seed from omegaconf import OmegaConf import torch from diffusers.utils.import_utils import is_xformers_available import torchvision from utils import mask_generation_before import os import cv2 config_path = "./configs/sample_i2v.yaml" args = OmegaConf.load(config_path) device = "cuda" if torch.cuda.is_available() else "cpu" css = """ h1 { text-align: center; } #component-0 { max-width: 730px; margin: auto; } """ def infer(prompt, image_inp, seed_inp, sampling_steps,width,height,infer_type): setup_seed(seed_inp) args.num_sampling_steps = sampling_steps img = cv2.imread(image_inp) new_size = [height,width] args.image_size = new_size if infer_type == 'ddpm': args.sample_method = 'ddpm' elif infer_type == 'ddim': args.sample_method = 'ddim' vae, model, text_encoder, diffusion = model_i2v_fun(args) vae.to(device) model.to(device) text_encoder.to(device) if args.use_fp16: vae.to(dtype=torch.float16) model.to(dtype=torch.float16) text_encoder.to(dtype=torch.float16) if args.enable_xformers_memory_efficient_attention and device=="cuda": if is_xformers_available(): model.enable_xformers_memory_efficient_attention() else: raise ValueError("xformers is not available. Make sure it is installed correctly") video_input, reserve_frames = get_input(image_inp, args) video_input = video_input.to(device).unsqueeze(0) mask = mask_generation_before(args.mask_type, video_input.shape, video_input.dtype, device) masked_video = video_input * (mask == 0) prompt = prompt + args.additional_prompt video_clip = auto_inpainting(args, video_input, masked_video, mask, prompt, vae, text_encoder, diffusion, model, device,) video_ = ((video_clip * 0.5 + 0.5) * 255).add_(0.5).clamp_(0, 255).to(dtype=torch.uint8).cpu().permute(0, 2, 3, 1) torchvision.io.write_video(os.path.join(args.save_img_path, prompt+ '.mp4'), video_, fps=8) return os.path.join(args.save_img_path, prompt+ '.mp4') # def clean(): # return gr.Image.update(value=None, visible=False), gr.Video.update(value=None) # return gr.Video.update(value=None) title = """
Apply SEINE to generate a video