import spaces import gradio as gr import numpy as np from PIL import Image import cv2 from moviepy.editor import VideoFileClip from share_btn import community_icon_html, loading_icon_html, share_js import torch from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler from diffusers.utils import export_to_video def convert_mp4_to_frames(video_path, duration=3): # Read the video file video = cv2.VideoCapture(video_path) # Get the frames per second (fps) of the video fps = video.get(cv2.CAP_PROP_FPS) # Calculate the number of frames to extract num_frames = int(fps * duration) frames = [] frame_count = 0 # Iterate through each frame while True: # Read a frame ret, frame = video.read() # If the frame was not successfully read or we have reached the desired duration, break the loop if not ret or frame_count == num_frames: break # Convert BGR to RGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Append the frame to the list of frames frames.append(frame) frame_count += 1 # Release the video object video.release() # Convert the list of frames to a numpy array frames = np.array(frames) return frames @spaces.GPU(duration=120) def infer(prompt, video_in, denoise_strength): negative_prompt = "text, watermark, copyright, blurry, nsfw" video = convert_mp4_to_frames(video_in, duration=3) video_resized = [Image.fromarray(frame).resize((1024, 576)) for frame in video] pipe_xl = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_XL", torch_dtype=torch.float32, revision="refs/pr/17") pipe_xl.vae.enable_slicing() pipe_xl.scheduler = DPMSolverMultistepScheduler.from_config(pipe_xl.scheduler.config) pipe_xl.enable_model_cpu_offload() pipe_xl.to("cpu") video_frames = pipe_xl(prompt, negative_prompt=negative_prompt, video=video_resized, strength=denoise_strength).frames del pipe_xl #torch.cuda.empty_cache() video_path = export_to_video(video_frames, output_video_path="xl_result.mp4") return "xl_result.mp4", gr.Group.update(visible=True) css = """ #col-container {max-width: 510px; margin-left: auto; margin-right: auto;} a {text-decoration-line: underline; font-weight: 600;} .animate-spin { animation: spin 1s linear infinite; } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } #share-btn-container { display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; } #share-btn-container:hover { background-color: #060606; } #share-btn { all: initial; color: #ffffff; font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important; right:0; } #share-btn * { all: unset; } #share-btn-container div:nth-child(-n+2){ width: auto !important; min-height: 0px !important; } #share-btn-container .wrap { display: none !important; } #share-btn-container.hidden { display: none!important; } img[src*='#center'] { display: block; margin: auto; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown( """
This space is specifically designed for upscaling content made from
the zeroscope_v2_576w space using vid2vid.
Remember to use the same prompt that was used to generate the original clip.
For demo purpose, video length is limited to 3 seconds.