0

I have the following piece of code:

import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY

openai.api_key = API_KEY

engine = pyttsx3.init()

r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
print(sr.Microphone.list_microphone_names())


conversation = ""
user_name = "Josode"

while True:
    with mic as source:
        print("\nlistening... speak clearly into mic.")
        r.adjust_for_ambient_noise(source, duration=0.2)
        audio = r.listen(source)
    print("no longer listening.\n")

    try:
        user_input = r.recognize_google(audio)
    except:
        continue

    prompt = user_name + ": " + user_input + "\n Ava:"

    conversation += prompt

    response = openai.Completion.create(engine="text-davinci-002", prompt=conversation, max_tokens=100)
    response_str = response["choices"][0]["text"].replace("\n", "")
    response_str = response_str.split(user_name + ": ", 1)[0].split("Ava: ", 1)[0]

    conversation += response_str + "\n"
    print(response_str)

    engine.say(response_str)
    engine.runAndWait()

When I run the file, I get just listening... speak clearly into mic. no longer listening. with no output from davinci.

Also, the mic print is ['LG FULL HD', 'Ari Chan’s AirPods', 'Ari Chan’s AirPods', 'MacBook Pro Microphone', 'MacBook Pro Speakers']. I am using index 1

API key is correct and imported. I have an account at Open AI and can use playground without a problem.

Do you see something that I don't see? It should work

DZR
  • 195
  • 1
  • 2
  • 12

1 Answers1

1

Most probably you are getting an exception in r.recognize_google(audio) so it forces continue again and again without any output, try to add something like this to debug it:

    import traceback

    ... 

    try:
        user_input = r.recognize_google(audio)
    except:
        print(traceback.format_exc())
        continue
svfat
  • 3,273
  • 1
  • 15
  • 34