Skip to content

Instantly share code, notes, and snippets.

@jsonsmth
Forked from harrisoncramer/Camelot
Created April 9, 2023 11:40
Show Gist options
  • Select an option

  • Save jsonsmth/51315f68ed3714b464bfcab2852e39b0 to your computer and use it in GitHub Desktop.

Select an option

Save jsonsmth/51315f68ed3714b464bfcab2852e39b0 to your computer and use it in GitHub Desktop.
GPT Voice Recognition Script
# vi: ft=bash
#!/bin/bash
TOKEN="your_token"
MODEL="text-davinci-003"
MAX_TOKENS=500
TEMPERATURE="0.5"
STOP_WORD="Camelot"
# Use the "hear" CLI tool (w/ Mac's built-in voice recognition)
hear -d -m -x $STOP_WORD > /tmp/voice.txt
# Convert the output to a tempfile, format it to Unix, grab the last line
mac2unix -q -f /tmp/voice.txt
FULL_QUERY=$(cat /tmp/voice.txt | tail -n1)
QUERY=$(echo "${FULL_QUERY%$STOP_WORD}?")
echo "${QUERY}"
# Convert to JSON for POST to ChatGPT's API
JSON_STRING=$(jq -n \
--arg model "$MODEL" \
--arg prompt "$QUERY" \
--argjson max_tokens "$MAX_TOKENS" \
--argjson temperature "$TEMPERATURE" \
'$ARGS.named')
# Send JSON to a temp file
echo $JSON_STRING > /tmp/query.json
# Make the Curl request
curl -s -X POST 'https://api.openai.com/v1/completions' -d @/tmp/query.json --header "Authorization: Bearer ${TOKEN}" --header 'Content-Type: application/json' > /tmp/response.json
# Convert to a response with JQ and send the output
cat /tmp/response.json | jq -r '.choices[0].text' | pbcopy
pbpaste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment