Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import torch
|
|
| 3 |
import gradio as gr
|
| 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
|
|
@@ -32,7 +33,7 @@ def parse_resolution(res_str: str):
|
|
| 32 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
# 3. GPUβdecorated video generation function
|
| 34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
-
@spaces.GPU(duration=
|
| 36 |
def generate_video(
|
| 37 |
prompt: str,
|
| 38 |
steps: int,
|
|
@@ -40,31 +41,35 @@ def generate_video(
|
|
| 40 |
fps: int,
|
| 41 |
resolution: str
|
| 42 |
) -> str:
|
| 43 |
-
# 3.1
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# 3.2 Run the diffusion pipeline
|
| 47 |
output = pipe(
|
| 48 |
prompt=prompt,
|
| 49 |
num_inference_steps=steps,
|
| 50 |
num_frames=frames,
|
| 51 |
-
height=height,
|
| 52 |
-
width=width
|
| 53 |
)
|
| 54 |
-
video_frames = output.frames[0]
|
| 55 |
|
| 56 |
-
# 3.3
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return video_path
|
| 59 |
|
| 60 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 61 |
# 4. Build the Gradio interface with interactive controls
|
| 62 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
-
with gr.Blocks(title="Textual Imagination: A text to video
|
| 64 |
gr.Markdown(
|
| 65 |
"""
|
| 66 |
-
# ποΈ Textual Imagination: A text to video
|
| 67 |
-
Generate videos from text
|
| 68 |
Adjust inference steps, frame count, fps, and resolution below.
|
| 69 |
"""
|
| 70 |
)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import CogVideoXPipeline
|
| 5 |
from diffusers.utils import export_to_video
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
# 1. Load & optimize the CogVideoX pipeline with CPU offload
|
|
|
|
| 33 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
# 3. GPUβdecorated video generation function
|
| 35 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
+
@spaces.GPU(duration=180) # allow up to 180s of GPU time
|
| 37 |
def generate_video(
|
| 38 |
prompt: str,
|
| 39 |
steps: int,
|
|
|
|
| 41 |
fps: int,
|
| 42 |
resolution: str
|
| 43 |
) -> str:
|
| 44 |
+
# 3.1 Determine target resolution and native resolution
|
| 45 |
+
target_h, target_w = parse_resolution(resolution)
|
| 46 |
|
| 47 |
+
# 3.2 Run the diffusion pipeline at native resolution
|
| 48 |
output = pipe(
|
| 49 |
prompt=prompt,
|
| 50 |
num_inference_steps=steps,
|
| 51 |
num_frames=frames,
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
+
video_frames = output.frames[0] # list of PIL Images at native size
|
| 54 |
|
| 55 |
+
# 3.3 Resize frames to user-specified resolution
|
| 56 |
+
resized_frames = [
|
| 57 |
+
frame.resize((target_w, target_h), Image.LANCZOS)
|
| 58 |
+
for frame in video_frames
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
# 3.4 Export to MP4 (H.264) with chosen FPS
|
| 62 |
+
video_path = export_to_video(resized_frames, "generated.mp4", fps=fps)
|
| 63 |
return video_path
|
| 64 |
|
| 65 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
# 4. Build the Gradio interface with interactive controls
|
| 67 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
+
with gr.Blocks(title="Textual Imagination: A text to video synthesis") as demo:
|
| 69 |
gr.Markdown(
|
| 70 |
"""
|
| 71 |
+
# ποΈ Textual Imagination: A text to video synthesis
|
| 72 |
+
Generate videos from text prompts.
|
| 73 |
Adjust inference steps, frame count, fps, and resolution below.
|
| 74 |
"""
|
| 75 |
)
|