Yhhxhfh commited on
Commit
70abd67
·
verified ·
1 Parent(s): b612fb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -57
app.py CHANGED
@@ -13,39 +13,29 @@ from diffusers import StableVideoDiffusionPipeline, StableVideoDragNUWAPipeline
13
  from diffusers.utils import export_to_video, export_to_gif
14
  from PIL import Image
15
 
 
 
 
 
 
16
  fps25Pipe = StableVideoDiffusionPipeline.from_pretrained(
17
  "vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
18
  )
19
- fps25Pipe.to("cuda")
20
 
21
  fps14Pipe = StableVideoDiffusionPipeline.from_pretrained(
22
  "stabilityai/stable-video-diffusion-img2vid", torch_dtype=torch.float16, variant="fp16"
23
  )
24
- fps14Pipe.to("cuda")
25
 
26
  dragnuwaPipe = StableVideoDragNUWAPipeline.from_pretrained(
27
  "a-r-r-o-w/dragnuwa-svd", torch_dtype=torch.float16, variant="fp16", low_cpu_mem_usage=False, device_map=None
28
  )
29
- dragnuwaPipe.to("cuda")
30
 
31
  max_64_bit_int = 2**63 - 1
32
 
33
- def animate(
34
- image: Image,
35
- seed: Optional[int] = 42,
36
- randomize_seed: bool = True,
37
- motion_bucket_id: int = 127,
38
- fps_id: int = 25,
39
- noise_aug_strength: float = 0.1,
40
- decoding_t: int = 3,
41
- video_format: str = "mp4",
42
- frame_format: str = "webp",
43
- version: str = "auto",
44
- width: int = 1024,
45
- height: int = 576,
46
- motion_control: bool = False,
47
- num_inference_steps: int = 25
48
- ):
49
  start = time.time()
50
 
51
  if image is None:
@@ -108,36 +98,18 @@ def animate(
108
  str(secondes) + " sec."
109
 
110
  return [
111
- # Display for video
112
- gr.update(value = video_path, visible = video_format != "gif"),
113
- # Display for gif
114
- gr.update(value = gif_path, visible = video_format == "gif"),
115
- # Download button
116
- gr.update(label = "💾 Download animation in *." + video_format + " format", value=result_path, visible=True),
117
- # Frames
118
- gr.update(label = "Generated frames in *." + frame_format + " format", format = frame_format, value = frames, visible = True),
119
- # Used seed
120
  seed,
121
- # Information
122
- gr.update(value = information, visible = True),
123
- # Reset button
124
- gr.update(visible = True)
125
  ]
126
 
127
  @torch.no_grad()
128
  @spaces.GPU(queue=False)
129
- def animate_on_gpu(
130
- image_data: Union[Image.Image, List[Image.Image]],
131
- seed: Optional[int] = 42,
132
- motion_bucket_id: int = 127,
133
- fps_id: int = 6,
134
- noise_aug_strength: float = 0.1,
135
- decoding_t: int = 3,
136
- version: str = "svdxt",
137
- width: int = 1024,
138
- height: int = 576,
139
- num_inference_steps: int = 25
140
- ):
141
  generator = torch.manual_seed(seed)
142
 
143
  if version == "dragnuwa":
@@ -149,37 +121,29 @@ def animate_on_gpu(
149
 
150
 
151
  def resize_image(image, output_size=(1024, 576)):
152
- # Do not touch the image if the size is good
153
  if image.width == output_size[0] and image.height == output_size[1]:
154
  return image
155
 
156
- # Calculate aspect ratios
157
- target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
158
- image_aspect = image.width / image.height # Aspect ratio of the original image
159
 
160
- # Resize if the original image is larger
161
  if image_aspect > target_aspect:
162
- # Resize the image to match the target height, maintaining aspect ratio
163
  new_height = output_size[1]
164
  new_width = int(new_height * image_aspect)
165
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
166
- # Calculate coordinates for cropping
167
  left = (new_width - output_size[0]) / 2
168
  top = 0
169
  right = (new_width + output_size[0]) / 2
170
  bottom = output_size[1]
171
  else:
172
- # Resize the image to match the target width, maintaining aspect ratio
173
  new_width = output_size[0]
174
  new_height = int(new_width / image_aspect)
175
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
176
- # Calculate coordinates for cropping
177
  left = 0
178
  top = (new_height - output_size[1]) / 2
179
  right = output_size[0]
180
  bottom = (new_height + output_size[1]) / 2
181
 
182
- # Crop the image
183
  return resized_image.crop((left, top, right, bottom))
184
 
185
  def reset():
@@ -232,7 +196,7 @@ with gr.Blocks() as demo:
232
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
233
 
234
  generate_btn = gr.Button(value="🚀 Animate", variant="primary")
235
- reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible = False)
236
 
237
  with gr.Column():
238
  video_output = gr.Video(label="Generated video", format="mp4", autoplay=True, show_download_button=False)
@@ -266,7 +230,7 @@ with gr.Blocks() as demo:
266
  reset_btn
267
  ], api_name="video")
