Reuse the code from a working space (2/2)

#8
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import torch
3
  import os
4
  from glob import glob
@@ -11,18 +12,17 @@ from PIL import Image
11
 
12
  import uuid
13
  import random
14
- import spaces
15
  from huggingface_hub import hf_hub_download
 
16
 
17
  pipe = StableVideoDiffusionPipeline.from_pretrained(
18
  "vdo/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
19
  )
20
  pipe.to("cuda")
21
- pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
22
 
23
  max_64_bit_int = 2**63 - 1
24
 
25
- @spaces.GPU(duration=250)
26
  def sample(
27
  image: Image,
28
  seed: Optional[int] = 42,
@@ -50,7 +50,7 @@ def sample(
50
  export_to_video(frames, video_path, fps=fps_id)
51
  torch.manual_seed(seed)
52
 
53
- return video_path, seed
54
 
55
  def resize_image(image, output_size=(1024, 576)):
56
  # Calculate aspect ratios
@@ -84,22 +84,24 @@ def resize_image(image, output_size=(1024, 576)):
84
  return cropped_image
85
 
86
  with gr.Blocks() as demo:
87
- gr.Markdown('''# Stable Video Diffusion
 
88
  ''')
89
  with gr.Row():
90
- with gr.Column():
91
- image = gr.Image(label="Upload your image", type="pil")
92
- generate_btn = gr.Button("Generate")
93
- video = gr.Video()
94
- with gr.Accordion("Advanced options", open=False):
95
- seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
96
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
97
- 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)
98
- 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)
 
 
99
 
100
  image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
101
- generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, seed], api_name="video")
102
 
103
  if __name__ == "__main__":
104
- demo.queue(max_size=20, api_open=False)
105
  demo.launch(share=True, show_api=False)
 
1
  import gradio as gr
2
+ #import gradio.helpers
3
  import torch
4
  import os
5
  from glob import glob
 
12
 
13
  import uuid
14
  import random
 
15
  from huggingface_hub import hf_hub_download
16
+ import spaces
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("cuda")
 
22
 
23
  max_64_bit_int = 2**63 - 1
24
 
25
+ @spaces.GPU(duration=120)
26
  def sample(
27
  image: Image,
28
  seed: Optional[int] = 42,
 
50
  export_to_video(frames, video_path, fps=fps_id)
51
  torch.manual_seed(seed)
52
 
53
+ return video_path, frames, seed
54
 
55
  def resize_image(image, output_size=(1024, 576)):
56
  # Calculate aspect ratios
 
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")
93
+ with gr.Accordion("Advanced options", open=False):
94
+ seed = gr.Slider(label="Seed", value=42, randomize=True, minimum=0, maximum=max_64_bit_int, step=1)
95
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
96
+ 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)
97
+ 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)
98
+ generate_btn = gr.Button(value="Animate", variant="primary")
99
+ with gr.Column():
100
+ video = gr.Video(label="Generated video")
101
+ gallery = gr.Gallery(label="Generated frames")
102
 
103
  image.upload(fn=resize_image, inputs=image, outputs=image, queue=False)
104
+ generate_btn.click(fn=sample, inputs=[image, seed, randomize_seed, motion_bucket_id, fps_id], outputs=[video, gallery, seed], api_name="video")
105
 
106
  if __name__ == "__main__":
 
107
  demo.launch(share=True, show_api=False)