Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
+
import random
|
6 |
+
from glob import glob
|
7 |
+
from pathlib import Path
|
8 |
+
from typing import Optional
|
9 |
+
from diffusers import StableVideoDiffusionPipeline
|
10 |
+
from diffusers.utils import load_image, export_to_video
|
11 |
+
from PIL import Image
|
12 |
+
from huggingface_hub import hf_hub_download
|
13 |
+
|
14 |
+
|
15 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
16 |
+
"stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
|
17 |
+
)
|
18 |
+
pipe.to("cuda")
|
19 |
+
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
20 |
+
max_64_bit_int = 2**63 - 1
|
21 |
+
|
22 |
+
# Function to sample video from the input image
|
23 |
+
def sample(
|
24 |
+
image: Image,
|
25 |
+
seed: Optional[int] = 42,
|
26 |
+
randomize_seed: bool = True,
|
27 |
+
motion_bucket_id: int = 127,
|
28 |
+
fps_id: int = 6,
|
29 |
+
version: str = "svd_xt",
|
30 |
+
cond_aug: float = 0.02,
|
31 |
+
decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
|
32 |
+
device: str = "cuda",
|
33 |
+
output_folder: str = "outputs",
|
34 |
+
):
|
35 |
+
if image.mode == "RGBA":
|
36 |
+
image = image.convert("RGB")
|
37 |
+
if randomize_seed:
|
38 |
+
seed = random.randint(0, max_64_bit_int)
|
39 |
+
|
40 |
+
generator = torch.manual_seed(seed)
|
41 |
+
|
42 |
+
os.makedirs(output_folder, exist_ok=True)
|
43 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
44 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
45 |
+
frames = pipe(image, decode_chunk_size=decoding_t, generator=generator, motion_bucket_id=motion_bucket_id, noise_aug_strength=0.1, num_frames=25).frames[0]
|
46 |
+
export_to_video(frames, video_path, fps=fps_id)
|
47 |
+
torch.manual_seed(seed)
|
48 |
+
return video_path, seed
|
49 |
+
|
50 |
+
# Function to resize the uploaded image
|
51 |
+
def resize_image(image, output_size=(1024, 576)):
|
52 |
+
target_aspect = output_size[0] / output_size[1]
|
53 |
+
image_aspect = image.width / image.height
|
54 |
+
|
55 |
+
if image_aspect > target_aspect:
|
56 |
+
new_height = output_size[1]
|
57 |
+
new_width = int(new_height * image_aspect)
|
58 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
59 |
+
left = (new_width - output_size[0]) / 2
|
60 |
+
top = 0
|
61 |
+
right = (new_width + output_size[0]) / 2
|
62 |
+
bottom = output_size[1]
|
63 |
+
else:
|
64 |
+
new_width = output_size[0]
|
65 |
+
new_height = int(new_width / image_aspect)
|
66 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
67 |
+
left = 0
|
68 |
+
top = (new_height - output_size[1]) / 2
|
69 |
+
right = output_size[0]
|
70 |
+
bottom = (new_height + output_size[1]) / 2
|
71 |
+
|
72 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
73 |
+
return cropped_image
|
74 |
+
|
75 |
+
# Dynamically load image files from the 'images' directory
|
76 |
+
def get_example_images():
|
77 |
+
image_dir = "images/"
|
78 |
+
image_files = glob(os.path.join(image_dir, "*.png")) + glob(os.path.join(image_dir, "*.jpg"))
|
79 |
+
return image_files
|
80 |
+
|
81 |
+
# Gradio interface setup
|
82 |
+
with gr.Blocks() as demo:
|
83 |
+
gr.Markdown('''# Stable Video Diffusion using Image 2 Video XT
|
84 |
+
#### Research release: generate `4s` vid from a single image at (`25 frames` at `6 fps`).''')
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
image = gr.Image(label="Upload your image", type="pil")
|
89 |
+
generate_btn = gr.Button("Generate")
|
90 |
+
video = gr.Video()
|
91 |
+
|
92 |
+
with gr.Accordion("Advanced options", open=False):
|
93 |
+
seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
|
94 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
95 |
+
motion_bucket_id = gr.Slider(label="Motion bucket id", value=127, minimum=1, maximum=255)
|
96 |
+
fps_id = gr.Slider(label="Frames per second", value=6, minimum=5, maximum=30)
|
97 |
+
|
98 |
+
image.upload(fn
|