mrcuddle commited on
Commit
0b8d6b0
·
verified ·
1 Parent(s): c823092

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -1,28 +1,43 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Load the model and pipeline
6
- model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
7
- pipe = pipeline("text-to-video-generation", model=model_id, torch_dtype=torch.float16, device="cuda")
8
 
9
- def generate_video(image, prompt):
10
- # Generate the video from the image and prompt
11
- video = pipe(prompt, image, num_inference_steps=50, guidance_scale=7.5)
12
- return video
13
 
14
  # Create the Gradio interface
15
  iface = gr.Interface(
16
  fn=generate_video,
17
  inputs=[
18
- gr.Image(type="pil", label="Input Image"),
19
- gr.Textbox(lines=2, placeholder="Enter a prompt...", label="Prompt")
 
20
  ],
21
  outputs=gr.Video(label="Generated Video"),
22
- title="Stable Video Diffusion img2vid-xt",
23
- description="Generate a video from an image using the stabilityai/stable-video-diffusion-img2vid-xt model."
24
  )
25
 
26
  # Launch the interface
27
- if __name__ == "__main__":
28
- iface.launch()
 
 
 
1
  import torch
2
+ from PIL import Image
3
+ import imageio
4
+ from diffusers import StableVideoDiffusionPipeline
5
+ import gradio as gr
6
+
7
+ # Load the pipeline
8
+ pipe = StableVideoDiffusionPipeline.from_pretrained(
9
+ "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
10
+ )
11
+ pipe.enable_model_cpu_offload()
12
+
13
+ def generate_video(image, seed=42, fps=7):
14
+ # Resize the image
15
+ image = image.resize((1024, 576))
16
+
17
+ # Set the generator seed
18
+ generator = torch.manual_seed(seed)
19
+
20
+ # Generate the frames
21
+ frames = pipe(image, decode_chunk_size=8, generator=generator).frames[0]
22
 
23
+ # Export the frames to a video
24
+ output_path = "generated.mp4"
25
+ imageio.mimwrite(output_path, frames, fps=fps)
26
 
27
+ return output_path
 
 
 
28
 
29
  # Create the Gradio interface
30
  iface = gr.Interface(
31
  fn=generate_video,
32
  inputs=[
33
+ gr.Image(type="pil", label="Upload Image"),
34
+ gr.Number(label="Seed", value=42),
35
+ gr.Number(label="FPS", value=7)
36
  ],
37
  outputs=gr.Video(label="Generated Video"),
38
+ title="Stable Video Diffusion",
39
+ description="Generate a video from an uploaded image using Stable Video Diffusion."
40
  )
41
 
42
  # Launch the interface
43
+ iface.launch()