How would one make it so this Personal Assistant is always in the background and also uses a wake work?

Viewed 48

basically I'm experimenting with coding a personal assistant but am unsure as to how to make her run in the background and be woken by a wake word; currently I only can manually run her in Pycharm or other IDEs. Id like to know how to have it always running like cortana as opposed to just only running when i open an IDE and click 'run'

import speech_recognition as sr
from  time import ctime
import webbrowser
import time
import playsound
import os
import gtts
import random
from gtts import gTTS
import subprocess


r = sr.Recognizer()
#records your audio like a little creep ;)
def record_audio(ask = False):
    with sr.Microphone() as source:
        if ask:
            vivian_speak(ask)

        audio = r.listen(source)
        voice_data = ""
        try:
            voice_data = r.recognize_google(audio)

        except sr.UnknownValueError:
            vivian_speak("Sorry, I couldn't hear you")
        except sr.RequestError:
            vivian_speak("Sorry, my speech API is currently offline. I apologize.")
        return voice_data
#this is just nerd shit to make the audio go from text to speech
def vivian_speak(audio_string):
    tts = gTTS(text=audio_string, lang='en')
    r = random.randint(1, 10000000)
    audio_file = 'audio-' + str(r) + '.mp3'
    tts.save(audio_file)
    playsound.playsound(audio_file)
    print(audio_string)
    os.remove(audio_file)
#this is how my beautiful robot baby speaks
def respond(voice_data):
    if 'road work ahead' in voice_data:
        vivian_speak('I sure hope it does')
    if 'League' in voice_data:
        vivian_speak('Opening League of Legends')
        subprocess.Popen(['C:\Riot Games\League of Legends\\LeagueClient.exe'])
    if 'what is your name' in voice_data:
        vivian_speak('My name is, Vivian.')
    if 'what time is it' in voice_data or 'what is the time' in voice_data:
        vivian_speak(ctime())
    if "search" in voice_data:
        search = record_audio("What should I search for?")
        url = 'https://google.com/search?q=' + search
        webbrowser.get().open(url)
        vivian_speak("Here are the results for " + search)
    if "find" in voice_data:
        location = record_audio("What would you like me to find?")
        url = 'https://google.nl/maps/place/' + location + "/&"
        webbrowser.get().open(url)
        vivian_speak("Here is the location of " + location)
    if 'how are you' in voice_data:
        vivian_speak('I am doing well today! How about yourself?')
        voice_data = record_audio()
        if 'good' in voice_data or 'great' in voice_data or 'okay' in voice_data or 'alright' in voice_data:
            vivian_speak("I'm glad! What can I do for you?")

        if 'bad' in voice_data or 'horrible' in voice_data or 'eh' in voice_data or 'terrible' in voice_data:
            vivian_speak("I'm sorry to hear that. What can i do to change that?")

    if 'close' in voice_data or 'exit' in voice_data or 'stop' in voice_data or 'bye' in voice_data or 'goodbye' in voice_data or 'thank you' in voice_data:
        vivian_speak("happy to help")
        exit()



#this lets me run multiple commands in a row
time.sleep(1)
vivian_speak("Hello there. What can i do for you?")
while 1:
    voice_data = record_audio()
    print(voice_data)
    respond(voice_data)
1 Answers

Instead of clicking run in your IDE, you may want to run Python in the command line.

For example, you'd run:

python NameOfFile.py

This will keep running until the program exits, or you close the command window.

Related