Spaces:
Paused
Paused
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,12 +31,12 @@ from safetensors.torch import load_file, save_file
|
|
| 31 |
from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
|
| 32 |
#import jax
|
| 33 |
#import jax.numpy as jnp
|
| 34 |
-
from numba import njit as cpu1, jit as cpu2
|
| 35 |
-
from numba.cuda import jit as
|
| 36 |
|
| 37 |
# optimization:
|
| 38 |
|
| 39 |
-
# @
|
| 40 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 41 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 42 |
# @cpu1(cache=True)
|
|
@@ -126,12 +126,17 @@ function custom(){
|
|
| 126 |
|
| 127 |
# functionality
|
| 128 |
|
| 129 |
-
@
|
| 130 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 131 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 132 |
# @cpu1(cache=True)
|
| 133 |
# @cpu2(cache=True)
|
| 134 |
def run(cmd, assert_success=False, capture_output=False, env=None, dry_run=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
if dry_run:
|
| 136 |
print(f"--> {cmd}")
|
| 137 |
result = 1
|
|
@@ -145,12 +150,17 @@ def run(cmd, assert_success=False, capture_output=False, env=None, dry_run=False
|
|
| 145 |
|
| 146 |
return result
|
| 147 |
|
| 148 |
-
@
|
| 149 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 150 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 151 |
# @cpu1(cache=True)
|
| 152 |
# @cpu2(cache=True)
|
| 153 |
def translate(text,lang):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
if text == None or lang == None:
|
| 155 |
return ""
|
| 156 |
text = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', text)).lower().strip()
|
|
@@ -191,22 +201,32 @@ def translate(text,lang):
|
|
| 191 |
print(ret)
|
| 192 |
return ret
|
| 193 |
|
| 194 |
-
@
|
| 195 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 196 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 197 |
# @cpu1(cache=True)
|
| 198 |
# @cpu2(cache=True)
|
| 199 |
def generate_random_string(length):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
characters = string.ascii_letters + string.digits
|
| 201 |
return ''.join(random.choice(characters) for _ in range(length))
|
| 202 |
|
| 203 |
-
@
|
| 204 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 205 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 206 |
# @cpu1(cache=True)
|
| 207 |
# @cpu2(cache=True)
|
| 208 |
@spaces.GPU(duration=65)
|
| 209 |
def Piper(image,positive,negative,motion):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
global last_motion
|
| 211 |
global ip_loaded
|
| 212 |
|
|
@@ -242,38 +262,48 @@ def Piper(image,positive,negative,motion):
|
|
| 242 |
num_frames=(fps*time)
|
| 243 |
)
|
| 244 |
|
| 245 |
-
@
|
| 246 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 247 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 248 |
# @cpu1(cache=True)
|
| 249 |
# @cpu2(cache=True)
|
| 250 |
def infer(pm):
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
@
|
| 272 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 273 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 274 |
# @cpu1(cache=True)
|
| 275 |
# @cpu2(cache=True)
|
| 276 |
-
def handle(i,m,p1,p2,*result):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
p1_en = translate(p1,"english")
|
| 278 |
p2_en = translate(p2,"english")
|
| 279 |
pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
|
|
@@ -287,12 +317,17 @@ def handle(i,m,p1,p2,*result):
|
|
| 287 |
ret.append(infer,pm)
|
| 288 |
return ret
|
| 289 |
|
| 290 |
-
@
|
| 291 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 292 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 293 |
# @cpu1(cache=True)
|
| 294 |
# @cpu2(cache=True)
|
| 295 |
def ui():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
| 297 |
with gr.Column(elem_id="col-container"):
|
| 298 |
gr.Markdown(f"""
|
|
@@ -345,12 +380,17 @@ def ui():
|
|
| 345 |
)
|
| 346 |
demo.queue().launch()
|
| 347 |
|
| 348 |
-
@
|
| 349 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 350 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 351 |
# @cpu1(cache=True)
|
| 352 |
# @cpu2(cache=True)
|
| 353 |
def pre():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
| 355 |
pipe.scheduler = DDIMScheduler(
|
| 356 |
clip_sample=False,
|
|
@@ -368,7 +408,8 @@ def pre():
|
|
| 368 |
# entry
|
| 369 |
|
| 370 |
if __name__ == "__main__":
|
| 371 |
-
|
| 372 |
-
|
|
|
|
| 373 |
|
| 374 |
# end
|
|
|
|
| 31 |
from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
|
| 32 |
#import jax
|
| 33 |
#import jax.numpy as jnp
|
| 34 |
+
from numba import njit as cpu1, jit as cpu2, cuda
|
| 35 |
+
from numba.cuda import jit as gpu
|
| 36 |
|
| 37 |
# optimization:
|
| 38 |
|
| 39 |
+
# @gpu(cache=True)
|
| 40 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 41 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 42 |
# @cpu1(cache=True)
|
|
|
|
| 126 |
|
| 127 |
# functionality
|
| 128 |
|
| 129 |
+
@gpu(cache=True)
|
| 130 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 131 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 132 |
# @cpu1(cache=True)
|
| 133 |
# @cpu2(cache=True)
|
| 134 |
def run(cmd, assert_success=False, capture_output=False, env=None, dry_run=False):
|
| 135 |
+
tx = cuda.threadIdx.x
|
| 136 |
+
bx = cuda.blockIdx.x
|
| 137 |
+
dx = cuda.blockDim.x
|
| 138 |
+
pos = tx + bx * dx
|
| 139 |
+
|
| 140 |
if dry_run:
|
| 141 |
print(f"--> {cmd}")
|
| 142 |
result = 1
|
|
|
|
| 150 |
|
| 151 |
return result
|
| 152 |
|
| 153 |
+
@gpu(cache=True)
|
| 154 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 155 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 156 |
# @cpu1(cache=True)
|
| 157 |
# @cpu2(cache=True)
|
| 158 |
def translate(text,lang):
|
| 159 |
+
tx = cuda.threadIdx.x
|
| 160 |
+
bx = cuda.blockIdx.x
|
| 161 |
+
dx = cuda.blockDim.x
|
| 162 |
+
pos = tx + bx * dx
|
| 163 |
+
|
| 164 |
if text == None or lang == None:
|
| 165 |
return ""
|
| 166 |
text = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', text)).lower().strip()
|
|
|
|
| 201 |
print(ret)
|
| 202 |
return ret
|
| 203 |
|
| 204 |
+
@gpu(cache=True)
|
| 205 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 206 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 207 |
# @cpu1(cache=True)
|
| 208 |
# @cpu2(cache=True)
|
| 209 |
def generate_random_string(length):
|
| 210 |
+
tx = cuda.threadIdx.x
|
| 211 |
+
bx = cuda.blockIdx.x
|
| 212 |
+
dx = cuda.blockDim.x
|
| 213 |
+
pos = tx + bx * dx
|
| 214 |
+
|
| 215 |
characters = string.ascii_letters + string.digits
|
| 216 |
return ''.join(random.choice(characters) for _ in range(length))
|
| 217 |
|
| 218 |
+
@gpu(cache=True)
|
| 219 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 220 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 221 |
# @cpu1(cache=True)
|
| 222 |
# @cpu2(cache=True)
|
| 223 |
@spaces.GPU(duration=65)
|
| 224 |
def Piper(image,positive,negative,motion):
|
| 225 |
+
tx = cuda.threadIdx.x
|
| 226 |
+
bx = cuda.blockIdx.x
|
| 227 |
+
dx = cuda.blockDim.x
|
| 228 |
+
pos = tx + bx * dx
|
| 229 |
+
|
| 230 |
global last_motion
|
| 231 |
global ip_loaded
|
| 232 |
|
|
|
|
| 262 |
num_frames=(fps*time)
|
| 263 |
)
|
| 264 |
|
| 265 |
+
@gpu(cache=True)
|
| 266 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 267 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 268 |
# @cpu1(cache=True)
|
| 269 |
# @cpu2(cache=True)
|
| 270 |
def infer(pm):
|
| 271 |
+
tx = cuda.threadIdx.x
|
| 272 |
+
bx = cuda.blockIdx.x
|
| 273 |
+
dx = cuda.blockDim.x
|
| 274 |
+
pos = tx + bx * dx
|
| 275 |
+
|
| 276 |
+
print("infer: started")
|
| 277 |
|
| 278 |
+
p1 = pm["p"]
|
| 279 |
+
name = generate_random_string(12)+".png"
|
| 280 |
|
| 281 |
+
neg = pm["n"]
|
| 282 |
+
if neg != "":
|
| 283 |
+
neg = f"{neg} where in the image"
|
| 284 |
+
|
| 285 |
+
_do = ['photographed', 'realistic', 'dynamic poze', 'deep field', 'reasonable', "natural", 'rough', 'best quality', 'focused', "highly detailed"]
|
| 286 |
+
if p1 != "":
|
| 287 |
+
_do.append(f"a new {p1} content in the image")
|
| 288 |
+
posi = ", ".join(_do)
|
| 289 |
+
|
| 290 |
+
if pm["i"] == None:
|
| 291 |
+
return None
|
| 292 |
+
out = Piper(pm["i"],posi,neg,pm["m"])
|
| 293 |
+
export_to_gif(out.frames[0],name,fps=fps)
|
| 294 |
+
return name
|
| 295 |
+
|
| 296 |
+
@gpu(cache=True)
|
| 297 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 298 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 299 |
# @cpu1(cache=True)
|
| 300 |
# @cpu2(cache=True)
|
| 301 |
+
def handle(i,m,p1,p2,*result):
|
| 302 |
+
tx = cuda.threadIdx.x
|
| 303 |
+
bx = cuda.blockIdx.x
|
| 304 |
+
dx = cuda.blockDim.x
|
| 305 |
+
pos = tx + bx * dx
|
| 306 |
+
|
| 307 |
p1_en = translate(p1,"english")
|
| 308 |
p2_en = translate(p2,"english")
|
| 309 |
pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
|
|
|
|
| 317 |
ret.append(infer,pm)
|
| 318 |
return ret
|
| 319 |
|
| 320 |
+
@gpu(cache=True)
|
| 321 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 322 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 323 |
# @cpu1(cache=True)
|
| 324 |
# @cpu2(cache=True)
|
| 325 |
def ui():
|
| 326 |
+
tx = cuda.threadIdx.x
|
| 327 |
+
bx = cuda.blockIdx.x
|
| 328 |
+
dx = cuda.blockDim.x
|
| 329 |
+
pos = tx + bx * dx
|
| 330 |
+
|
| 331 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
| 332 |
with gr.Column(elem_id="col-container"):
|
| 333 |
gr.Markdown(f"""
|
|
|
|
| 380 |
)
|
| 381 |
demo.queue().launch()
|
| 382 |
|
| 383 |
+
@gpu(cache=True)
|
| 384 |
# @cpu1(cache=True,nopython=True,parallel=True)
|
| 385 |
# @cpu2(cache=True,nopython=True,parallel=True)
|
| 386 |
# @cpu1(cache=True)
|
| 387 |
# @cpu2(cache=True)
|
| 388 |
def pre():
|
| 389 |
+
tx = cuda.threadIdx.x
|
| 390 |
+
bx = cuda.blockIdx.x
|
| 391 |
+
dx = cuda.blockDim.x
|
| 392 |
+
pos = tx + bx * dx
|
| 393 |
+
|
| 394 |
pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
| 395 |
pipe.scheduler = DDIMScheduler(
|
| 396 |
clip_sample=False,
|
|
|
|
| 408 |
# entry
|
| 409 |
|
| 410 |
if __name__ == "__main__":
|
| 411 |
+
|
| 412 |
+
pre[32,32]()
|
| 413 |
+
ui[32,32]()
|
| 414 |
|
| 415 |
# end
|