paint-brush
Building Covid-19 Voice Assistantby@KGPrajwal
350 reads
350 reads

Building Covid-19 Voice Assistant

by K G PrajwalMay 27th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The coronavirus has caused havoc across the globe. Millions of people are infected and thousands are dead. The pandemic is still on the rise as all the countries in the world have imposed a lock down. Everybody has the rights to know how this virus has affected our lives as we continue to wage a war against it. So, I write this article about my project with a voice that addresses the current situation of the world. The project mainly covers the fundamentals of web scraping, voice assistance and multi threading in python.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Building Covid-19 Voice Assistant
K G Prajwal HackerNoon profile picture


The coronavirus has caused havoc across the globe. Millions of people are infected and thousands are dead. The pandemic is still on the rise as all the countries in the world have imposed a lock down. Everybody has the rights to know how this virus has affected our lives as we continue to wage a war against it. So, I write this article about my project with a voice that addresses the current situation of the world.

The Voice Assistant Project

In this project, you’ll learn how to build a COVID19 voice assistant using Python. The project mainly covers the fundamentals of web scraping, voice assistance and multi threading in python.

Web Scraping

The project makes use of a tool called ParseHub. ParseHub is free web scraper that is very powerful and is easy to use. This tool allows you to scrape the web merely just by clicking on the elements you would like to get out from that website. So, the first step is to download ParseHub.

We will be scraping from the very famous website for statistics which is Worldometers that is regularly kept updated about the coronavirus information. Open up ParseHub -> Start New Project -> Paste the link to
worldometers.

Click on the elements you would like to scrape from the page and assign
unique tags to them. Use the relative tool to link let’s say, ‘Coronavirus Cases’ on the page with its number. The ParseHub is clearly very efficient and uses an AI to understand which elements you would want. To try this, link ‘USA’ with the ‘total cases’ and it does rest of the countries in the list for you. Amazing! What this is doing behind the scenes is constructing a JSON format of what you are linking above.

After you are done with the linking, hit ‘Get Data’, which may take a while.
Then make sure, on the next screen you note down the API key, Project
token and Run token.

Voice Assistant

Copy paste the code given in Part1.py and I shall walk you through what is really going on.

These points explain the Part1.py file:

  • The requirements for this part of the project are the following python packages: requests, json, pyttsx3, SpeechRecognition, re. So, pip install all these packages.
  • Use your API key, project token and run token in their respective variables.
  • The class Data has the following methods:
  1. get_data(): fetch the data from parsehub using your most recent run for the project on their servers and return it.
  2. get_total_cases(): get the total cases of COVID-19 around the world.
  3. get_country_deaths(): get the total number of COVID-19 deaths in the world.
  4. get_country_data(): get the number COVID-19 cases and deaths in any particular country.
  5. get_list_of_countries(): get the list of all the countries.
  • The speak() method initializes the pyttsx3 engine to say a text passed into it as a parameter.
  • The get_audio() method listens to the speech input by the user through the microphone using the recognize_google() method by Google and returns the input text.
  • Finally the main() function is responsible for recognizing the input speech
    using regular expression patterns and classify them as patterns for cases and deaths in world or cases and deaths in any country.
  • The matched pattern calls that particular function and speaks the number out loud.

Multi threading

As of now, your assistant speaks only the data you fetched initially. It
is important to keep the data updated, as this global pandemic is still
on, it would be soothing to hear it when your assistant says that the
number is declining.

Delete Part1.py and copy the Part2.py into your application.

The Part2.py has just an extra function and a handler for it in addition to Part1.py. This function is the update() function. The function, doesn’t fetch from the last run on parsehub, but instead initializes a new run on the parsehub servers for your project. This will take a while, so please give it some time when you are updating.

The most interesting part is to reduce this time. So, we make use of multi threading using the multithreading library in python. By doing this, we ensure that the voice assistant is running on one thread and the updating of data takes place parallely on another.

Result

The most interesting part of this project is its result.

Run the Part2.py file and when the console says “Listening…”, say “number
of total cases” and the assistant would reply with the number of COVID19 cases across the world. Now say, “total deaths in India” and the
assistant would reply with this number.

You can say, “update” and the assistant will update the values for you which may take some time. Please wait patiently.

You will have to say “stop” to exit from the running application.

😲Yay!!!
You did it! You just built a voice assistant that updates you about the
coronavirus by only using your voice. You can further develop this
project into amazing things. To get complete insight checkout my GitHub.

If you got any errors, feel free to reach out to me on my LinkedIn.

Hopefully, you enjoyed as well as understood this project.

Thank You.

Previously published at https://medium.com/@KGPrajwal/covid19-voice-assistant-63c37b1f02f9