Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
a70f4ae
1
Parent(s):
60630de
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from utils import video_to_frames, add_dict_to_yaml_file, save_video, seed_every
|
|
| 6 |
from tokenflow_pnp import TokenFlow
|
| 7 |
from preprocess_utils import *
|
| 8 |
from tokenflow_utils import *
|
|
|
|
| 9 |
# load sd model
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
model_id = "stabilityai/stable-diffusion-2-1-base"
|
|
@@ -51,6 +52,11 @@ def get_example():
|
|
| 51 |
]
|
| 52 |
return case
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def prep(config):
|
| 56 |
# timesteps to save
|
|
@@ -115,6 +121,7 @@ def preprocess_and_invert(input_video,
|
|
| 115 |
n_timesteps = 50,
|
| 116 |
batch_size: int = 8,
|
| 117 |
n_frames: int = 40,
|
|
|
|
| 118 |
inversion_prompt:str = '',
|
| 119 |
|
| 120 |
):
|
|
@@ -131,13 +138,22 @@ def preprocess_and_invert(input_video,
|
|
| 131 |
preprocess_config['steps'] = steps
|
| 132 |
preprocess_config['batch_size'] = batch_size
|
| 133 |
preprocess_config['save_steps'] = int(n_timesteps)
|
| 134 |
-
preprocess_config['n_frames'] = n_frames
|
| 135 |
preprocess_config['seed'] = seed
|
| 136 |
preprocess_config['inversion_prompt'] = inversion_prompt
|
| 137 |
-
preprocess_config['frames'] = video_to_frames(input_video)
|
| 138 |
preprocess_config['data_path'] = input_video.split(".")[0]
|
| 139 |
-
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
if randomize_seed:
|
| 142 |
seed = randomize_seed_fn()
|
| 143 |
seed_everything(seed)
|
|
@@ -150,7 +166,7 @@ def preprocess_and_invert(input_video,
|
|
| 150 |
inverted_latents = gr.State(value=total_inverted_latents)
|
| 151 |
do_inversion = False
|
| 152 |
|
| 153 |
-
return frames, latents, inverted_latents, do_inversion
|
| 154 |
|
| 155 |
|
| 156 |
def edit_with_pnp(input_video,
|
|
@@ -167,6 +183,7 @@ def edit_with_pnp(input_video,
|
|
| 167 |
pnp_f_t: float = 0.8,
|
| 168 |
batch_size: int = 8, #needs to be the same as for preprocess
|
| 169 |
n_frames: int = 40,#needs to be the same as for preprocess
|
|
|
|
| 170 |
n_timesteps: int = 50,
|
| 171 |
gudiance_scale: float = 7.5,
|
| 172 |
inversion_prompt: str = "", #needs to be the same as for preprocess
|
|
@@ -189,7 +206,7 @@ def edit_with_pnp(input_video,
|
|
| 189 |
|
| 190 |
|
| 191 |
if do_inversion:
|
| 192 |
-
frames, latents, inverted_latents, do_inversion =
|
| 193 |
input_video,
|
| 194 |
frames,
|
| 195 |
latents,
|
|
@@ -201,7 +218,10 @@ def edit_with_pnp(input_video,
|
|
| 201 |
n_timesteps,
|
| 202 |
batch_size,
|
| 203 |
n_frames,
|
|
|
|
| 204 |
inversion_prompt)
|
|
|
|
|
|
|
| 205 |
do_inversion = False
|
| 206 |
|
| 207 |
|
|
@@ -221,7 +241,6 @@ def edit_with_pnp(input_video,
|
|
| 221 |
# demo #
|
| 222 |
########
|
| 223 |
|
| 224 |
-
|
| 225 |
intro = """
|
| 226 |
<div style="text-align:center">
|
| 227 |
<h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">
|
|
@@ -233,8 +252,6 @@ intro = """
|
|
| 233 |
</div>
|
| 234 |
"""
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
with gr.Blocks(css="style.css") as demo:
|
| 239 |
|
| 240 |
gr.HTML(intro)
|
|
@@ -282,10 +299,12 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 282 |
|
| 283 |
with gr.Column(min_width=100):
|
| 284 |
inversion_prompt = gr.Textbox(lines=1, label="Inversion prompt", interactive=True, placeholder="")
|
| 285 |
-
batch_size = gr.Slider(label='Batch size', minimum=1, maximum=
|
| 286 |
-
value=8, step=1, interactive=True)
|
| 287 |
n_frames = gr.Slider(label='Num frames', minimum=2, maximum=200,
|
| 288 |
-
value=24, step=1, interactive=True)
|
|
|
|
|
|
|
| 289 |
n_timesteps = gr.Slider(label='Diffusion steps', minimum=25, maximum=100,
|
| 290 |
value=50, step=25, interactive=True)
|
| 291 |
n_fps = gr.Slider(label='Frames per second', minimum=1, maximum=60,
|
|
@@ -336,13 +355,15 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 336 |
n_timesteps,
|
| 337 |
batch_size,
|
| 338 |
n_frames,
|
|
|
|
| 339 |
inversion_prompt
|
| 340 |
],
|
| 341 |
outputs = [frames,
|
| 342 |
latents,
|
| 343 |
inverted_latents,
|
| 344 |
-
do_inversion
|
| 345 |
-
|
|
|
|
| 346 |
])
|
| 347 |
|
| 348 |
run_button.click(fn = edit_with_pnp,
|
|
@@ -359,6 +380,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 359 |
pnp_f_t,
|
| 360 |
batch_size,
|
| 361 |
n_frames,
|
|
|
|
| 362 |
n_timesteps,
|
| 363 |
gudiance_scale,
|
| 364 |
inversion_prompt,
|
|
|
|
| 6 |
from tokenflow_pnp import TokenFlow
|
| 7 |
from preprocess_utils import *
|
| 8 |
from tokenflow_utils import *
|
| 9 |
+
import math
|
| 10 |
# load sd model
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model_id = "stabilityai/stable-diffusion-2-1-base"
|
|
|
|
| 52 |
]
|
| 53 |
return case
|
| 54 |
|
| 55 |
+
def largest_divisor(n):
|
| 56 |
+
for i in range(2, int(math.sqrt(n)) + 1):
|
| 57 |
+
if n % i == 0:
|
| 58 |
+
return n // i
|
| 59 |
+
return n
|
| 60 |
|
| 61 |
def prep(config):
|
| 62 |
# timesteps to save
|
|
|
|
| 121 |
n_timesteps = 50,
|
| 122 |
batch_size: int = 8,
|
| 123 |
n_frames: int = 40,
|
| 124 |
+
n_seconds: int = 1,
|
| 125 |
inversion_prompt:str = '',
|
| 126 |
|
| 127 |
):
|
|
|
|
| 138 |
preprocess_config['steps'] = steps
|
| 139 |
preprocess_config['batch_size'] = batch_size
|
| 140 |
preprocess_config['save_steps'] = int(n_timesteps)
|
| 141 |
+
#preprocess_config['n_frames'] = n_frames
|
| 142 |
preprocess_config['seed'] = seed
|
| 143 |
preprocess_config['inversion_prompt'] = inversion_prompt
|
| 144 |
+
preprocess_config['frames'], frames_per_second = video_to_frames(input_video)
|
| 145 |
preprocess_config['data_path'] = input_video.split(".")[0]
|
|
|
|
| 146 |
|
| 147 |
+
total_vid_duration = preprocess_config['frames']/frames_per_second
|
| 148 |
+
|
| 149 |
+
if(total_vid_duration < 1):
|
| 150 |
+
preprocess_config['n_frames'] = preprocess_config['frames']
|
| 151 |
+
else:
|
| 152 |
+
preprocess_config['n_frames'] = frames_per_second/n_seconds
|
| 153 |
+
|
| 154 |
+
if preprocess_config['n_frames'] % batch_size != 0:
|
| 155 |
+
preprocess_config['batch_size'] = largest_divisor(batch_size)
|
| 156 |
+
|
| 157 |
if randomize_seed:
|
| 158 |
seed = randomize_seed_fn()
|
| 159 |
seed_everything(seed)
|
|
|
|
| 166 |
inverted_latents = gr.State(value=total_inverted_latents)
|
| 167 |
do_inversion = False
|
| 168 |
|
| 169 |
+
return frames, latents, inverted_latents, do_inversion, preprocess_config['batch_size'], preprocess_config['n_frames']
|
| 170 |
|
| 171 |
|
| 172 |
def edit_with_pnp(input_video,
|
|
|
|
| 183 |
pnp_f_t: float = 0.8,
|
| 184 |
batch_size: int = 8, #needs to be the same as for preprocess
|
| 185 |
n_frames: int = 40,#needs to be the same as for preprocess
|
| 186 |
+
n_seconds: int = 1,
|
| 187 |
n_timesteps: int = 50,
|
| 188 |
gudiance_scale: float = 7.5,
|
| 189 |
inversion_prompt: str = "", #needs to be the same as for preprocess
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
if do_inversion:
|
| 209 |
+
frames, latents, inverted_latents, do_inversion, batch_size, n_frames = preprocess_and_invert(
|
| 210 |
input_video,
|
| 211 |
frames,
|
| 212 |
latents,
|
|
|
|
| 218 |
n_timesteps,
|
| 219 |
batch_size,
|
| 220 |
n_frames,
|
| 221 |
+
n_seconds,
|
| 222 |
inversion_prompt)
|
| 223 |
+
config["batch_size"] = batch_size
|
| 224 |
+
config["n_frames"] = n_frames
|
| 225 |
do_inversion = False
|
| 226 |
|
| 227 |
|
|
|
|
| 241 |
# demo #
|
| 242 |
########
|
| 243 |
|
|
|
|
| 244 |
intro = """
|
| 245 |
<div style="text-align:center">
|
| 246 |
<h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">
|
|
|
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
|
|
|
|
|
|
| 255 |
with gr.Blocks(css="style.css") as demo:
|
| 256 |
|
| 257 |
gr.HTML(intro)
|
|
|
|
| 299 |
|
| 300 |
with gr.Column(min_width=100):
|
| 301 |
inversion_prompt = gr.Textbox(lines=1, label="Inversion prompt", interactive=True, placeholder="")
|
| 302 |
+
batch_size = gr.Slider(label='Batch size', minimum=1, maximum=100,
|
| 303 |
+
value=8, step=1, interactive=True, visible=False)
|
| 304 |
n_frames = gr.Slider(label='Num frames', minimum=2, maximum=200,
|
| 305 |
+
value=24, step=1, interactive=True, visible=False)
|
| 306 |
+
n_seconds = gr.Slider(label='Num seconds', info="How many seconds of your video to process",
|
| 307 |
+
minimum=1, maximum=2, step=1)
|
| 308 |
n_timesteps = gr.Slider(label='Diffusion steps', minimum=25, maximum=100,
|
| 309 |
value=50, step=25, interactive=True)
|
| 310 |
n_fps = gr.Slider(label='Frames per second', minimum=1, maximum=60,
|
|
|
|
| 355 |
n_timesteps,
|
| 356 |
batch_size,
|
| 357 |
n_frames,
|
| 358 |
+
n_seconds,
|
| 359 |
inversion_prompt
|
| 360 |
],
|
| 361 |
outputs = [frames,
|
| 362 |
latents,
|
| 363 |
inverted_latents,
|
| 364 |
+
do_inversion,
|
| 365 |
+
batch_size,
|
| 366 |
+
n_frames
|
| 367 |
])
|
| 368 |
|
| 369 |
run_button.click(fn = edit_with_pnp,
|
|
|
|
| 380 |
pnp_f_t,
|
| 381 |
batch_size,
|
| 382 |
n_frames,
|
| 383 |
+
n_seconds,
|
| 384 |
n_timesteps,
|
| 385 |
gudiance_scale,
|
| 386 |
inversion_prompt,
|