Voice Assistant for Desktop:
Introduction
A virtual assistant, also called an AI assistant or digital assistant, is an application program that understands natural language voice commands and completes tasks for the user.
For building any voice based assistant you need two main functions. One for listening to your commands and another to respond to your commands. Along with these two core functions, you need the customized instructions that you will feed your assistant.
The first step is to install and import all the necessary libraries. Use pip install to install the libraries before importing them. Following are some of the key libraries used in this program:
- The SpeechRecognition library allows Python to access audio from your system’s microphone, transcribe the audio, and save it.
- Google’s text-to-speech package, gTTS converts your audio questions to text. The response from the look-up function that you write for fetching answer to the question is converted to an audio phrase by gTTS. This package interfaces with Google Translate’s API.
- Playsound package is used to give voice to the answer. Playsound allows Python to play MP3 files.
- Web browser package provides a high-level interface that allows displaying Web-based pages to users. Selenium is another option for displaying web pages. However, for using this you need to install and provide the browser-specific web driver.
- Wikipedia is used to fetch a variety of information from the Wikipedia website.
- The first function, recognize voice(), will be responsible for capturing our voice (which we input through the Microphone), recognizing it, and returning the "text" version of it.
- Then we will take that "text" version of our voice and give it to another function called reply(), which will be responsible for replying back to us and doing all sorts of other crazy things (like searching google, telling the current time, etc.).
- Finally, a function called speak(), which will take whatever text we give it and converts it into speech.
Requirements
- You should have python3.3 or a higher version installed on your computer.
- You should have venv installed. If you are using Python 3.3 or newer, then the venv is already included in the Python standard library and requires no additional installation.
- You should have a microphone (your laptop's builtin one or the one on your earphone will do the job)
- You should need an Internet connection.
- Finally, you should have a modern code editor like visual studio code.
Initial Setups
- First, create a folder named voice_assistant anywhere on your computer.
- Then open it inside visual studio code.
Now let's make a new virtual environment using venv and activate it. To do that:
- Open Terminal > New Terminal.
- Then type:
- python3 -m venv venv
- To activate it, if you are on windows, type the following:
- venv\Scripts\activate.bat
- Finally, create a new file named "main.py" directly inside the voice_assistant folder like below:
This command will create a virtual environment named venv for us.
Now you should see something like this:
Note: Virtual environments like venv help us to keep all the dependencies related to the current project in its own environment isolated from the main computer. That's one of the main reasons why we are using it.
Comments
Post a Comment