multimodalart HF staff commited on
Commit
d111c40
1 Parent(s): 0922892

Add video2video

Browse files
Files changed (1) hide show
  1. app.py +119 -13
app.py CHANGED
@@ -4,9 +4,16 @@ import random
4
  import threading
5
  import time
6
 
 
 
 
 
 
 
7
  import gradio as gr
8
  import torch
9
- from diffusers import CogVideoXPipeline, CogVideoXDDIMScheduler,CogVideoXDPMScheduler
 
10
  from datetime import datetime, timedelta
11
 
12
  from diffusers.image_processor import VaeImageProcessor
@@ -27,6 +34,8 @@ pipe.scheduler = CogVideoXDPMScheduler.from_config(pipe.scheduler.config, timest
27
  pipe.transformer.to(memory_format=torch.channels_last)
28
  pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
29
 
 
 
30
  os.makedirs("./output", exist_ok=True)
31
  os.makedirs("./gradio_tmp", exist_ok=True)
32
 
@@ -46,6 +55,76 @@ Other times the user will not want modifications , but instead want a new image
46
  Video descriptions must have the same num of words as examples below. Extra words will be ignored.
47
  """
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def convert_prompt(prompt: str, retry_times: int = 3) -> str:
51
  if not os.environ.get("OPENAI_API_KEY"):
@@ -96,9 +175,10 @@ def convert_prompt(prompt: str, retry_times: int = 3) -> str:
96
  return response.choices[0].message.content
97
  return prompt
98
 
99
-
100
  def infer(
101
  prompt: str,
 
 
102
  num_inference_steps: int,
103
  guidance_scale: float,
104
  seed: int = -1,
@@ -106,16 +186,31 @@ def infer(
106
  ):
107
  if seed == -1:
108
  seed = random.randint(0, 2 ** 8 - 1)
109
- video_pt = pipe(
110
- prompt=prompt,
111
- num_videos_per_prompt=1,
112
- num_inference_steps=num_inference_steps,
113
- num_frames=49,
114
- use_dynamic_cfg=True,
115
- output_type="pt",
116
- guidance_scale=guidance_scale,
117
- generator=torch.Generator(device="cpu").manual_seed(seed),
118
- ).frames
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  return (video_pt, seed)
121
 
@@ -146,6 +241,7 @@ def delete_old_files():
146
 
147
 
148
  threading.Thread(target=delete_old_files, daemon=True).start()
 
149
 
150
  with gr.Blocks() as demo:
151
  gr.Markdown("""
@@ -169,6 +265,11 @@ with gr.Blocks() as demo:
169
 
170
  """)
171
  with gr.Row():
 
 
 
 
 
172
  with gr.Column():
173
  prompt = gr.Textbox(label="Prompt (Less than 200 Words)", placeholder="Enter your prompt here", lines=5)
174
 
@@ -313,7 +414,12 @@ with gr.Blocks() as demo:
313
  )
314
 
315
  enhance_button.click(enhance_prompt_func, inputs=[prompt], outputs=[prompt])
316
-
 
 
 
 
 
317
  if __name__ == "__main__":
318
  demo.queue(max_size=15)
319
  demo.launch()
 
4
  import threading
5
  import time
6
 
7
+ import cv2
8
+ import numpy as np
9
+ import tempfile
10
+ import imageio
11
+ import imageio_ffmpeg
12
+
13
  import gradio as gr
14
  import torch
15
+ from diffusers import CogVideoXPipeline, CogVideoXDDIMScheduler,CogVideoXDPMScheduler, CogVideoXVideoToVideoPipeline
16
+ from diffusers.utils import export_to_video, load_video
17
  from datetime import datetime, timedelta
18
 
19
  from diffusers.image_processor import VaeImageProcessor
 
34
  pipe.transformer.to(memory_format=torch.channels_last)
35
  pipe.transformer = torch.compile(pipe.transformer, mode="max-autotune", fullgraph=True)
36
 
37
+ pipe_video = CogVideoXVideoToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b", transformer=pipe.transformer, vae=pipe.vae, scheduler=pipe.scheduler, tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder, torch_dtype=torch.bfloat16)
38
+
39
  os.makedirs("./output", exist_ok=True)
40
  os.makedirs("./gradio_tmp", exist_ok=True)
41
 
 
55
  Video descriptions must have the same num of words as examples below. Extra words will be ignored.
56
  """
57
 
58
+ def get_video_dimensions(input_video_path):
59
+ reader = imageio_ffmpeg.read_frames(input_video_path)
60
+ metadata = next(reader)
61
+ return metadata['size']
62
+
63
+ def center_crop_resize(input_video_path, target_width=720, target_height=480):
64
+ # Open the video file
65
+ cap = cv2.VideoCapture(input_video_path)
66
+
67
+ # Get original video properties
68
+ orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
69
+ orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
70
+ orig_fps = cap.get(cv2.CAP_PROP_FPS)
71
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
72
+
73
+ # Calculate resize factor
74
+ width_factor = target_width / orig_width
75
+ height_factor = target_height / orig_height
76
+ resize_factor = max(width_factor, height_factor)
77
+
78
+ # Calculate intermediate size
79
+ inter_width = int(orig_width * resize_factor)
80
+ inter_height = int(orig_height * resize_factor)
81
+
82
+ # Calculate frame skip
83
+ target_fps = 8
84
+ ideal_skip = max(0, math.ceil(orig_fps / target_fps) - 1)
85
+ skip = min(5, ideal_skip) # Cap at 5
86
+
87
+ # Adjust skip if not enough frames
88
+ while (total_frames / (skip + 1)) < 49 and skip > 0:
89
+ skip -= 1
90
+
91
+ processed_frames = []
92
+ frame_count = 0
93
+ total_read = 0
94
+
95
+ while frame_count < 49 and total_read < total_frames:
96
+ ret, frame = cap.read()
97
+ if not ret:
98
+ break
99
+
100
+ if total_read % (skip + 1) == 0:
101
+ # Resize frame
102
+ resized = cv2.resize(frame, (inter_width, inter_height), interpolation=cv2.INTER_AREA)
103
+
104
+ # Center crop
105
+ start_x = (inter_width - target_width) // 2
106
+ start_y = (inter_height - target_height) // 2
107
+ cropped = resized[start_y:start_y+target_height, start_x:start_x+target_width]
108
+
109
+ processed_frames.append(cropped)
110
+ frame_count += 1
111
+
112
+ total_read += 1
113
+
114
+ cap.release()
115
+
116
+ # Save the processed video to a temporary file
117
+ with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
118
+ temp_video_path = temp_file.name
119
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
120
+ out = cv2.VideoWriter(temp_video_path, fourcc, target_fps, (target_width, target_height))
121
+
122
+ for frame in processed_frames:
123
+ out.write(frame)
124
+
125
+ out.release()
126
+
127
+ return temp_video_path
128
 
129
  def convert_prompt(prompt: str, retry_times: int = 3) -> str:
130
  if not os.environ.get("OPENAI_API_KEY"):
 
175
  return response.choices[0].message.content
176
  return prompt
177
 
 
178
  def infer(
179
  prompt: str,
180
+ video_input: str,
181
+ video_strenght: float,
182
  num_inference_steps: int,
183
  guidance_scale: float,
184
  seed: int = -1,
 
186
  ):
187
  if seed == -1:
188
  seed = random.randint(0, 2 ** 8 - 1)
189
+ if(video_input):
190
+ video = load_video(video_input)[:49] # Limit to 49 frames
191
+ video_pt = pipe(
192
+ video=video,
193
+ prompt=prompt,
194
+ num_inference_steps=num_inference_steps,
195
+ num_videos_per_prompt=1,
196
+ strength=video_strenght,
197
+ num_frames=49,
198
+ use_dynamic_cfg=True,
199
+ output_type="pt",
200
+ guidance_scale=guidance_scale,
201
+ generator=torch.Generator(device="cpu").manual_seed(seed),
202
+ ).frames
203
+ else:
204
+ video_pt = pipe(
205
+ prompt=prompt,
206
+ num_videos_per_prompt=1,
207
+ num_inference_steps=num_inference_steps,
208
+ num_frames=49,
209
+ use_dynamic_cfg=True,
210
+ output_type="pt",
211
+ guidance_scale=guidance_scale,
212
+ generator=torch.Generator(device="cpu").manual_seed(seed),
213
+ ).frames
214
 
215
  return (video_pt, seed)
216
 
 
241
 
242
 
243
  threading.Thread(target=delete_old_files, daemon=True).start()
244
+ examples = [["horse.mp4", "Pixel art of a horse running"]]
245
 
246
  with gr.Blocks() as demo:
247
  gr.Markdown("""
 
265
 
266
  """)
267
  with gr.Row():
268
+ with gr.Accordion("Video-to-video", open=False):
269
+ video_input = gr.Video(label="Input Video (will be cropped to 49 frames, 6 seconds at 8fps)")
270
+ strength = gr.Slider(0.1, 1.0, value=0.8, step=0.01, label="Strength")
271
+ examples_component = gr.Examples(examples, fn=process_video, inputs=[input_video, prompt], outputs=output_video, cache_examples="lazy")
272
+ examples_component.dataset._components = [input_video]
273
  with gr.Column():
274
  prompt = gr.Textbox(label="Prompt (Less than 200 Words)", placeholder="Enter your prompt here", lines=5)
275
 
 
414
  )
415
 
416
  enhance_button.click(enhance_prompt_func, inputs=[prompt], outputs=[prompt])
417
+
418
+ input_video.upload(
419
+ resize_if_unfit,
420
+ inputs=[input_video],
421
+ outputs=[input_video]
422
+ )
423
  if __name__ == "__main__":
424
  demo.queue(max_size=15)
425
  demo.launch()