268
 
269
- reset_btn.click(fn = reset, inputs = [], outputs = [
270
  image,
271
  seed,
272
  randomize_seed,
@@ -281,7 +245,7 @@ with gr.Blocks() as demo:
281
  height,
282
  motion_control,
283
  num_inference_steps
284
- ], queue = False, show_progress = False)
285
 
286
  gr.Examples(
287
  examples=[
 
13
  from diffusers.utils import export_to_video, export_to_gif
14
  from PIL import Image
15
 
16
+ if torch.cuda.is_available():
17
+ device = "cuda"
18
+ else:
19
+ device = "cpu"
20
+
21
  fps25Pipe = StableVideoDiffusionPipeline.from_pretrained(
22
  "vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
23
  )
24
+ fps25Pipe.to(device)
25
 
26
  fps14Pipe = StableVideoDiffusionPipeline.from_pretrained(
27
  "stabilityai/stable-video-diffusion-img2vid", torch_dtype=torch.float16, variant="fp16"
28
  )
29
+ fps14Pipe.to(device)
30
 
31
  dragnuwaPipe = StableVideoDragNUWAPipeline.from_pretrained(
32
  "a-r-r-o-w/dragnuwa-svd", torch_dtype=torch.float16, variant="fp16", low_cpu_mem_usage=False, device_map=None
33
  )
34
+ dragnuwaPipe.to(device)
35
 
36
  max_64_bit_int = 2**63 - 1
37
 
38
+ def animate(image, seed=42, randomize_seed=True, motion_bucket_id=127, fps_id=25, noise_aug_strength=0.1, decoding_t=3, video_format="mp4", frame_format="webp", version="auto", width=1024, height=576, motion_control=False, num_inference_steps=25):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  start = time.time()
40
 
41
  if image is None:
 
98
  str(secondes) + " sec."
99
 
100
  return [
101
+ gr.update(value=video_path, visible=video_format != "gif"),
102
+ gr.update(value=gif_path, visible=video_format == "gif"),
103
+ gr.update(label="💾 Download animation in *." + video_format + " format", value=result_path, visible=True),
104
+ gr.update(label="Generated frames in *." + frame_format + " format", format=frame_format, value=frames, visible=True),
 
 
 
 
 
105
  seed,
106
+ gr.update(value=information, visible=True),
107
+ gr.update(visible=True)
 
 
108
  ]
109
 
110
  @torch.no_grad()
111
  @spaces.GPU(queue=False)
112
+ def animate_on_gpu(image_data, seed=42, motion_bucket_id=127, fps_id=6, noise_aug_strength=0.1, decoding_t=3, version="svdxt", width=1024, height=576, num_inference_steps=25):
 
 
 
 
 
 
 
 
 
 
 
113
  generator = torch.manual_seed(seed)
114
 
115
  if version == "dragnuwa":
 
121
 
122
 
123
  def resize_image(image, output_size=(1024, 576)):
 
124
  if image.width == output_size[0] and image.height == output_size[1]:
125
  return image
126
 
127
+ target_aspect = output_size[0] / output_size[1]
128
+ image_aspect = image.width / image.height
 
129
 
 
130
  if image_aspect > target_aspect:
 
131
  new_height = output_size[1]
132
  new_width = int(new_height * image_aspect)
133
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
 
134
  left = (new_width - output_size[0]) / 2
135
  top = 0
136
  right = (new_width + output_size[0]) / 2
137
  bottom = output_size[1]
138
  else:
 
139
  new_width = output_size[0]
140
  new_height = int(new_width / image_aspect)
141
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
 
142
  left = 0
143
  top = (new_height - output_size[1]) / 2
144
  right = output_size[0]
145
  bottom = (new_height + output_size[1]) / 2
146
 
 
147
  return resized_image.crop((left, top, right, bottom))
148
 
149
  def reset():
 
196
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
197
 
198
  generate_btn = gr.Button(value="🚀 Animate", variant="primary")
199
+ reset_btn = gr.Button(value="🧹 Reinit page", variant="stop", elem_id="reset_button", visible=False)
200
 
201
  with gr.Column():
202
  video_output = gr.Video(label="Generated video", format="mp4", autoplay=True, show_download_button=False)
 
230
  reset_btn
231
  ], api_name="video")
232
 
233
+ reset_btn.click(fn=reset, inputs=[], outputs=[
234
  image,
235
  seed,
236
  randomize_seed,
 
245
  height,
246
  motion_control,
247
  num_inference_steps
248
+ ], queue=False, show_progress=False)
249
 
250
  gr.Examples(
251
  examples=[