ImportError:无法导入名称“GenericAssistant”来自“神经意图”;

发布于 2025-01-20 10:50:41 字数 4779 浏览 2 评论 0原文

我正在尝试建立一个简单的语音助手。但是当我尝试运行时,我会遇到这样的错误。

> C:\Users\foobar>python
> C:\Users\foobar\Downloads\VoiceAssistance\VoiceAssistance\test.py
> Traceback (most recent call last): File
> "C:\Users\foobar\Downloads\VoiceAssistance\VoiceAssistance\test.py",
> line 1, in from neuralintents import GenericAssitant ImportError:
> cannot import name 'GenericAssitant' from 'neuralintents'
> (C:\Users\foobar\AppData\Local\Programs\Python\Python310\lib\site-packages\neuralintents_init_.py)

我也安装了这些东西;

pip install pyttsx3
pip install automatic-speech-recognition
pip install speech-recognition-python
pip install neuralintents

那就是我的源代码;

from neuralintents import GenericAssistant
import speech_recognition
import pyttsx3 as tts
import sys

recognizer = speech_recognition.Recognizer()

speaker = tts.init()
speaker.setProperty('rate',150)

todo_list = ['Go Shopping','Clean Room','Record Video']


def create_note():
    global recognizer

    speaker.say("What do you want to write onto your note boss?")
    speaker.runAndWait()

    done = False

    while not done:
        try:

            with speech_recognition.Microphone() as mic:

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                note = recognizer.recognize_google(audio)
                note = note.lower()

                speaker.say("Choose a filename boss!")
                speaker.runAndWait()

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                filename = recognizer.recognize_google(audio)
                filename = filename.lower()

            with open(f"{filename}.txt", 'w') as f:
                f.write(note)
                done = True
                speaker.say(f"I successfully created the note {filename} boss")
                speaker.runAndWait()

        except speaker.recognition.UnknownValueError:
            recognizer = speech_recognition.Recognizer()
            speaker.say("I did not understand you! Please try again! boss")
            speaker.runAndWait()


def add_todo():

    global recognizer

    speaker.say("What todo do you want to add boss?")
    speaker.runAndWait()

    done = False

    while not done:
        try:

            with speech_recognition.Microphone() as mic:

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                item=recognizer.recognize_google(audio)
                item=item.lower()

                todo_list.append(item)
                done=True

                speaker.say(f"I added {item} to the to do list boss")
                speaker.runAndWait()

        except speech_recognition.UnknownValueError:
            recognizer = speech_recognition.Recognizer()
            speaker.say("I did not understand. Please try again boss!")
            speaker.runAndWait()


def show_todos():

    speaker.say("the items on your to do list are the following boss")
    for item in todo_list:
        speaker.say(item)
    speaker.runAndWait()


def hello():
    speaker.say("Hello.What can I do for you Boss?")
    speaker.runAndWait()


def quit():
    speaker.say("Okay Boss")
    speaker.runAndWait()
    sys.exit(0)


mappings = {
    "greeting": hello,
    "create_note": create_note,
    "add_todo": add_todo,
    "show_todos": show_todos,
    "exit": quit

}

assistant = GenericAssistant('intents.json', intent_methods=mappings)
assistant.train_model()

while True:

    try:
        with speech_recognition.Microphone() as mic:

            recognizer.adjust_for_ambient_noise(mic,duration=0.2)
            audio = recognizer.listen(mic)

            message=recognizer.recognize_google(audio)
            message=message.lower()

        assistant.request(message)

    except speech_recognition.UnknownValueError:
        recognizer=speech_recognition.Recognizer()

和意图文件;

{"intents":
[
  {"tag": "greeting",
   "patterns": ["Hey","Hello","What's up?","How is it going?","Hi"],
   "responses": ["Hi Boss!"]},

  {"tag": "create_note",
   "patterns": ["Please create a new note","New note","Create a note","I want to create a note"],
   "responses": [""]},

  {"tag": "add_todo",
   "patterns": ["I want to add a todo","add a new todo","add a todo to my list","new item on my todo list"],
   "responses": [""]},
  {"tag": "show_todos",
    "patterns": ["Show my todos","What are my todos","What is on my todo list"],
    "responses": [""]},
  {"tag": "exit",
   "pattern": ["Bye","Stop","Quit","I want to quit","Goodbye","I want to exit","I have to go"]},
]}

