Files
conjurer/voice_recognition.py
T
2024-08-28 14:44:54 +02:00

78 lines
2.7 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
# 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'
class Transcriber(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._last_member = None
@commands.Cog.listener()
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
await channel.send(f'Witaj u Nas {member.mention}.')
@commands.hybrid_command(name="witaj")
async def witaj(self, ctx, *, member: discord.Member = None):
"""Says hello"""
member = member or ctx.author
if self._last_member is None or self._last_member.id != member.id:
await ctx.send(f'Mhm {member.name}~')
else:
await ctx.send(f'Powtarzas sie {member.name}')
self._last_member = member
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="connect_for_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
vc = await ctx.author.voice.channel.connect(cls=voice_recv.VoiceRecvClient)
vc.listen(voice_recv.BasicSink(callback))
@commands.command(name="stop_transcribe")
async def stop(self, ctx):
await ctx.voice_client.disconnect()
@commands.command(name="kill_transcribe_client")
async def die(self, ctx):
ctx.voice_client.stop()
await ctx.bot.close()
async def setup(bot):
await bot.add_cog(Transcriber(bot))