Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,27 +4,46 @@ import gradio as gr
|
|
| 4 |
from diffusers import CogVideoXPipeline
|
| 5 |
from diffusers.utils import export_to_video
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
| 8 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 9 |
"THUDM/CogVideoX1.5-5B",
|
| 10 |
torch_dtype=torch.bfloat16
|
| 11 |
)
|
| 12 |
-
pipe.enable_model_cpu_offload()
|
| 13 |
-
pipe.vae.enable_slicing()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Run
|
| 28 |
output = pipe(
|
| 29 |
prompt=prompt,
|
| 30 |
num_inference_steps=steps,
|
|
@@ -32,53 +51,63 @@ def generate_video(prompt: str, steps: int, frames: int, fps: int, resolution: s
|
|
| 32 |
height=height,
|
| 33 |
width=width
|
| 34 |
)
|
| 35 |
-
|
| 36 |
|
| 37 |
-
# Export to MP4
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
gr.Markdown(
|
| 43 |
"""
|
| 44 |
-
# ποΈ
|
| 45 |
-
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
)
|
| 49 |
-
with gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
gen_button.click(
|
| 76 |
fn=generate_video,
|
| 77 |
-
inputs=[prompt_input, steps_slider, frames_slider, fps_slider,
|
| 78 |
outputs=video_output
|
| 79 |
)
|
| 80 |
|
| 81 |
-
#
|
|
|
|
|
|
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch(
|
| 84 |
server_name="0.0.0.0",
|
|
|
|
| 4 |
from diffusers import CogVideoXPipeline
|
| 5 |
from diffusers.utils import export_to_video
|
| 6 |
|
| 7 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
+
# 1. Load & optimize the CogVideoX pipeline with CPU offload
|
| 9 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 11 |
"THUDM/CogVideoX1.5-5B",
|
| 12 |
torch_dtype=torch.bfloat16
|
| 13 |
)
|
| 14 |
+
pipe.enable_model_cpu_offload() # auto move submodules between CPU/GPU
|
| 15 |
+
pipe.vae.enable_slicing() # slice VAE for extra VRAM savings
|
| 16 |
|
| 17 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
+
# 2. Resolution parsing & sanitization
|
| 19 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
def make_divisible_by_8(x: int) -> int:
|
| 21 |
+
return (x // 8) * 8
|
| 22 |
+
|
| 23 |
+
def parse_resolution(res_str: str):
|
| 24 |
"""
|
| 25 |
+
Convert strings like "480p" into (height, width) both divisible by 8
|
| 26 |
+
while preserving ~16:9 aspect ratio.
|
| 27 |
"""
|
| 28 |
+
h = int(res_str.rstrip("p"))
|
| 29 |
+
w = int(h * 16 / 9)
|
| 30 |
+
return make_divisible_by_8(h), make_divisible_by_8(w)
|
| 31 |
+
|
| 32 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
+
# 3. GPUβdecorated video generation function
|
| 34 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
@spaces.GPU(duration=600) # allow up to 10Β minutes of GPU time
|
| 36 |
+
def generate_video(
|
| 37 |
+
prompt: str,
|
| 38 |
+
steps: int,
|
| 39 |
+
frames: int,
|
| 40 |
+
fps: int,
|
| 41 |
+
resolution: str
|
| 42 |
+
) -> str:
|
| 43 |
+
# 3.1 Parse & sanitize resolution
|
| 44 |
+
height, width = parse_resolution(resolution)
|
| 45 |
|
| 46 |
+
# 3.2 Run the diffusion pipeline
|
| 47 |
output = pipe(
|
| 48 |
prompt=prompt,
|
| 49 |
num_inference_steps=steps,
|
|
|
|
| 51 |
height=height,
|
| 52 |
width=width
|
| 53 |
)
|
| 54 |
+
video_frames = output.frames[0]
|
| 55 |
|
| 56 |
+
# 3.3 Export to MP4 (H.264) with chosen FPS
|
| 57 |
+
video_path = export_to_video(video_frames, "generated.mp4", fps=fps)
|
| 58 |
+
return video_path
|
| 59 |
|
| 60 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
+
# 4. Build the Gradio interface with interactive controls
|
| 62 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
+
with gr.Blocks(title="CogVideoX TextβtoβVideo Demo") as demo:
|
| 64 |
gr.Markdown(
|
| 65 |
"""
|
| 66 |
+
# ποΈ CogVideoX1.5β5B TextβtoβVideo
|
| 67 |
+
Generate up to 10Β s of video from your prompt.
|
| 68 |
+
Adjust inference steps, frame count, fps, and resolution below.
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
prompt_input = gr.Textbox(
|
| 74 |
+
label="Prompt",
|
| 75 |
+
placeholder="e.g., A futuristic city at dawn",
|
| 76 |
+
lines=2
|
| 77 |
+
)
|
| 78 |
+
steps_slider = gr.Slider(
|
| 79 |
+
minimum=1, maximum=100, step=1, value=50,
|
| 80 |
+
label="Inference Steps"
|
| 81 |
+
)
|
| 82 |
+
frames_slider = gr.Slider(
|
| 83 |
+
minimum=16, maximum=320, step=1, value=161,
|
| 84 |
+
label="Total Frames"
|
| 85 |
+
)
|
| 86 |
+
fps_slider = gr.Slider(
|
| 87 |
+
minimum=1, maximum=60, step=1, value=16,
|
| 88 |
+
label="Frames per Second (FPS)"
|
| 89 |
+
)
|
| 90 |
+
res_dropdown = gr.Dropdown(
|
| 91 |
+
choices=["360p", "480p", "720p", "1080p"],
|
| 92 |
+
value="480p",
|
| 93 |
+
label="Resolution"
|
| 94 |
+
)
|
| 95 |
+
gen_button = gr.Button("Generate Video")
|
| 96 |
+
with gr.Column():
|
| 97 |
+
video_output = gr.Video(
|
| 98 |
+
label="Generated Video",
|
| 99 |
+
format="mp4"
|
| 100 |
+
)
|
| 101 |
|
| 102 |
gen_button.click(
|
| 103 |
fn=generate_video,
|
| 104 |
+
inputs=[prompt_input, steps_slider, frames_slider, fps_slider, res_dropdown],
|
| 105 |
outputs=video_output
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
# 5. Launch: disable SSR so Gradio blocks and stays alive
|
| 110 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 111 |
if __name__ == "__main__":
|
| 112 |
demo.launch(
|
| 113 |
server_name="0.0.0.0",
|