Created
July 28, 2023 19:48
-
-
Save ilmaisin/262bc6372783d3857643ff67b60d84fe to your computer and use it in GitHub Desktop.
Speech to text bot for matterbridge using OpenAI's Whisper API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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