Introduction
We use several voice assistants by various companies from around the world, like Amazon’s Alexa, Google’s Google Assistant, Apple’s Siri. In this article, we will discuss creating a voice assistant using the Python programming language and its libraries. We are using different libraries which are available in Python. As I said in my earlier articles Python has a lot of library files that are used for different purposes. In this article, we will see how to create a voice assistant using Python.
Installing Libraries
For creating this program we need to install some library files from the internet for the purpose of our usage. These library files can be installed using the following terminal commands.
- pip install Pyaudio
- pip install speech_recognition
- pip install playsound
- pip install gtts
- pip install ssl
- pip install certify
- pip install webrowser
Step 1 - Importing libraries
At the first stage of programming, we need to import the libraries and functions which are required for the execution of the program. We may import libraries like speech_recognition for recognition of what we are speaking, playground for the purpose of making the system to speak or play sound, gtts (Google text to Speech) for making the system to interact with you and for getting a voice, random for the purpose of giving the random names for the audio files generated by the program, ctime for the purpose of telling you the time if you required, web browser for the purpose for opening and accessing the browser of your system and os for the purpose of destroying the audio files created by the program.
- import speech_recognition as sr # recognise speech
- import playsound # to play an audio file
- from gtts import gTTS # google text to speech
- import random
- from time import ctime # get time details
- import webbrowser # open browser
- import ssl
- import certifi
- import time
- import os # to remove created audio files
Step 2 - Recognition of Speech
The next important step in voice assistant is recognition of speech and the speaker. To make the system understand what we are saying we need to convert the voice to text. For the purpose of learning more about this concept please refer to my friend Naveenkumar Paramasivam's article by clicking here. Then we need to save the name of the person who is interacting with the system.
- r = sr.Recognizer() # initialise a recogniser
- # listen for audio and convert it to text:
- def record_audio(ask=False):
- with sr.Microphone() as source: # microphone as source
- if ask:
- speak(ask)
- audio = r.listen(source) # listen for the audio via source
- voice_data = ''
- try:
- voice_data = r.recognize_google(audio) # convert audio to text
- except sr.UnknownValueError: # error: recognizer does not understand
- speak('I did not get that')
- except sr.RequestError:
- speak('Sorry, the service is down') # error: recognizer is not connected
- print(f">> {voice_data.lower()}") # print what user said
- return voice_data.lower()
Step 3 - Replying to the question
To make the system reply during the conversation we are using Google's text to speech library for the purpose of making the computer speak out. First we need to make the system respond in the form of text and then using the gtts library we can make the system read the text.
- def speak(audio_string):
- tts = gTTS(text=audio_string, lang='en') # text to speech(voice)
- r = random.randint(1,20000000)
- audio_file = 'audio' + str(r) + '.mp3'
- tts.save(audio_file) # save as mp3
- playsound.playsound(audio_file) # play the audio file
- print(f"May Day: {audio_string}") # print what app said
- os.remove(audio_file) # remove audio file
Step 4 - Responding for the questions
At least you need to make the system respond to the questions asked by the user. In this program, I just included a few ways of responding to the questions like details about the stocks, searching something on the browser and youtube, and some more basic things. You may customize it based on your requirement.

Armagan GultekinPosted Jul 16, 2022, 12:12 PM
Traceback (most recent call last): File "c:\Users\magultekin\Desktop\main.py", line 124, in <module> voice_data = record_audio() # get the voice input File "c:\Users\magultekin\Desktop\main.py", line 24, in record_audio with sr.Microphone() as source: # microphone as source File "C:\Program Files\Python310\lib\site-packages\speech_recognition\__init__.py", line 138, in __enter__ self.audio.open( File "C:\Users\magultekin\AppData\Roaming\Python\Python310\site-packages\pyaudio.py", line 750, in open stream = Stream(self, *args, **kwargs) File "C:\Users\magultekin\AppData\Roaming\Python\Python310\site-packages\pyaudio.py", line 441, in __init__ self._stream = pa.open(**arguments) OSError: [Errno -9999] Unanticipated host error