Spaces:
Running
Running
Yaron Koresh
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -27,9 +27,9 @@ from safetensors.torch import load_file, save_file
|
|
27 |
from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
|
28 |
#import jax
|
29 |
#import jax.numpy as jnp
|
30 |
-
from numba import cuda
|
31 |
from numba.cuda import autojit as gpu, grid
|
32 |
-
|
33 |
# logging
|
34 |
|
35 |
warnings.filterwarnings("ignore")
|
@@ -101,6 +101,7 @@ footer {
|
|
101 |
display: flex;
|
102 |
}
|
103 |
"""
|
|
|
104 |
js="""
|
105 |
function custom(){
|
106 |
document.querySelector("div#prompt input").setAttribute("maxlength","38")
|
@@ -110,11 +111,9 @@ function custom(){
|
|
110 |
|
111 |
# functionality
|
112 |
|
|
|
113 |
def run(cmd):
|
114 |
-
|
115 |
-
#y = cuda.threadIdx.y + cuda.blockIdx.y * cuda.blockDim.y
|
116 |
-
#z = cuda.threadIdx.z + cuda.blockIdx.z * cuda.blockDim.z
|
117 |
-
|
118 |
result = subprocess.run(cmd, shell=True, capture_output=True, env=None)
|
119 |
if result.returncode != 0:
|
120 |
logging.error(
|
@@ -126,11 +125,6 @@ def run(cmd):
|
|
126 |
@gpu()
|
127 |
def translate(text,lang):
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
text=text[]
|
132 |
-
lang=lang[pos]
|
133 |
-
|
134 |
if text == None or lang == None:
|
135 |
return ""
|
136 |
text = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', text)).lower().strip()
|
@@ -171,35 +165,14 @@ def translate(text,lang):
|
|
171 |
print(ret)
|
172 |
return ret
|
173 |
|
174 |
-
@
|
175 |
def generate_random_string(length):
|
176 |
-
|
177 |
-
tx = cuda.threadIdx.x
|
178 |
-
bx = cuda.blockIdx.x
|
179 |
-
dx = cuda.blockDim.x
|
180 |
-
pos = tx + bx * dx
|
181 |
-
except:
|
182 |
-
pos = 0
|
183 |
-
|
184 |
-
length=length[pos]
|
185 |
-
|
186 |
characters = string.ascii_letters + string.digits
|
187 |
return ''.join(random.choice(characters) for _ in range(length))
|
188 |
|
189 |
-
@gpu(
|
190 |
def Piper(image,positive,negative,motion):
|
191 |
-
try:
|
192 |
-
tx = cuda.threadIdx.x
|
193 |
-
bx = cuda.blockIdx.x
|
194 |
-
dx = cuda.blockDim.x
|
195 |
-
pos = tx + bx * dx
|
196 |
-
except:
|
197 |
-
pos = 0
|
198 |
-
|
199 |
-
image=image[pos]
|
200 |
-
positive=positive[pos]
|
201 |
-
negative=negative[pos]
|
202 |
-
motion=motion[pos]
|
203 |
|
204 |
global last_motion
|
205 |
global ip_loaded
|
@@ -236,22 +209,16 @@ def Piper(image,positive,negative,motion):
|
|
236 |
num_frames=(fps*time)
|
237 |
)
|
238 |
|
239 |
-
@gpu(
|
240 |
def infer(pm):
|
241 |
-
|
242 |
-
tx = cuda.threadIdx.x
|
243 |
-
bx = cuda.blockIdx.x
|
244 |
-
dx = cuda.blockDim.x
|
245 |
-
pos = tx + bx * dx
|
246 |
-
except:
|
247 |
-
pos = 0
|
248 |
|
249 |
-
pm = pm[
|
250 |
|
251 |
print("infer: started")
|
252 |
|
253 |
p1 = pm["p"]
|
254 |
-
name = generate_random_string(
|
255 |
|
256 |
neg = pm["n"]
|
257 |
if neg != "":
|
@@ -264,46 +231,25 @@ def infer(pm):
|
|
264 |
|
265 |
if pm["i"] == None:
|
266 |
return None
|
267 |
-
out = Piper[
|
268 |
export_to_gif(out.frames[0],name,fps=fps)
|
269 |
return name
|
270 |
|
271 |
-
@cpu(cache=True)
|
272 |
def handle(i,m,p1,p2,result):
|
273 |
-
try:
|
274 |
-
tx = cuda.threadIdx.x
|
275 |
-
bx = cuda.blockIdx.x
|
276 |
-
dx = cuda.blockDim.x
|
277 |
-
pos = tx + bx * dx
|
278 |
-
except:
|
279 |
-
pos = 0
|
280 |
|
281 |
-
|
282 |
-
|
283 |
-
p1=p1[pos]
|
284 |
-
p2=p2[pos]
|
285 |
-
result=result[pos]
|
286 |
-
|
287 |
-
p1_en = translate([p1],["english"])
|
288 |
-
p2_en = translate([p2],["english"])
|
289 |
pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
|
290 |
ln = len(result)
|
291 |
rng = list(range(ln))
|
292 |
arr = [pm for _ in rng]
|
293 |
#with Pool(f'{ ln }:ppn=2', queue='productionQ', timelimit='5:00:00', workdir='.') as pool:
|
294 |
#return pool.map(infer,arr)
|
295 |
-
ret = infer[
|
296 |
return ret
|
297 |
|
298 |
-
@
|
299 |
def ui():
|
300 |
-
try:
|
301 |
-
tx = cuda.threadIdx.x
|
302 |
-
bx = cuda.blockIdx.x
|
303 |
-
dx = cuda.blockDim.x
|
304 |
-
pos = tx + bx * dx
|
305 |
-
except:
|
306 |
-
pos = 0
|
307 |
|
308 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
309 |
with gr.Column(elem_id="col-container"):
|
@@ -353,19 +299,12 @@ def ui():
|
|
353 |
|
354 |
gr.on(
|
355 |
triggers=[run_button.click, prompt.submit, prompt2.submit],
|
356 |
-
fn=handle,inputs=[
|
357 |
)
|
358 |
demo.queue().launch()
|
359 |
|
360 |
-
@
|
361 |
def pre():
|
362 |
-
try:
|
363 |
-
tx = cuda.threadIdx.x
|
364 |
-
bx = cuda.blockIdx.x
|
365 |
-
dx = cuda.blockDim.x
|
366 |
-
pos = tx + bx * dx
|
367 |
-
except:
|
368 |
-
pos = 0
|
369 |
|
370 |
pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
371 |
pipe.scheduler = DDIMScheduler(
|
@@ -381,20 +320,12 @@ def pre():
|
|
381 |
pipe.enable_vae_slicing()
|
382 |
pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
|
383 |
|
384 |
-
@cpu(cache=True)
|
385 |
def entry():
|
386 |
-
try:
|
387 |
-
tx = cuda.threadIdx.x
|
388 |
-
bx = cuda.blockIdx.x
|
389 |
-
dx = cuda.blockDim.x
|
390 |
-
pos = tx + bx * dx
|
391 |
-
except:
|
392 |
-
pos = 0
|
393 |
-
|
394 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
395 |
mp.set_start_method("spawn", force=True)
|
396 |
-
pre()
|
397 |
-
ui()
|
398 |
|
399 |
# entry
|
400 |
|
|
|
27 |
from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
|
28 |
#import jax
|
29 |
#import jax.numpy as jnp
|
30 |
+
from numba import cuda, njit as cpu, void, int64 as int, float64 as float, boolean as bool
|
31 |
from numba.cuda import autojit as gpu, grid
|
32 |
+
from numba.types import unicode_type as string
|
33 |
# logging
|
34 |
|
35 |
warnings.filterwarnings("ignore")
|
|
|
101 |
display: flex;
|
102 |
}
|
103 |
"""
|
104 |
+
|
105 |
js="""
|
106 |
function custom(){
|
107 |
document.querySelector("div#prompt input").setAttribute("maxlength","38")
|
|
|
111 |
|
112 |
# functionality
|
113 |
|
114 |
+
@cpu(string(string),cache=True,parallel=True)
|
115 |
def run(cmd):
|
116 |
+
|
|
|
|
|
|
|
117 |
result = subprocess.run(cmd, shell=True, capture_output=True, env=None)
|
118 |
if result.returncode != 0:
|
119 |
logging.error(
|
|
|
125 |
@gpu()
|
126 |
def translate(text,lang):
|
127 |
|
|
|
|
|
|
|
|
|
|
|
128 |
if text == None or lang == None:
|
129 |
return ""
|
130 |
text = re.sub(f'[{string.punctuation}]', '', re.sub('[\s+]', ' ', text)).lower().strip()
|
|
|
165 |
print(ret)
|
166 |
return ret
|
167 |
|
168 |
+
@gpu()
|
169 |
def generate_random_string(length):
|
170 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
characters = string.ascii_letters + string.digits
|
172 |
return ''.join(random.choice(characters) for _ in range(length))
|
173 |
|
174 |
+
@gpu()
|
175 |
def Piper(image,positive,negative,motion):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
global last_motion
|
178 |
global ip_loaded
|
|
|
209 |
num_frames=(fps*time)
|
210 |
)
|
211 |
|
212 |
+
@gpu()
|
213 |
def infer(pm):
|
214 |
+
x = grid(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
+
pm = pm[x]
|
217 |
|
218 |
print("infer: started")
|
219 |
|
220 |
p1 = pm["p"]
|
221 |
+
name = generate_random_string[1,32](12)+".png"
|
222 |
|
223 |
neg = pm["n"]
|
224 |
if neg != "":
|
|
|
231 |
|
232 |
if pm["i"] == None:
|
233 |
return None
|
234 |
+
out = Piper[1,32](pm["i"],posi,neg,pm["m"])
|
235 |
export_to_gif(out.frames[0],name,fps=fps)
|
236 |
return name
|
237 |
|
|
|
238 |
def handle(i,m,p1,p2,result):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
+
p1_en = translate[1,32](p1,"english")
|
241 |
+
p2_en = translate[1,32](p2,"english")
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
|
243 |
ln = len(result)
|
244 |
rng = list(range(ln))
|
245 |
arr = [pm for _ in rng]
|
246 |
#with Pool(f'{ ln }:ppn=2', queue='productionQ', timelimit='5:00:00', workdir='.') as pool:
|
247 |
#return pool.map(infer,arr)
|
248 |
+
ret = infer[ln,32](arr)
|
249 |
return ret
|
250 |
|
251 |
+
@gpu()
|
252 |
def ui():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
|
255 |
with gr.Column(elem_id="col-container"):
|
|
|
299 |
|
300 |
gr.on(
|
301 |
triggers=[run_button.click, prompt.submit, prompt2.submit],
|
302 |
+
fn=handle,inputs=[img,motion,prompt,prompt2,result],outputs=result
|
303 |
)
|
304 |
demo.queue().launch()
|
305 |
|
306 |
+
@gpu()
|
307 |
def pre():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
|
309 |
pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
310 |
pipe.scheduler = DDIMScheduler(
|
|
|
320 |
pipe.enable_vae_slicing()
|
321 |
pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
|
322 |
|
323 |
+
@cpu(void(),cache=True,parallel=True)
|
324 |
def entry():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
326 |
mp.set_start_method("spawn", force=True)
|
327 |
+
pre[1,32]()
|
328 |
+
ui[1,32]()
|
329 |
|
330 |
# entry
|
331 |
|