Yaron Koresh commited on
Commit
455379c
·
verified ·
1 Parent(s): 7985d5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -50
app.py CHANGED
@@ -28,7 +28,7 @@ from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, Eul
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
 
@@ -50,7 +50,6 @@ root.addHandler(handler2)
50
 
51
  last_motion=None
52
  dtype = torch.float16
53
- result=[]
54
  device = "cuda"
55
  #repo = "ByteDance/AnimateDiff-Lightning"
56
  #ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
@@ -122,7 +121,7 @@ def run(cmd):
122
  sys.exit()
123
  return str(result.stdout)
124
 
125
- @gpu()
126
  def translate(text,lang):
127
 
128
  if text == None or lang == None:
@@ -165,60 +164,61 @@ def translate(text,lang):
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
 
179
 
180
- if last_motion != motion:
 
 
181
  pipe.unload_lora_weights()
182
- if motion != "":
183
- pipe.load_lora_weights(motion, adapter_name="motion")
184
  pipe.fuse_lora()
185
  pipe.set_adapters(["motion"], [0.7])
186
- last_motion = motion
187
 
188
  pipe.to(device,dtype)
189
 
190
- if negative=="":
191
- return pipe(
192
- prompt=positive,
193
  height=height,
194
  width=width,
195
- ip_adapter_image=image.convert("RGB").resize((width,height)),
196
  num_inference_steps=step,
197
  guidance_scale=accu,
198
  num_frames=(fps*time)
199
  )
200
 
