Yaron Koresh commited on
Commit
f94caf3
·
verified ·
1 Parent(s): 8d7b4f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -92
app.py CHANGED
@@ -42,28 +42,6 @@ run("apt install mpich libopenmpi-dev")
42
  run("python -m pip install --upgrade pip")
43
  run("pip install -r req.txt")
44
 
45
- def deps():
46
- import spaces
47
- import torch
48
- import gradio as gr
49
- import numpy as np
50
- from lxml.html import fromstring
51
- #from transformers import pipeline
52
- from torch import multiprocessing as mp, nn
53
- #from torch.multiprocessing import Pool
54
- #from pathos.multiprocessing import ProcessPool as Pool
55
- #from pathos.threading import ThreadPool as Pool
56
- #from diffusers.pipelines.flux import FluxPipeline
57
- from diffusers.utils import export_to_gif, load_image
58
- from diffusers.models.modeling_utils import ModelMixin
59
- from huggingface_hub import hf_hub_download
60
- from safetensors.torch import load_file, save_file
61
- from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
62
- #import jax
63
- #import jax.numpy as jnp
64
- from pyina.launchers import TorqueMpiPool as Pool
65
- deps()
66
-
67
  last_motion=None
68
  dtype = torch.float16
69
  result=[]
@@ -125,6 +103,33 @@ function custom(){
125
  }
126
  """
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  def translate(text,lang):
129
  if text == None or lang == None:
130
  return ""
@@ -166,11 +171,13 @@ def translate(text,lang):
166
  print(ret)
167
  return ret
168
 
 
169
  def generate_random_string(length):
170
  characters = string.ascii_letters + string.digits
171
  return ''.join(random.choice(characters) for _ in range(length))
172
 
173
- @spaces.GPU(duration=65)
 
174
  def Piper(image,positive,negative,motion):
175
  global last_motion
176
  global ip_loaded
@@ -207,6 +214,7 @@ def Piper(image,positive,negative,motion):
207
  num_frames=(fps*time)
208
  )
209
 
 
210
  def infer(pm):
211
  print("infer: started")
212
 
@@ -228,82 +236,94 @@ def infer(pm):
228
  export_to_gif(out.frames[0],name,fps=fps)
229
  return name
230
 
231
- def main(i,m,p1,p2,*result):
232
-
233
- p1_en = translate(p1,"english")
234
- p2_en = translate(p2,"english")
235
- pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
236
- ln = len(result)
237
- rng = list(range(ln))
238
- arr = [pm for _ in rng]
 
 
 
 
 
239
 
240
- with Pool(f'{ ln }:ppn=2', queue='productionQ', timelimit='5:00:00', workdir='.') as pool:
241
- return pool.map(infer,arr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
- pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
244
- pipe.scheduler = DDIMScheduler(
245
  clip_sample=False,
246
  beta_start=0.00085,
247
  beta_end=0.012,
248
  beta_schedule="linear",
249
  timestep_spacing="trailing",
250
  steps_offset=1
251
- )
252
- pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
253
- pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin")
254
- pipe.enable_vae_slicing()
255
- pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
256
 
257
- mp.set_start_method("spawn", force=True)
258
 
259
- with gr.Blocks(theme=gr.themes.Soft(),css=css,js=js) as demo:
260
- with gr.Column(elem_id="col-container"):
261
- gr.Markdown(f"""
262
- # MULTI-LANGUAGE IMAGE GENERATOR
263
- """)
264
- with gr.Row():
265
- img = gr.Image(label="STATIC PHOTO",show_label=True,container=True,type="pil")
266
- with gr.Row():
267
- prompt = gr.Textbox(
268
- elem_id="prompt",
269
- placeholder="INCLUDE",
270
- container=False,
271
- max_lines=1
272
- )
273
- with gr.Row():
274
- prompt2 = gr.Textbox(
275
- elem_id="prompt2",
276
- placeholder="EXCLUDE",
277
- container=False,
278
- max_lines=1
279
- )
280
- with gr.Row():
281
- motion = gr.Dropdown(
282
- label='CAMERA',
283
- show_label=True,
284
- container=True,
285
- choices=[
286
- ("(No Effect)", ""),
287
- ("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
288
- ("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
289
- ("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
290
- ("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
291
- ("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
292
- ("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
293
- ("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
294
- ("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
295
- ],
296
- value="",
297
- interactive=True
298
- )
299
- with gr.Row():
300
- run_button = gr.Button("START",elem_classes="btn",scale=0)
301
- with gr.Row():
302
- result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
303
- result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
304
-
305
- gr.on(
306
- triggers=[run_button.click, prompt.submit, prompt2.submit],
307
- fn=main,inputs=[img,motion,prompt,prompt2,*result],outputs=result
308
- )
309
- demo.queue().launch()
 
42
  run("python -m pip install --upgrade pip")
43
  run("pip install -r req.txt")
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  last_motion=None
46
  dtype = torch.float16
47
  result=[]
 
103
  }
104
  """
105
 
