Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
# Create the Gradio interface
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
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 |
-
|
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()
|
|