orderlymirror commited on
Commit
6b67faa
Β·
verified Β·
1 Parent(s): da0a094

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -36
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
- # 1. Initialize the CogVideoX1.5‑5B‑SAT pipeline on ZeroGPU
7
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
 
8
  pipe = CogVideoXPipeline.from_pretrained(
9
- "Wan-AI/Wan2.1-T2V-14B", # High‑quality 10s model :contentReference[oaicite:5]{index=5}
10
- torch_dtype=torch.float16
11
- ).to(device)
12
- pipe.enable_model_cpu_offload() # Memory optimization :contentReference[oaicite:6]{index=6}
13
- pipe.enable_vae_slicing() # Further memory savings :contentReference[oaicite:7]{index=7}
14
-
15
- # 2. Define the video generation function
16
- def generate_video(prompt: str):
 
 
 
 
17
  """
18
- Generates a 10‑second video (160 frames at 16Β fps) for the given prompt.
19
- Returns the path to the saved MP4 file.
20
  """
21
- # 160 frames @16 fps β†’ 10-second duration :contentReference[oaicite:8]{index=8}
22
- result = pipe(
23
- prompt,
24
- num_frames=160,
25
- fps=16,
 
 
 
 
 
 
 
 
26
  )
27
- frames = result.frames[0]
28
-
29
- # Export frames to MP4; Gradio's Video component accepts file paths
30
- video_path = export_to_video(frames, "generated.mp4", fps=16)
31
- return video_path
32
-
33
- # 3. Build the Gradio interface
34
- with gr.Blocks(title="CogVideoX1.5‑5B‑SAT Text‑to‑Video") as demo:
 
 
35
  gr.Markdown(
36
  """
37
- # πŸŽ₯ Text‑to‑Video with CogVideoX1.5‑5B‑SAT
38
- Enter a prompt and generate up to **10Β s** of high‑quality video.
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="Video Prompt",
45
- placeholder="e.g., A majestic eagle soaring over mountain peaks",
46
  lines=2,
47
  )
48
- generate_btn = gr.Button("Generate Video")
49
- video_output = gr.Video(label="Your Generated Video")
50
-
51
- generate_btn.click(
 
 
 
52
  fn=generate_video,
53
  inputs=prompt_input,
54
  outputs=video_output,
55
  )
56
 
57
- # 4. Launch (Spaces will handle server settings automatically)
 
 
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()