Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
#import gradio.helpers
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
from glob import glob
|
6 |
+
from pathlib import Path
|
7 |
+
from typing import Optional
|
8 |
+
|
9 |
+
from diffusers import StableVideoDiffusionPipeline
|
10 |
+
from diffusers.utils import load_image, export_to_video
|
11 |
+
from PIL import Image
|
12 |
+
|
13 |
+
import uuid
|
14 |
+
import random
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
|
17 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
18 |
+
torch.cuda.max_memory_allocated(device=device)
|
19 |
+
torch.cuda.empty_cache()
|
20 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained("https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16")
|
21 |
+
pipe.to("cuda")
|
22 |
+
pipe.enable_xformers_memory_efficient_attention()
|
23 |
+
#pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
24 |
+
#pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
|
25 |
+
|
26 |
+
max_64_bit_int = 2**63 - 1
|
27 |
+
|
28 |
+
def sample(
|
29 |
+
image: Image,
|
30 |
+
seed: Optional[int] = 42,
|
31 |
+
randomize_seed: bool = True,
|
32 |
+
motion_bucket_id: int = 127,
|
33 |
+
fps_id: int = 6,
|
34 |
+
version: str = "svd_xt",
|
35 |
+
cond_aug: float = 0.02,
|
36 |
+
decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
|
37 |
+
device: str = "cuda",
|
38 |
+
output_folder: str = "outputs",
|
39 |
+
):
|
40 |
+
if image.mode == "RGBA":
|
41 |
+
image = image.convert("RGB")
|
42 |
+
|
43 |
+
if(randomize_seed):
|
44 |
+
seed = random.randint(0, max_64_bit_int)
|
45 |
+
generator = torch.manual_seed(seed)
|
46 |
+
|
47 |
+
os.makedirs(output_folder, exist_ok=True)
|
48 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
49 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
50 |
+
|
51 |
+
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]
|
52 |
+
export_to_video(frames, video_path, fps=fps_id)
|
53 |
+
torch.manual_seed(seed)
|
54 |
+
|
55 |
+
return video_path, seed
|
56 |
+
|
57 |
+
def resize_image(image, output_size=(1024, 576)):
|
58 |
+
# Calculate aspect ratios
|
59 |
+
target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
|
60 |
+
image_aspect = image.width / image.height # Aspect ratio of the original image
|
61 |
+
|
62 |
+
# Resize then crop if the original image is larger
|
63 |
+
if image_aspect > target_aspect:
|
64 |
+
# Resize the image to match the target height, maintaining aspect ratio
|
65 |
+
new_height = output_size[1]
|
66 |
+
new_width = int(new_height * image_aspect)
|
67 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
68 |
+
# Calculate coordinates for cropping
|
69 |
+
left = (new_width - output_size[0]) / 2
|
70 |
+
top = 0
|
71 |
+
right = (new_width + output_size[0]) / 2
|
72 |
+
bottom = output_size[1]
|
73 |
+
else:
|
74 |
+
# Resize the image to match the target width, maintaining aspect ratio
|
75 |
+
new_width = output_size[0]
|
76 |
+
new_height = int(new_width / image_aspect)
|
77 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
78 |
+
# Calculate coordinates for cropping
|
79 |
+
left = 0
|
80 |
+
top = (new_height - output_size[1]) / 2
|
81 |
+
right = output_size[0]
|
82 |
+
bottom = (new_height + output_size[1]) / 2
|
83 |
+
|
84 |
+
# Crop the image
|
85 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
86 |
+
return cropped_image
|
87 |
+
|
88 |
+
with gr.Blocks() as demo:
|
89 |
+
gr.Markdown('''# Community demo for Stable Video Diffusion - Img2Vid - XT ([model](https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt), [paper](https://stability.ai/research/stable-video-diffusion-scaling-latent-video-diffusion-models-to-large-datasets), [stability's ui waitlist](https://stability.ai/contact))
|
90 |
+
#### Research release ([_non-commercial_](https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt/blob/main/LICENSE)): generate `4s` vid from a single image at (`25 frames` at `6 fps`). this demo uses [🧨 diffusers for low VRAM and fast generation](https://huggingface.co/docs/diffusers/main/en/using-diffusers/svd).
|
91 |
+
''')
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
image = gr.Image(label="Upload your image", type="pil")
|
95 |
+
generate_btn = gr.Button("Generate")
|
96 |
+
video = gr.Video()
|
97 |
+
with gr.Accordion("Advanced options", open=False):
|
98 |
+
seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
|
99 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
100 |
+
motion_bucket_id = gr.Slider(label="Motion bucket id", info="Controls how much motion to add/remove from the image", value=127, minimum=1, maximum=255)
|
101 |
+
fps_id = gr.Slider(label="Frames per second", info="The length of your video in seconds will be 25/fps", value=6, minimum=5, maximum=30)
|
102 |
+
|
103 |
+
image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
|
104 |
+
generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, seed], api_name="video")
|
105 |
+
gr.Examples(
|
106 |
+
examples=[
|
107 |
+
"images/blink_meme.png",
|
108 |
+
"images/confused2_meme.png",
|
109 |
+
"images/disaster_meme.png",
|
110 |
+
"images/distracted_meme.png",
|
111 |
+
"images/hide_meme.png",
|
112 |
+
"images/nazare_meme.png",
|
113 |
+
"images/success_meme.png",
|
114 |
+
"images/willy_meme.png",
|
115 |
+
"images/wink_meme.png"
|
116 |
+
],
|
117 |
+
inputs=image,
|
118 |
+
outputs=[video, seed],
|
119 |
+
fn=sample,
|
120 |
+
cache_examples=True,
|
121 |
+
)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
demo.queue(max_size=20, api_open=False)
|
125 |
+
demo.launch(show_api=False)
|