Skip to content

Advanced Python- Making a digital assistant

Posted in VIDEOS

PYTHON Advanced – Making a digital assistant – A little bit of everything || HOX FRAMEWORK
Welcome,
Here i showed you how to make a simple digital assistant in python with only 3 modules (2 if you don’t include SpeechRecognition).
It is very simple and it requires only basic knowledge of python, windows operating system ,and knowledge of how to install modules.
You will have to make a free account at WolframAlpha.com, there you can find an option to get your API.
So now that you have created your account and got your API key,lets get started!
Firstly we have to check what voice pack does our Windows OS have installed:
1.
Now copy the whole name of the voice, it will look something like this:
“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-GB_HAZEL_11.0”
Once you added the key to the code, imported the modules and defined the
engine initializer you can set properties you want to, also don’t forget to set
your voice as a property like i did in engine.setProperty(‘voice’,eng_voice).
And then the API key needs to be passed to wolframalpha, like i did with:
client = wolframalpha.Client(k)
then define Main ,where all our stuff is going to be, and make sure you call it
after defining all stuff.
In main go with TRY and EXCEPT and make sure you handle some exceptions.
Inside TRY you define your stuff, whatever you want,all like this:
if query == “open browser”:
or
if query.startswith(“say “):
To use the engine to say stuff:
engine.say(“Something”)
engine.runAndWait()
-DO NOT FORGET engine.runAndWait() !!
-And now to make sure all other queries (non defined) are passed to wolframalpha
we will do:
else:
res = client.query(query) #pass the query
try:
key = next(res.results).text #try getting results, with next because it gets too big of an answer sometimes
print (key) #print it out
engine.say(key) #say it
engine.runAndWait()
except StopIteration:
engine.say(“Try repeating that. Or modify your query”) #exception for wrong inputs engine.runAndWait()
If you want to you can easily upgrade your digital assistant by adding more ELIF statements before ELSE. You can add as much as you want to.
Full code below. Thank you for visiting and watching ,have a nice day ! 🙂
1.
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty(‘voices’)
for voice in voices:
print(voice, voice.id)
engine.setProperty(‘voice’, voice.id)
engine.say(“Hello World!”)
engine.runAndWait()
engine.stop()
2.
import wolframalpha
import pyttsx3
engine = pyttsx3.init()
engine.setProperty(‘rate’,180)
eng_voice = “HKEY” #YOUR VOICE KEY
engine.setProperty(‘voice’,eng_voice)
with open(“api.txt”,”r”)as apikey:
for k in apikey:
client = wolframalpha.Client(k) #you can use just this line, k-your api key

api key example: E6RG6U-QZ6GFR1RR7 (no this one is not valid i made it up to show you what the look like)

def Main():
try:
engine.say(“What can i help you with?”)
engine.runAndWait()
query = str(input(“Ask me anything >”))
print(“Thinking”)
engine.say(“Thinking.”)
engine.runAndWait()
if query == “open browser”:
import os
engine.say(“Opening the browser.”)
engine.runAndWait()
os.system(“start vivaldi.exe”)

    elif query == "open steam":
        import os
        engine.say("Opening steam.")
        engine.runAndWait()
        os.system("start steam.exe") #but it isnt in PATH so it doesnt work
    elif query.startswith("whats my ip"):
        from requests import get
        ip = get('https://api.ipify.org').text
        ip2 = format(ip)
        print(ip2)
        engine.say("Your IP is %s"% ip2)
        engine.runAndWait()

    elif query.startswith("say "):
        query2 = query[4:]
        print("saying : ",query2)
        engine.say(query2)
        engine.runAndWait()
#
        #ADDING NEW FEATURES:
    elif query.startswith("play "):
        #here you can add youtube player ,when user types in the query after play
        #we will strip it and add it to youtube link, which will open youtube's website searching
        #for the same question,there is better ways to do this but they are too complex
        #you can drop this part out if you want to its not neccesary its just
        #an example of what you could do
        engine.say("Loading good music.")
        engine.runAndWait()
        import webbrowser
        mainin = list(query)
        if len(mainin) == 0:
            print("Open what?")
        elif len(mainin) == 1:
            print("enter and artist and a song name")
        else:
            print("Good. Loading the song list.")
            songkey0 = ''.join(mainin)
            songkey = songkey0[5:]
            songkey2 = songkey.replace(" ", "+")
        linkey = ('https://www.youtube.com/results?search_query=%s'% songkey2)
        webbrowser.open_new_tab(linkey)
            #By the way i clumsyly did this, you can just strip play like
        #i stripped "say " up there and add those words to a link
        #link example when searching is this:
        #https://www.youtube.com/results?search_query=darude+sandstorm+despacito
#
    else:
        res = client.query(query)
        try:
            key = next(res.results).text
            print (key)
            engine.say(key)
            engine.runAndWait()

        except StopIteration:
            engine.say("Try repeating that. Or modify your query")
            engine.runAndWait()

        except AttributeError:
            engine.say("Try repeating that. Or modify your query")
            engine.runAndWait()
        except Exception:
            engine.say("Try repeating that. Or modify your query")
            engine.runAndWait()
except KeyboardInterrupt:
    engine.stop()
except ReferenceError:
    engine.stop()

Main()