mrcuddle commited on
Commit
4ef7343
·
verified ·
1 Parent(s): 0d22f81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -38
app.py CHANGED
@@ -1,51 +1,34 @@
1
- import torch
2
- from PIL import Image
3
- import imageio
4
- from diffusers import StableVideoDiffusionPipeline
5
- from diffusers.utils import load_image, export_to_video
6
  import gradio as gr
7
- import spaces
 
8
 
9
- # Load the pipeline
10
- pipe = StableVideoDiffusionPipeline.from_pretrained(
11
- "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
12
- )
13
  pipe.to("cuda")
14
- pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
15
- pipe.enable_model_cpu_offload()
16
- pipe.unet.enable_forward_chunking()
17
-
18
- @spaces.GPU(duration=300)
19
- def generate_video(image, seed=42, fps=7, motion_bucket_id=180, noise_aug_strength=0.1):
20
- # Resize the image
21
- image = image.resize((1024, 576))
22
-
23
- # Set the generator seed
24
- generator = torch.manual_seed(seed)
25
-
26
- # Generate the frames
27
- frames = pipe(image, decode_chunk_size=2, generator=generator, num_frames=25, motion_bucket_id=motion_bucket_id, noise_aug_strength=noise_aug_strength).frames[0]
28
-
29
- # Export the frames to a video
30
- output_path = "generated.mp4"
31
- export_to_video(frames, output_path, fps=fps)
32
 
33
- return output_path
 
 
 
34
 
35
  # Create the Gradio interface
36
  iface = gr.Interface(
37
  fn=generate_video,
38
  inputs=[
39
- gr.Image(type="pil", label="Upload Image"),
40
- gr.Number(label="Seed", value=42),
41
- gr.Number(label="FPS", value=7),
42
- gr.Number(label="Motion Bucket ID", value=180),
43
- gr.Number(label="Noise Aug Strength", value=0.1)
44
  ],
45
- outputs=gr.Video(label="Generated Video"),
46
- title="Stable Video Diffusion",
47
- description="Generate a video from an uploaded image using Stable Video Diffusion."
48
  )
49
 
50
  # Launch the interface
51
- iface.launch()
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from diffusers import StableVideoDiffusionPipeline, EulerDiscreteScheduler
3
+ import torch
4
 
5
+ # Load the Stable Video Diffusion model
6
+ model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
7
+ pipe = StableVideoDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="main")
8
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
9
  pipe.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def generate_video(image, prompt, num_frames=25, resolution=(576, 1024)):
12
+ # Generate the video
13
+ video_frames = pipe(prompt, image=image, num_frames=num_frames, height=resolution[0], width=resolution[1]).frames
14
+ return video_frames
15
 
16
  # Create the Gradio interface
17
  iface = gr.Interface(
18
  fn=generate_video,
19
  inputs=[
20
+ gr.inputs.Image(type="pil", label="Upload Image"),
21
+ gr.inputs.Textbox(lines=2, placeholder="Enter prompt...", label="Prompt"),
22
+ gr.inputs.Slider(1, 50, step=1, default=25, label="Number of Frames"),
23
+ gr.inputs.Number(label="Resolution Height", default=576),
24
+ gr.inputs.Number(label="Resolution Width", default=1024)
25
  ],
26
+ outputs=gr.outputs.Video(label="Generated Video"),
27
+ title="Image to Video with Stable Diffusion XT",
28
+ description="Upload an image and enter a prompt to generate a video."
29
  )
30
 
31
  # Launch the interface
32
+ if __name__ == "__main__":
33
+ iface.launch()
34
+