zR commited on
Commit
6a6c565
β€’
1 Parent(s): 78a5545
Files changed (1) hide show
  1. app.py +47 -9
app.py CHANGED
@@ -6,7 +6,7 @@ import time
6
  import gradio as gr
7
  import numpy as np
8
  import torch
9
- from diffusers import CogVideoXPipeline, CogVideoXDPMScheduler
10
  from datetime import datetime, timedelta
11
  from openai import OpenAI
12
  import spaces
@@ -18,7 +18,6 @@ import PIL
18
  dtype = torch.float16
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=dtype).to(device)
21
- pipe.scheduler = CogVideoXDPMScheduler.from_config(pipe.scheduler.config)
22
 
23
  sys_prompt = """You are part of a team of bots that creates videos. You work with an assistant bot that will draw anything you say in square brackets.
24
 
@@ -115,13 +114,17 @@ def infer(
115
  negative_prompt_embeds=torch.zeros_like(prompt_embeds),
116
  ).frames[0]
117
 
 
 
 
 
 
118
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
119
  video_path = f"./output/{timestamp}.mp4"
120
  os.makedirs(os.path.dirname(video_path), exist_ok=True)
121
- export_to_video_imageio(video[1:], video_path)
122
  return video_path
123
 
124
-
125
  def convert_to_gif(video_path):
126
  clip = mp.VideoFileClip(video_path)
127
  clip = clip.set_fps(8)
@@ -172,10 +175,10 @@ with gr.Blocks() as demo:
172
 
173
  with gr.Column():
174
  gr.Markdown("**Optional Parameters** (default values are recommended)<br>"
175
- "50 steps are recommended for most cases for a trade-off between speed and quality, spend ~200 second<br>"
176
- "You can reduce the inference steps to speed up generation, but this may result in lower video quality.")
177
  with gr.Row():
178
- num_inference_steps = gr.Slider(label="Inference Steps", value=50, minimum=1, maximum=50)
179
  guidance_scale = gr.Number(label="Guidance Scale", value=6.0)
180
  generate_button = gr.Button("🎬 Generate Video")
181
 
@@ -185,11 +188,46 @@ with gr.Blocks() as demo:
185
  download_video_button = gr.File(label="πŸ“₯ Download Video", visible=False)
186
  download_gif_button = gr.File(label="πŸ“₯ Download GIF", visible=False)
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  def generate(prompt, num_inference_steps, guidance_scale, progress=gr.Progress(track_tqdm=True)):
190
- video_path = infer(prompt, num_inference_steps, guidance_scale, progress=progress)
 
191
  video_update = gr.update(visible=True, value=video_path)
192
-
193
  gif_path = convert_to_gif(video_path)
194
  gif_update = gr.update(visible=True, value=gif_path)
195
 
 
6
  import gradio as gr
7
  import numpy as np
8
  import torch
9
+ from diffusers import CogVideoXPipeline
10
  from datetime import datetime, timedelta
11
  from openai import OpenAI
12
  import spaces
 
18
  dtype = torch.float16
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=dtype).to(device)
 
21
 
22
  sys_prompt = """You are part of a team of bots that creates videos. You work with an assistant bot that will draw anything you say in square brackets.
23
 
 
114
  negative_prompt_embeds=torch.zeros_like(prompt_embeds),
115
  ).frames[0]
116
 
117
+
118
+ return video
119
+
120
+
121
+ def save_video(tensor):
122
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
123
  video_path = f"./output/{timestamp}.mp4"
124
  os.makedirs(os.path.dirname(video_path), exist_ok=True)
125
+ export_to_video_imageio(tensor[1:], video_path)
126
  return video_path
127
 
 
128
  def convert_to_gif(video_path):
129
  clip = mp.VideoFileClip(video_path)
130
  clip = clip.set_fps(8)
 
175
 
176
  with gr.Column():
177
  gr.Markdown("**Optional Parameters** (default values are recommended)<br>"
178
+ "Turn Inference Steps larger if you want more detailed video, but it will be slower.<br>"
179
+ "50 steps are recommended for most cases. will cause 120 seconds for inference.<br>")
180
  with gr.Row():
