Yaron Koresh commited on
Commit
fb14070
·
verified ·
1 Parent(s): 0dc131f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -9
app.py CHANGED
@@ -10,6 +10,11 @@ from string import ascii_letters, digits
10
  import requests
11
  import sys
12
  import warnings
 
 
 
 
 
13
 
14
  # external
15
 
@@ -25,8 +30,6 @@ from diffusers.models.modeling_utils import ModelMixin
25
  from huggingface_hub import hf_hub_download
26
  from safetensors.torch import load_file, save_file
27
  from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
28
- from numba import jit, void as vid, int64 as int, float64 as flt, boolean as bol, uint8 as rgb
29
- from numba.types import unicode_type as str
30
  from functools import partial
31
 
32
  # logging
@@ -45,11 +48,6 @@ formatter = logging.Formatter('\n >>> [%(levelname)s] %(asctime)s %(name)s: %(me
45
  handler2.setFormatter(formatter)
46
  root.addHandler(handler2)
47
 
48
- # jit decorator shortcuts
49
-
50
- math = partial(jit, cache=True, parallel=True)
51
- loop = partial(jit, cache=True, forceobj=True)
52
-
53
  # storage data
54
 
55
  last_motion=""
@@ -135,8 +133,128 @@ pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapt
135
  pipe.enable_vae_slicing()
136
  pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # functionality
139
-
140
  def run(cmd):
141
  return str(subprocess.run(cmd, shell=True, capture_output=True, env=None).stdout)
142
 
@@ -222,7 +340,6 @@ def calc(img,p1,p2,motion):
222
  num_frames=(fps*time)
223
  )
224
 
225
- @loop
226
  def handle(*inp):
227
 
228
  inp[1] = translate(inp[1],"english")
 
10
  import requests
11
  import sys
12
  import warnings
13
+ import time
14
+ from concurrent.futures import ProcessPoolExecutor
15
+ import threading
16
+ import asyncio
17
+ from queue import Queue as BlockingQueue
18
 
19
  # external
20
 
 
30
  from huggingface_hub import hf_hub_download
31
  from safetensors.torch import load_file, save_file
32
  from diffusers import DiffusionPipeline, AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler, DDIMScheduler, StableDiffusionXLPipeline, UNet2DConditionModel, AutoencoderKL, UNet3DConditionModel
 
 
33
  from functools import partial
34
 
35
  # logging
 
48
  handler2.setFormatter(formatter)
49
  root.addHandler(handler2)
50
 
 
 
 
 
 
51
  # storage data
52
 
53
  last_motion=""
 
133
  pipe.enable_vae_slicing()
134
  pipe.enable_free_init(method="butterworth", use_fast_sampling=fast)
135
 
136
+ # Threading
137
+
138
+ class TwoSidedQueue:
139
+ def __init__(self, queue_in, queue_out):
140
+ self._queue_in = queue_in
141
+ self._queue_out = queue_out
142
+ self._sides = {
143
+ 'empty': queue_out,
144
+ 'full': queue_out,
145
+ 'get': queue_in,
146
+ 'get_nowait': queue_in,
147
+ 'join': queue_out,
148
+ 'put': queue_out,
149
+ 'put_nowait': queue_out,
150
+ 'qsize': queue_out,
151
+ 'task_done': queue_in,
152
+ }
153
+ def __getattr__(self, name):
154
+ return getattr(self._sides.get(name, self._queue_in), name)
155
+
156
+
157
+ class LaunchAsync:
158
+ def __init__(self, coro, *args, **kwargs):
159
+ self._coro = coro
160
+ self._args = args
161
+ self._kwargs = kwargs
162
+ self._thread = None
163
+ self._loop = None
164
+ self._task = None
165
+ self._queue_in = None
166
+ self._queue_out = None
167
+ self._size = 0
168
+
169
+ def size(self, size):
170
+ self._size = size or 0
171
+ return self
172
+
173
+ def put(self, data, *, timeout=None):
174
+ """
175
+ `put` data in for the `coro` to `get` out. Will block if the maximum `size` was reached.
176
+
177
+ Does nothing if the `coro` is dead.
178
+ """
179
+ try:
180
+ return asyncio.run_coroutine_threadsafe(self._queue_out.put(data), self._loop).result(timeout)
181
+ except RuntimeError:
182
+ if self._loop.is_running():
183
+ raise
184
+ else:
185
+ return None
186
+
187
+ def get(self, *, timeout=None):
188
+ """
189
+ `get` data out of the `coro` it `put` in. Will block if the queue is empty.
190
+
191
+ Returns `None` if the `coro` is dead.
192
+ """
193
+ try:
194
+ return asyncio.run_coroutine_threadsafe(self._queue_in.get(), self._loop).result(timeout)
195
+ except RuntimeError:
196
+ if self._loop.is_running():
197
+ raise
198
+ else:
199
+ return None
200
+
201
+ def dead(self):
202
+ """
203
+ Return `true` if the other side is dead (the `coro` has exited, with or without error).
204
+ """
205
+ return not self._loop.is_running()
206
+
207
+ def __enter__(self):
208
+ # asyncio.run is used as it's a battle-tested way to safely set up a new loop and tear
209
+ # it down. However it does mean it's necessary to wait for the task to run before it's
210
+ # possible to get said loop and task back. For this, the usual blocking queue is used.
211
+ oneshot = BlockingQueue(1)
212
+ self._thread = threading.Thread(target=asyncio.run, args=(
213
+ self._run(self._coro, self._size, oneshot, self._args, self._kwargs),))
214
+ self._thread.start()
215
+ self._loop, self._task, self._queue_in, self._queue_out = oneshot.get()
216
+ return self
217
+
218
+ def __exit__(self, exc_type, exc_value, exc_traceback):
219
+ try:
220
+ self._loop.call_soon_threadsafe(self._task.cancel)
221
+ except RuntimeError:
222
+ if self._loop.is_running():
223
+ raise
224
+ finally:
225
+ self._thread.join()
226
+
227
+ @staticmethod
228
+ async def _run(coro, size, oneshot, args, kwargs):
229
+ # asyncio.Queue's are created here so that they pick up the right loop.
230
+ queue_in, queue_out = asyncio.Queue(size), asyncio.Queue(size)
231
+ oneshot.put((asyncio.get_event_loop(), asyncio.current_task(), queue_in, queue_out))
232
+ try:
233
+ # `queue_in` and `queue_out` are intentionally swapped here.
234
+ await coro(TwoSidedQueue(queue_out, queue_in), *args, **kwargs)
235
+ except asyncio.CancelledError:
236
+ pass
237
+
238
+ class Command:
239
+ def __init__(self, func, data=None):
240
+ self.func = func
241
+ self.data = data
242
+
243
+ def parallel(*pairs):
244
+ async def async_main(queue):
245
+ while True:
246
+ command = await queue.get()
247
+ await queue.put(command.func(*command.data))
248
+
249
+ with LaunchAsync(async_main) as queue:
250
+ for pair in pairs:
251
+ f = pair.pop()
252
+ queue.put(Command(f, pair))
253
+ response = queue.get()
254
+ return response
255
+
256
  # functionality
257
+
258
  def run(cmd):
259
  return str(subprocess.run(cmd, shell=True, capture_output=True, env=None).stdout)
260
 
 
340
  num_frames=(fps*time)
341
  )
342
 
 
343
  def handle(*inp):
344
 
345
  inp[1] = translate(inp[1],"english")