Spaces:
Sleeping
Sleeping
salomonsky
commited on
Commit
•
9c9643c
1
Parent(s):
5b1ae10
Update app.py
Browse files
app.py
CHANGED
@@ -12,80 +12,99 @@ import random
|
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
import spaces
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
pipe = StableVideoDiffusionPipeline.from_pretrained("vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16")
|
16 |
pipe.to("cpu")
|
17 |
-
max_64_bit_int = 2**63 - 1
|
18 |
-
@spaces.GPU(duration=120)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def sample(
|
21 |
-
image
|
22 |
-
seed
|
23 |
-
randomize_seed
|
24 |
-
motion_bucket_id
|
25 |
-
fps_id
|
26 |
-
version
|
27 |
-
cond_aug
|
28 |
-
decoding_t
|
29 |
-
device
|
30 |
-
output_folder
|
31 |
):
|
32 |
-
if
|
33 |
-
|
34 |
-
|
35 |
-
if(randomize_seed):
|
36 |
-
seed = random.randint(0, max_64_bit_int)
|
37 |
-
generator = torch.manual_seed(seed)
|
38 |
|
39 |
-
os.makedirs(output_folder, exist_ok=True)
|
40 |
-
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
41 |
-
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
42 |
|
43 |
-
frames =
|
44 |
-
|
45 |
-
torch.manual_seed(seed)
|
46 |
|
47 |
-
return video_path, frames, seed
|
48 |
|
49 |
-
def resize_image(image, output_size=(1024, 576)):
|
50 |
-
target_aspect = output_size[0] / output_size[1]
|
51 |
-
image_aspect = image.width / image.height
|
52 |
-
|
53 |
-
if image_aspect > target_aspect:
|
54 |
-
new_height = output_size[1]
|
55 |
-
new_width = int(new_height * image_aspect)
|
56 |
-
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
57 |
-
left = (new_width - output_size[0]) / 2
|
58 |
-
top = 0
|
59 |
-
right = (new_width + output_size[0]) / 2
|
60 |
-
bottom = output_size[1]
|
61 |
-
else:
|
62 |
-
new_width = output_size[0]
|
63 |
-
new_height = int(new_width / image_aspect)
|
64 |
-
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
65 |
-
left = 0
|
66 |
-
top = (new_height - output_size[1]) / 2
|
67 |
-
right = output_size[0]
|
68 |
-
bottom = (new_height + output_size[1]) / 2
|
69 |
-
|
70 |
-
cropped_image = resized_image.crop((left, top, right, bottom))
|
71 |
-
return cropped_image
|
72 |
|
73 |
with gr.Blocks() as demo:
|
74 |
-
with gr.Row():
|
75 |
-
with gr.Column():
|
76 |
-
image = gr.Image(label="Upload your image", type="pil")
|
77 |
-
with gr.Accordion("Advanced options", open=False):
|
78 |
-
seed = gr.Slider(label="Seed", value=
|
79 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
80 |
-
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)
|
81 |
-
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)
|
82 |
-
generate_btn = gr.Button(value="Animate", variant="primary")
|
83 |
-
with gr.Column():
|
84 |
-
video = gr.Video(label="Generated video")
|
85 |
-
gallery = gr.Gallery(label="Generated frames")
|
86 |
-
|
87 |
-
image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
|
88 |
-
generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, gallery, seed], api_name="video")
|
|
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
-
demo.launch(share=True, show_api=False)
|
|
|
12 |
from huggingface_hub import hf_hub_download
|
13 |
import spaces
|
14 |
|
15 |
+
|
16 |
+
MAX_64_BIT_INT = 2**63 - 1
|
17 |
+
DEFAULT_SEED = 42
|
18 |
+
DEFAULT_OUTPUT_FOLDER = "outputs"
|
19 |
+
|
20 |
+
|
21 |
pipe = StableVideoDiffusionPipeline.from_pretrained("vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16")
|
22 |
pipe.to("cpu")
|
|
|
|
|
23 |
|
24 |
+
|
25 |
+
def resize_image(image, output_size=(1024, 576)):
|
26 |
+
target_aspect = output_size[0] / output_size[1]
|
27 |
+
image_aspect = image.width / image.height
|
28 |
+
|
29 |
+
if image_aspect > target_aspect:
|
30 |
+
new_height = output_size[1]
|
31 |
+
new_width = int(new_height * image_aspect)
|
32 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
33 |
+
left = (new_width - output_size[0]) / 2
|
34 |
+
top = 0
|
35 |
+
right = (new_width + output_size[0]) / 2
|
36 |
+
bottom = output_size[1]
|
37 |
+
else:
|
38 |
+
new_width = output_size[0]
|
39 |
+
new_height = int(new_width / image_aspect)
|
40 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
41 |
+
left = 0
|
42 |
+
top = (new_height - output_size[1]) / 2
|
43 |
+
right = output_size[0]
|
44 |
+
bottom = (new_height + output_size[1]) / 2
|
45 |
+
|
46 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
47 |
+
return cropped_image
|
48 |
+
|
49 |
+
|
50 |
+
def generate_video(image, seed, motion_bucket_id, fps_id):
|
51 |
+
if image.mode == "RGBA":
|
52 |
+
image = image.convert("RGB")
|
53 |
+
|
54 |
+
generator = torch.manual_seed(seed)
|
55 |
+
frames = pipe(image, decode_chunk_size=3, generator=generator, motion_bucket_id=motion_bucket_id, noise_aug_strength=0.1, num_frames=25).frames[0]
|
56 |
+
return frames
|
57 |
+
|
58 |
+
|
59 |
+
def export_video(frames, video_path, fps_id):
|
60 |
+
export_to_video(frames, video_path, fps=fps_id)
|
61 |
+
|
62 |
+
|
63 |
+
@spaces.GPU(duration=120)
|
64 |
def sample(
|
65 |
+
image,
|
66 |
+
seed=DEFAULT_SEED,
|
67 |
+
randomize_seed=True,
|
68 |
+
motion_bucket_id=127,
|
69 |
+
fps_id=6,
|
70 |
+
version="svd_xt",
|
71 |
+
cond_aug=0.02,
|
72 |
+
decoding_t=3,
|
73 |
+
device="cuda",
|
74 |
+
output_folder=DEFAULT_OUTPUT_FOLDER,
|
75 |
):
|
76 |
+
if randomize_seed:
|
77 |
+
seed = random.randint(0, MAX_64_BIT_INT)
|
78 |
+
generator = torch.manual_seed(seed)
|
|
|
|
|
|
|
79 |
|
80 |
+
os.makedirs(output_folder, exist_ok=True)
|
81 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
82 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
83 |
|
84 |
+
frames = generate_video(image, seed, motion_bucket_id, fps_id)
|
85 |
+
export_video(frames, video_path, fps_id)
|
86 |
+
torch.manual_seed(seed)
|
87 |
|
88 |
+
return video_path, frames, seed
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
with gr.Blocks() as demo:
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
image = gr.Image(label="Upload your image", type="pil")
|
95 |
+
with gr.Accordion("Advanced options", open=False):
|
96 |
+
seed = gr.Slider(label="Seed", value=DEFAULT_SEED, randomize=True, minimum=0, maximum=MAX_64_BIT_INT, step=1)
|
97 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
+
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)
|
99 |
+
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)
|
100 |
+
generate_btn = gr.Button(value="Animate", variant="primary")
|
101 |
+
with gr.Column():
|
102 |
+
video = gr.Video(label="Generated video")
|
103 |
+
gallery = gr.Gallery(label="Generated frames")
|
104 |
+
|
105 |
+
image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
|
106 |
+
generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, gallery, seed], api_name="video")
|
107 |
+
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
+
demo.launch(share=True, show_api=False)
|