awacke1 commited on
Commit
e6b3471
1 Parent(s): 1f63fe5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,28 +1,22 @@
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
16
 
17
- #gradio.helpers.CACHED_FOLDER = '/data/cache'
18
-
19
  pipe = StableVideoDiffusionPipeline.from_pretrained(
20
  "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
21
  )
22
  pipe.to("cuda")
23
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
24
- #pipe.vae = torch.compile(pipe.vae, mode="reduce-overhead", fullgraph=True)
25
-
26
  max_64_bit_int = 2**63 - 1
27
 
28
  def sample(
@@ -39,19 +33,22 @@ def sample(
39
  ):
40
  if image.mode == "RGBA":
41
  image = image.convert("RGB")
42
-
43
  if(randomize_seed):
44
  seed = random.randint(0, max_64_bit_int)
45
  generator = torch.manual_seed(seed)
46
-
 
47
  os.makedirs(output_folder, exist_ok=True)
48
  base_count = len(glob(os.path.join(output_folder, "*.mp4")))
49
  video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
50
 
51
  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]
 
 
52
  export_to_video(frames, video_path, fps=fps_id)
53
  torch.manual_seed(seed)
54
-
 
55
  return video_path, seed
56
 
57
  def resize_image(image, output_size=(1024, 576)):
@@ -65,6 +62,7 @@ def resize_image(image, output_size=(1024, 576)):
65
  new_height = output_size[1]
66
  new_width = int(new_height * image_aspect)
67
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
 
68
  # Calculate coordinates for cropping
69
  left = (new_width - output_size[0]) / 2
70
  top = 0
@@ -75,6 +73,7 @@ def resize_image(image, output_size=(1024, 576)):
75
  new_width = output_size[0]
76
  new_height = int(new_width / image_aspect)
77
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
 
78
  # Calculate coordinates for cropping
79
  left = 0
80
  top = (new_height - output_size[1]) / 2
@@ -86,14 +85,18 @@ def resize_image(image, output_size=(1024, 576)):
86
  return cropped_image
87
 
88
  with gr.Blocks() as demo:
89
- 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))
90
- #### 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).
 
 
91
  ''')
 
92
  with gr.Row():
93
  with gr.Column():
94
  image = gr.Image(label="Upload your image", type="pil")
95
  generate_btn = gr.Button("Generate")
96
  video = gr.Video()
 
97
  with gr.Accordion("Advanced options", open=False):
98
  seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
99
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
@@ -104,15 +107,15 @@ with gr.Blocks() as demo:
104
  generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, seed], api_name="video")
105
  gr.Examples(
106
  examples=[
107
- "images/blink_meme.png",
108
- "images/confused2_meme.png",
109
- "images/disaster_meme.png",
110
- "images/distracted_meme.png",
111
- "images/hide_meme.png",
112
- "images/nazare_meme.png",
113
- "images/success_meme.png",
114
- "images/willy_meme.png",
115
- "images/wink_meme.png"
116
  ],
117
  inputs=image,
118
  outputs=[video, seed],
 
1
  import gradio as gr
 
2
  import torch
3
  import os
4
+ import uuid
5
+ import random
6
+
7
  from glob import glob
8
  from pathlib import Path
9
  from typing import Optional
 
10
  from diffusers import StableVideoDiffusionPipeline
11
  from diffusers.utils import load_image, export_to_video
12
  from PIL import Image
 
 
 
13
  from huggingface_hub import hf_hub_download
14
 
 
 
15
  pipe = StableVideoDiffusionPipeline.from_pretrained(
16
  "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
17
  )
18
  pipe.to("cuda")
19
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
 
 
20
  max_64_bit_int = 2**63 - 1
21
 
22
  def sample(
 
33
  ):
34
  if image.mode == "RGBA":
35
  image = image.convert("RGB")
 
36
  if(randomize_seed):
37
  seed = random.randint(0, max_64_bit_int)
38
  generator = torch.manual_seed(seed)
39
+
40
+ # Count completed mp4 videos and set the path
41
  os.makedirs(output_folder, exist_ok=True)
42
  base_count = len(glob(os.path.join(output_folder, "*.mp4")))
43
  video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
44
 
45
  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]
46
+
47
+ # Export frames to video
48
  export_to_video(frames, video_path, fps=fps_id)
49
  torch.manual_seed(seed)
50
+
51
+ # Return the video and seed
52
  return video_path, seed
53
 
54
  def resize_image(image, output_size=(1024, 576)):
 
62
  new_height = output_size[1]
63
  new_width = int(new_height * image_aspect)
64
  resized_image = image.resize((new_width, new_height), Image.LANCZOS)
65
+
66
  # Calculate coordinates for cropping
67
  left = (new_width - output_size[0]) / 2
68
  top = 0
 
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
+
77
  # Calculate coordinates for cropping
78
  left = 0
79
  top = (new_height - output_size[1]) / 2
 
85
  return cropped_image
86
 
87
  with gr.Blocks() as demo:
88
+ gr.Markdown('''# Stable Video Diffusion using Image 2 Video XT ([model](https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt),
89
+ [paper](https://stability.ai/research/stable-video-diffusion-scaling-latent-video-diffusion-models-to-large-datasets),
90
+ [stability's ui waitlist](https://stability.ai/contact))
91
+ #### 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).
92
  ''')
93
+
94
  with gr.Row():
95
  with gr.Column():
96
  image = gr.Image(label="Upload your image", type="pil")
97
  generate_btn = gr.Button("Generate")
98
  video = gr.Video()
99
+
100
  with gr.Accordion("Advanced options", open=False):
101
  seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
102
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
107
  generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, seed], api_name="video")
108
  gr.Examples(
109
  examples=[
110
+ "images/01.png",
111
+ "images/02.png",
112
+ "images/03.png",
113
+ "images/04.png",
114
+ "images/05.png",
115
+ "images/06.png",
116
+ "images/07.png",
117
+ "images/08.png",
118
+ "images/09.png"
119
  ],
120
  inputs=image,
121
  outputs=[video, seed],