Update gradio_app.py
Browse files- gradio_app.py +346 -348
gradio_app.py
CHANGED
|
@@ -1,349 +1,347 @@
|
|
| 1 |
-
import os, argparse
|
| 2 |
-
import sys
|
| 3 |
-
import gradio as gr
|
| 4 |
-
# from scripts.gradio.i2v_test_application import Image2Video
|
| 5 |
-
sys.path.insert(1, os.path.join(sys.path[0], 'lvdm'))
|
| 6 |
-
import spaces
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import os
|
| 10 |
-
import time
|
| 11 |
-
from omegaconf import OmegaConf
|
| 12 |
-
import torch
|
| 13 |
-
from scripts.evaluation.funcs import load_model_checkpoint, save_videos, batch_ddim_sampling, get_latent_z
|
| 14 |
-
from utils.utils import instantiate_from_config
|
| 15 |
-
from huggingface_hub import hf_hub_download
|
| 16 |
-
from einops import repeat
|
| 17 |
-
import torchvision.transforms as transforms
|
| 18 |
-
from pytorch_lightning import seed_everything
|
| 19 |
-
from einops import rearrange
|
| 20 |
-
from cldm.model import load_state_dict
|
| 21 |
-
import cv2
|
| 22 |
-
|
| 23 |
-
import torch
|
| 24 |
-
print("cuda available:", torch.cuda.is_available())
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
from huggingface_hub import snapshot_download
|
| 28 |
-
import os
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def download_model():
|
| 33 |
-
REPO_ID = 'fbnnb/TC_sketch'
|
| 34 |
-
filename_list = ['tc_sketch.pt']
|
| 35 |
-
tar_dir = './checkpoints/tooncrafter_1024_interp_sketch/'
|
| 36 |
-
if not os.path.exists(tar_dir):
|
| 37 |
-
os.makedirs(tar_dir)
|
| 38 |
-
for filename in filename_list:
|
| 39 |
-
local_file = os.path.join(tar_dir, filename)
|
| 40 |
-
if not os.path.exists(local_file):
|
| 41 |
-
hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=tar_dir, local_dir_use_symlinks=False)
|
| 42 |
-
print("downloaded")
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def get_latent_z_with_hidden_states(model, videos):
|
| 46 |
-
b, c, t, h, w = videos.shape
|
| 47 |
-
x = rearrange(videos, 'b c t h w -> (b t) c h w')
|
| 48 |
-
encoder_posterior, hidden_states = model.first_stage_model.encode(x, return_hidden_states=True)
|
| 49 |
-
|
| 50 |
-
hidden_states_first_last = []
|
| 51 |
-
### use only the first and last hidden states
|
| 52 |
-
for hid in hidden_states:
|
| 53 |
-
hid = rearrange(hid, '(b t) c h w -> b c t h w', t=t)
|
| 54 |
-
hid_new = torch.cat([hid[:, :, 0:1], hid[:, :, -1:]], dim=2)
|
| 55 |
-
hidden_states_first_last.append(hid_new)
|
| 56 |
-
|
| 57 |
-
z = model.get_first_stage_encoding(encoder_posterior).detach()
|
| 58 |
-
z = rearrange(z, '(b t) c h w -> b c t h w', b=b, t=t)
|
| 59 |
-
return z, hidden_states_first_last
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def extract_frames(video_path):
|
| 64 |
-
# εη»γγ‘γ€γ«γθͺγΏθΎΌγ
|
| 65 |
-
cap = cv2.VideoCapture(video_path)
|
| 66 |
-
|
| 67 |
-
frame_list = []
|
| 68 |
-
frame_num = 0
|
| 69 |
-
|
| 70 |
-
while True:
|
| 71 |
-
# γγ¬γΌγ γθͺγΏθΎΌγ
|
| 72 |
-
ret, frame = cap.read()
|
| 73 |
-
if not ret:
|
| 74 |
-
break
|
| 75 |
-
|
| 76 |
-
# γγ¬γΌγ γγͺγΉγγ«θΏ½ε
|
| 77 |
-
frame_list.append(frame)
|
| 78 |
-
frame_num += 1
|
| 79 |
-
|
| 80 |
-
print("load video length:", len(frame_list))
|
| 81 |
-
# εη»γγ‘γ€γ«γιγγ
|
| 82 |
-
cap.release()
|
| 83 |
-
|
| 84 |
-
return frame_list
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
resolution = '576_1024'
|
| 88 |
-
resolution = (576, 1024)
|
| 89 |
-
download_model()
|
| 90 |
-
print("after download model")
|
| 91 |
-
result_dir = "./results/"
|
| 92 |
-
if not os.path.exists(result_dir):
|
| 93 |
-
os.mkdir(result_dir)
|
| 94 |
-
|
| 95 |
-
#ToonCrafterModel
|
| 96 |
-
ckpt_path='checkpoints/tooncrafter_1024_interp_sketch/tc_sketch.pt'
|
| 97 |
-
config_file='configs/inference_1024_v1.0.yaml'
|
| 98 |
-
config = OmegaConf.load(config_file)
|
| 99 |
-
model_config = config.pop("model", OmegaConf.create())
|
| 100 |
-
model_config['params']['unet_config']['params']['use_checkpoint']=False
|
| 101 |
-
|
| 102 |
-
model = instantiate_from_config(model_config)
|
| 103 |
-
assert os.path.exists(ckpt_path), "Error: checkpoint Not Found!"
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# cn_model
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
h
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
print("
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
print(
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
#
|
| 164 |
-
#
|
| 165 |
-
#
|
| 166 |
-
#
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
# cn_tensor =
|
| 170 |
-
|
| 171 |
-
#
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
-
#
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
#
|
| 178 |
-
|
| 179 |
-
#
|
| 180 |
-
#
|
| 181 |
-
#
|
| 182 |
-
#
|
| 183 |
-
#
|
| 184 |
-
#
|
| 185 |
-
#
|
| 186 |
-
#
|
| 187 |
-
#
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
img_tensor =
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
img_tensor2 =
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
videos = torch.cat([videos,
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
img_tensor_repeat =
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
prompt_str
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
saved_result_dir
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
with gr.Row():
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
with gr.Row():
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
dynamicrafter_iface.
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
dynamicrafter_iface.launch(server_name='0.0.0.0', server_port=12345)
|
| 348 |
-
# dynamicrafter_iface.launch()
|
| 349 |
# print("launched...")
|
|
|
|
| 1 |
+
import os, argparse
|
| 2 |
+
import sys
|
| 3 |
+
import gradio as gr
|
| 4 |
+
# from scripts.gradio.i2v_test_application import Image2Video
|
| 5 |
+
sys.path.insert(1, os.path.join(sys.path[0], 'lvdm'))
|
| 6 |
+
import spaces
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import time
|
| 11 |
+
from omegaconf import OmegaConf
|
| 12 |
+
import torch
|
| 13 |
+
from scripts.evaluation.funcs import load_model_checkpoint, save_videos, batch_ddim_sampling, get_latent_z
|
| 14 |
+
from utils.utils import instantiate_from_config
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
from einops import repeat
|
| 17 |
+
import torchvision.transforms as transforms
|
| 18 |
+
from pytorch_lightning import seed_everything
|
| 19 |
+
from einops import rearrange
|
| 20 |
+
from cldm.model import load_state_dict
|
| 21 |
+
import cv2
|
| 22 |
+
|
| 23 |
+
import torch
|
| 24 |
+
print("cuda available:", torch.cuda.is_available())
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
from huggingface_hub import snapshot_download
|
| 28 |
+
import os
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def download_model():
|
| 33 |
+
REPO_ID = 'fbnnb/TC_sketch'
|
| 34 |
+
filename_list = ['tc_sketch.pt']
|
| 35 |
+
tar_dir = './checkpoints/tooncrafter_1024_interp_sketch/'
|
| 36 |
+
if not os.path.exists(tar_dir):
|
| 37 |
+
os.makedirs(tar_dir)
|
| 38 |
+
for filename in filename_list:
|
| 39 |
+
local_file = os.path.join(tar_dir, filename)
|
| 40 |
+
if not os.path.exists(local_file):
|
| 41 |
+
hf_hub_download(repo_id=REPO_ID, filename=filename, local_dir=tar_dir, local_dir_use_symlinks=False)
|
| 42 |
+
print("downloaded")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def get_latent_z_with_hidden_states(model, videos):
|
| 46 |
+
b, c, t, h, w = videos.shape
|
| 47 |
+
x = rearrange(videos, 'b c t h w -> (b t) c h w')
|
| 48 |
+
encoder_posterior, hidden_states = model.first_stage_model.encode(x, return_hidden_states=True)
|
| 49 |
+
|
| 50 |
+
hidden_states_first_last = []
|
| 51 |
+
### use only the first and last hidden states
|
| 52 |
+
for hid in hidden_states:
|
| 53 |
+
hid = rearrange(hid, '(b t) c h w -> b c t h w', t=t)
|
| 54 |
+
hid_new = torch.cat([hid[:, :, 0:1], hid[:, :, -1:]], dim=2)
|
| 55 |
+
hidden_states_first_last.append(hid_new)
|
| 56 |
+
|
| 57 |
+
z = model.get_first_stage_encoding(encoder_posterior).detach()
|
| 58 |
+
z = rearrange(z, '(b t) c h w -> b c t h w', b=b, t=t)
|
| 59 |
+
return z, hidden_states_first_last
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def extract_frames(video_path):
|
| 64 |
+
# εη»γγ‘γ€γ«γθͺγΏθΎΌγ
|
| 65 |
+
cap = cv2.VideoCapture(video_path)
|
| 66 |
+
|
| 67 |
+
frame_list = []
|
| 68 |
+
frame_num = 0
|
| 69 |
+
|
| 70 |
+
while True:
|
| 71 |
+
# γγ¬γΌγ γθͺγΏθΎΌγ
|
| 72 |
+
ret, frame = cap.read()
|
| 73 |
+
if not ret:
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
# γγ¬γΌγ γγͺγΉγγ«θΏ½ε
|
| 77 |
+
frame_list.append(frame)
|
| 78 |
+
frame_num += 1
|
| 79 |
+
|
| 80 |
+
print("load video length:", len(frame_list))
|
| 81 |
+
# εη»γγ‘γ€γ«γιγγ
|
| 82 |
+
cap.release()
|
| 83 |
+
|
| 84 |
+
return frame_list
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
resolution = '576_1024'
|
| 88 |
+
resolution = (576, 1024)
|
| 89 |
+
download_model()
|
| 90 |
+
print("after download model")
|
| 91 |
+
result_dir = "./results/"
|
| 92 |
+
if not os.path.exists(result_dir):
|
| 93 |
+
os.mkdir(result_dir)
|
| 94 |
+
|
| 95 |
+
#ToonCrafterModel
|
| 96 |
+
ckpt_path='checkpoints/tooncrafter_1024_interp_sketch/tc_sketch.pt'
|
| 97 |
+
config_file='configs/inference_1024_v1.0.yaml'
|
| 98 |
+
config = OmegaConf.load(config_file)
|
| 99 |
+
model_config = config.pop("model", OmegaConf.create())
|
| 100 |
+
model_config['params']['unet_config']['params']['use_checkpoint']=False
|
| 101 |
+
|
| 102 |
+
model = instantiate_from_config(model_config)
|
| 103 |
+
assert os.path.exists(ckpt_path), "Error: checkpoint Not Found!"
|
| 104 |
+
model = load_model_checkpoint(model, ckpt_path)
|
| 105 |
+
model.eval()
|
| 106 |
+
|
| 107 |
+
# cn_model.load_state_dict(load_state_dict(cn_ckpt_path, location='cpu'))
|
| 108 |
+
# cn_model.eval()
|
| 109 |
+
|
| 110 |
+
# model.control_model = cn_model
|
| 111 |
+
# model_list.append(model)
|
| 112 |
+
|
| 113 |
+
save_fps = 8
|
| 114 |
+
print("resolution:", resolution)
|
| 115 |
+
print("init done.")
|
| 116 |
+
|
| 117 |
+
def transpose_if_needed(tensor):
|
| 118 |
+
h = tensor.shape[-2]
|
| 119 |
+
w = tensor.shape[-1]
|
| 120 |
+
if h > w:
|
| 121 |
+
tensor = tensor.permute(0, 2, 1)
|
| 122 |
+
return tensor
|
| 123 |
+
|
| 124 |
+
def untranspose(tensor):
|
| 125 |
+
ndim = tensor.ndim
|
| 126 |
+
return tensor.transpose(ndim-1, ndim-2)
|
| 127 |
+
|
| 128 |
+
@spaces.GPU(duration=200)
|
| 129 |
+
def get_image(image, sketch, prompt, steps=50, cfg_scale=7.5, eta=1.0, fs=3, seed=123, control_scale=0.6):
|
| 130 |
+
print("enter fn")
|
| 131 |
+
# control_frames = extract_frames(frame_guides)
|
| 132 |
+
print("extract frames")
|
| 133 |
+
seed_everything(seed)
|
| 134 |
+
transform = transforms.Compose([
|
| 135 |
+
transforms.Resize(min(resolution)),
|
| 136 |
+
transforms.CenterCrop(resolution),
|
| 137 |
+
])
|
| 138 |
+
print("before empty cache")
|
| 139 |
+
torch.cuda.empty_cache()
|
| 140 |
+
print('start:', prompt, time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
|
| 141 |
+
start = time.time()
|
| 142 |
+
gpu_id=0
|
| 143 |
+
if steps > 60:
|
| 144 |
+
steps = 60
|
| 145 |
+
|
| 146 |
+
global model
|
| 147 |
+
# model = model_list[gpu_id]
|
| 148 |
+
model = model.cuda()
|
| 149 |
+
|
| 150 |
+
batch_size=1
|
| 151 |
+
channels = model.model.diffusion_model.out_channels
|
| 152 |
+
frames = model.temporal_length
|
| 153 |
+
h, w = resolution[0] // 8, resolution[1] // 8
|
| 154 |
+
noise_shape = [batch_size, channels, frames, h, w]
|
| 155 |
+
|
| 156 |
+
# text cond
|
| 157 |
+
transposed = False
|
| 158 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 159 |
+
text_emb = model.get_learned_conditioning([prompt])
|
| 160 |
+
print("before control")
|
| 161 |
+
#control cond
|
| 162 |
+
# if frame_guides is not None:
|
| 163 |
+
# cn_videos = []
|
| 164 |
+
# for frame in control_frames:
|
| 165 |
+
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 166 |
+
# frame = cv2.bitwise_not(frame)
|
| 167 |
+
# cn_tensor = torch.from_numpy(frame).unsqueeze(2).permute(2, 0, 1).float().to(model.device)
|
| 168 |
+
|
| 169 |
+
# #cn_tensor = (cn_tensor / 255. - 0.5) * 2
|
| 170 |
+
# cn_tensor = ( cn_tensor/255.0 )
|
| 171 |
+
# cn_tensor = transpose_if_needed(cn_tensor)
|
| 172 |
+
# cn_tensor_resized = transform(cn_tensor) #3,h,w
|
| 173 |
+
|
| 174 |
+
# cn_video = cn_tensor_resized.unsqueeze(0).unsqueeze(2) # bc1hw
|
| 175 |
+
# cn_videos.append(cn_video)
|
| 176 |
+
|
| 177 |
+
# cn_videos = torch.cat(cn_videos, dim=2)
|
| 178 |
+
# if cn_videos.shape[2] > frames:
|
| 179 |
+
# idxs = []
|
| 180 |
+
# for i in range(frames):
|
| 181 |
+
# index = int((i + 0.5) * cn_videos.shape[2] / frames)
|
| 182 |
+
# idxs.append(min(index, cn_videos.shape[2] - 1))
|
| 183 |
+
# cn_videos = cn_videos[:, :, idxs, :, :]
|
| 184 |
+
# print("cn_videos.shape after slicing", cn_videos.shape)
|
| 185 |
+
# model_list = []
|
| 186 |
+
# for model in model_list:
|
| 187 |
+
# model.control_scale = control_scale
|
| 188 |
+
# model_list.append(model)
|
| 189 |
+
|
| 190 |
+
# else:
|
| 191 |
+
cn_videos = None
|
| 192 |
+
|
| 193 |
+
print("image cond")
|
| 194 |
+
|
| 195 |
+
# img cond
|
| 196 |
+
img_tensor = torch.from_numpy(image).permute(2, 0, 1).float().to(model.device)
|
| 197 |
+
input_h, input_w = img_tensor.shape[1:]
|
| 198 |
+
img_tensor = (img_tensor / 255. - 0.5) * 2
|
| 199 |
+
img_tensor = transpose_if_needed(img_tensor)
|
| 200 |
+
|
| 201 |
+
image_tensor_resized = transform(img_tensor) #3,h,w
|
| 202 |
+
videos = image_tensor_resized.unsqueeze(0).unsqueeze(2) # bc1hw
|
| 203 |
+
print("get latent z")
|
| 204 |
+
# z = get_latent_z(model, videos) #bc,1,hw
|
| 205 |
+
videos = repeat(videos, 'b c t h w -> b c (repeat t) h w', repeat=frames//2)
|
| 206 |
+
|
| 207 |
+
if sketch is not None:
|
| 208 |
+
img_tensor2 = torch.from_numpy(sketch).permute(2, 0, 1).float().to(model.device)
|
| 209 |
+
img_tensor2 = (img_tensor2 / 255. - 0.5) * 2
|
| 210 |
+
img_tensor2 = transpose_if_needed(img_tensor2)
|
| 211 |
+
image_tensor_resized2 = transform(img_tensor2) #3,h,w
|
| 212 |
+
videos2 = image_tensor_resized2.unsqueeze(0).unsqueeze(2) # bchw
|
| 213 |
+
videos2 = repeat(videos2, 'b c t h w -> b c (repeat t) h w', repeat=frames//2)
|
| 214 |
+
|
| 215 |
+
videos = torch.cat([videos, videos2], dim=2)
|
| 216 |
+
else:
|
| 217 |
+
videos = torch.cat([videos, videos], dim=2)
|
| 218 |
+
|
| 219 |
+
z, hs = get_latent_z_with_hidden_states(model, videos)
|
| 220 |
+
|
| 221 |
+
img_tensor_repeat = torch.zeros_like(z)
|
| 222 |
+
|
| 223 |
+
img_tensor_repeat[:,:,:1,:,:] = z[:,:,:1,:,:]
|
| 224 |
+
img_tensor_repeat[:,:,-1:,:,:] = z[:,:,-1:,:,:]
|
| 225 |
+
|
| 226 |
+
print("image embedder")
|
| 227 |
+
cond_images = model.embedder(img_tensor.unsqueeze(0)) ## blc
|
| 228 |
+
img_emb = model.image_proj_model(cond_images)
|
| 229 |
+
|
| 230 |
+
imtext_cond = torch.cat([text_emb, img_emb], dim=1)
|
| 231 |
+
|
| 232 |
+
fs = torch.tensor([fs], dtype=torch.long, device=model.device)
|
| 233 |
+
# print("cn videos:",cn_videos.shape, "img emb:", img_emb.shape)
|
| 234 |
+
cond = {"c_crossattn": [imtext_cond], "fs": fs, "c_concat": [img_tensor_repeat], "control_cond": cn_videos}
|
| 235 |
+
|
| 236 |
+
print("before sample loop")
|
| 237 |
+
## inference
|
| 238 |
+
batch_samples = batch_ddim_sampling(model, cond, noise_shape, n_samples=1, ddim_steps=steps, ddim_eta=eta, cfg_scale=cfg_scale, hs=hs)
|
| 239 |
+
|
| 240 |
+
## remove the last frame
|
| 241 |
+
if image2 is None:
|
| 242 |
+
batch_samples = batch_samples[:,:,:,:-1,...]
|
| 243 |
+
## b,samples,c,t,h,w
|
| 244 |
+
prompt_str = prompt.replace("/", "_slash_") if "/" in prompt else prompt
|
| 245 |
+
prompt_str = prompt_str.replace(" ", "_") if " " in prompt else prompt_str
|
| 246 |
+
prompt_str=prompt_str[:40]
|
| 247 |
+
if len(prompt_str) == 0:
|
| 248 |
+
prompt_str = 'empty_prompt'
|
| 249 |
+
|
| 250 |
+
global result_dir
|
| 251 |
+
global save_fps
|
| 252 |
+
if input_h > input_w:
|
| 253 |
+
batch_samples = untranspose(batch_samples)
|
| 254 |
+
|
| 255 |
+
save_videos(batch_samples, result_dir, filenames=[prompt_str], fps=save_fps)
|
| 256 |
+
print(f"Saved in {prompt_str}. Time used: {(time.time() - start):.2f} seconds")
|
| 257 |
+
model = model.cpu()
|
| 258 |
+
saved_result_dir = os.path.join(result_dir, f"{prompt_str}.mp4")
|
| 259 |
+
print("result saved to:", saved_result_dir)
|
| 260 |
+
return saved_result_dir
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
# @spaces.GPU
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
i2v_examples_interp_1024 = [
|
| 268 |
+
['prompts/1024_interp/frame_000000.jpg', 'prompts/1024_interp/frame_000041.jpg', 'a cat is eating', 50, 7.5, 1.0, 10, 123]
|
| 269 |
+
]
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def dynamicrafter_demo(result_dir='./tmp/', res=1024):
|
| 275 |
+
if res == 1024:
|
| 276 |
+
resolution = '576_1024'
|
| 277 |
+
css = """#input_img {max-width: 1024px !important} #output_vid {max-width: 1024px; max-height:576px}"""
|
| 278 |
+
elif res == 512:
|
| 279 |
+
resolution = '320_512'
|
| 280 |
+
css = """#input_img {max-width: 512px !important} #output_vid {max-width: 512px; max-height: 320px} #input_img2 {max-width: 512px !important} #output_vid {max-width: 512px; max-height: 320px}"""
|
| 281 |
+
elif res == 256:
|
| 282 |
+
resolution = '256_256'
|
| 283 |
+
css = """#input_img {max-width: 256px !important} #output_vid {max-width: 256px; max-height: 256px}"""
|
| 284 |
+
else:
|
| 285 |
+
raise NotImplementedError(f"Unsupported resolution: {res}")
|
| 286 |
+
# image2video = Image2Video(result_dir, resolution=resolution)
|
| 287 |
+
with gr.Blocks(analytics_enabled=False, css=css) as dynamicrafter_iface:
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
with gr.Tab(label='ToonCrafter_320x512'):
|
| 292 |
+
with gr.Column():
|
| 293 |
+
with gr.Row():
|
| 294 |
+
with gr.Column():
|
| 295 |
+
with gr.Row():
|
| 296 |
+
i2v_input_image = gr.Image(label="Input Image1",elem_id="input_img")
|
| 297 |
+
# frame_guides = gr.Video(label="Input Guidance",elem_id="input_guidance", autoplay=True,show_share_button=True)
|
| 298 |
+
with gr.Row():
|
| 299 |
+
i2v_input_text = gr.Text(label='Prompts')
|
| 300 |
+
with gr.Row():
|
| 301 |
+
i2v_seed = gr.Slider(label='Random Seed', minimum=0, maximum=50000, step=1, value=123)
|
| 302 |
+
i2v_eta = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, label='ETA', value=1.0, elem_id="i2v_eta")
|
| 303 |
+
i2v_cfg_scale = gr.Slider(minimum=1.0, maximum=15.0, step=0.5, label='CFG Scale', value=7.5, elem_id="i2v_cfg_scale")
|
| 304 |
+
with gr.Row():
|
| 305 |
+
i2v_steps = gr.Slider(minimum=1, maximum=60, step=1, elem_id="i2v_steps", label="Sampling steps", value=50)
|
| 306 |
+
i2v_motion = gr.Slider(minimum=5, maximum=30, step=1, elem_id="i2v_motion", label="FPS", value=10)
|
| 307 |
+
control_scale = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, elem_id="i2v_ctrl_scale", label="control_scale", value=0.6)
|
| 308 |
+
i2v_end_btn = gr.Button("Generate")
|
| 309 |
+
with gr.Column():
|
| 310 |
+
with gr.Row():
|
| 311 |
+
i2v_input_sketch = gr.Image(label="Input End SKetch",elem_id="input_img2")
|
| 312 |
+
with gr.Row():
|
| 313 |
+
i2v_output_video = gr.Video(label="Generated Video",elem_id="output_vid",autoplay=True,show_share_button=True)
|
| 314 |
+
|
| 315 |
+
gr.Examples(examples=i2v_examples_interp_1024,
|
| 316 |
+
inputs=[i2v_input_image, i2v_input_sketch, i2v_input_text, i2v_steps, i2v_cfg_scale, i2v_eta, i2v_motion, i2v_seed, control_scale],
|
| 317 |
+
outputs=[i2v_output_video],
|
| 318 |
+
fn = get_image,
|
| 319 |
+
cache_examples=False,
|
| 320 |
+
)
|
| 321 |
+
i2v_end_btn.click(inputs=[i2v_input_image, i2v_input_sketch, i2v_input_text, i2v_steps, i2v_cfg_scale, i2v_eta, i2v_motion, i2v_seed, control_scale],
|
| 322 |
+
outputs=[i2v_output_video],
|
| 323 |
+
fn = get_image
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
return dynamicrafter_iface
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def get_parser():
|
| 331 |
+
parser = argparse.ArgumentParser()
|
| 332 |
+
return parser
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
if __name__ == "__main__":
|
| 336 |
+
parser = get_parser()
|
| 337 |
+
args = parser.parse_args()
|
| 338 |
+
|
| 339 |
+
result_dir = os.path.join('./', 'results')
|
| 340 |
+
dynamicrafter_iface = dynamicrafter_demo(result_dir)
|
| 341 |
+
dynamicrafter_iface.queue(max_size=12)
|
| 342 |
+
print("launching...")
|
| 343 |
+
# dynamicrafter_iface.launch(max_threads=1, share=True)
|
| 344 |
+
|
| 345 |
+
dynamicrafter_iface.launch(server_name='0.0.0.0', server_port=12345)
|
| 346 |
+
# dynamicrafter_iface.launch()
|
|
|
|
|
|
|
| 347 |
# print("launched...")
|