This commit is contained in:
Brad Ganley 2023-04-27 14:06:57 -05:00
parent 755a02ac7a
commit c5f8e3a21a

View File

@ -1,40 +1,37 @@
#!/usr/bin/env python #!/bin/bash
import openai
import sys
import os
import datetime
openai.api_key = os.getenv("OPENAI_API_KEY") # Check if an argument was passed
if [ $# -eq 0 ]
then
echo "Usage: $0 <audio file>"
exit 1
fi
def send_message(message): # Check if the audio file exists
response = openai.Completion.create(model="text-davinci-003", prompt=message, max_tokens=200) if [ ! -f "$1" ]
return response.choices[0].text.strip() then
echo "Error: File '$1' does not exist"
exit 1
fi
def read_input(): # Get the current date and time
if not sys.stdin.isatty(): datetime=$(date +"%Y%m%d-%H%M%S")
return sys.stdin.read().strip()
elif len(sys.argv) > 1:
return sys.argv[1]
else:
return ""
input_data = read_input() # Determine the file extension
extension="${1##*.}"
if input_data: # Convert the audio file to text using Whisper
if input_data.endswith('.mp3'): response=$(curl -s \
with open(input_data, "rb") as audio_file: -H "Authorization: Bearer ${OPENAI_API_KEY}" \
transcript = openai.Audio.transcribe("whisper-1", audio_file) -F "file=@$1" \
-F "model=whisper-1" \
-F "response_format=json" \
https://api.openai.com/v1/audio/transcriptions)
# Extract the text from the response
text=$(echo "$response" | jq -r '.text')
# Save the text to a file with a unique filename based on the date
echo "$text" > "${datetime}.${extension}.txt"
echo "Transcription saved to ${datetime}.${extension}.txt"
timestamp = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = f"{timestamp}.txt"
with open(filename, "w") as f:
f.write(transcript["text"])
print(f"Transcription saved to {filename}")
else:
user_input = f"Here's the output of a command: {input_data}. {sys.argv[1]}"
response = send_message(user_input)
print(response)
else:
user_input = input("You: ")
response = send_message(user_input)
print(response)