Update voice_processing.py
Browse files- voice_processing.py +49 -1
voice_processing.py
CHANGED
@@ -245,4 +245,52 @@ voice_mapping = {
|
|
245 |
|
246 |
hubert_model = load_hubert()
|
247 |
|
248 |
-
rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
|
246 |
hubert_model = load_hubert()
|
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 |
+
|
259 |
+
async def tts(self, model_name, tts_text, tts_voice, index_rate, use_uploaded_voice, uploaded_voice):
|
260 |
+
task = asyncio.create_task(self._tts_task(model_name, tts_text, tts_voice, index_rate, use_uploaded_voice, uploaded_voice))
|
261 |
+
await self.queue.put(task)
|
262 |
+
|
263 |
+
if not self.is_processing:
|
264 |
+
asyncio.create_task(self._process_queue())
|
265 |
+
|
266 |
+
return await task
|
267 |
+
|
268 |
+
async def _tts_task(self, model_name, tts_text, tts_voice, index_rate, use_uploaded_voice, uploaded_voice):
|
269 |
+
async with self.semaphore:
|
270 |
+
return await tts(model_name, tts_text, tts_voice, index_rate, use_uploaded_voice, uploaded_voice)
|
271 |
+
|
272 |
+
async def _process_queue(self):
|
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
|