106
+ @njit(cache=True,nopython=True,parallel=True)
107
+ def deps():
108
+ try:
109
+ import spaces
110
+ import torch
111
+ import gradio as gr
112
+ import numpy as np
113
+ from lxml.html import fromstring
114
+ #from transformers import pipeline
115
+ from torch import multiprocessing as mp, nn
116
+ #from torch.multiprocessing import Pool
117
+ #from pathos.multiprocessing import ProcessPool as Pool
118
+ #from pathos.threading import ThreadPool as Pool
119
+ #from diffusers.pipelines.flux import FluxPipeline
120
+ from diffusers.utils import export_to_gif, load_image
121
+ from diffusers.models.modeling_utils import ModelMixin
122
+ from huggingface_hub import hf_hub_download
123
+ from safetensors.torch import load_file, save_file
124
+ from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
125
+ #import jax
126
+ #import jax.numpy as jnp
127
+ from pyina.launchers import TorqueMpiPool as Pool
128
+ from numba import jit,njit
129
+ except Exception as e:
130
+ pass
131
+
132
+ @njit(cache=True,nopython=True,parallel=True)
133
  def translate(text,lang):
134
  if text == None or lang == None:
135
  return ""
 
171
  print(ret)
172
  return ret
173
 
174
+ @njit(cache=True,nopython=True,parallel=True)
175
  def generate_random_string(length):
176
  characters = string.ascii_letters + string.digits
177
  return ''.join(random.choice(characters) for _ in range(length))
178
 
179
+ #@spaces.GPU(duration=65)
180
+ @njit(cache=True,nopython=True,parallel=True)
181
  def Piper(image,positive,negative,motion):
182
  global last_motion
183
  global ip_loaded
 
214
  num_frames=(fps*time)
215
  )
216
 
217
+ @njit(cache=True,nopython=True,parallel=True)
218
  def infer(pm):
219
  print("infer: started")
220
 
 
236
  export_to_gif(out.frames[0],name,fps=fps)
237
  return name
238
 
239
+ @njit(cache=True,nopython=True,parallel=True)
240
+ def handle(i,m,p1,p2,*result):
241
+ p1_en = translate(p1,"english")
242
+ p2_en = translate(p2,"english")
243
+ pm = {"p":p1_en,"n":p2_en,"m":m,"i":i}
244
+ ln = len(result)
245
+ rng = list(range(ln))
246
+ arr = [pm for _ in rng]
247
+ #with Pool(f'{ ln }:ppn=2', queue='productionQ', timelimit='5:00:00', workdir='.') as pool:
248
+ #return pool.map(infer,arr)
249
+ ret = []
250
+ ret.append(infer,pm) for _ in range(ln)
251
+ return ret
252
 
253
+ @njit(cache=True,nopython=True,parallel=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
+ img = gr.Image(label="STATIC PHOTO",show_label=True,container=True,type="pil")
262
+ with gr.Row():
263
+ prompt = gr.Textbox(
264
+ elem_id="prompt",
265
+ placeholder="INCLUDE",
266
+ container=False,
267
+ max_lines=1
268
+ )
269
+ with gr.Row():
270
+ prompt2 = gr.Textbox(
271
+ elem_id="prompt2",
272
+ placeholder="EXCLUDE",
273
+ container=False,
274
+ max_lines=1
275
+ )
276
+ with gr.Row():
277
+ motion = gr.Dropdown(
278
+ label='CAMERA',
279
+ show_label=True,
280
+ container=True,
281
+ choices=[
282
+ ("(No Effect)", ""),
283
+ ("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
284
+ ("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
285
+ ("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
286
+ ("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
287
+ ("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
288
+ ("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
289
+ ("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
290
+ ("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
291
+ ],
292
+ value="",
293
+ interactive=True
294
+ )
295
+ with gr.Row():
296
+ run_button = gr.Button("START",elem_classes="btn",scale=0)
297
+ with gr.Row():
298
+ result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
299
+ result.append(gr.Image(interactive=False,elem_classes="image-container", label="Result", show_label=False, type='filepath', show_share_button=False))
300
+
301
+ gr.on(
302
+ triggers=[run_button.click, prompt.submit, prompt2.submit],
303
+ fn=handle,inputs=[img,motion,prompt,prompt2,*result],outputs=result
304
+ )
305
+ demo.queue().launch()
306
+
307
+ @njit(cache=True,nopython=True,parallel=True)
308
+ def pre():
309
+ global pipe
310
 
311
+ pipe = AnimateDiffPipeline.from_pretrained(base, vae=vae, motion_adapter=adapter, torch_dtype=dtype).to(device)
312
+ pipe.scheduler = DDIMScheduler(
313
  clip_sample=False,
314
  beta_start=0.00085,
315
  beta_end=0.012,
316
  beta_schedule="linear",
317
  timestep_spacing="trailing",
318
  steps_offset=1
319
+ )
320
+ pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
321
+ pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin")
322
+ pipe.enable_vae_slicing()
323
+ pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
324
 
325
+ mp.set_start_method("spawn", force=True)
326
 
327
+ deps()
328
+ pre()
329
+ ui()