vinesmsuic commited on
Commit
da4c013
1 Parent(s): 4f91916
Files changed (2) hide show
  1. app.py +4 -5
  2. app_cosxl.py +426 -0
app.py CHANGED
@@ -14,8 +14,7 @@ from PIL import Image
14
  import torch
15
  import numpy as np
16
 
17
- from black_box_image_edit.cosxl_edit import CosXLEdit
18
- #from black_box_image_edit.instructpix2pix import InstructPix2Pix
19
  from prepare_video import crop_and_resize_video
20
  from edit_image import infer_video
21
 
@@ -41,7 +40,7 @@ demo_examples = [
41
  TEMP_DIR = "_demo_temp"
42
 
43
 
44
- image_edit_model = CosXLEdit()
45
 
46
  @torch.no_grad()
47
  @spaces.GPU(duration=30)
@@ -316,7 +315,7 @@ with gr.Blocks() as demo:
316
  gr.Markdown("Official 🤗 Gradio demo for [AnyV2V: A Plug-and-Play Framework For Any Video-to-Video Editing Tasks](https://tiger-ai-lab.github.io/AnyV2V/)")
317
 
318
  with gr.Tabs():
319
- with gr.TabItem('AnyV2V(I2VGenXL) + CosXLEdit'):
320
  gr.Markdown("# Preprocessing Video Stage")
321
  gr.Markdown("In this demo, AnyV2V only support video with 2 seconds duration and 8 fps. If your video is not in this format, we will preprocess it for you. Click on the Preprocess video button!")
322
  with gr.Row():
@@ -340,7 +339,7 @@ with gr.Blocks() as demo:
340
  pv_longest_to_width = gr.Checkbox(label="Resize Longest Dimension to Width")
341
 
342
  gr.Markdown("# Image Editing Stage")
343
- gr.Markdown("Edit the first frame of the video to your liking! Click on the Edit the first frame button after inputting the editing instruction prompt. This image editing stage is powered by CosXLEdit. You can try edit the image multiple times until you are happy with the result! You can also choose to download the first frame of the video and edit it with other software (e.g. Photoshop, GIMP, etc.) or use other image editing models to obtain the edited frame and upload it directly.")
344
  with gr.Row():
345
  with gr.Column():
346
  src_first_frame = gr.Image(label="First Frame", type="filepath", interactive=False)
 
14
  import torch
15
  import numpy as np
16
 
17
+ from black_box_image_edit.instructpix2pix import InstructPix2Pix
 
18
  from prepare_video import crop_and_resize_video
19
  from edit_image import infer_video
20
 
 
40
  TEMP_DIR = "_demo_temp"
41
 
42
 
43
+ image_edit_model = InstructPix2Pix()
44
 
45
  @torch.no_grad()
46
  @spaces.GPU(duration=30)
 
315
  gr.Markdown("Official 🤗 Gradio demo for [AnyV2V: A Plug-and-Play Framework For Any Video-to-Video Editing Tasks](https://tiger-ai-lab.github.io/AnyV2V/)")
316
 
317
  with gr.Tabs():
318
+ with gr.TabItem('AnyV2V(I2VGenXL) + InstructPix2Pix'):
319
  gr.Markdown("# Preprocessing Video Stage")
320
  gr.Markdown("In this demo, AnyV2V only support video with 2 seconds duration and 8 fps. If your video is not in this format, we will preprocess it for you. Click on the Preprocess video button!")
321
  with gr.Row():
 
339
  pv_longest_to_width = gr.Checkbox(label="Resize Longest Dimension to Width")
340
 
341
  gr.Markdown("# Image Editing Stage")
342
+ gr.Markdown("Edit the first frame of the video to your liking! Click on the Edit the first frame button after inputting the editing instruction prompt. This image editing stage is powered by InstructPix2Pix. You can try edit the image multiple times until you are happy with the result! You can also choose to download the first frame of the video and edit it with other software (e.g. Photoshop, GIMP, etc.) or use other image editing models to obtain the edited frame and upload it directly.")
343
  with gr.Row():
344
  with gr.Column():
345
  src_first_frame = gr.Image(label="First Frame", type="filepath", interactive=False)
app_cosxl.py ADDED
@@ -0,0 +1,426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+
4
+ import os
5
+ import sys
6
+ import time
7
+ import subprocess
8
+ import shutil
9
+
10
+ import random
11
+ from omegaconf import OmegaConf
12
+ from moviepy.editor import VideoFileClip
13
+ from PIL import Image
14
+ import torch
15
+ import numpy as np
16
+
17
+ from black_box_image_edit.cosxl_edit import CosXLEdit
18
+ #from black_box_image_edit.instructpix2pix import InstructPix2Pix
19
+ from prepare_video import crop_and_resize_video
20
+ from edit_image import infer_video
21
+
22
+ sys.path.insert(0, "i2vgen-xl")
23
+ from utils import load_ddim_latents_at_t
24
+ from pipelines.pipeline_i2vgen_xl import I2VGenXLPipeline
25
+ from run_group_ddim_inversion import ddim_inversion
26
+ from run_group_pnp_edit import init_pnp
27
+ from diffusers import DDIMInverseScheduler, DDIMScheduler
28
+ from diffusers.utils import load_image
29
+ import imageio
30
+
31
+ DEBUG_MODE = False
32
+
33
+ demo_examples = [
34
+ ["./demo/Man Walking.mp4", "./demo/Man Walking/edited_first_frame/turn the man into darth vader.png", "man walking", 0.1, 0.1, 1.0],
35
+ ["./demo/A kitten turning its head on a wooden floor.mp4", "./demo/A kitten turning its head on a wooden floor/edited_first_frame/A dog turning its head on a wooden floor.png", "A dog turning its head on a wooden floor", 0.2, 0.2, 0.5],
36
+ ["./demo/An Old Man Doing Exercises For The Body And Mind.mp4", "./demo/An Old Man Doing Exercises For The Body And Mind/edited_first_frame/jack ma.png", "a man doing exercises for the body and mind", 0.8, 0.8, 1.0],
37
+ ["./demo/Ballet.mp4", "./demo/Ballet/edited_first_frame/van gogh style.png", "girl dancing ballet, in the style of van gogh", 1.0, 1.0, 1.0],
38
+ ["./demo/A Couple In A Public Display Of Affection.mp4", "./demo/A Couple In A Public Display Of Affection/edited_first_frame/Snowing.png", "A couple in a public display of affection, snowing", 0.3, 0.3, 1.0]
39
+ ]
40
+
41
+ TEMP_DIR = "_demo_temp"
42
+
43
+
44
+ image_edit_model = CosXLEdit()
45
+
46
+ @torch.no_grad()
47
+ @spaces.GPU(duration=30)
48
+ def perform_edit(video_path, prompt, force_512=False, seed=42, negative_prompt=""):
49
+ edited_image_path = infer_video(image_edit_model,
50
+ video_path,
51
+ output_dir=TEMP_DIR,
52
+ prompt=prompt,
53
+ prompt_type="instruct",
54
+ force_512=force_512,
55
+ seed=seed,
56
+ negative_prompt=negative_prompt,
57
+ overwrite=True)
58
+ return edited_image_path
59
+
60
+
61
+ # Set up default inversion config file
62
+ config = {
63
+ # DDIM inversion
64
+ "inverse_config": {
65
+ "image_size": [512, 512],
66
+ "n_frames": 16,
67
+ "cfg": 1.0,
68
+ "target_fps": 8,
69
+ "ddim_inv_prompt": "",
70
+ "prompt": "",
71
+ "negative_prompt": "",
72
+ },
73
+ "pnp_config": {
74
+ "random_ratio": 0.0,
75
+ "target_fps": 8,
76
+ },
77
+ }
78
+ config = OmegaConf.create(config)
79
+
80
+ # Initialize the I2VGenXL pipeline
81
+ pipe = I2VGenXLPipeline.from_pretrained(
82
+ "ali-vilab/i2vgen-xl",
83
+ torch_dtype=torch.float16,
84
+ variant="fp16",
85
+ ).to("cuda:0")
86
+
87
+ # Initialize the DDIM inverse scheduler
88
+ inverse_scheduler = DDIMInverseScheduler.from_pretrained(
89
+ "ali-vilab/i2vgen-xl",
90
+ subfolder="scheduler",
91
+ )
92
+ # Initialize the DDIM scheduler
93
+ ddim_scheduler = DDIMScheduler.from_pretrained(
94
+ "ali-vilab/i2vgen-xl",
95
+ subfolder="scheduler",
96
+ )
97
+
98
+ @torch.no_grad()
99
+ @spaces.GPU(duration=150)
100
+ def perform_anyv2v(
101
+ video_path,
102
+ video_prompt,
103
+ video_negative_prompt,
104
+ edited_first_frame_path,
105
+ conv_inj,
106
+ spatial_inj,
107
+ temp_inj,
108
+ num_inference_steps,
109
+ guidance_scale,
110
+ ddim_init_latents_t_idx,
111
+ ddim_inversion_steps,
112
+ seed,
113
+ ):
114
+
115
+ tmp_dir = os.path.join(TEMP_DIR, "AnyV2V")
116
+ if os.path.exists(tmp_dir):
117
+ shutil.rmtree(tmp_dir)
118
+ os.makedirs(tmp_dir)
119
+
120
+ ddim_latents_path = os.path.join(tmp_dir, "ddim_latents")
121
+
122
+ def read_frames(video_path):
123
+ frames = []
124
+ with imageio.get_reader(video_path) as reader:
125
+ for i, frame in enumerate(reader):
126
+ pil_image = Image.fromarray(frame)
127
+ frames.append(pil_image)
128
+ return frames
129
+ frame_list = read_frames(str(video_path))
130
+
131
+ config.inverse_config.image_size = list(frame_list[0].size)
132
+ config.inverse_config.n_steps = ddim_inversion_steps
133
+ config.inverse_config.n_frames = len(frame_list)
134
+ config.inverse_config.output_dir = ddim_latents_path
135
+ ddim_init_latents_t_idx = min(ddim_init_latents_t_idx, num_inference_steps - 1)
136
+
137
+ # Step 1. DDIM Inversion
138
+ first_frame = frame_list[0]
139
+
140
+ generator = torch.Generator(device="cuda:0")
141
+ generator = generator.manual_seed(seed)
142
+ _ddim_latents = ddim_inversion(
143
+ config.inverse_config,
144
+ first_frame,
145
+ frame_list,
146
+ pipe,
147
+ inverse_scheduler,
148
+ generator,
149
+ )
150
+
151
+ # Step 2. DDIM Sampling + PnP feature and attention injection
152
+ # Load the edited first frame
153
+ edited_1st_frame = load_image(edited_first_frame_path).resize(
154
+ config.inverse_config.image_size, resample=Image.Resampling.LANCZOS
155
+ )
156
+ # Load the initial latents at t
157
+ ddim_scheduler.set_timesteps(num_inference_steps)
158
+ print(f"ddim_scheduler.timesteps: {ddim_scheduler.timesteps}")
159
+ ddim_latents_at_t = load_ddim_latents_at_t(
160
+ ddim_scheduler.timesteps[ddim_init_latents_t_idx],
161
+ ddim_latents_path=ddim_latents_path,
162
+ )
163
+ print(
164
+ f"ddim_scheduler.timesteps[t_idx]: {ddim_scheduler.timesteps[ddim_init_latents_t_idx]}"
165
+ )
166
+ print(f"ddim_latents_at_t.shape: {ddim_latents_at_t.shape}")
167
+
168
+ # Blend the latents
169
+ random_latents = torch.randn_like(ddim_latents_at_t)
170
+ print(
171
+ f"Blending random_ratio (1 means random latent): {config.pnp_config.random_ratio}"
172
+ )
173
+ mixed_latents = (
174
+ random_latents * config.pnp_config.random_ratio
175
+ + ddim_latents_at_t * (1 - config.pnp_config.random_ratio)
176
+ )
177
+
178
+ # Init Pnp
179
+ config.pnp_config.n_steps = num_inference_steps
180
+ config.pnp_config.pnp_f_t = conv_inj
181
+ config.pnp_config.pnp_spatial_attn_t = spatial_inj
182
+ config.pnp_config.pnp_temp_attn_t = temp_inj
183
+ config.pnp_config.ddim_init_latents_t_idx = ddim_init_latents_t_idx
184
+ init_pnp(pipe, ddim_scheduler, config.pnp_config)
185
+ # Edit video
186
+ pipe.register_modules(scheduler=ddim_scheduler)
187
+
188
+ edited_video = pipe.sample_with_pnp(
189
+ prompt=video_prompt,
190
+ image=edited_1st_frame,
191
+ height=config.inverse_config.image_size[1],
192
+ width=config.inverse_config.image_size[0],
193
+ num_frames=config.inverse_config.n_frames,
194
+ num_inference_steps=config.pnp_config.n_steps,
195
+ guidance_scale=guidance_scale,
196
+ negative_prompt=video_negative_prompt,
197
+ target_fps=config.pnp_config.target_fps,
198
+ latents=mixed_latents,
199
+ generator=generator,
200
+ return_dict=True,
201
+ ddim_init_latents_t_idx=ddim_init_latents_t_idx,
202
+ ddim_inv_latents_path=ddim_latents_path,
203
+ ddim_inv_prompt=config.inverse_config.ddim_inv_prompt,
204
+ ddim_inv_1st_frame=first_frame,
205
+ ).frames[0]
206
+
207
+ edited_video = [
208
+ frame.resize(config.inverse_config.image_size, resample=Image.LANCZOS)
209
+ for frame in edited_video
210
+ ]
211
+
212
+ def images_to_video(images, output_path, fps=24):
213
+ writer = imageio.get_writer(output_path, fps=fps)
214
+
215
+ for img in images:
216
+ img_np = np.array(img)
217
+ writer.append_data(img_np)
218
+
219
+ writer.close()
220
+ output_path = os.path.join(tmp_dir, "edited_video.mp4")
221
+ images_to_video(
222
+ edited_video, output_path, fps=config.pnp_config.target_fps
223
+ )
224
+ return output_path
225
+
226
+
227
+
228
+ def get_first_frame_as_pil(video_path):
229
+ with VideoFileClip(video_path) as clip:
230
+ # Extract the first frame (at t=0) as an array
231
+ first_frame_array = clip.get_frame(0)
232
+ # Convert the numpy array to a PIL Image
233
+ first_frame_image = Image.fromarray(first_frame_array)
234
+ return first_frame_image
235
+
236
+ def btn_preprocess_video_fn(video_path, width, height, start_time, end_time, center_crop, x_offset, y_offset, longest_to_width):
237
+ def check_video(video_path):
238
+ with VideoFileClip(video_path) as clip:
239
+ if clip.duration == 2 and clip.fps == 8:
240
+ return True
241
+ else:
242
+ return False
243
+
244
+ if check_video(video_path) == False:
245
+ processed_video_path = crop_and_resize_video(input_video_path=video_path,
246
+ output_folder=TEMP_DIR,
247
+ clip_duration=2,
248
+ width=width,
249
+ height=height,
250
+ start_time=start_time,
251
+ end_time=end_time,
252
+ center_crop=center_crop,
253
+ x_offset=x_offset,
254
+ y_offset=y_offset,
255
+ longest_to_width=longest_to_width)
256
+
257
+ return processed_video_path
258
+ else:
259
+ return video_path
260
+
261
+ def btn_image_edit_fn(video_path, instruct_prompt, ie_force_512, ie_seed, ie_neg_prompt):
262
+ """
263
+ Generate an image based on the video and text input.
264
+ This function should be replaced with your actual image generation logic.
265
+ """
266
+ # Placeholder logic for image generation
267
+
268
+ if ie_seed < 0:
269
+ ie_seed = int.from_bytes(os.urandom(2), "big")
270
+ print(f"Using seed: {ie_seed}")
271
+
272
+ edited_image_path = perform_edit(video_path=video_path,
273
+ prompt=instruct_prompt,
274
+ force_512=ie_force_512,
275
+ seed=ie_seed,
276
+ negative_prompt=ie_neg_prompt)
277
+ return edited_image_path
278
+
279
+
280
+ def btn_infer_fn(video_path,
281
+ video_prompt,
282
+ video_negative_prompt,
283
+ edited_first_frame_path,
284
+ conv_inj,
285
+ spatial_inj,
286
+ temp_inj,
287
+ num_inference_steps,
288
+ guidance_scale,
289
+ ddim_init_latents_t_idx,
290
+ ddim_inversion_steps,
291
+ seed,
292
+ ):
293
+ if seed < 0:
294
+ seed = int.from_bytes(os.urandom(2), "big")
295
+ print(f"Using seed: {seed}")
296
+
297
+ result_video_path = perform_anyv2v(video_path=video_path,
298
+ video_prompt=video_prompt,
299
+ video_negative_prompt=video_negative_prompt,
300
+ edited_first_frame_path=edited_first_frame_path,
301
+ conv_inj=conv_inj,
302
+ spatial_inj=spatial_inj,
303
+ temp_inj=temp_inj,
304
+ num_inference_steps=num_inference_steps,
305
+ guidance_scale=guidance_scale,
306
+ ddim_init_latents_t_idx=ddim_init_latents_t_idx,
307
+ ddim_inversion_steps=ddim_inversion_steps,
308
+ seed=seed)
309
+
310
+ return result_video_path
311
+
312
+ # Create the UI
313
+ #=====================================
314
+ with gr.Blocks() as demo:
315
+ gr.Markdown("# <img src='https://tiger-ai-lab.github.io/AnyV2V/static/images/icon.png' width='30'/> AnyV2V")
316
+ gr.Markdown("Official 🤗 Gradio demo for [AnyV2V: A Plug-and-Play Framework For Any Video-to-Video Editing Tasks](https://tiger-ai-lab.github.io/AnyV2V/)")
317
+
318
+ with gr.Tabs():
319
+ with gr.TabItem('AnyV2V(I2VGenXL) + CosXLEdit'):
320
+ gr.Markdown("# Preprocessing Video Stage")
321
+ gr.Markdown("In this demo, AnyV2V only support video with 2 seconds duration and 8 fps. If your video is not in this format, we will preprocess it for you. Click on the Preprocess video button!")
322
+ with gr.Row():
323
+ with gr.Column():
324
+ video_raw = gr.Video(label="Raw Video Input")
325
+ btn_pv = gr.Button("Preprocess Video")
326
+
327
+ with gr.Column():
328
+ video_input = gr.Video(label="Preprocessed Video Input", interactive=False)
329
+ with gr.Column():
330
+ advanced_settings_pv = gr.Accordion("Advanced Settings for Video Preprocessing", open=False)
331
+ with advanced_settings_pv:
332
+ with gr.Column():
333
+ pv_width = gr.Number(label="Width", value=512, minimum=1, maximum=4096)
334
+ pv_height = gr.Number(label="Height", value=512, minimum=1, maximum=4096)
335
+ pv_start_time = gr.Number(label="Start Time (End time - Start time must be = 2)", value=0, minimum=0)
336
+ pv_end_time = gr.Number(label="End Time (End time - Start time must be = 2)", value=2, minimum=0)
337
+ pv_center_crop = gr.Checkbox(label="Center Crop", value=True)
338
+ pv_x_offset = gr.Number(label="Horizontal Offset (-1 to 1)", value=0, minimum=-1, maximum=1)
339
+ pv_y_offset = gr.Number(label="Vertical Offset (-1 to 1)", value=0, minimum=-1, maximum=1)
340
+ pv_longest_to_width = gr.Checkbox(label="Resize Longest Dimension to Width")
341
+
342
+ gr.Markdown("# Image Editing Stage")
343
+ gr.Markdown("Edit the first frame of the video to your liking! Click on the Edit the first frame button after inputting the editing instruction prompt. This image editing stage is powered by CosXLEdit. You can try edit the image multiple times until you are happy with the result! You can also choose to download the first frame of the video and edit it with other software (e.g. Photoshop, GIMP, etc.) or use other image editing models to obtain the edited frame and upload it directly.")
344
+ with gr.Row():
345
+ with gr.Column():
346
+ src_first_frame = gr.Image(label="First Frame", type="filepath", interactive=False)
347
+ image_instruct_prompt = gr.Textbox(label="Editing instruction prompt")
348
+ btn_image_edit = gr.Button("Edit the first frame")
349
+ with gr.Column():
350
+ image_input_output = gr.Image(label="Edited Frame", type="filepath")
351
+ with gr.Column():
352
+ advanced_settings_image_edit = gr.Accordion("Advanced Settings for Image Editing", open=True)
353
+ with advanced_settings_image_edit:
354
+ with gr.Column():
355
+ ie_neg_prompt = gr.Textbox(label="Negative Prompt", value="low res, blurry, watermark, jpeg artifacts")
356
+ ie_seed = gr.Number(label="Seed (-1 means random)", value=-1, minimum=-1, maximum=sys.maxsize)
357
+ ie_force_512 = gr.Checkbox(label="Force resize to 512x512 before feeding into the image editing model")
358
+
359
+ gr.Markdown("# Video Editing Stage")
360
+ gr.Markdown("Enjoy the full control of the video editing process using the edited image and the preprocessed video! Click on the Run AnyV2V button after inputting the video description prompt. Try tweak with the setting if the output does not satisfy you!")
361
+ with gr.Row():
362
+ with gr.Column():
363
+ video_prompt = gr.Textbox(label="Video description prompt")
364
+ settings_anyv2v = gr.Accordion("Settings for AnyV2V")
365
+ with settings_anyv2v:
366
+ with gr.Column():
367
+ av_pnp_f_t = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Convolutional injection (pnp_f_t)")
368
+ av_pnp_spatial_attn_t = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Spatial Attention injection (pnp_spatial_attn_t)")
369
+ av_pnp_temp_attn_t = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.5, label="Temporal Attention injection (pnp_temp_attn_t)")
370
+ btn_infer = gr.Button("Run Video Editing")
371
+ with gr.Column():
372
+ video_output = gr.Video(label="Video Output")
373
+ with gr.Column():
374
+ advanced_settings_anyv2v = gr.Accordion("Advanced Settings for AnyV2V", open=False)
375
+ with advanced_settings_anyv2v:
376
+ with gr.Column():
377
+ av_ddim_init_latents_t_idx = gr.Number(label="DDIM Initial Latents t Index", value=0, minimum=0)
378
+ av_ddim_inversion_steps = gr.Number(label="DDIM Inversion Steps", value=100, minimum=1)
379
+ av_num_inference_steps = gr.Number(label="Number of Inference Steps", value=50, minimum=1)
380
+ av_guidance_scale = gr.Number(label="Guidance Scale", value=9, minimum=0)
381
+ av_seed = gr.Number(label="Seed (-1 means random)", value=42, minimum=-1, maximum=sys.maxsize)
382
+ av_neg_prompt = gr.Textbox(label="Negative Prompt", value="Distorted, discontinuous, Ugly, blurry, low resolution, motionless, static, disfigured, disconnected limbs, Ugly faces, incomplete arms")
383
+
384
+ examples = gr.Examples(examples=demo_examples,
385
+ label="Examples (Just click on Video Editing button after loading them into the UI)",
386
+ inputs=[video_input, image_input_output, video_prompt, av_pnp_f_t, av_pnp_spatial_attn_t, av_pnp_temp_attn_t])
387
+ gr.Markdown('Reference: You can find good source videos from https://www.pexels.com/videos/')
388
+
389
+ btn_pv.click(
390
+ btn_preprocess_video_fn,
391
+ inputs=[video_raw, pv_width, pv_height, pv_start_time, pv_end_time, pv_center_crop, pv_x_offset, pv_y_offset, pv_longest_to_width],
392
+ outputs=video_input
393
+ )
394
+
395
+ btn_image_edit.click(
396
+ btn_image_edit_fn,
397
+ inputs=[video_input, image_instruct_prompt, ie_force_512, ie_seed, ie_neg_prompt],
398
+ outputs=image_input_output
399
+ )
400
+
401
+ btn_infer.click(
402
+ btn_infer_fn,
403
+ inputs=[video_input,
404
+ video_prompt,
405
+ av_neg_prompt,
406
+ image_input_output,
407
+ av_pnp_f_t,
408
+ av_pnp_spatial_attn_t,
409
+ av_pnp_temp_attn_t,
410
+ av_num_inference_steps,
411
+ av_guidance_scale,
412
+ av_ddim_init_latents_t_idx,
413
+ av_ddim_inversion_steps,
414
+ av_seed],
415
+ outputs=video_output
416
+ )
417
+
418
+ video_input.change(fn=get_first_frame_as_pil, inputs=video_input, outputs=src_first_frame)
419
+
420
+ #=====================================
421
+
422
+ # Minimizing usage of GPU Resources
423
+ torch.set_grad_enabled(False)
424
+
425
+
426
+ demo.launch()