1
1
# Adding this at the very top of app.py to make 'generative-models' directory discoverable
2
- import sys
3
2
import os
4
- sys .path .append (os .path .join (os .path .dirname (__file__ ), 'generative-models' ))
3
+ import sys
4
+
5
+ sys .path .append (os .path .join (os .path .dirname (__file__ ), "generative-models" ))
5
6
6
7
import math
8
+ import random
9
+ import uuid
7
10
from glob import glob
8
11
from pathlib import Path
9
12
from typing import Optional
10
13
11
14
import cv2
15
+ import gradio as gr
12
16
import numpy as np
13
17
import torch
14
18
from einops import rearrange , repeat
15
19
from fire import Fire
20
+ from huggingface_hub import hf_hub_download
16
21
from omegaconf import OmegaConf
17
22
from PIL import Image
18
23
from torchvision .transforms import ToTensor
19
24
25
+ from scripts .sampling .simple_video_sample import (
26
+ get_batch , get_unique_embedder_keys_from_conditioner , load_model )
20
27
from scripts .util .detection .nsfw_and_watermark_dectection import \
21
28
DeepFloydDataFiltering
22
29
from sgm .inference .helpers import embed_watermark
23
30
from sgm .util import default , instantiate_from_config
24
- from scripts .sampling .simple_video_sample import load_model , get_unique_embedder_keys_from_conditioner , get_batch
25
-
26
- import gradio as gr
27
- import uuid
28
- import random
29
- from huggingface_hub import hf_hub_download
30
31
31
32
# To download all svd models
32
- #hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid-xt", filename="svd_xt.safetensors", local_dir="checkpoints")
33
- #hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid", filename="svd.safetensors", local_dir="checkpoints")
34
- #hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid-xt-1-1", filename="svd_xt_1_1.safetensors", local_dir="checkpoints")
33
+ # hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid-xt", filename="svd_xt.safetensors", local_dir="checkpoints")
34
+ # hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid", filename="svd.safetensors", local_dir="checkpoints")
35
+ # hf_hub_download(repo_id="stabilityai/stable-video-diffusion-img2vid-xt-1-1", filename="svd_xt_1_1.safetensors", local_dir="checkpoints")
35
36
36
37
37
38
# Define the repo, local directory and filename
38
- repo_id = "stabilityai/stable-video-diffusion-img2vid-xt-1-1" # replace with "stabilityai/stable-video-diffusion-img2vid-xt" or "stabilityai/stable-video-diffusion-img2vid" for other models
39
- filename = "svd_xt_1_1.safetensors" # replace with "svd_xt.safetensors" or "svd.safetensors" for other models
39
+ repo_id = "stabilityai/stable-video-diffusion-img2vid-xt-1-1" # replace with "stabilityai/stable-video-diffusion-img2vid-xt" or "stabilityai/stable-video-diffusion-img2vid" for other models
40
+ filename = "svd_xt_1_1.safetensors" # replace with "svd_xt.safetensors" or "svd.safetensors" for other models
40
41
local_dir = "checkpoints"
41
42
local_file_path = os .path .join (local_dir , filename )
42
43
43
44
# Check if the file already exists
44
45
if not os .path .exists (local_file_path ):
45
46
# If the file doesn't exist, download it
46
- hf_hub_download (
47
- repo_id = repo_id ,
48
- filename = filename ,
49
- local_dir = local_dir
50
- )
47
+ hf_hub_download (repo_id =repo_id , filename = filename , local_dir = local_dir )
51
48
print ("File downloaded." )
52
49
else :
53
50
print ("File already exists. No need to download." )
54
51
55
52
56
- version = "svd_xt_1_1" # replace with 'svd_xt' or 'svd' for other models
53
+ version = "svd_xt_1_1" # replace with 'svd_xt' or 'svd' for other models
57
54
device = "cuda"
58
55
max_64_bit_int = 2 ** 63 - 1
59
56
71
68
num_steps ,
72
69
)
73
70
71
+
74
72
def sample (
75
73
input_path : str = "assets/test_image.png" , # Can either be image file or folder with image files
76
74
seed : Optional [int ] = None ,
@@ -82,18 +80,18 @@ def sample(
82
80
decoding_t : int = 7 , # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
83
81
device : str = "cuda" ,
84
82
output_folder : str = "outputs" ,
85
- progress = gr .Progress (track_tqdm = True )
83
+ progress = gr .Progress (track_tqdm = True ),
86
84
):
87
85
"""
88
86
Simple script to generate a single sample conditioned on an image `input_path` or multiple images, one for each
89
87
image file in folder `input_path`. If you run out of VRAM, try decreasing `decoding_t`.
90
88
"""
91
- fps_id = int (fps_id ) # casting float slider values to int)
92
- if ( randomize_seed ) :
89
+ fps_id = int (fps_id ) # casting float slider values to int)
90
+ if randomize_seed :
93
91
seed = random .randint (0 , max_64_bit_int )
94
-
92
+
95
93
torch .manual_seed (seed )
96
-
94
+
97
95
path = Path (input_path )
98
96
all_img_paths = []
99
97
if path .is_file ():
@@ -223,7 +221,7 @@ def denoiser(input, sigma, c):
223
221
frame = cv2 .cvtColor (frame , cv2 .COLOR_RGB2BGR )
224
222
writer .write (frame )
225
223
writer .release ()
226
-
224
+
227
225
return video_path , seed
228
226
229
227
@@ -260,24 +258,51 @@ def resize_image(image_path, output_size=(1024, 576)):
260
258
261
259
return cropped_image
262
260
261
+
263
262
with gr .Blocks () as demo :
264
- 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))
263
+ gr .Markdown (
264
+ """# 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))
265
265
#### 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`). Generation takes ~60s in an A100. [Join the waitlist for Stability's upcoming web experience](https://stability.ai/contact).
266
- ''' )
267
- with gr .Row ():
268
- with gr .Column ():
269
- image = gr .Image (label = "Upload your image" , type = "filepath" )
270
- generate_btn = gr .Button ("Generate" )
271
- video = gr .Video ()
272
- with gr .Accordion ("Advanced options" , open = False ):
273
- seed = gr .Slider (label = "Seed" , value = 42 , randomize = True , minimum = 0 , maximum = max_64_bit_int , step = 1 )
274
- randomize_seed = gr .Checkbox (label = "Randomize seed" , value = True )
275
- 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 )
276
- 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 )
277
-
278
- image .upload (fn = resize_image , inputs = image , outputs = image , queue = False )
279
- generate_btn .click (fn = sample , inputs = [image , seed , randomize_seed , motion_bucket_id , fps_id ], outputs = [video , seed ], api_name = "video" )
280
-
266
+ """
267
+ )
268
+ with gr .Row ():
269
+ with gr .Column ():
270
+ image = gr .Image (label = "Upload your image" , type = "filepath" )
271
+ generate_btn = gr .Button ("Generate" )
272
+ video = gr .Video ()
273
+ with gr .Accordion ("Advanced options" , open = False ):
274
+ seed = gr .Slider (
275
+ label = "Seed" ,
276
+ value = 42 ,
277
+ randomize = True ,
278
+ minimum = 0 ,
279
+ maximum = max_64_bit_int ,
280
+ step = 1 ,
281
+ )
282
+ randomize_seed = gr .Checkbox (label = "Randomize seed" , value = True )
283
+ motion_bucket_id = gr .Slider (
284
+ label = "Motion bucket id" ,
285
+ info = "Controls how much motion to add/remove from the image" ,
286
+ value = 127 ,
287
+ minimum = 1 ,
288
+ maximum = 255 ,
289
+ )
290
+ fps_id = gr .Slider (
291
+ label = "Frames per second" ,
292
+ info = "The length of your video in seconds will be 25/fps" ,
293
+ value = 6 ,
294
+ minimum = 5 ,
295
+ maximum = 30 ,
296
+ )
297
+
298
+ image .upload (fn = resize_image , inputs = image , outputs = image , queue = False )
299
+ generate_btn .click (
300
+ fn = sample ,
301
+ inputs = [image , seed , randomize_seed , motion_bucket_id , fps_id ],
302
+ outputs = [video , seed ],
303
+ api_name = "video" ,
304
+ )
305
+
281
306
if __name__ == "__main__" :
282
307
demo .queue (max_size = 20 )
283
308
demo .launch (share = True )
0 commit comments