Skip to content

Instantly share code, notes, and snippets.

@harrisoncramer
Created April 8, 2023 17:27
Show Gist options
  • Save harrisoncramer/394c6ff1cae3ad4aa7d7d3250b43f5c8 to your computer and use it in GitHub Desktop.
Save harrisoncramer/394c6ff1cae3ad4aa7d7d3250b43f5c8 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