I'm trying to build a simple voice assistant.But i got a error like that when i try to run ;

> C:\Users\foobar>python
> C:\Users\foobar\Downloads\VoiceAssistance\VoiceAssistance\test.py
> Traceback (most recent call last): File
> "C:\Users\foobar\Downloads\VoiceAssistance\VoiceAssistance\test.py",
> line 1, in from neuralintents import GenericAssitant ImportError:
> cannot import name 'GenericAssitant' from 'neuralintents'
> (C:\Users\foobar\AppData\Local\Programs\Python\Python310\lib\site-packages\neuralintents_init_.py)

Also i installed those things;

pip install pyttsx3
pip install automatic-speech-recognition
pip install speech-recognition-python
pip install neuralintents

And thats my source code;

from neuralintents import GenericAssistant
import speech_recognition
import pyttsx3 as tts
import sys

recognizer = speech_recognition.Recognizer()

speaker = tts.init()
speaker.setProperty('rate',150)

todo_list = ['Go Shopping','Clean Room','Record Video']


def create_note():
    global recognizer

    speaker.say("What do you want to write onto your note boss?")
    speaker.runAndWait()

    done = False

    while not done:
        try:

            with speech_recognition.Microphone() as mic:

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                note = recognizer.recognize_google(audio)
                note = note.lower()

                speaker.say("Choose a filename boss!")
                speaker.runAndWait()

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                filename = recognizer.recognize_google(audio)
                filename = filename.lower()

            with open(f"{filename}.txt", 'w') as f:
                f.write(note)
                done = True
                speaker.say(f"I successfully created the note {filename} boss")
                speaker.runAndWait()

        except speaker.recognition.UnknownValueError:
            recognizer = speech_recognition.Recognizer()
            speaker.say("I did not understand you! Please try again! boss")
            speaker.runAndWait()


def add_todo():

    global recognizer

    speaker.say("What todo do you want to add boss?")
    speaker.runAndWait()

    done = False

    while not done:
        try:

            with speech_recognition.Microphone() as mic:

                recognizer.adjust_for_ambient_noise(mic,duration=0.2)
                audio = recognizer.listen(mic)

                item=recognizer.recognize_google(audio)
                item=item.lower()

                todo_list.append(item)
                done=True

                speaker.say(f"I added {item} to the to do list boss")
                speaker.runAndWait()

        except speech_recognition.UnknownValueError:
            recognizer = speech_recognition.Recognizer()
            speaker.say("I did not understand. Please try again boss!")
            speaker.runAndWait()


def show_todos():

    speaker.say("the items on your to do list are the following boss")
    for item in todo_list:
        speaker.say(item)
    speaker.runAndWait()


def hello():
    speaker.say("Hello.What can I do for you Boss?")
    speaker.runAndWait()


def quit():
    speaker.say("Okay Boss")
    speaker.runAndWait()
    sys.exit(0)


mappings = {
    "greeting": hello,
    "create_note": create_note,
    "add_todo": add_todo,
    "show_todos": show_todos,
    "exit": quit

}

assistant = GenericAssistant('intents.json', intent_methods=mappings)
assistant.train_model()

while True:

    try:
        with speech_recognition.Microphone() as mic:

            recognizer.adjust_for_ambient_noise(mic,duration=0.2)
            audio = recognizer.listen(mic)

            message=recognizer.recognize_google(audio)
            message=message.lower()

        assistant.request(message)

    except speech_recognition.UnknownValueError:
        recognizer=speech_recognition.Recognizer()

And intents file;

{"intents":
[
  {"tag": "greeting",
   "patterns": ["Hey","Hello","What's up?","How is it going?","Hi"],
   "responses": ["Hi Boss!"]},

  {"tag": "create_note",
   "patterns": ["Please create a new note","New note","Create a note","I want to create a note"],
   "responses": [""]},

  {"tag": "add_todo",
   "patterns": ["I want to add a todo","add a new todo","add a todo to my list","new item on my todo list"],
   "responses": [""]},
  {"tag": "show_todos",
    "patterns": ["Show my todos","What are my todos","What is on my todo list"],
    "responses": [""]},
  {"tag": "exit",
   "pattern": ["Bye","Stop","Quit","I want to quit","Goodbye","I want to exit","I have to go"]},
]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2025-01-27 10:50:41

我认为你必须安装

pip install speechrecognition

i think you must install

pip install speechrecognition
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文