简体   繁体   English

我的语音助手程序没有记录我所说的任何内容

[英]My voice assistant program doesn't register anything I say

I downloaded a python code from GitHub.我从 GitHub 下载了一个 python 代码。

It doesn't register anything I'm saying.它没有记录我所说的任何内容。

My 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=0)
conversation = ""
user_name = "You"
bot_name = "DAI"

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

    user_input = r.recognize_google(audio)
    prompt = user_name + ": " + user_input + "\n" + bot_name+ ": "
    conversation += prompt  # allows for context

    # fetch response from open AI api
    response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=100)
    response_str = response["choices"][0]["text"].replace("\n", "")
    response_str_split = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]

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

    engine.say(response_str_split)
    engine.runAndWait()

I get this error:我收到此错误:

/usr/local/bin/python3.11 /Users/danieforsell22b/Desktop/GPT3VoiceBot/gpt3Bot.py 

listening...
Traceback (most recent call last):
  File "/Users/danieforsell22b/Desktop/GPT3VoiceBot/gpt3Bot.py", line 27, in <module>
    user_input = r.recognize_google(audio)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/__init__.py", line 879, in recognize_google
    flac_data = audio_data.get_flac_data(
                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/__init__.py", line 495, in get_flac_data
    flac_converter = get_flac_converter()
                     ^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/__init__.py", line 1744, in get_flac_converter
    raise OSError("FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent")
OSError: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
no longer listening.


Process finished with exit code 1

Are you using Windows?您使用的是 Windows 吗? It seems either you do not have flac installed on your machine or the library code can not find it.看来您的机器上没有安装 flac,或者库代码找不到它。 If you are using Windows, try to install flac first and modify the __init__.py of the speech recognition library like this.如果你用的是Windows,试试先安装flac,把语音识别库的__init__.py修改成这样。

from

flac_converter = shutil_which("flac") 

to

flac_converter = shutil_which("flac.exe")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM