Building Your Own Virtual Assistant Using Python

Building Your Own Virtual Assistant Using Python

Have you ever wondered how a virtual assistant works and how is it built? But can we build our own virtual artificial intelligence assistant? Of course, you can, thanks to speech recognition and python! Building a personal assistant allows you to add customized features that are specific to your needs. It enables you to live a more productive and modern lifestyle!

 

Check out this blog to learn how to build your own virtual assistant using Python 

in a few easy steps!

Step 1: Install the necessary libraries and modules.

To create a personal voice assistant, you will need to install the following packages on your system.


1) Speech recognition — Speech recognition is a key feature in home automation and artificial intelligence systems. The SpeechRecognition package is extremely flexible because it serves as a wrapper for numerous major speech APIs. It would be capable of identifying the user’s speech/verbal commands and identifying the input words/input phrases, i.e., the words that will trigger the assistant to respond.

2) pyttsx3 — pyttxs3 is a python text-to-speech conversion package. This package supports text-to-speech engines on Mac os x, Windows, and on Linux too.

Once done, you’ll need to run the following commands on the terminal to add these:
· pip install pyttsx3

· pip install SpeechRecognition

 

Step 2: Import the modules and libraries
Let’s bring in the libraries we just installed, as well as a few standard Python

import pyttsx3
import speech_recognition as sr

import webbrowser

import DateTime

 

The Python DateTime module is included with Python, thus there’s no need to install it separately. The DateTime module is the classes for working with date and time which are included in this module. These classes provide a wide range of functions for working with dates, times, and time intervals.

Step 3: Voice Input 

Establishing voice communication is the first and foremost step in developing your own personal virtual assistant. Using the libraries we just installed, we’ll write two functions: one for listening and one for reacting. Let’s start by importing the libraries we installed, as well as a few typical Python libraries:

import speech_recognition as sr

from time import ctime

import time

import os

from gtts import gTTS

import requests, json

 

The function called listen triggers your machine’s microphone with the SpeechRecognition library, then converts the audio to text in the form of a string. Google Text-to-Speech (gTTS) is a Python library and command-line utility for interacting with Google Translate’s text-to-speech API. For more audio modification, writes spoken mp3 data to a file, a file-like object (bytestring), or stdout.

 

Learn about the Future of work with AI Powered Virtual Assistants!


The next component can be a basic audio greeting now that you’ve installed your text-to-voice engine. To make the virtual assistant human-friendly, put any term you’d like the virtual assistant to use here.

def listen():

    r = sr.Recognizer()

    with sr.Microphone() as source:

        print(“Hello, I am your Virtual Assistant. How Can I Help You Today”)

        audio = r.listen(source)

    data = “”

    try:

        data = r.recognize_google(audio)

        print(“You said: ” + data)

    except sr.UnknownValueError:

        print(“Sorry! Audio was not recognized by Google Speech Recognition.”)

    except sr.RequestError as e:

        print(“Request Failed; {0}”.format(e))

    return data

Now we’ll use the gTTS library and create a respond function that takes a string input, prints it, and then converts it to an audio file. Your OS saves this audio file to the local directory and then plays it.

def respond(audioString):

    print(audioString)

    tts = gTTS(text=audioString, lang=’en’)

    tts.save(“speech.mp3”)

    os.system(“mpg321 speech.mp3”)

Listening and responding establishes one of the most fundamental parts of any virtual assistant for voice interaction. With this, we can now develop our digital assistant and add some basic functionality now that we have the basic building pieces in place.

Step 4: Voiced Answers
To build our virtual assistant, we’ll create a new function named digital assistant and provide it a few simple responses:

def digital_assistant(data):

    if “How are you doing” in data:

        listening = True

        respond(“I am well”)

 

    if “What time is it exactly” in data:

        listening = True

        respond(ctime())

    if “stop listening” in data:

        listening = False

        print(‘Sorry! Listening stopped’)

        return listening

    return listening

Find out the difference between Intelligent Virtual Assistants and Traditional Chatbot here!


Conclusion
We’ve finally finished developing our own AI virtual assistant using. You can also add more features to your virtual assistant to make it execute more tasks.

Algoscale is one of the leading Artificial Intelligence and Data Analytics Companies in the USA, helping businesses become more agile and smart by adopting creative and effective artificial intelligence solutions. Our AI solutions combine business, development, and operations data to provide actionable insights and a unified perspective of our clients’ changing business environments, allowing them to take advantage of automation and improve business outcomes over time.

Recent Posts