In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
User
How can i create a discord bot that listens to a voice channel, recognizes commands through a speech model combined with a text classifier and invokes the given command?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To create a simple discord bot responding to voice commands, we can use the PyCord API to read audio from a voice channel we connect to, transcribe it via speech recognition and classify which command was meant to be executed by the user.
For speech recognition, we will use OpenAI's Whisper model, which is a state-of-the-art speech recognition model and is capable of exceeding human performance on many occasions.
We will then feed the transcription into flan-t5, one of the most competitive open source language models and prompt it to classify which command - if any - was meant by the user’s speech.
If we detect a command, we can react by writing code which handles this specific case.
Firstly though, we need to create an application on the Discord Developer Portal.
After navigating to https://discord.com/developers/applications, you can click on "New Application" and enter your desired name for your bot.
In the navigation bar on the left, we can then select the newly created application.
We can then navigate to the OAuth2 URL Generator and select the permisions we need: We will definetely need "Send Messages", "Connect", "Use Voice Activity", but you can always resort to "Administrator" if you want to play it safe and prevent any unexpected errors caused by the permision system.
We then use the generated link to make the bot join a given discord server.
You also want to copy the API key, as you will need to paste it into the script later on.
We will need to install the following python packages
code
pip install pycord pynacl numpy transformers
We will also need to install PyTorch, which can be different depending on your operating system and whether you have an Nvidia graphics card.
Navigate to https://pytorch.org/get-started/locally/ for a wizard where you can select your between the stable and nightly branch, your operating system, python package manager and language.
However, in the general case this command should work fine:
code
pip3/pip install torch torchvision torchaudio
Also make sure that the opus library is installed on your system and that the python script can find it through the PATH environment variable.
On macOS or Linux, you can install opus via your package manager of choice.
On Ubuntu you could install the opus library via apt with the following command:
On macOS, you would run:
brew install opus opusfile libogg
And on Windows, well, good luck...
The safest bet would be to grab opus.dll from https://opus-codec.org/downloads/ and paste it into C:\Windows\System32, or if you want to be a bit more tidy,
create a new folder in C:\Program Files\ and add that folder to the PATH environment variable.
If opus is not correctly installed in your environment, you will get the following error, so be cautious!
code
Traceback (most recent call last):
File "/opt/python3.9/site-packages/discord/opus.py", line 549, in run
data.decoded_data = self.get_decoder(data.ssrc).decode(
File "/opt/python3.9/site-packages/discord/opus.py", line 570, in get_decoder
self.decoder[ssrc] = Decoder()
File "/opt/python3.9/site-packages/discord/opus.py", line 437, in __init__
_OpusStruct.get_opus_version()
File "/opt/python3.9/site-packages/discord/opus.py", line 362, in get_opus_version
raise OpusNotLoaded()
discord.opus.OpusNotLoaded
With your environment setup, you should be able to run the following script:
python
This should implement the groundwork for the functionality you are asking for. You can add new commands simply by adding new options to the prompt for flan-t5 and a new if branch, with the code for the command you want to implement. If you want your bot to join your voice channel, simply execute the command /join and it will start listening for commands.
import time
from threading import Thread, Lock
import discord
import torch as torch
from discord.sinks import Sink
import numpy as np
from torchaudio.transforms import Resample
from transformers import pipeline
API_KEY = "<YOUR API KEY HERE>"
bot = discord.Bot()
device = "cuda:0" if torch.cuda.is_available() else "cpu"
CHUNK_LENGTH_SECONDS = 5 # Note that a command cannot take longer than 5 seconds to say with this chunk length
# Whisper
whisper = pipeline(
"automatic-speech-recognition",
model="openai/whisper-base",
chunk_length_s=CHUNK_LENGTH_SECONDS,
device=device,
)
# Flan-T5 small
flan = pipeline(
'text2text-generation',
model='google/flan-t5-small',
device=device,
)
def handle_command(command: str, vc):
# Do stuff with the voice command as text here
prompt = """
Classify which command was said.
Options:
1: Leave the voice channel
2: Play some music
3: No command was invoked
"""
response = flan("User Input: " + command + prompt + "\nClassification result:")[0]['generated_text']
if "1" in response:
print("Leaving voice channel")
bot.loop.create_task(vc.disconnect())
elif "2" in response:
print("Playing music")
vc.play(discord.FFmpegPCMAudio("music.mp3")) # place a song in the same directory as the script
elif "3" in response:
print("No command was invoked")
else:
print("Invalid response")
class SpeechRecognizingSink(Sink):
def consume_sliding_window(self):
while True:
time.sleep(0.5)
if self.vc is None or self.vc.decoder is None:
continue
sampling_rate = self.vc.decoder.SAMPLING_RATE
sample_size = self.vc.decoder.SAMPLE_SIZE // self.vc.decoder.CHANNELS
# if the sliding window is long enough, or we have not received audio for some time, process it
if len(self.sliding_window) >= sampling_rate * CHUNK_LENGTH_SECONDS \
or time.time() - self.last_received > CHUNK_LENGTH_SECONDS:
self.sliding_window_lock.acquire()
# get the first chunk_length_seconds of the sliding window
chunk = self.sliding_window[:sampling_rate * CHUNK_LENGTH_SECONDS]
# remove the first chunk_length_seconds from the sliding window
self.sliding_window = self.sliding_window[sampling_rate * CHUNK_LENGTH_SECONDS:]
self.sliding_window_lock.release()
# if empty or all zeros, skip
if len(chunk) == 0 or np.all(chunk == 0):
continue
# convert to float32
chunk = chunk.astype(np.float32)
# normalize from -int_min to int_max to -1 and 1
chunk /= 2 ** (8 * sample_size - 1)
# resample to 16kHz using torchaudio
chunk = torch.from_numpy(chunk).to(device)
chunk = Resample(sampling_rate, 16000)(chunk)
chunk = chunk.cpu().numpy()
# process the chunk
result = whisper(chunk)
text = result["text"]
handle_command(text, self.vc)
def __init__(self):
super().__init__()
self.sliding_window = np.array([], dtype=np.int16)
self.sliding_window_lock = Lock()
self.consumer_thread = Thread(target=self.consume_sliding_window)
self.consumer_thread.start()
self.last_received = time.time()
def write(self, data, user):
n_channels = self.vc.decoder.CHANNELS
sample_size = self.vc.decoder.SAMPLE_SIZE // n_channels
if sample_size == 1:
dtype = np.uint8
elif sample_size == 2:
dtype = np.int16
elif sample_size == 4:
dtype = np.int32
else:
raise ValueError("Invalid sample size")
new_samples = np.frombuffer(data, dtype=dtype)
# make mono
new_samples = new_samples[::n_channels]
self.sliding_window_lock.acquire()
# add new samples to the sliding window
self.sliding_window = np.concatenate((self.sliding_window, new_samples))
self.sliding_window_lock.release()
self.last_received = time.time()
def cleanup(self):
pass
def on_format(self, encoding):
pass
@bot.command()
async def join(ctx): # If you're using commands.Bot, this will also work.
voice = ctx.author.voice
if not voice:
await ctx.respond("You aren't in a voice channel!")
return
vc = await voice.channel.connect() # Connect to the voice channel the author is in.
vc.start_recording(
SpeechRecognizingSink(), # The sink that will process the audio.
None,
ctx.channel
)
await ctx.respond("Joined voice channel!")
bot.run(API_KEY)