Skip to content

Instantly share code, notes, and snippets.

@Daniel-V-Richardson
Created January 24, 2023 14:44
Show Gist options
  • Save Daniel-V-Richardson/a009bac48efd55db4887a05eaffa836b to your computer and use it in GitHub Desktop.
Save Daniel-V-Richardson/a009bac48efd55db4887a05eaffa836b 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()
@Gottano
Copy link

Gottano commented Apr 6, 2023

How could this code be changed to use GPT 3.5 turbo?

@sashimirolls
Copy link

How could this code be changed to use GPT 3.5 turbo?
Change this part.
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
)

@DanishProjectsHub
Copy link

Traceback (most recent call last):
File "C:\Users\Danish\PycharmProjects\pythonProject\main.py", line 98, in
response = openai.Completion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Danish\PycharmProjects\pythonProject\venv\Lib\site-packages\openai\api_resources\completion.py", line 25, in create
return super().create(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Danish\PycharmProjects\pythonProject\venv\Lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
^^^^^^^^^^^^^^^^^^
File "C:\Users\Danish\PycharmProjects\pythonProject\venv\Lib\site-packages\openai\api_requestor.py", line 298, in request
resp, got_stream = self._interpret_response(result, stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Danish\PycharmProjects\pythonProject\venv\Lib\site-packages\openai\api_requestor.py", line 700, in _interpret_response
self._interpret_response_line(
File "C:\Users\Danish\PycharmProjects\pythonProject\venv\Lib\site-packages\openai\api_requestor.py", line 765, in _interpret_response_line
raise self.handle_error_response(
openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.

i am getting this error
do we need to buy the openai api or its free?

@andreFarhan
Copy link

Hello, how do I change the accent?

@mdr3444
Copy link

mdr3444 commented Nov 15, 2023

kages\httpx_transports\default.py", line 97, in
httpcore.UnsupportedProtocol: UnsupportedProtocol,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'httpcore' has no attribute 'UnsupportedProtocol'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment