orderlymirror commited on
Commit
d76cb95
Β·
verified Β·
1 Parent(s): ca8748e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -22
app.py CHANGED
@@ -9,55 +9,64 @@ pipe = CogVideoXPipeline.from_pretrained(
9
  "THUDM/CogVideoX1.5-5B",
10
  torch_dtype=torch.bfloat16
11
  )
12
- # Enable on‐the‐fly CPU/GPU offloading; no manual .to("cuda")
13
  pipe.enable_model_cpu_offload()
14
- # Further reduce peak VRAM with VAE slicing
15
  pipe.vae.enable_slicing()
16
 
17
- # 2. Decorate only the generation function that needs GPU
18
- @spaces.GPU(duration=180)
19
- def generate_video(prompt: str) -> str:
20
  """
21
- Generates ~10s of video (161 frames @ default 16 FPS) from text.
22
  Returns path to the saved MP4 file.
23
  """
24
- # Offloading moves weights automatically; just call the pipeline
25
  output = pipe(
26
  prompt=prompt,
27
- num_inference_steps=3,
28
- num_frames=50
29
  )
30
- frames = output.frames[0]
31
 
32
- # Export frames to MP4 (H.264) for browser playback
33
- return export_to_video(frames, "generated.mp4", fps=15)
34
 
35
- # 3. Build the Gradio interface
36
- with gr.Blocks(title="CogVideoX Text-to-Video") as demo:
37
  gr.Markdown(
38
  """
39
- # 🎞️ CogVideoX1.5-5B Textβ†’Video
40
- Enter a descriptive prompt below and click **Generate Video**
41
- to create up to 10 seconds of high-quality video,
42
- powered by ZeroGPU on Hugging Face Spaces.
43
  """
44
  )
45
- with gr.Row():
46
  prompt_input = gr.Textbox(
47
  label="Prompt",
48
- placeholder="e.g., A majestic eagle soaring above snowy peaks",
49
  lines=2
50
  )
 
 
 
 
 
 
 
 
 
 
 
 
51
  gen_button = gr.Button("Generate Video")
52
  video_output = gr.Video(label="Generated Video", format="mp4")
53
 
54
  gen_button.click(
55
  fn=generate_video,
56
- inputs=prompt_input,
57
  outputs=video_output
58
  )
59
 
60
- # 4. Launch the app (SSR disabled to keep the process alive)
61
  if __name__ == "__main__":
62
  demo.launch(
63
  server_name="0.0.0.0",
 
9
  "THUDM/CogVideoX1.5-5B",
10
  torch_dtype=torch.bfloat16
11
  )
 
12
  pipe.enable_model_cpu_offload()
 
13
  pipe.vae.enable_slicing()
14
 
15
+ # 2. GPU-decorated generation function
16
+ @spaces.GPU(duration=600)
17
+ def generate_video(prompt: str, steps: int, frames: int, fps: int) -> str:
18
  """
19
+ Generates video from text with adjustable steps, frames, and fps.
20
  Returns path to the saved MP4 file.
21
  """
22
+ # Run pipeline; offload handles device placement
23
  output = pipe(
24
  prompt=prompt,
25
+ num_inference_steps=steps,
26
+ num_frames=frames
27
  )
28
+ frame_list = output.frames[0]
29
 
30
+ # Export to MP4 for browser playback
31
+ return export_to_video(frame_list, "generated.mp4", fps=fps)
32
 
33
+ # 3. Build the Gradio interface with sliders
34
+ with gr.Blocks(title="CogVideoX Interactive Text-to-Video") as demo:
35
  gr.Markdown(
36
  """
37
+ # 🎞️ Interactive Text‑to‑Video Demo
38
+ Adjust the sliders below to control the number of diffusion steps,
39
+ total frames (length), and frames per second (fps) for your video.
 
40
  """
41
  )
42
+ with gr.Column():
43
  prompt_input = gr.Textbox(
44
  label="Prompt",
45
+ placeholder="A serene forest at dawn",
46
  lines=2
47
  )
48
+ steps_slider = gr.Slider(
49
+ minimum=1, maximum=100, step=1, value=25,
50
+ label="Inference Steps"
51
+ )
52
+ frames_slider = gr.Slider(
53
+ minimum=16, maximum=320, step=1, value=161,
54
+ label="Total Frames (approx. length)"
55
+ )
56
+ fps_slider = gr.Slider(
57
+ minimum=1, maximum=60, step=1, value=16,
58
+ label="Frames per Second (fps)"
59
+ )
60
  gen_button = gr.Button("Generate Video")
61
  video_output = gr.Video(label="Generated Video", format="mp4")
62
 
63
  gen_button.click(
64
  fn=generate_video,
65
+ inputs=[prompt_input, steps_slider, frames_slider, fps_slider],
66
  outputs=video_output
67
  )
68
 
69
+ # 4. Launch the app with SSR disabled
70
  if __name__ == "__main__":
71
  demo.launch(
72
  server_name="0.0.0.0",