configs/transcribe.sh

41 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env python
import openai
import sys
import os
import datetime
openai.api_key = os.getenv("OPENAI_API_KEY")
def send_message(message):
response = openai.Completion.create(model="text-davinci-003", prompt=message, max_tokens=200)
return response.choices[0].text.strip()
def read_input():
if not sys.stdin.isatty():
return sys.stdin.read().strip()
elif len(sys.argv) > 1:
return sys.argv[1]
else:
return ""
input_data = read_input()
if input_data:
if input_data.endswith('.mp3'):
with open(input_data, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
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)