首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GUI和AI逻辑流程

GUI和AI逻辑流程
EN

Stack Overflow用户
提问于 2018-09-02 18:11:54
回答 1查看 209关注 0票数 0

作为一个自学项目,我用我对python所知道的做了一些研究,做了一个基本的语音助手。

Link to the code is here

我基本上是将音频转换为文本,然后将其拆分以查找关键字,然后触发一个响应或可以说是一个动作,这不是很智能,但它目前是有效的。

如果你愿意,有没有比上千行的if和elif更好的方法,更有效的方法呢?

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

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

PS:我实现了,嗯,某种程度上实现了,通过将函数保持在while循环中,始终侦听功能或保持运行功能。我想找到一种方法,语音语音触发器以及唤醒助理。在这方面的任何帮助都将不胜感激。

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

代码如下:

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2018-09-05 06:46:00

一种可能的解决方案是使用更简单的GUI包。也许图形用户界面包PySimpleGUI是一个合适的选择。它可以解决您的GUI问题,让您可以自由地处理项目的其他部分。

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

您可以从复制该代码并修改它开始。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52135974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档