salomonsky commited on
Commit
14b2aaa
·
verified ·
1 Parent(s): b9c9676

Update app.py

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