From 26c34c6fa921c737ed79ca0cd40c7a83f1ab838a Mon Sep 17 00:00:00 2001 From: mtuszowski Date: Fri, 20 Sep 2024 16:26:01 +0200 Subject: [PATCH] asynv --- voice_recognition.py | 53 ++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/voice_recognition.py b/voice_recognition.py index 6230618..a589fbb 100644 --- a/voice_recognition.py +++ b/voice_recognition.py @@ -5,7 +5,6 @@ import assemblyai as aai import discord from discord.ext import commands, voice_recv, tasks from discord.opus import Decoder as OpusDecoder -from queue import Queue, Empty import time import threading # Replace with your API key @@ -127,11 +126,10 @@ class SRBuffer(voice_recv.AudioSink): class Transcriber(commands.Cog): - def __init__(self, bot, worker, queue): + def __init__(self, bot): self.bot = bot self.threads = [] - self.comm_queue = queue - self.worker = worker + self.comm_queue = asyncio.Queue() self.wsink = SRBuffer(self.comm_queue) @commands.hybrid_command(name="transcribe") @@ -151,7 +149,7 @@ class Transcriber(commands.Cog): logger.info(self.bot.voice_clients) self.check_data.start() logger.info("Start worker!~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - self.worker.start() + asyncio.run(self.transcribe_output_queue()) vc.listen(self.wsink) @@ -196,35 +194,28 @@ class Transcriber(commands.Cog): self.comm_queue.put(stop_token) await ctx.voice_client.disconnect() - -#this needs to be async and transcript fun awaited -def transcribe_output_queue(queue): - logger.info("Transcript worker start") - config = aai.TranscriptionConfig(speaker_labels=True, language_code="pl") - transcriber = aai.Transcriber() - while True: - item = queue.get() - if item.type == "STOP": - logger.info("Queue ended") - break - elif item.type == "send_file": - transcript = "" - for iter in item.data: - transcript = transcriber.transcribe(iter, config=config) - for utterance in transcript.utterances: - logger.info("%s : %s", item.user, utterance.text) - elif item.type == "user_cleanup": - logger.info("User %s disconnected - cleanup action") - logger.info("End transcript worker") + async def transcribe_output_queue(self): + logger.info("Transcript worker start") + config = aai.TranscriptionConfig(speaker_labels=True, language_code="pl") + transcriber = aai.Transcriber() + while True: + item = self.queue.get() + if item.type == "STOP": + logger.info("Queue ended") + break + elif item.type == "send_file": + transcript = "" + for iter in item.data: + transcript = transcriber.transcribe(iter, config=config) + for utterance in transcript.utterances: + logger.info("%s : %s", item.user, utterance.text) + elif item.type == "user_cleanup": + logger.info("User %s disconnected - cleanup action") + logger.info("End transcript worker") async def setup(bot): logger = logging.getLogger("discord") - logger.info("Loading voice transcribed module phase one") - worker = threading.Thread(target=transcribe_output_queue) - logger.info("Loading voice transcribed phasetwo") - queue = Queue() - logger.info("Loading voice transcribed module phase three") - await bot.add_cog(Transcriber(bot, worker, queue)) + await bot.add_cog(Transcriber(bot)) logger.info("Loading voice transcribed module done") # 1. zapisuj kwestie człowieka do pliku w którym będzie można stwierdzić kto co powiedział (callback z basicaudio + write z wavesinka). Zamykaj plik i wysyłaj do transkrypcji w momencie ciszy dłuższej niż 0.5s. Jeśli człowiek nadaje cały czas dawaj sygnał że nie można go transkrybować - i wyłaczaj zapis.