【问题标题】:GUI and AI Logical FlowGUI 和 AI 逻辑流程
【发布时间】:2018-09-05 06:46:00
【问题描述】:

我使用我在 python 上的知识和一些研究,作为一个自学项目,创建了一个基本的语音助手。

Link to the code is here

我基本上是把音频转成文字,然后拆分出来找关键词然后触发响应或者动作可以这么说,不是很智能但是暂时还可以。

除了一千行 ifs 和 elifs 之外,我还能如何寻找关键字,是否有更好的方法、有效的方法?

我遇到的另一个问题是,我为该程序构建了一个 GUI 界面,因此我可以通过单击按钮与其进行交互,但问题是,单击按钮后窗口没有响应,事实证明它是已知的问题,我不知道如何解决它,因为我不知道线程、进程和队列的概念。我希望有人可以帮助我解决我的问题。

我想指出,如果我必须为这个项目做任何学习,我会很感兴趣,因为整个项目背后的想法是学习如何编码或构建人工智能,这听起来可能很愚蠢

PS:我实现了,嗯,有点做,总是监听功能或通过将函数保持在 while 循环中来保持运行功能。我想找到一种语音触发方式以及唤醒助手。非常感谢这方面的任何帮助。

还有,帮我给这个助理起个名字,最好是女性。

代码在这里:

import os
import time
import random
import webbrowser
import tkinter as tk
from gtts import gTTS
from mutagen.mp3 import MP3
from PIL import ImageTk, Image
from playsound import playsound
import speech_recognition as sr
from weather import Weather, Unit

def startAssistant():
    keepRunning = 1
    while keepRunning is 1:
        mainFunction()
        if mainFunction() is 0: break

def doNothing(): print("I don't do anything apart from printing this line of course!")

def mainFunction():

    f = open("assistant.txt", "a")

    # Printing what a user is saying for better user experience
    def say(text):
        print(text)
        f.write("\n" + text + "\n")
        return text

    # This function will take inputs to talk back
    def talkBack(text, recordingName):
        # Variable Declaration
        extension = ".mp3"

        # Synthesising the reponse as speech
        tts = gTTS(text=say(text), lang="en-us")

        # Saving the response files
        fileName = recordingName + extension
        audioPath = "audioFiles\\"
        responseFile = audioPath + fileName

        # Checking to see if the file is already created
        if not os.path.exists(responseFile):
            tts.save(responseFile)
        # Playing the audio
        playsound(responseFile)

    # Initialising things here
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()

    # Asking for input and saving that
    with microphone as source:
        print ("Speak:")
        audio = recognizer.listen(source)

    # Converting audio into text
    convertedAudio = recognizer.recognize_google(audio)
    convertedAudioSplit = convertedAudio.split()

    # Printing what was picked up when the user Spoke and also logging it
    print("\n" + convertedAudio + "\n")
    f.write("\n" + convertedAudio + "\n")

    # Start of a conversation
    if "hello" in convertedAudioSplit:
        talkBack("Hi, how are you doing today?", "hello")

    # Wishing people based on the time of the day  
    elif "morning" in convertedAudioSplit:
        talkBack("Good morning! The sun's shining bright, let's head out for a run. We'll get back and make a healthy breakfast for ourselves", "morning")
    elif "afternoon" in convertedAudioSplit:
        talkBack("Good afternoon! You must be hungry right about now, why don't you break for lunch?", "afternoon")
    elif "night" in convertedAudioSplit:
        talkBack("Nighty night sleepy pot! Get a good night's sleep while I learn more to be more helpful to you tomorrow.", "night")

    # Getting her information
    elif "doing" in convertedAudioSplit:
        talkBack("I am doing very good, Thank you for asking!", "doing")

    # Making the assistant open web browser with a URL
    elif "Google" in convertedAudioSplit:
        talkBack("Okay, lets get you to Google.", "google")
        # Opening the browser with the required URL
        webbrowser.open("https://www.google.com/", new = 1)

    # Brings the weather report
    elif "weather" in convertedAudioSplit:
        weatherVariable = Weather(unit=Unit.CELSIUS)
        location = weatherVariable.lookup_by_location('bangalore')
        condition = location.condition.text
        talkBack("It is {0} right now in Bengaluru.".format(condition), "weather")

    # Exiting the program on user's consent
    elif "exit" in convertedAudioSplit:
        talkBack("Sure, if that's what you want! I will miss you, have a good day.", "exit")
        return 0

    # If there is an UnknownValueError, this will kick in
    elif sr.UnknownValueError:
        talkBack("I am sorry, I couldn't quite get what you said. Could you please say that again?", "UnknownValueError")

    # When things go out of the box
    else:
        # Out of scope reply
        talkBack("I am a demo version. When you meet the completed me, you will be surprised.", "somethingElse")
        return 0

root = tk.Tk()

root.title("Voice Assistant")
mainFrame = tk.Frame(root, width = 1024, height = 720, bg = "turquoise", borderwidth = 5)

menu = tk.Menu(root)
root.config(menu=menu)
subMenu = tk.Menu(menu)

startButton = tk.Button(mainFrame, text="Interact", command = startAssistant)
startButton.place(relx = 0.5, rely = 1.0, anchor = tk.S)

menu.add_cascade(label="File", menu=subMenu)

subMenu.add_command(label="Do Nothing", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=root.quit)

mainFrame.pack()

root.mainloop()

【问题讨论】:

  • 我还不想把这个作为答案发布......你会考虑为 tkinter 使用包装器包吗? PySimpleGUI 是一个使使用 tkinter 更容易和更简单的包。 Demo_Chatterbot.py 包中发布了一个机器学习机器人示例,可能会引起一些兴趣。如果这看起来是一个合理的答案,我很乐意写一些包含更多信息的东西。
  • @MikeyB,我有兴趣研究这里的选项。感谢您分享您的观点。我一定会调查的

标签: python python-3.x tkinter speech-recognition text-to-speech


【解决方案1】:

一个潜在的解决方案是使用更简单的 GUI 包。也许 GUI 包 PySimpleGUI 会很合适。它可以解决您的 GUI 问题,让您腾出时间处理项目的其他部分。

查看实现聊天前端的Chat Demo。还有一个 Chatterbot Demo 实现了 Chatterbot 项目的前端。

您可以先复制该代码并进行修改。

【讨论】:

  • 感谢您的回复,我会检查一下
  • 感谢您的帮助,我已经有一段时间没有时间看这个项目了。我今天有时间消磨时间,我阅读了 PySimpleGUI 文档。我发现一个注释要求用户不要在线程中实现 PySimpleGUI,因为它基于 tkinter,并且由于 tkinter 不支持线程,所以这不起作用。我所做的工作包括需要很长时间才能将焦点返回到 GUI 窗口的任务,我需要一个在 AI 说话时不会停止响应的 GUI。你能帮忙吗?
  • 您可以使用 GUI 运行线程。我不建议尝试将 GUI 放入线程中。只要您定期调用 GUI,它就不会停止工作。
  • 好的,如果我有可行的解决方案,我会试一试并更新答案
  • 我无法让它工作!如果我不实现线程,界面仍然会卡住等待焦点恢复到自身。如果我在一个线程上运行具有该功能的程序,而在另一个线程上运行具有同步的 gui,则两者都单独运行,导致麦克风多次打开,整个程序崩溃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多