Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,79 @@
|
|
| 1 |
import torch
|
| 2 |
-
import gradio as gr
|
| 3 |
from diffusers import CogVideoXPipeline
|
| 4 |
from diffusers.utils import export_to_video
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 9 |
-
"
|
| 10 |
-
torch_dtype=torch.
|
| 11 |
-
).to(device)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
-
Generates a
|
| 19 |
-
Returns the
|
| 20 |
"""
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
-
frames =
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
gr.Markdown(
|
| 36 |
"""
|
| 37 |
-
#
|
| 38 |
-
Enter a prompt and generate up to
|
| 39 |
-
Powered by Wan-AI/Wan2.1-T2V-14B on Hugging Face ZeroGPU.
|
| 40 |
"""
|
| 41 |
)
|
| 42 |
with gr.Row():
|
| 43 |
prompt_input = gr.Textbox(
|
| 44 |
-
label="
|
| 45 |
-
placeholder="e.g., A
|
| 46 |
lines=2,
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
video_output = gr.Video(
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
fn=generate_video,
|
| 53 |
inputs=prompt_input,
|
| 54 |
outputs=video_output,
|
| 55 |
)
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch()
|
|
|
|
| 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οΈβ£ Load & optimize the CogVideoX pipeline
|
| 8 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
pipe = CogVideoXPipeline.from_pretrained(
|
| 11 |
+
"THUDM/CogVideoX1.5-5B",
|
| 12 |
+
torch_dtype=torch.bfloat16
|
| 13 |
+
).to(device) # :contentReference[oaicite:5]{index=5}
|
| 14 |
+
|
| 15 |
+
# Memory optimizations for A100 (ZeroGPU) or any limitedβGPU environment
|
| 16 |
+
pipe.enable_model_cpu_offload() # β¬οΈ from ~33Β GB β ~19Β GB :contentReference[oaicite:6]{index=6}
|
| 17 |
+
pipe.vae.enable_slicing() # β¬οΈ further to ~11Β GB :contentReference[oaicite:7]{index=7}
|
| 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Β FPS) for the given prompt.
|
| 25 |
+
Returns the filepath to the saved video.
|
| 26 |
"""
|
| 27 |
+
# Inference settings
|
| 28 |
+
guidance_scale = 6.0
|
| 29 |
+
num_inference_steps = 50
|
| 30 |
+
num_frames = 161 # yields ~10s @16Β FPS :contentReference[oaicite:8]{index=8}
|
| 31 |
+
fps = 16
|
| 32 |
+
|
| 33 |
+
# Run the pipeline
|
| 34 |
+
output = pipe(
|
| 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 = output.frames[0] # batch_size=1
|
| 42 |
+
|
| 43 |
+
# Convert frames β MP4 (H.264) for browser playability :contentReference[oaicite:9]{index=9}
|
| 44 |
+
file_path = export_to_video(frames, "generated.mp4", fps=fps)
|
| 45 |
+
return file_path
|
| 46 |
+
|
| 47 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
# 3οΈβ£ Gradio UI
|
| 49 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
with gr.Blocks(title="CogVideoX TextβtoβVideo Demo") as demo:
|
| 51 |
gr.Markdown(
|
| 52 |
"""
|
| 53 |
+
# ποΈ CogVideoX1.5β5B TextβVideo
|
| 54 |
+
Enter a descriptive prompt and generate **up toΒ 10Β s** of video.
|
|
|
|
| 55 |
"""
|
| 56 |
)
|
| 57 |
with gr.Row():
|
| 58 |
prompt_input = gr.Textbox(
|
| 59 |
+
label="Prompt",
|
| 60 |
+
placeholder="e.g., A vibrant hot air balloon festival at sunrise",
|
| 61 |
lines=2,
|
| 62 |
)
|
| 63 |
+
gen_button = gr.Button("Generate")
|
| 64 |
+
video_output = gr.Video(
|
| 65 |
+
label="Generated Video",
|
| 66 |
+
format="mp4" # ensure browserβplayable :contentReference[oaicite:10]{index=10}
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
gen_button.click(
|
| 70 |
fn=generate_video,
|
| 71 |
inputs=prompt_input,
|
| 72 |
outputs=video_output,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 76 |
+
# 4οΈβ£ Launch (Spaces autoβhandles server config & auth)
|
| 77 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
if __name__ == "__main__":
|
| 79 |
demo.launch()
|