8000 Black and isort · CodersSampling/generative-models@c51e4e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c51e4e3

Browse files
author
Tim Dockhorn
committed
Black and isort
1 parent 1e30a2d commit c51e4e3

File tree

4 files changed

+90
-55
lines changed

4 files changed

+90
-55
lines changed

scripts/demo/gradio_app.py

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
11
# Adding this at the very top of app.py to make 'generative-models' directory discoverable
2-
import sys
32
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"))
56

67
import math
8+
import random
9+
import uuid
710
from glob import glob
811
from pathlib import Path
912
from typing import Optional
1013

1114
import cv2
15+
import gradio as gr
1216
import numpy as np
1317
import torch
1418
from einops import rearrange, repeat
1519
from fire import Fire
20+
from huggingface_hub import hf_hub_download
1621
from omegaconf import OmegaConf
1722
from PIL import Image
1823
from torchvision.transforms import ToTensor
1924

25+
from scripts.sampling.simple_video_sample import (
26+
get_batch, get_unique_embedder_keys_from_conditioner, load_model)
2027
from scripts.util.detection.nsfw_and_watermark_dectection import \
2128
DeepFloydDataFiltering
2229
from sgm.inference.helpers import embed_watermark
2330
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
3031

3132
# 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")
3536

3637

3738
# 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
4041
local_dir = "checkpoints"
4142
local_file_path = os.path.join(local_dir, filename)
4243

4344
# Check if the file already exists
4445
if not os.path.exists(local_file_path):
4546
# 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)
5148
print("File downloaded.")
5249
else:
5350
print("File already exists. No need to download.")
5451

5552

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
5754
device = "cuda"
5855
max_64_bit_int = 2**63 - 1
5956

@@ -71,6 +68,7 @@
7168
num_steps,
7269
)
7370

71+
7472
def sample(
7573
input_path: str = "assets/test_image.png", # Can either be image file or folder with image files
7674
seed: Optional[int] = None,
@@ -82,18 +80,18 @@ def sample(
8280
decoding_t: int = 7, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
8381
device: str = "cuda",
8482
output_folder: str = "outputs",
85-
progress=gr.Progress(track_tqdm=True)
83+
progress=gr.Progress(track_tqdm=True),
8684
):
8785
"""
8886
Simple script to generate a single sample conditioned on an image `input_path` or multiple images, one for each
8987
image file in folder `input_path`. If you run out of VRAM, try decreasing `decoding_t`.
9088
"""
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:
9391
seed = random.randint(0, max_64_bit_int)
94-
92+
9593
torch.manual_seed(seed)
96-
94+
9795
path = Path(input_path)
9896
all_img_paths = []
9997
if path.is_file():
@@ -223,7 +221,7 @@ def denoiser(input, sigma, c):
223221
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
224222
writer.write(frame)
225223
writer.release()
226-
224+
227225
return video_path, seed
228226

229227

@@ -260,24 +258,51 @@ def resize_image(image_path, output_size=(1024, 576)):
260258

261259
return cropped_image
262260

261+
263262
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))
265265
#### 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+
281306
if __name__ == "__main__":
282307
demo.queue(max_size=20)
283308
demo.launch(share=True)

scripts/demo/turbo.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from streamlit_helpers import *
21
from st_keyup import st_keyup
2+
from streamlit_helpers import *
3+
34
from sgm.modules.diffusionmodules.sampling import EulerAncestralSampler
45

56
VERSION2SPECS = {
@@ -193,7 +194,7 @@ def decrement_counter():
193194

194195
with head_cols[2]:
195196
n_steps = st.number_input(label="number of steps", min_value=1, max_value=4)
196-
197+
197198
sampler = SubstepSampler(
198199
n_sample_steps=1,
199200
num_steps=1000,
@@ -203,8 +204,12 @@ def decrement_counter():
203204
),
204205
)
205206
sampler.n_sample_steps = n_steps
206-
default_prompt = "A cinematic shot of a baby racoon wearing an intricate italian priest robe."
207-
prompt = st_keyup("Enter a value", value=default_prompt, debounce=300, key="interactive_text")
207+
default_prompt = (
208+
"A cinematic shot of a baby racoon wearing an intricate italian priest robe."
209+
)
210+
prompt = st_keyup(
211+
"Enter a value", value=default_prompt, debounce=300, key="interactive_text"
212+
)
208213

209214
cols = st.columns([1, 5, 1])
210215
if mode != "skip":
@@ -217,7 +222,13 @@ def decrement_counter():
217222

218223
sampler.noise_sampler = SeededNoise(seed=st.session_state.seed)
219224
out = sample(
220-
model, sampler, H=512, W=512, seed=st.session_state.seed, prompt=prompt, filter=state.get("filter")
225+
model,
226+
sampler,
227+
H=512,
228+
W=512,
229+
seed=st.session_state.seed,
230+
prompt=prompt,
231+
filter=state.get("filter"),
221232
)
222233
with cols[1]:
223234
st.image(out[0])

sgm/modules/autoencoding/temporal_ae.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import torch
44
from einops import rearrange, repeat
55

6-
from sgm.modules.diffusionmodules.model import (
7-
XFORMERS_IS_AVAILABLE,
8-
AttnBlock,
9-
Decoder,
10-
MemoryEfficientAttnBlock,
11-
ResnetBlock,
12-
)
13-
from sgm.modules.diffusionmodules.openaimodel import ResBlock, timestep_embedding
6+
from sgm.modules.diffusionmodules.model import (XFORMERS_IS_AVAILABLE,
7+
AttnBlock, Decoder,
8+
MemoryEfficientAttnBlock,
9+
ResnetBlock)
10+
from sgm.modules.diffusionmodules.openaimodel import (ResBlock,
11+
timestep_embedding)
1412
from sgm.modules.video_attention import VideoTransformerBlock
1513
from sgm.util import partialclass
1614

sgm/modules/video_attention.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import torch
22

33
from ..modules.attention import *
4-
from ..modules.diffusionmodules.util import AlphaBlender, linear, timestep_embedding
4+
from ..modules.diffusionmodules.util import (AlphaBlender, linear,
5+
timestep_embedding)
56

67

78
class TimeMixSequential(nn.Sequential):

0 commit comments

Comments
 (0)
0