salomonsky commited on
Commit
b9f491e
·
verified ·
1 Parent(s): 1649481

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -17
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import gradio as gr
2
- #import gradio.helpers
3
  import torch
4
  import os
5
  from glob import glob
6
  from pathlib import Path
7
  from typing import Optional
8
-
9
  from diffusers import StableVideoDiffusionPipeline
10
  from diffusers.utils import load_image, export_to_video
11
  from PIL import Image
12
-
13
  import uuid
14
  import random
15
  from huggingface_hub import hf_hub_download
@@ -18,7 +15,7 @@ import spaces
18
  pipe = StableVideoDiffusionPipeline.from_pretrained(
19
  "vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
20
  )
21
- pipe.to("cuda")
22
 
23
  max_64_bit_int = 2**63 - 1
24
 
@@ -31,7 +28,7 @@ def sample(
31
  fps_id: int = 6,
32
  version: str = "svd_xt",
33
  cond_aug: float = 0.02,
34
- decoding_t: int = 3, # Number of frames decoded at a time! This eats most VRAM. Reduce if necessary.
35
  device: str = "cuda",
36
  output_folder: str = "outputs",
37
  ):
@@ -53,40 +50,30 @@ def sample(
53
  return video_path, frames, seed
54
 
55
  def resize_image(image, output_size=(1024, 576)):
56
- # Calculate aspect ratios
57
- target_aspect = output_size[0] / output_size[1] # Aspect ratio of the desired size
58
- image_aspect = image.width / image.height # Aspect ratio of the original image
59
 
60
- # Resize then crop if the original image is larger
61
  if image_aspect > target_aspect:
62
- # Resize the image to match the target height, maintaining aspect ratio
63
  new_height = output_size[1]
64
  new_width = int(new_height * image_aspect)
65
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
66
- # Calculate coordinates for cropping
67
  left = (new_width - output_size[0]) / 2
68
  top = 0
69
  right = (new_width + output_size[0]) / 2
70
  bottom = output_size[1]
71
  else:
72
- # Resize the image to match the target width, maintaining aspect ratio
73
  new_width = output_size[0]
74
  new_height = int(new_width / image_aspect)
75
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
76
- # Calculate coordinates for cropping
77
  left = 0
78
  top = (new_height - output_size[1]) / 2
79
  right = output_size[0]
80
  bottom = (new_height + output_size[1]) / 2
81
 
82
- # Crop the image
83
  cropped_image = resized_image.crop((left, top, right, bottom))
84
  return cropped_image
85
 
86
  with gr.Blocks() as demo:
87
- 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), [stability's ui waitlist](https://stability.ai/contact))
88
- #### 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`). this demo uses [🧨 diffusers for low VRAM and fast generation](https://huggingface.co/docs/diffusers/main/en/using-diffusers/svd).
89
- ''')
90
  with gr.Row():
91
  with gr.Column():
92
  image = gr.Image(label="Upload your image", type="pil")
 
1
  import gradio as gr
 
2
  import torch
3
  import os
4
  from glob import glob
5
  from pathlib import Path
6
  from typing import Optional
 
7
  from diffusers import StableVideoDiffusionPipeline
8
  from diffusers.utils import load_image, export_to_video
9
  from PIL import Image
 
10
  import uuid
11
  import random
12
  from huggingface_hub import hf_hub_download
 
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
 
 
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
  ):
 
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")