Spaces:
Running
on
Zero
Running
on
Zero
Commit
•
89df602
1
Parent(s):
4f5581d
Reuse the code of a working space (Python code) (#6)
Browse files- Reuse the code of a working space (Python code) (0dbf91c605718ef47c74ecae87adce105e776a50)
Co-authored-by: Fabrice TIERCELIN <Fabrice-TIERCELIN@users.noreply.huggingface.co>
app.py
CHANGED
@@ -1,44 +1,109 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
from diffusers.utils import export_to_video
|
4 |
import torch
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from PIL import Image
|
|
|
|
|
|
|
|
|
7 |
import spaces
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
# Define the Gradio interface
|
13 |
-
interface = gr.Interface(
|
14 |
-
fn=lambda img: generate_video(img),
|
15 |
-
inputs=gr.Image(type="pil"),
|
16 |
-
outputs=gr.Video(),
|
17 |
-
title="Stable Video Diffusion",
|
18 |
-
description="Upload an image to generate a video",
|
19 |
-
theme="soft"
|
20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
video_path = os.path.join("outputs", f"{base_count:06d}.mp4")
|
39 |
-
export_to_video(video_frames, video_path, fps=6)
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
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 |
import spaces
|
17 |
+
#gradio.helpers.CACHED_FOLDER = '/data/cache'
|
18 |
|
19 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
20 |
+
"multimodalart/stable-video-diffusion", torch_dtype=torch.float16, variant="fp16"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
)
|
22 |
+
pipe.to("cuda")
|
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 |
+
@spaces.GPU(duration=120)
|
29 |
+
def sample(
|
30 |
+
image: Image,
|
31 |
+
seed: Optional[int] = 42,
|
32 |
+
randomize_seed: bool = True,
|
33 |
+
motion_bucket_id: int = 127,
|
34 |
+
fps_id: int = 6,
|
35 |
+
version: str = "svd_xt",
|
36 |
+
cond_aug: float = 0.02,
|
37 |
+
decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
|
38 |
+
device: str = "cuda",
|
39 |
+
output_folder: str = "outputs",
|
40 |
+
):
|
41 |
+
if image.mode == "RGBA":
|
42 |
+
image = image.convert("RGB")
|
43 |
+
|
44 |
+
if(randomize_seed):
|
45 |
+
seed = random.randint(0, max_64_bit_int)
|
46 |
+
generator = torch.manual_seed(seed)
|
47 |
+
|
48 |
+
os.makedirs(output_folder, exist_ok=True)
|
49 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
50 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
51 |
|
52 |
+
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]
|
53 |
+
export_to_video(frames, video_path, fps=fps_id)
|
54 |
+
torch.manual_seed(seed)
|
55 |
+
|
56 |
+
return video_path, seed
|
57 |
|
58 |
+
def resize_image(image, output_size=(1024, 576)):
|
59 |
+
# Calculate aspect ratios
|
60 |
+
target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
|
61 |
+
image_aspect = image.width / image.height # Aspect ratio of the original image
|
62 |
|
63 |
+
# Resize then crop if the original image is larger
|
64 |
+
if image_aspect > target_aspect:
|
65 |
+
# Resize the image to match the target height, maintaining aspect ratio
|
66 |
+
new_height = output_size[1]
|
67 |
+
new_width = int(new_height * image_aspect)
|
68 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
69 |
+
# Calculate coordinates for cropping
|
70 |
+
left = (new_width - output_size[0]) / 2
|
71 |
+
top = 0
|
72 |
+
right = (new_width + output_size[0]) / 2
|
73 |
+
bottom = output_size[1]
|
74 |
+
else:
|
75 |
+
# Resize the image to match the target width, maintaining aspect ratio
|
76 |
+
new_width = output_size[0]
|
77 |
+
new_height = int(new_width / image_aspect)
|
78 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
79 |
+
# Calculate coordinates for cropping
|
80 |
+
left = 0
|
81 |
+
top = (new_height - output_size[1]) / 2
|
82 |
+
right = output_size[0]
|
83 |
+
bottom = (new_height + output_size[1]) / 2
|
84 |
|
85 |
+
# Crop the image
|
86 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
87 |
+
return cropped_image
|
|
|
|
|
88 |
|
89 |
+
with gr.Blocks() as demo:
|
90 |
+
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))
|
91 |
+
#### 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).
|
92 |
+
''')
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
image = gr.Image(label="Upload your image", type="pil")
|
96 |
+
generate_btn = gr.Button("Generate")
|
97 |
+
video = gr.Video()
|
98 |
+
with gr.Accordion("Advanced options", open=False):
|
99 |
+
seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
|
100 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
+
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)
|
102 |
+
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)
|
103 |
+
|
104 |
+
image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
|
105 |
+
generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, seed], api_name="video")
|
106 |
|
107 |
+
if __name__ == "__main__":
|
108 |
+
#demo.queue(max_size=20, api_open=False)
|
109 |
+
demo.launch(share=True, show_api=False)
|