201
- return pipe(
202
- prompt=positive,
203
- negative_prompt=negative,
204
  height=height,
205
  width=width,
206
- ip_adapter_image=image.convert("RGB").resize((width,height)),
207
  num_inference_steps=step,
208
  guidance_scale=accu,
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,34 +231,37 @@ def infer(pm):
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"):
256
  gr.Markdown(f"""
257
  # MULTI-LANGUAGE IMAGE GENERATOR
258
  """)
259
  with gr.Row():
 
260
  img = gr.Image(label="STATIC PHOTO",show_label=True,container=True,type="pil")
261
  with gr.Row():
 
262
  prompt = gr.Textbox(
263
  elem_id="prompt",
264
  placeholder="INCLUDE",
@@ -266,6 +269,7 @@ def ui():
266
  max_lines=1
267
  )
268
  with gr.Row():
 
269
  prompt2 = gr.Textbox(
270
  elem_id="prompt2",
271
  placeholder="EXCLUDE",
@@ -273,6 +277,7 @@ def ui():
273
  max_lines=1
274
  )
275
  with gr.Row():
 
276
  motion = gr.Dropdown(
277
  label='CAMERA',
278
  show_label=True,
@@ -292,20 +297,18 @@ def ui():
292
  interactive=True
293
  )
294
  with gr.Row():
295
- run_button = gr.Button("START",elem_classes="btn",scale=0)
 
296
  with gr.Row():
297
- result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
298
- result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
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(
311
  clip_sample=False,
@@ -320,15 +323,28 @@ def pre():
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
 
332
- entry()
333
 
334
  # end
 
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 jit as gpu, grid
32
  from numba.types import unicode_type as string
33
  # logging
34
 
 
50
 
51
  last_motion=None
52
  dtype = torch.float16
 
53
  device = "cuda"
54
  #repo = "ByteDance/AnimateDiff-Lightning"
55
  #ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
 
121
  sys.exit()
122
  return str(result.stdout)
123
 
124
+ @gpu(string(string,string),device=True,inline=True)
125
  def translate(text,lang):
126
 
127
  if text == None or lang == None:
 
164
  print(ret)
165
  return ret
166
 
167
+ @gpu(string(int),device=True,inline=True)
168
  def generate_random_string(length):
169
 
170
  characters = string.ascii_letters + string.digits
171
  return ''.join(random.choice(characters) for _ in range(length))
172
 
173
+ @gpu(void(),device=True,inline=True)
174
+ def Piper():
175
 
176
  global last_motion
177
  global ip_loaded
178
+ global out
179
 
180
+ x = grid(1)
181
+
182
+ if last_motion != pinp["motion"]:
183
  pipe.unload_lora_weights()
184
+ if pinp["motion"] != "":
185
+ pipe.load_lora_weights(pinp["motion"], adapter_name="motion")
186
  pipe.fuse_lora()
187
  pipe.set_adapters(["motion"], [0.7])
188
+ last_motion = pinp["motion"]
189
 
190
  pipe.to(device,dtype)
191
 
192
+ if pinp["negative"]=="":
193
+ out[x] = pipe(
194
+ prompt=pinp["positive"],
195
  height=height,
196
  width=width,
197
+ ip_adapter_image=pinp["image"].convert("RGB").resize((width,height)),
198
  num_inference_steps=step,
199
  guidance_scale=accu,
200
  num_frames=(fps*time)
201
  )
202
 
203
+ out[x] = pipe(
204
+ prompt=pinp["positive"],
205
+ negative_prompt=pinp["negative"],
206
  height=height,
207
  width=width,
208
+ ip_adapter_image=pinp["image"].convert("RGB").resize((width,height)),
209
  num_inference_steps=step,
210
  guidance_scale=accu,
211
  num_frames=(fps*time)
212
  )
213
 
214
+ @gpu(void(),device=True,inline=True)
215
+ def infer():
216
+ global pinp
217
+ global out
218
+
219
+ out =[]
 
220
 
221
  p1 = pm["p"]
 
222
 
223
  neg = pm["n"]
224
  if neg != "":
 
231
 
232
  if pm["i"] == None:
233
  return None
 
 
 
234
 
235
+ pinp={"image":pm["i"],"positive":posi,"negative":neg,"motion":pm["m"]}
236
 
237
+ ln = len(result)
238
+ Piper[ln,32]()
239
+ for i in range(ln):
240
+ name = generate_random_string[1,32](12)+".png"
241
+ export_to_gif(out[i].frames[0],name,fps=fps)
242
+ out[i] = name
243
+
244
+ @cpu(string[:](),cache=True,parallel=True)
245
+ def handle():
246
+ global pm
247
  p1_en = translate[1,32](p1,"english")
248
  p2_en = translate[1,32](p2,"english")
249
  pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
250
+ infer[1,32]()
251
+ return out
 
 
 
 
 
252
 
253
+ @gpu(void(),device=True,inline=True)
254
  def ui():
 
255
  with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
256
  with gr.Column(elem_id="col-container"):
257
  gr.Markdown(f"""
258
  # MULTI-LANGUAGE IMAGE GENERATOR
259
  """)
260
  with gr.Row():
261
+ global img
262
  img = gr.Image(label="STATIC PHOTO",show_label=True,container=True,type="pil")
263
  with gr.Row():
264
+ global prompt
265
  prompt = gr.Textbox(
266
  elem_id="prompt",
267
  placeholder="INCLUDE",
 
269
  max_lines=1
270
  )
271
  with gr.Row():
272
+ global prompt2
273
  prompt2 = gr.Textbox(
274
  elem_id="prompt2",
275
  placeholder="EXCLUDE",
 
277
  max_lines=1
278
  )
279
  with gr.Row():
280
+ global motion
281
  motion = gr.Dropdown(
282
  label='CAMERA',
283
  show_label=True,
 
297
  interactive=True
298
  )
299
  with gr.Row():
300
+ global run_button
301
+ run_button = gr.Button("START",elem_classes="btn",scale=0)
302
  with gr.Row():
303
+ global result
304
+ result = []
305
+ result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
306
+ result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
 
 
 
307
  demo.queue().launch()
308
 
309
+ @gpu(void(),device=True,inline=True)
310
  def pre():
311
+ global pipe
312
  pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
313
  pipe.scheduler = DDIMScheduler(
314
  clip_sample=False,
 
323
  pipe.enable_vae_slicing()
324
  pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
325
 
326
+ @gpu(void(),device=True,inline=True)
327
+ def events():
328
+ gr.on(
329
+ triggers=[
330
+ run_button.click,
331
+ prompt.submit,
332
+ prompt2.submit
333
+ ],
334
+ fn=handle
335
+ output=result
336
+ )
337
+
338
  @cpu(void(),cache=True,parallel=True)
339
  def entry():
340
  os.chdir(os.path.abspath(os.path.dirname(__file__)))
341
  mp.set_start_method("spawn", force=True)
342
  pre[1,32]()
343
  ui[1,32]()
344
+ events[1,32]()
345
 
346
  # entry
347
 
348
+ entry[1,32]()
349
 
350
  # end