Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,63 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
+
from diffusers import CogVideoXImageToVideoPipeline
|
| 5 |
+
from diffusers.utils import export_to_video, load_image
|
| 6 |
+
|
| 7 |
+
# Load model once
|
| 8 |
+
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
| 9 |
+
"THUDM/CogVideoX1.5-5B-I2V",
|
| 10 |
+
torch_dtype=torch.bfloat16
|
| 11 |
+
)
|
| 12 |
+
pipe.enable_sequential_cpu_offload()
|
| 13 |
+
pipe.vae.enable_tiling()
|
| 14 |
+
pipe.vae.enable_slicing()
|
| 15 |
+
|
| 16 |
+
def generate_video(image, prompt):
|
| 17 |
+
if image is None:
|
| 18 |
+
raise gr.Error("Please upload an input image")
|
| 19 |
+
if not prompt:
|
| 20 |
+
raise gr.Error("Please enter a text prompt")
|
| 21 |
+
|
| 22 |
+
# Load uploaded image
|
| 23 |
+
input_image = load_image(image)
|
| 24 |
+
|
| 25 |
+
# Generate video
|
| 26 |
+
video_frames = pipe(
|
| 27 |
+
prompt=prompt,
|
| 28 |
+
image=input_image,
|
| 29 |
+
num_videos_per_prompt=1,
|
| 30 |
+
num_inference_steps=50,
|
| 31 |
+
num_frames=81,
|
| 32 |
+
guidance_scale=6,
|
| 33 |
+
generator=torch.Generator(device="cuda").manual_seed(42),
|
| 34 |
+
).frames[0]
|
| 35 |
+
|
| 36 |
+
# Save to temporary file
|
| 37 |
+
output_path = f"output_{int(time.time())}.mp4"
|
| 38 |
+
export_to_video(video_frames, output_path, fps=8)
|
| 39 |
+
|
| 40 |
+
return output_path
|
| 41 |
+
|
| 42 |
+
with gr.Blocks(title="CogVideoX Image-to-Video") as demo:
|
| 43 |
+
gr.Markdown("# π₯ CogVideoX Image-to-Video Generation")
|
| 44 |
+
gr.Markdown("Transform images into videos using AI! Upload an image and enter a description to generate a video.")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
image_input = gr.Image(label="Input Image", type="filepath")
|
| 49 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Describe the video you want to generate...")
|
| 50 |
+
submit_btn = gr.Button("Generate Video")
|
| 51 |
+
|
| 52 |
+
with gr.Column():
|
| 53 |
+
video_output = gr.Video(label="Generated Video")
|
| 54 |
+
gr.Examples(examples=examples, inputs=[image_input, prompt_input])
|
| 55 |
+
|
| 56 |
+
submit_btn.click(
|
| 57 |
+
fn=generate_video,
|
| 58 |
+
inputs=[image_input, prompt_input],
|
| 59 |
+
outputs=video_output,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|