首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >个人AI辅助程序不运行

个人AI辅助程序不运行
EN

Stack Overflow用户
提问于 2021-03-02 06:40:51
回答 1查看 151关注 0票数 0

我从互联网上的一篇文章中获得了这段代码,并且已经完成了故障排除,现在它用退出代码0运行。它不会做任何事情,比如问候或要求输入或什么也不做。它只会运行并给出退出代码。

我在安装ecapture时遇到了问题,似乎很多使用Python3.9的人都有这个问题,所以我已经把它注释掉了。

这是我得到代码的来源。

https://towardsdatascience.com/how-to-build-your-own-ai-personal-assistant-using-python-f57247b4494b

代码语言:javascript
运行
复制
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import time
import subprocess
"""from ecapture import ecapture as ec"""
import wolframalpha
import json
import requests

engine=pyttsx3.init('sapi5')
voice=engine.getProperty('voices')
engine.setProperty('voice', 'voices[0].id')

def speak(text):
    engine.say(text)
    engine.runAndWait()

def wishMe():
    hour=datetime.datetime.now().hour
    if hour>=0 and hour<12:
        speak("Hello,Good Morning")
        print("Hello,Good Morning")
    elif hour >= 12 and hour <18:
        speak("Hello, Good Afternoon")
        print("Hello, Good Afternoon")
    else:
        speak ("Hello, Good Evening")
        print ("Hello, Good Evening")
    def takeCommand():
        r=sr.Recognizer()
        with sr.Microphone() as source:
            print ("Listening...")
            audio=r.listen(source)

            try:
                statement=r.recognize_google(audio,language='en-uk')
                print(f"user said:{statement}\n")

            except Exception as e:
                speak ("Run that by me again")
                return "None"
            return statement

    print ("Loading your AI personal assistant G-One")
    speak ("Loading your AI personal assistant G-One")
    wishMe()


    if __name__ == '__main__':

        while True:
            speak("How may I be of assistance?")
            statement = takeCommand().lower()
            if statement==0:
                continue

    if "good bye" in statement or "ok bye" in statement or "that'll be all" in statement:
        speak ('your personal assistant G-One is shutting down, Good bye')
        print ('your personal assistant G-One is shutting down, Good bye')

    if 'wikipedia' in statement:
        speak('Searching Wikipedia...')
        statement =statement.replace("wikipedia", "")
        results =wikipedia.summary(statement, sentences=3)
        speak ("According to Wikipedia")
        print (results)
        speak (results)


    elif 'open youtube' in statement:
        webbrowser.open_new_tab("https://www.youtube.com")
        speak ("youtube is now open")
        time.sleep(5)

    elif 'open google' in statement:
        webbrowser.open_new_tab("https://www.google.com")
        speak ("Google chrome is now open")
        time.sleep(5)

    elif 'open gmail' in statement:
        webbrowser.open_new_tab("gmail.com")
        speak ("Gmail is now open")
        time.sleep(5)

    elif time in statement:
        strTime=datetime.datetime.now().strftime("%H:%M:%S")
        speak(f"the time is {strTime}")

    elif 'news' in statement:
        news = webbrowser.open_new_tab("https://news.google.com/topics/CAAqJggKIiBDQkFTRWdvSUwyMHZNRFZxYUdjU0FtVnVHZ0pKVGlnQVAB?hl=en-IN&gl=IN&ceid=IN%3Aen")
        speak('Here are some headlines from Google news')
        time.sleep(6)


    elif 'search' in statement:
        statement = statement.replace("search","")
        webbrowser.open_new_tab(statement)
        time.sleep(5)

    elif 'ask' in statement:
        speak('I can answer to computational and geographical questions, which of these would you like to ask now')
        question=takeCommand()
        app_id="LURX84-VPJ6LEHWXV" """Wolfram App ID"""
        client = wolframalpha.Client('R2K75H-7ELALHR35X')
        res = client.query(question)
        answer = next(res.results).text
        speak (answer)
        print (answer)

    elif 'who are you' in statement or 'what are your capabilities' in statement:
        speak ('I am Arthur, and am still in my infancy. I am capable of accomplishing minor tasks at this point in time such as'
               'opening google products such as youtube, gmail, and chrome.'
               'I have other functions such as relating the time, taking pictures,searching wikipedia, relating the weather'
               'as well as reading headlines from google news')

    elif 'who made you' in statement or 'who built you' in statement or 'who discovered you' in statement:
        speak("I was built by Ahmed Hamadto")
        print ("I was built by Ahmed Hamadto")

    """include the weather part of the AI here"""

    if 'log off' in statement or 'lights out' in statement:
        speak ("You've got 10 seconds to clear things up")
        subprocess.call(["shutdown","/1"])

    time.sleep(3)


"""    elif 'camera'in statement or 'take a photo' in statement or 'snap this' in statement:
        ec.capture(0,"robo camera","img.jpg")"""
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-02 06:44:40

你的压痕是错的。您已经将if __name__ == "__main__"放入wishMe函数中。Put在外面,应该可以正常工作。

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

https://stackoverflow.com/questions/66434490

复制
相关文章

相似问题

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