Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ pipeline = StableVideoDiffusionPipeline.from_pretrained(
|
|
| 14 |
pipeline.to(device)
|
| 15 |
|
| 16 |
@spaces.GPU(duration=120)
|
| 17 |
-
def generate_video(image_path, seed):
|
| 18 |
# Load and preprocess the image
|
| 19 |
image = load_image(image_path)
|
| 20 |
image = image.resize((1024, 576))
|
|
@@ -25,9 +25,13 @@ def generate_video(image_path, seed):
|
|
| 25 |
# Generate the video frames
|
| 26 |
frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Export the frames to a video file
|
| 29 |
output_video_path = "generated.mp4"
|
| 30 |
-
export_to_video(frames, output_video_path, fps=
|
| 31 |
|
| 32 |
return output_video_path
|
| 33 |
|
|
@@ -35,25 +39,40 @@ def generate_video(image_path, seed):
|
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
gr.Markdown("# Stable Video Diffusion")
|
| 37 |
gr.Markdown("Generate a video from an uploaded image using Stable Video Diffusion.")
|
| 38 |
-
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
image_input = gr.Image(type="filepath", label="Upload Image")
|
| 42 |
seed_input = gr.Number(label="Seed", value=666666)
|
|
|
|
|
|
|
|
|
|
| 43 |
generate_button = gr.Button("Generate Video")
|
| 44 |
with gr.Column():
|
| 45 |
video_output = gr.Video(label="Generated Video")
|
| 46 |
-
|
| 47 |
with gr.Row():
|
| 48 |
-
example_image = gr.Image("example.jpeg", label="Example Image")
|
| 49 |
-
example_video = gr.Video("generated.mp4", label="Example Video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
generate_button.click(
|
| 52 |
fn=generate_video,
|
| 53 |
-
inputs=[image_input, seed_input],
|
| 54 |
outputs=video_output
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the interface
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
demo.launch()
|
|
|
|
| 14 |
pipeline.to(device)
|
| 15 |
|
| 16 |
@spaces.GPU(duration=120)
|
| 17 |
+
def generate_video(image_path, seed, fps, duration, use_duration):
|
| 18 |
# Load and preprocess the image
|
| 19 |
image = load_image(image_path)
|
| 20 |
image = image.resize((1024, 576))
|
|
|
|
| 25 |
# Generate the video frames
|
| 26 |
frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
|
| 27 |
|
| 28 |
+
# Calculate fps if duration is specified
|
| 29 |
+
if use_duration:
|
| 30 |
+
fps = len(frames) / duration
|
| 31 |
+
|
| 32 |
# Export the frames to a video file
|
| 33 |
output_video_path = "generated.mp4"
|
| 34 |
+
export_to_video(frames, output_video_path, fps=fps)
|
| 35 |
|
| 36 |
return output_video_path
|
| 37 |
|
|
|
|
| 39 |
with gr.Blocks() as demo:
|
| 40 |
gr.Markdown("# Stable Video Diffusion")
|
| 41 |
gr.Markdown("Generate a video from an uploaded image using Stable Video Diffusion.")
|
| 42 |
+
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
image_input = gr.Image(type="filepath", label="Upload Image")
|
| 46 |
seed_input = gr.Number(label="Seed", value=666666)
|
| 47 |
+
use_duration_toggle = gr.Checkbox(label="Use Duration Instead of FPS")
|
| 48 |
+
fps_input = gr.Number(label="FPS", value=25, minimum=1, maximum=60, visible=True)
|
| 49 |
+
duration_input = gr.Number(label="Duration (seconds)", value=1, minimum=1, maximum=60, visible=False)
|
| 50 |
generate_button = gr.Button("Generate Video")
|
| 51 |
with gr.Column():
|
| 52 |
video_output = gr.Video(label="Generated Video")
|
| 53 |
+
|
| 54 |
with gr.Row():
|
| 55 |
+
example_image = gr.Image(value="example.jpeg", label="Example Image")
|
| 56 |
+
example_video = gr.Video(value="generated.mp4", label="Example Video")
|
| 57 |
+
|
| 58 |
+
def toggle_visibility(use_duration):
|
| 59 |
+
return {
|
| 60 |
+
fps_input: gr.update(visible=not use_duration),
|
| 61 |
+
duration_input: gr.update(visible=use_duration)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
use_duration_toggle.change(
|
| 65 |
+
fn=toggle_visibility,
|
| 66 |
+
inputs=use_duration_toggle,
|
| 67 |
+
outputs=[fps_input, duration_input]
|
| 68 |
+
)
|
| 69 |
|
| 70 |
generate_button.click(
|
| 71 |
fn=generate_video,
|
| 72 |
+
inputs=[image_input, seed_input, fps_input, duration_input, use_duration_toggle],
|
| 73 |
outputs=video_output
|
| 74 |
)
|
| 75 |
|
| 76 |
# Launch the interface
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|