Skip to content

Instantly share code, notes, and snippets.

@belkarx
Created April 1, 2024 02:53
Show Gist options
  • Save belkarx/7d4bb43af651b651a2c7b45d7550255b to your computer and use it in GitHub Desktop.
Save belkarx/7d4bb43af651b651a2c7b45d7550255b to your computer and use it in GitHub Desktop.
simplest possible assistant ever (getting this right is incredibly hard)
from openai import OpenAI
import pyttsx3
import sounddevice as sd
import numpy as np
import io
from scipy.io.wavfile import write
from threading import Thread
import os
import sys
import queue
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
def record_response(filename='response.wav'):
q = queue.Queue()
def callback(indata, frames, time, status):
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
print("Recording... Press Enter to stop.")
try:
# Default values for sample rate and channels
sample_rate = 44100
channels = 1
with sd.InputStream(samplerate=sample_rate, channels=channels, callback=callback):
input() # Wait for Enter key press
audio_data = []
while not q.empty():
audio_data.append(q.get())
audio_data = np.concatenate(audio_data, axis=0)
# Write to file
write(filename, sample_rate, audio_data)
except Exception as e:
print("An error occurred:", str(e))
return filename
def transcribe_audio(filename):
with open(filename, 'rb') as audio_file:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
print(transcript)
return transcript['data']['text']
def generate_response(text):
response = client.chat.completions.create(model="gpt-4",
messages=[{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": text}])
return response.choices[0].message['content']
def text_to_speech(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
if __name__ == "__main__":
print("Assistant: How are you?")
record_response()
user_input = transcribe_audio('response.wav')
assistant_response = generate_response(user_input)
print(f"Assistant: {assistant_response}")
text_to_speech(assistant_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment