mrcuddle commited on
Commit
49dc777
·
verified ·
1 Parent(s): 443ba67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -2,23 +2,18 @@ import torch
2
  import gradio as gr
3
  from diffusers import StableVideoDiffusionPipeline
4
  from diffusers.utils import load_image, export_to_video
5
- import os
6
  import spaces
7
 
8
  # Check if GPU is available
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- # Load the pipeline with optimizations
12
  pipeline = StableVideoDiffusionPipeline.from_pretrained(
13
  "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
14
  )
15
  pipeline.to(device)
16
 
17
- # Enable forward chunking to reduce memory usage
18
- pipeline.unet.enable_forward_chunking(chunk_size=1)
19
-
20
- @spaces.GPU
21
- # Define the video generation function
22
  def generate_video(image_path, seed):
23
  # Load and preprocess the image
24
  image = load_image(image_path)
@@ -37,21 +32,28 @@ def generate_video(image_path, seed):
37
  return output_video_path
38
 
39
  # Create the Gradio interface
40
- iface = gr.Interface(
41
- fn=generate_video,
42
- inputs=[
43
- gr.Image(type="filepath", label="Upload Image"),
44
- gr.Number(label="Seed", value=666666)
45
- ],
46
- outputs=gr.Video(label="Generated Video"),
47
- title="Stable Video Diffusion",
48
- description="Generate a video from an uploaded image using Stable Video Diffusion.",
49
- examples=[
50
- ["example.jpeg", 666666]
51
- ],
52
- cache_examples=True # Enable caching of examples
53
- )
 
 
 
 
 
 
 
54
 
55
  # Launch the interface
56
  if __name__ == "__main__":
57
- iface.launch()
 
2
  import gradio as gr
3
  from diffusers import StableVideoDiffusionPipeline
4
  from diffusers.utils import load_image, export_to_video
 
5
  import spaces
6
 
7
  # Check if GPU is available
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
+ # Load the pipeline
11
  pipeline = StableVideoDiffusionPipeline.from_pretrained(
12
  "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
13
  )
14
  pipeline.to(device)
15
 
16
+ @spaces.GPU(duration=120)
 
 
 
 
17
  def generate_video(image_path, seed):
18
  # Load and preprocess the image
19
  image = load_image(image_path)
 
32
  return output_video_path
33
 
34
  # Create the Gradio interface
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("# Stable Video Diffusion")
37
+ gr.Markdown("Generate a video from an uploaded image using Stable Video Diffusion.")
38
+
39
+ with gr.Row():
40
+ with gr.Column():
41
+ image_input = gr.Image(type="filepath", label="Upload Image")
42
+ seed_input = gr.Number(label="Seed", value=666666)
43
+ generate_button = gr.Button("Generate Video")
44
+ with gr.Column():
45
+ video_output = gr.Video(label="Generated Video")
46
+
47
+ with gr.Row():
48
+ example_image = gr.Image("example.jpeg", label="Example Image")
49
+ example_video = gr.Video("generated.mp4", label="Example Video")
50
+
51
+ generate_button.click(
52
+ fn=generate_video,
53
+ inputs=[image_input, seed_input],
54
+ outputs=video_output
55
+ )
56
 
57
  # Launch the interface
58
  if __name__ == "__main__":
59
+ demo.launch()