mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-14 21:38:38 +00:00
83 lines
2.9 KiB
Python
83 lines
2.9 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):
|
|
self.bot = bot
|
|
self._last_member = None
|
|
|
|
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):
|
|
def callback(user, data: voice_recv.VoiceData):
|
|
print(f"Got packet from {user}")
|
|
|
|
## voice power level, how loud the user is speaking
|
|
# ext_data = packet.extension_data.get(voice_recv.ExtensionID.audio_power)
|
|
# value = int.from_bytes(ext_data, 'big')
|
|
# power = 127-(value & 127)
|
|
# print('#' * int(power * (79/128)))
|
|
## instead of 79 you can use shutil.get_terminal_size().columns-1
|
|
logger.info("Attempt transcribe")
|
|
vc = await ctx.author.voice.channel.connect(cls=voice_recv.VoiceRecvClient)
|
|
logger.info("Connected")
|
|
vc.listen(voice_recv.BasicSink(callback))
|
|
|
|
@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)) |