Skip to content

Instantly share code, notes, and snippets.

@ilmaisin
Created July 28, 2023 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilmaisin/262bc6372783d3857643ff67b60d84fe to your computer and use it in GitHub Desktop.
Save ilmaisin/262bc6372783d3857643ff67b60d84fe to your computer and use it in GitHub Desktop.
Speech to text bot for matterbridge using OpenAI's Whisper API
#!/bin/bash
whispertoken='REDACTED'
bridgetoken='REDACTED'
saidverb='sanoi'
curl -N http://localhost:4242/api/stream -H "Authorization: Bearer $bridgetoken" -s -m 1 > /dev/null # flush cache
curl -N http://localhost:4242/api/stream -H "Authorization: Bearer $bridgetoken" -s |
while IFS= read -r line;
do
filename=$(echo $line | jq -r .Extra.file[0].Name;)
if [[ "$filename" == *.oga ]];
then
speech=$(echo $line | jq -r .Extra.file[0].Data | base64 -d |
ffmpeg -hide_banner -loglevel error -vn -i - -acodec libmp3lame -f mp3 - |
curl --no-progress-meter --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
-H 'Content-Type: multipart/form-data' \
-H "Authorization: Bearer $whispertoken" \
-H "Transfer-Encoding: chunked" --form "file=@-;filename=audio.mp3" --form model=whisper-1 |
jq -r .text;)
nick=$(echo $line | jq -r .username;)
outmessage="$nick $saidverb: $speech"
gateway=$(echo $line | jq -r .gateway;)
curl --no-progress-meter -XPOST -H 'Content-Type: application/json' \
-H "Authorization: Bearer $bridgetoken" \
-d '{"text":"'"$outmessage"'","username":"s2tBOT","gateway":"'"$gateway"'"}' \
http://localhost:4242/api/message
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment