orderlymirror commited on
Commit
ec8f341
Β·
verified Β·
1 Parent(s): b104782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -43
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️⃣ 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(
80
- server_name="0.0.0.0", # listen externally
81
- server_port=7860, # default HF Spaces port
82
- ssr_mode=False, # disable SSR to block the main thread
83
- share=True, # optional for local tunnels
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
  )