mrcuddle commited on
Commit
2a768ff
·
verified ·
1 Parent(s): 0d3b369

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -49
app.py CHANGED
@@ -1,55 +1,48 @@
1
- import gradio as gr
2
- from diffusers import StableVideoDiffusionPipeline, EulerDiscreteScheduler
3
  import torch
 
 
4
  from PIL import Image
5
- import tempfile
6
- import imageio
7
- import spaces
8
-
9
- # Load the Stable Video Diffusion model
10
- model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
11
- try:
12
- pipe = StableVideoDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="main")
13
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
14
- pipe.to("cuda")
15
- except Exception as e:
16
- raise RuntimeError(f"Failed to load the model: {e}")
17
-
18
- @spaces.GPU
19
- def generate_video(image, num_frames=25, height=576, width=1024):
20
- try:
21
- # Convert the image to a format suitable for the pipeline
22
- image = Image.open(image)
23
- # Generate the video
24
- video_frames = pipe(image=image, num_frames=num_frames, height=height, width=width).frames
25
- # Save the video frames to a temporary file
26
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
27
- video_path = temp_video.name
28
- # Save the frames as a video using imageio
29
- imageio.mimsave(video_path, video_frames, fps=30)
30
- return video_path
31
- except Exception as e:
32
- raise RuntimeError(f"Failed to generate the video: {e}")
 
33
 
34
  # Create the Gradio interface
35
- with gr.Blocks() as demo:
36
- gr.Markdown("## Image to Video with Stable Diffusion XT")
37
- with gr.Row():
38
- with gr.Column():
39
- image_input = gr.Image(type="filepath", label="Upload Image")
40
- num_frames_input = gr.Slider(1, 50, step=1, value=25, label="Number of Frames")
41
- height_input = gr.Number(label="Resolution Height", value=576)
42
- width_input = gr.Number(label="Resolution Width", value=1024)
43
- run_button = gr.Button("Generate Video")
44
- with gr.Column():
45
- video_output = gr.Video(label="Generated Video")
46
-
47
- run_button.click(
48
- generate_video,
49
- inputs=[image_input, num_frames_input, height_input, width_input],
50
- outputs=video_output
51
- )
52
 
53
  # Launch the interface
54
- if __name__ == "__main__":
55
- demo.launch()
 
 
 
1
  import torch
2
+ import gradio as gr
3
+ from diffusers import StableVideoDiffusionPipeline
4
  from PIL import Image
5
+ import numpy as np
6
+ from moviepy.editor import ImageSequenceClip
7
+
8
+ # Load the pipeline
9
+ pipeline = StableVideoDiffusionPipeline.from_pretrained(
10
+ "stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
11
+ )
12
+ pipeline.enable_model_cpu_offload()
13
+
14
+ def generate_video(image, seed):
15
+ # Preprocess the image
16
+ image = Image.open(image)
17
+ image = image.resize((1024, 576))
18
+
19
+ # Set the generator seed
20
+ generator = torch.manual_seed(seed)
21
+
22
+ # Generate the video frames
23
+ frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
24
+
25
+ # Convert frames to a format suitable for video export
26
+ frames = [(frame * 255).astype(np.uint8) for frame in frames]
27
+
28
+ # Export the frames to a video file
29
+ clip = ImageSequenceClip(frames, fps=7)
30
+ output_video_path = "generated.mp4"
31
+ clip.write_videofile(output_video_path, codec="libx264")
32
+
33
+ return output_video_path
34
 
35
  # Create the Gradio interface
36
+ iface = gr.Interface(
37
+ fn=generate_video,
38
+ inputs=[
39
+ gr.Image(type="file", label="Upload Image"),
40
+ gr.Number(label="Seed", value=42)
41
+ ],
42
+ outputs=gr.Video(label="Generated Video"),
43
+ title="Stable Video Diffusion",
44
+ description="Generate a video from an uploaded image using Stable Video Diffusion."
45
+ )
 
 
 
 
 
 
 
46
 
47
  # Launch the interface
48
+ iface.launch()