Skip to content

Instantly share code, notes, and snippets.

@joskid
Forked from Daniel-V-Richardson/assistant.py
Created March 18, 2023 21:32
Show Gist options
  • Save joskid/8647e73aa9e71995afb73457b80908b0 to your computer and use it in GitHub Desktop.
Save joskid/8647e73aa9e71995afb73457b80908b0 to your computer and use it in GitHub Desktop.
Simple AI Voice Assistant using OpenAI API
import speech_recognition as sr
import pyttsx3
import openai
openai.api_key = "Your API Key"
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[1].id)
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
conversation = ""
user_name = "Dan"
bot_name = "John"
while True:
with mic as source:
print("\n Listening...")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening")
try:
user_input = r.recognize_google(audio)
except:
continue
prompt = user_name+":"+user_input + "\n"+bot_name+":"
conversation += prompt
response = openai.Completion.create(
model="text-davinci-003",
prompt=conversation,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str =response_str.split(
user_name + ":" ,1)[0].split(bot_name+ ":",1)[0]
conversation+= response_str +"\n"
print(response_str)
engine.say(response_str)
engine.runAndWait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment