Files
conjurer/voice_recognition.py
T
2024-09-17 17:19:00 +02:00

83 lines
2.8 KiB
Python

# Start by making sure the `assemblyai` package is installed.
# If not, you can install it by running the following command:
# pip install -U assemblyai
#
# Note: Some macOS users may need to use `pip3` instead of `pip`.
import assemblyai as aai
import discord
from discord.ext import commands, voice_recv
import logging
# Replace with your API key
aai.settings.api_key = "aa9962f0088a449a9c4ab2361e96cc08"
discord.opus._load_default()
# URL of the file to transcribe
FILE_URL = "https://github.com/AssemblyAI-Community/audio-examples/raw/main/20230607_me_canadian_wildfires.mp3"
# You can also transcribe a local file by passing in a file path
# FILE_URL = './path/to/file.mp3'
logger = logging.getLogger("discord")
'''
class SRAudioSinkHammerVersion(AudioSink):
def __init__(
self,
*,
process_cb: Optional[SRProcessDataCB] = None,
text_cb: Optional[SRTextCB] = None,
default_recognizer: SRRecognizerMethod = 'google',
phrase_time_limit: int = 10,
ignore_silence_packets: bool = True,
):
super().__init__(None)
self.process_cb: Optional[SRProcessDataCB] = process_cb
self.text_cb: Optional[SRTextCB] = text_cb
self.phrase_time_limmit: int = phrase_time_limit
self.ignore_silence_packets: bool = ignore_silence_packets
self.default_recognizer: SRRecognizerMethod = default_recognizer
self._stream_data: defaultdict[int, _StreamData] = defaultdict(
lambda: _StreamData(stopper=None, recognizer=sr.Recognizer(), buffer=array.array('B'))
)
'''
class Transcriber(commands.Cog):
def __init__(self, bot):
def callback(user, data: voice_recv.VoiceData):
logger.info(f"Got packet from {user}")
self.bot = bot
self._last_member = None
bsink = voice_recv.BasicSink(callback)
wsink = voice_recv.WaveSink(destination="/home/pi/Conjurer/wav.wav")
fsink = voice_recv.FFmpegSink(filename = ".home/pi/Conjurer/mp3.mp3")
self.sink_list = [bsink, wsink, fsink]
async def transcribe():
config = aai.TranscriptionConfig(speaker_labels=True)
transcriber = aai.Transcriber()
transcript = transcriber.transcribe(
FILE_URL,
config=config
)
for utterance in transcript.utterances:
print(f"Speaker {utterance.speaker}: {utterance.text}")
@commands.hybrid_command(name="transcribe")
async def test(self, ctx):
logger.info("Attempt transcribe")
vc = await ctx.author.voice.channel.connect(cls=voice_recv.VoiceRecvClient)
logger.info("Connected")
msink = voice_recv.MultiAudioSink(destinations= self.sink_list)
vc.listen(msink)
@commands.command(name="stop_transcribe")
async def stop(self, ctx):
await ctx.voice_client.disconnect()
async def setup(bot):
await bot.add_cog(Transcriber(bot))