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
import openai
import sys
import os
import datetime
#!/bin/bash
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):
response = openai.Completion.create(model="text-davinci-003", prompt=message, max_tokens=200)
return response.choices[0].text.strip()
# Check if the audio file exists
if [ ! -f "$1" ]
then
echo "Error: File '$1' does not exist"
exit 1
fi
def read_input():
if not sys.stdin.isatty():
return sys.stdin.read().strip()
elif len(sys.argv) > 1:
return sys.argv[1]
else:
return ""
# Get the current date and time
datetime=$(date +"%Y%m%d-%H%M%S")
input_data = read_input()
# Determine the file extension
extension="${1##*.}"
if input_data:
if input_data.endswith('.mp3'):
with open(input_data, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
# Convert the audio file to text using Whisper
response=$(curl -s \
-H "Authorization: Bearer ${OPENAI_API_KEY}" \
-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)