Spaces:
Build error
Build error
Update app.py
Browse files
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
|
|
|
|
| 8 |
|
| 9 |
-
# Load the
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 41 |
-
gr.
|
| 42 |
-
gr.Number(label="
|
| 43 |
-
gr.Number(label="
|
| 44 |
],
|
| 45 |
-
outputs=gr.Video(label="Generated Video"),
|
| 46 |
-
title="
|
| 47 |
-
description="
|
| 48 |
)
|
| 49 |
|
| 50 |
# Launch the interface
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|