个人 AI 助手程序不是 运行

Personal AI assistant program not running

我从 Internet 上的一篇文章中获得了这段代码并已完成故障排除,现在它以退出代码 0 运行。它不执行任何操作,例如打招呼或请求输入或什么都不做。它只是运行并给出退出代码。

我在安装ecapture时遇到了问题,似乎很多Python 3.9的人都遇到过这个问题所以我把它注释掉了。

这是我获得代码的来源。

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


# 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")"""

你的缩进有误。您已将 if __name__ == "__main__" 放入 wishMe 函数中。 Put 在外面,它应该可以正常工作。