orderlymirror commited on
Commit
77f9377
Β·
verified Β·
1 Parent(s): acebc28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
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=1200) # allow up to 10Β minutes of GPU time
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 Parse & sanitize resolution
44
- height, width = parse_resolution(resolution)
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 Export to MP4 (H.264) with chosen FPS
57
- video_path = export_to_video(video_frames, "generated.mp4", fps=fps)
 
 
 
 
 
 
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 systhesis") as demo:
64
  gr.Markdown(
65
  """
66
- # 🎞️ Textual Imagination: A text to video systhesis
67
- Generate videos from text prompt.
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
  )