MAZALA2024 commited on
Commit
8ba4d72
·
verified ·
1 Parent(s): e0c5b9e

Update voice_processing.py

Browse files
Files changed (1) hide show
  1. voice_processing.py +21 -9
voice_processing.py CHANGED
@@ -111,6 +111,7 @@ def get_model_names():
111
  model_root = "weights" # Assuming this is where your models are stored
112
  return [d for d in os.listdir(model_root) if os.path.isdir(f"{model_root}/{d}")]
113
 
 
114
  def run_async_in_thread(fn, *args):
115
  loop = asyncio.new_event_loop()
116
  asyncio.set_event_loop(loop)
@@ -118,6 +119,12 @@ def run_async_in_thread(fn, *args):
118
  loop.close()
119
  return result
120
 
 
 
 
 
 
 
121
  async def tts(
122
  model_name,
123
  tts_text,
@@ -240,11 +247,12 @@ hubert_model = load_hubert()
240
 
241
  rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
242
 
 
243
  class TTSProcessor:
244
  def __init__(self, config):
245
  self.config = config
246
  self.executor = ThreadPoolExecutor(max_workers=config.n_cpu)
247
- self.semaphore = asyncio.Semaphore(10) # Limit to 10 concurrent tasks
248
  self.queue = asyncio.Queue()
249
  self.is_processing = False
250
 
@@ -265,20 +273,24 @@ class TTSProcessor:
265
  self.is_processing = True
266
  while not self.queue.empty():
267
  task = await self.queue.get()
268
- try:
269
- await task
270
- except Exception as e:
271
- print(f"Error processing TTS task: {str(e)}")
272
- finally:
273
- self.queue.task_done()
274
  self.is_processing = False
275
 
276
  # Initialize the TTSProcessor
277
  tts_processor = TTSProcessor(config)
278
 
279
- async def parallel_tts(tasks):
 
280
  return await asyncio.gather(*(tts_processor.tts(*task) for task in tasks))
281
 
282
  def parallel_tts_wrapper(tasks):
283
  loop = asyncio.get_event_loop()
284
- return loop.run_until_complete(parallel_tts(tasks))
 
 
 
 
 
 
 
 
111
  model_root = "weights" # Assuming this is where your models are stored
112
  return [d for d in os.listdir(model_root) if os.path.isdir(f"{model_root}/{d}")]
113
 
114
+ # Add this helper function to ensure a new event loop is created if none exists
115
  def run_async_in_thread(fn, *args):
116
  loop = asyncio.new_event_loop()
117
  asyncio.set_event_loop(loop)
 
119
  loop.close()
120
  return result
121
 
122
+ def parallel_tts(tasks):
123
+ with ThreadPoolExecutor() as executor:
124
+ futures = [executor.submit(run_async_in_thread, tts, *task) for task in tasks]
125
+ results = [future.result() for future in futures]
126
+ return results
127
+
128
  async def tts(
129
  model_name,
130
  tts_text,
 
247
 
248
  rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
249
 
250
+ # Add the optimized TTSProcessor
251
  class TTSProcessor:
252
  def __init__(self, config):
253
  self.config = config
254
  self.executor = ThreadPoolExecutor(max_workers=config.n_cpu)
255
+ self.semaphore = asyncio.Semaphore(config.max_concurrent_tts)
256
  self.queue = asyncio.Queue()
257
  self.is_processing = False
258
 
 
273
  self.is_processing = True
274
  while not self.queue.empty():
275
  task = await self.queue.get()
276
+ await task
277
+ self.queue.task_done()
 
 
 
 
278
  self.is_processing = False
279
 
280
  # Initialize the TTSProcessor
281
  tts_processor = TTSProcessor(config)
282
 
283
+ # Update parallel_tts to use TTSProcessor
284
+ async def parallel_tts_processor(tasks):
285
  return await asyncio.gather(*(tts_processor.tts(*task) for task in tasks))
286
 
287
  def parallel_tts_wrapper(tasks):
288
  loop = asyncio.get_event_loop()
289
+ return loop.run_until_complete(parallel_tts_processor(tasks))
290
+
291
+ # Keep the original parallel_tts function
292
+ # def parallel_tts(tasks):
293
+ # with ThreadPoolExecutor() as executor:
294
+ # futures = [executor.submit(run_async_in_thread, tts, *task) for task in tasks]
295
+ # results = [future.result() for future in futures]
296
+ # return results