Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from diffusers import CogVideoXPipeline
|
| 4 |
+
from diffusers.utils import export_to_video
|
| 5 |
+
|
| 6 |
+
# 1. Initialize the CogVideoX1.5β5BβSAT pipeline on ZeroGPU
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
pipe = CogVideoXPipeline.from_pretrained(
|
| 9 |
+
"THUDM/CogVideoX1.5-5B-SAT", # Highβquality 10s model :contentReference[oaicite:5]{index=5}
|
| 10 |
+
torch_dtype=torch.float16
|
| 11 |
+
).to(device)
|
| 12 |
+
pipe.enable_model_cpu_offload() # Memory optimization :contentReference[oaicite:6]{index=6}
|
| 13 |
+
pipe.enable_vae_slicing() # Further memory savings :contentReference[oaicite:7]{index=7}
|
| 14 |
+
|
| 15 |
+
# 2. Define the video generation function
|
| 16 |
+
def generate_video(prompt: str):
|
| 17 |
+
"""
|
| 18 |
+
Generates a 10βsecond video (160 frames at 16Β fps) for the given prompt.
|
| 19 |
+
Returns the path to the saved MP4 file.
|
| 20 |
+
"""
|
| 21 |
+
# 160 frames @16 fps β 10-second duration :contentReference[oaicite:8]{index=8}
|
| 22 |
+
result = pipe(
|
| 23 |
+
prompt,
|
| 24 |
+
num_frames=160,
|
| 25 |
+
fps=16,
|
| 26 |
+
)
|
| 27 |
+
frames = result.frames[0]
|
| 28 |
+
|
| 29 |
+
# Export frames to MP4; Gradio's Video component accepts file paths
|
| 30 |
+
video_path = export_to_video(frames, "generated.mp4", fps=16)
|
| 31 |
+
return video_path
|
| 32 |
+
|
| 33 |
+
# 3. Build the Gradio interface
|
| 34 |
+
with gr.Blocks(title="CogVideoX1.5β5BβSAT TextβtoβVideo") as demo:
|
| 35 |
+
gr.Markdown(
|
| 36 |
+
"""
|
| 37 |
+
# π₯ TextβtoβVideo with CogVideoX1.5β5BβSAT
|
| 38 |
+
Enter a prompt and generate up to **10Β s** of highβquality video.
|
| 39 |
+
Powered by THUDM/CogVideoX1.5β5BβSAT on Hugging Face ZeroGPU.
|
| 40 |
+
"""
|
| 41 |
+
)
|
| 42 |
+
with gr.Row():
|
| 43 |
+
prompt_input = gr.Textbox(
|
| 44 |
+
label="Video Prompt",
|
| 45 |
+
placeholder="e.g., A majestic eagle soaring over mountain peaks",
|
| 46 |
+
lines=2,
|
| 47 |
+
)
|
| 48 |
+
generate_btn = gr.Button("Generate Video")
|
| 49 |
+
video_output = gr.Video(label="Your Generated Video")
|
| 50 |
+
|
| 51 |
+
generate_btn.click(
|
| 52 |
+
fn=generate_video,
|
| 53 |
+
inputs=prompt_input,
|
| 54 |
+
outputs=video_output,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# 4. Launch (Spaces will handle server settings automatically)
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|