181
+ num_inference_steps = gr.Number(label="Inference Steps", value=50)
182
  guidance_scale = gr.Number(label="Guidance Scale", value=6.0)
183
  generate_button = gr.Button("🎬 Generate Video")
184
 
 
188
  download_video_button = gr.File(label="πŸ“₯ Download Video", visible=False)
189
  download_gif_button = gr.File(label="πŸ“₯ Download GIF", visible=False)
190
 
191
+ gr.Markdown("""
192
+ <table border="1" style="width: 100%; text-align: left; margin-top: 20px;">
193
+ <tr>
194
+ <th>Prompt</th>
195
+ <th>Video URL</th>
196
+ <th>Inference Steps</th>
197
+ <th>Guidance Scale</th>
198
+ </tr>
199
+ <tr>
200
+ <td>A detailed wooden toy ship with intricately carved masts and sails is seen gliding smoothly over a plush, blue carpet that mimics the waves of the sea. The ship's hull is painted a rich brown, with tiny windows. The carpet, soft and textured, provides a perfect backdrop, resembling an oceanic expanse. Surrounding the ship are various other toys and children's items, hinting at a playful environment. The scene captures the innocence and imagination of childhood, with the toy ship's journey symbolizing endless adventures in a whimsical, indoor setting.</td>
201
+ <td><a href="https://github.com/THUDM/CogVideo/raw/main/resources/videos/1.mp4">Video 1</a></td>
202
+ <td>50</td>
203
+ <td>6</td>
204
+ </tr>
205
+ <tr>
206
+ <td>The camera follows behind a white vintage SUV with a black roof rack as it speeds up a steep dirt road surrounded by pine trees on a steep mountain slope, dust kicks up from it’s tires, the sunlight shines on the SUV as it speeds along the dirt road, casting a warm glow over the scene. The dirt road curves gently into the distance, with no other cars or vehicles in sight. The trees on either side of the road are redwoods, with patches of greenery scattered throughout. The car is seen from the rear following the curve with ease, making it seem as if it is on a rugged drive through the rugged terrain. The dirt road itself is surrounded by steep hills and mountains, with a clear blue sky above with wispy clouds.</td>
207
+ <td><a href="https://github.com/THUDM/CogVideo/raw/main/resources/videos/2.mp4">Video 2</a></td>
208
+ <td>50</td>
209
+ <td>6</td>
210
+ </tr>
211
+ <tr>
212
+ <td>A street artist, clad in a worn-out denim jacket and a colorful bandana, stands before a vast concrete wall in the heart, holding a can of spray paint, spray-painting a colorful bird on a mottled wall.</td>
213
+ <td><a href="https://github.com/THUDM/CogVideo/raw/main/resources/videos/3.mp4">Video 3</a></td>
214
+ <td>50</td>
215
+ <td>6</td>
216
+ </tr>
217
+ <tr>
218
+ <td>In the haunting backdrop of a war-torn city, where ruins and crumbled walls tell a story of devastation, a poignant close-up frames a young girl. Her face is smudged with ash, a silent testament to the chaos around her. Her eyes glistening with a mix of sorrow and resilience, capturing the raw emotion of a world that has lost its innocence to the ravages of conflict.</td>
219
+ <td><a href="https://github.com/THUDM/CogVideo/raw/main/resources/videos/4.mp4">Video 4</a></td>
220
+ <td>50</td>
221
+ <td>6</td>
222
+ </tr>
223
+ </table>
224
+ """)
225
+
226
 
227
  def generate(prompt, num_inference_steps, guidance_scale, progress=gr.Progress(track_tqdm=True)):
228
+ tensor = infer(prompt, num_inference_steps, guidance_scale, progress=progress)
229
+ video_path = save_video(tensor)
230
  video_update = gr.update(visible=True, value=video_path)
 
231
  gif_path = convert_to_gif(video_path)
232
  gif_update = gr.update(visible=True, value=gif_path)
233