Data Science



Everything You Need to Know About Building a Personal Voice Assistant Using Python

Personal Voice Assistant

We are no strangers to digital assistants, be it Alexa or Siri or Ok Google. Do you know the little chatbox that helps with queries present on almost every webpage these days? Those are called digital assistants, which undergo conversations with human beings, both verbal and typed, to give us access to the information we are looking for.

The very first invention of a program that went close to being a digital assistant was Audrey, built by Bell Laboratories in 1952, which could understand the digits from 0 to 9. Next came the revolutionary IBM Shoebox 10 years later which could understand 6 control words besides the 10 digits. Fast-forwarding through Carnegie Mellon’s Harpy, Microsoft’s Clippy, and others, we come to October 4, 2011, when Apple introduced Siri for the very first time, followed by Amazon’s Alexa in 2014.

With such a fascinating history, you might wonder how to build your own personal voice assistant in a world which has made technology seem so easy. Web development courses are your answer that will eventually lead you to become a full stack Python developer who is the one juggling such jobs.

The Need and Growth of AI/Digital Voice Assistants

What is a full stack developer’s role? Or, is web development a good career? Such questions are quite unsurprising to swarm your mind. So, signing up for a full stack web development course,  it is good to understand what the role entails. This job involves creating the front end or the client end which is basically what we as users see and interact with, as well as the back-end or the server end where the functioning and operations are coded. 

Hence, in a world where the dependency on technology is growing steadfast, it’s no surprise that the future of artificial intelligence stands bright. The pandemic has brought about an utter reliance on technology and the ultimate goal now has become to reduce huma labor for repetitive tasks. 

With the rapid growth in wireless inventions, smart applications, and innovations, digital voice assistants become all the more necessary to function with utmost ease.

How to Build a Personal Voice Assistant Using Python?

To build such an assistant, the foremost thing you need is speech recognition. Python is equipped with an application programming interface (API) called SpeechRecognition which will allow audio to be converted into text.

Why do we need this conversion? So that our voice audio is translated into text or a code that our virtual assistant can process digitally and carry out our command. For things to run smoothly, we need various other modules as well. A Python module is nothing but some related codes that are saved in a file, with a “.py” extension. 

The modules called subprocesses are inbuilt in Python, whose commands deal with system subprocesses like restart and shut down. You will find all the details you need in any good full stack web development course.

The Packages Required and the Libraries to Import

Since we know what modules are, a package is a list of modules and a library is a list of packages. Let’s say one is creating an app and he/she needs to do some multiplication or addition and a list of operations is in a library. So, he/she can easily import one library to do those operations, which is why, importing packages and libraries is important. This is much like how we take a particular book we need from a physical library. 

Libraries like Pandas and NumPy, are certain Python libraries one can turn to. Coming to our digital assistant, a Python full stack developer course online will offer you the steps for implementation of the code that will make the assistant run perfectly. The packages and libraries need to be imported simply to make the job less complicated, smooth, and effective.

Let’s say we name our digital voice assistant Marvin and you want to command it to work out impeccable answers via WolframAlpha, an answer engine that does the same. You will thus write: 

marvin install wolframalpha

For any further installation of any application or website, the code can be written the same way. A full stack web developer course has comprehensive study plans that teach everything from scratch.

The Implementation Steps

As we begin the required coding to create a personal voice assistant, the first step is to install and import, as stated and shown before. Let us imagine that we needed and installed subprocess, WolframAlpha, Speech Recognition, Web Browser, Pyttsx3 (a module needed to convert text to speech), Ecapture (to click images on the computer), Datetime (tells the date and time), BeautifulSoup (a library with which we can easily get information from webpages), etc. The coding would be:

import subprocess

import wolframalpha

import pyttsx3

import speech_recognition as sr

import datetime

import webbrowser

from ecapture import ecapture as ec

from bs4 import BeautifulSoup

Next, to decide the voice of digital assistant, we choose SAPI5, a Microsoft technology used for the recognition of sound and consequent speech synthesis. After setting our engine to Pyttsx3 so that the text to speech can run, our code will be:

engine = pyttsx3.init(‘sapi5‘)

voices = engine.getProperty(‘voices‘)

engine.setProperty(‘voice‘, voices[0].id)

“0” here denotes a male voice, “1” will be female, “3” will be a boy child’s voice, and so on till 9. 

Now that we have our voice and wish to hear a greeting timed to the hours of the day, this code will suffice:

def speak(audio):

    engine.say(audio)

    engine.runAndWait()

def wishMe():

    hour = int(datetime.datetime.now().hour)

    if hour>= 0 and hour<12:

        speak(“Good Morning Sir !”)

    elif hour>= 12 and hour<18:

        speak(“Good Afternoon Sir !”)  

    else:

        speak(“Good Evening Sir !”)

Now to command your digital voice assistant according to your needs, you need to set up the command function: 

