Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,89 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from diffusers import CogVideoXPipeline
|
| 3 |
from diffusers.utils import export_to_video
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
# 1
|
| 8 |
-
#
|
| 9 |
-
device =
|
| 10 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 11 |
"THUDM/CogVideoX1.5-5B",
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
-
# 2οΈβ£ Video generation function
|
| 21 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
def generate_video(prompt: str) -> str:
|
| 23 |
"""
|
| 24 |
-
Generates a ~10s MP4 video (161 frames @16
|
| 25 |
Returns the filepath to the saved video.
|
| 26 |
"""
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
num_inference_steps = 50
|
| 30 |
-
num_frames
|
| 31 |
-
fps
|
| 32 |
|
| 33 |
-
# Run the pipeline
|
| 34 |
-
|
| 35 |
prompt=prompt,
|
| 36 |
guidance_scale=guidance_scale,
|
| 37 |
num_inference_steps=num_inference_steps,
|
| 38 |
num_frames=num_frames,
|
| 39 |
fps=fps,
|
| 40 |
)
|
| 41 |
-
frames =
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
return file_path
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
# 3
|
| 49 |
-
#
|
| 50 |
-
with gr.Blocks(title="CogVideoX Text
|
| 51 |
gr.Markdown(
|
| 52 |
"""
|
| 53 |
-
|
| 54 |
-
Enter a descriptive prompt and generate
|
|
|
|
| 55 |
"""
|
| 56 |
)
|
| 57 |
with gr.Row():
|
| 58 |
prompt_input = gr.Textbox(
|
| 59 |
label="Prompt",
|
| 60 |
-
placeholder="e.g., A
|
| 61 |
lines=2,
|
| 62 |
)
|
| 63 |
-
|
| 64 |
video_output = gr.Video(
|
| 65 |
label="Generated Video",
|
| 66 |
-
format="mp4"
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
fn=generate_video,
|
| 71 |
inputs=prompt_input,
|
| 72 |
outputs=video_output,
|
| 73 |
)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
# 4
|
| 77 |
-
#
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch(
|
| 80 |
-
server_name="0.0.0.0",
|
| 81 |
-
server_port=7860,
|
| 82 |
-
ssr_mode=False,
|
| 83 |
-
|
| 84 |
-
debug=True # optional debug output
|
| 85 |
)
|
| 86 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import spaces
|
| 3 |
from diffusers import CogVideoXPipeline
|
| 4 |
from diffusers.utils import export_to_video
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# ------------------------------------------------------------------------------
|
| 8 |
+
# 1. Load and optimize the CogVideoX pipeline on CPU by default
|
| 9 |
+
# ------------------------------------------------------------------------------
|
| 10 |
+
device = torch.device("cpu")
|
| 11 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 12 |
"THUDM/CogVideoX1.5-5B",
|
| 13 |
+
).to(device)
|
| 14 |
+
# Memory optimizations for ZeroGPU or limited GPU environments
|
| 15 |
+
pipe.enable_model_cpu_offload()
|
| 16 |
+
pipe.vae.enable_slicing()
|
| 17 |
|
| 18 |
+
# ------------------------------------------------------------------------------
|
| 19 |
+
# 2. Decorated GPU function for ZeroGPU
|
| 20 |
+
# ------------------------------------------------------------------------------
|
| 21 |
+
@spaces.GPU(duration=180) # request up to 180s of GPU time
|
|
|
|
|
|
|
|
|
|
| 22 |
def generate_video(prompt: str) -> str:
|
| 23 |
"""
|
| 24 |
+
Generates a ~10s MP4 video (161 frames @16 FPS) from the given prompt.
|
| 25 |
Returns the filepath to the saved video.
|
| 26 |
"""
|
| 27 |
+
# Move pipeline to GPU for inference
|
| 28 |
+
pipe.to("cuda")
|
| 29 |
+
# Inference parameters
|
| 30 |
+
guidance_scale = 6.0
|
| 31 |
num_inference_steps = 50
|
| 32 |
+
num_frames = 161 # ~10s at 16 FPS
|
| 33 |
+
fps = 16
|
| 34 |
|
| 35 |
+
# Run the diffusion pipeline
|
| 36 |
+
result = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
guidance_scale=guidance_scale,
|
| 39 |
num_inference_steps=num_inference_steps,
|
| 40 |
num_frames=num_frames,
|
| 41 |
fps=fps,
|
| 42 |
)
|
| 43 |
+
frames = result.frames[0]
|
| 44 |
+
|
| 45 |
+
# Move pipeline back to CPU to free GPU memory
|
| 46 |
+
pipe.to("cpu")
|
| 47 |
|
| 48 |
+
# Export frames to MP4 for Gradio
|
| 49 |
+
return export_to_video(frames, "generated.mp4", fps=fps)
|
|
|
|
| 50 |
|
| 51 |
+
# ------------------------------------------------------------------------------
|
| 52 |
+
# 3. Define the Gradio interface
|
| 53 |
+
# ------------------------------------------------------------------------------
|
| 54 |
+
with gr.Blocks(title="CogVideoX Text-to-Video with ZeroGPU") as demo:
|
| 55 |
gr.Markdown(
|
| 56 |
"""
|
| 57 |
+
## ποΈ Text-to-Video Generator
|
| 58 |
+
Enter a descriptive prompt and generate up to 10s of video
|
| 59 |
+
powered by CogVideoX1.5-5B on Hugging Face ZeroGPU.
|
| 60 |
"""
|
| 61 |
)
|
| 62 |
with gr.Row():
|
| 63 |
prompt_input = gr.Textbox(
|
| 64 |
label="Prompt",
|
| 65 |
+
placeholder="e.g., A majestic eagle soaring over mountain peaks",
|
| 66 |
lines=2,
|
| 67 |
)
|
| 68 |
+
generate_btn = gr.Button("Generate Video")
|
| 69 |
video_output = gr.Video(
|
| 70 |
label="Generated Video",
|
| 71 |
+
format="mp4"
|
| 72 |
)
|
| 73 |
|
| 74 |
+
generate_btn.click(
|
| 75 |
fn=generate_video,
|
| 76 |
inputs=prompt_input,
|
| 77 |
outputs=video_output,
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# ------------------------------------------------------------------------------
|
| 81 |
+
# 4. Launch the Gradio app
|
| 82 |
+
# ------------------------------------------------------------------------------
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch(
|
| 85 |
+
server_name="0.0.0.0",
|
| 86 |
+
server_port=7860,
|
| 87 |
+
ssr_mode=False,
|
| 88 |
+
debug=True,
|
|
|
|
| 89 |
)
|
|
|