def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        print(“Listening…”)

        r.pause_threshold = 1

        audio = r.listen(source)

    try:

        print(“Recognizing…”)   

        query = r.recognize_google(audio, language =’en-in’)

        print(f“User said: {query}\n”)

    except Exception as e:

        print(e)   

        print(“Unable to Recognize your voice.”

        return “None”

Once done setting up the command function, try giving it a command. Let’s say a command for sending e-mails:

def sendEmail(to, content):

    server = smtplib.SMTP(‘smtp.gmail.com’, 587)

    server.ehlo()

    server.starttls()

    # Enable low security in gmail

    server.login(‘your email id’, ‘your email password’)

    server.sendmail(‘your email id’, to, content)

    server.close()

Once this is established, you can try it out practically: 

        elif ‘send a mail’ in query:

            try:

                speak(“What should I say?”)

                content = takeCommand()

                speak(“whom should i send”)

                to = input()   

                sendEmail(to, content)

                speak(“Email has been sent !”)

            except Exception as e:

                print(e)

                speak(“I am not able to send this email”)

For calculating something, this code should be inserted: 

elif “calculate” in query:

            app_id = “Wolframalpha api id”

            client = wolframalpha.Client(app_id)

            indx = query.lower().split().index(‘calculate’)

            query = query.split()[indx + 1:]

            res = client.query(‘ ‘.join(query))

            answer = next(res.results).text

            print(“The answer is “ + answer)

            speak(“The answer is “ + answer)

To open YouTube or Google, it’s a fairly simple task as follows: 

 elif ‘open youtube’ in query:

            speak(“Here you go to Youtube\n”)

            webbrowser.open(“youtube.com”)

        elif ‘open google’ in query:

            speak(“Here you go to Google\n”)

            webbrowser.open(“google.com”)

To play some music, you can try this code:

elif ‘play music’ in query or “play song” in query:

            speak(“Here you go with music”)

            # music_dir = “S:\\Song”

            music_dir = “C:\\Users\\SURAJ\\Music”

            songs = os.listdir(music_dir)

            print(songs)   

            random = os.startfile(os.path.join(music_dir, songs[1]))

To know the time or date, this code will help:

        elif ‘the time’ in query:

            strTime = datetime.datetime.now().strftime(“% H:% M:% S”)   

            speak(f“Sir, the time is {strTime}”)

To take a picture, the command should be:

elif “camera” in query or “take a photo” in query:

            ec.capture(0, “Marvin Camera “, “img.jpg”)

The functions that your personal voice assistant will carry out might be ongoing functions like how it can help you or simple greetings or they might be summoning functions like taking a picture, playing music, reporting the news, or forecasting the weather among several others.

Like this, with the required codes, you can program your personal voice assistant to carry out your commands proficiently, say certain phrases and even crack jokes, exactly like our Google Assistant or Alexa. And, with this, you’re done creating your very own assistant!

Why This is a Good Idea for a Data Science and Web Development Project?

As web development courses keep garnering popularity, it is natural to think is web development a good career? If yes, then why? Well, technology and tech jobs are one of the fastest-growing job sectors in the world. And, a full-stack developer job along with a data science job will be one of the highest-paying tech jobs in the coming decade. 

With our society and environment constantly striving towards an effortless living, technology is bound to dominate and in extension, so will any tech job. With a whopping 11.5 million jobs in data science and a 31.4% rise in AI jobs according to the Bureau of Labour Statistics (BLS), a career as a full stack Python developer looks brighter than ever. This is why, going into data science and web development is guaranteed to yield results that will exceed your expectations and will prove to be worthwhile and a full stack developer certification will only be your first step.

What will You Learn by Doing This Project?

After resolving all queries related to a full stack developer and what they do, it becomes an attractive job opportunity that is greatly sought-after. But just a job, future, and salary aren’t the only aspects that matter. What matters most is what you are able to take away from such jobs.

With data science already an established sector and full stack developers growing their roots, this is an area that will allow for a variety of opportunities and projects that you can pursue at the same time. Combining these two, you will be able to culminate your knowledge and learn about all the following topics:

  • Web development
  • HTML and JavaScript
  • Coding languages and programming languages 
  • Machine learning
  • Designing data models
  • Knowledge of cloud architecture, DevOps, and database management systems

Such programs allow you not to only work but learn new things as you do so, gain productive experience and prepare for the work environment you’re about to encounter.

Since all tech jobs are tied together by threads of similarity, if you have a good grasp of web development, full stack development would come easy to you. And, if you know Python or other programming languages, then data science would be greatly interesting. 

Web development courses offer you excellent programs to get you in touch with the fields you need to be working on, complete with a full stack developer certification. 

Programming and coding for applications with Python can be learnt as part of either a Data Science certification program or a Full Stack Development certification program, as it finds applications across domains.

Learn in-demand skills and get guaranteed job oportunities

    Contact Us