<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prajakta Sawant on Medium]]></title>
        <description><![CDATA[Stories by Prajakta Sawant on Medium]]></description>
        <link>https://medium.com/@prajakta16?source=rss-7641ba4f16c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*g0ICfi0TVOcJ7NZWKn6faw.jpeg</url>
            <title>Stories by Prajakta Sawant on Medium</title>
            <link>https://medium.com/@prajakta16?source=rss-7641ba4f16c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 07:25:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prajakta16/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“Build Your Voice Assistant Using Python”]]></title>
            <link>https://prajakta16.medium.com/build-your-voice-assistant-using-python-c590f906a771?source=rss-7641ba4f16c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c590f906a771</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[pyttsx3]]></category>
            <category><![CDATA[speech-recognition]]></category>
            <dc:creator><![CDATA[Prajakta Sawant]]></dc:creator>
            <pubDate>Fri, 16 Oct 2020 14:04:49 GMT</pubDate>
            <atom:updated>2020-10-16T14:04:49.476Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everyone!! In this article I am sharing my learnings from the Voice-Assistant Event which I attended recently. Hope you will find this article interesting and you will build your first Voice Assistant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BreondhHQrfcxenB9VNw0g.jpeg" /></figure><p>In this article I will show you how to build your own voice assistant using the Python Programming Language! In general voice assistants react to the voice commands and give the user relevant information about their inquiry. They are able to process orders of products, answer questions, perform actions like playing music or start a simple phone call with a friend and much more.</p><h4>Let’s get started</h4><p>Important libraries required in this program are as follows: -</p><p><strong>1. Pyttsx3</strong></p><p>Pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3. An application invokes the pyttsx3.init() factory function to get a reference to a pyttsx3 Engine instance. It is a very easy to use tool which converts the entered text into speech.</p><p>The pyttsx3 module supports two voices one for male and one for female which is provided by “sapi5” api for windows.</p><p>It supports three TTS engines :</p><p><strong><em>sapi5</em></strong> — SAPI5 on Windows</p><p><strong><em>nsss</em></strong> — NSSpeechSynthesizer on Mac OS X</p><p><strong><em>espeak</em></strong> — eSpeak on every other platform</p><p>To install the pyttsx3 module, first of all, you have to open the terminal and write</p><pre>pip install pyttsx3</pre><p><strong>2. SpeechRecognition</strong></p><p>All of the magic in SpeechRecognition happens with the Recognizer class.</p><p>The primary purpose of a Recognizer instance is, of course, to recognize speech. Each instance comes with a variety of settings and functionality for recognizing speech from an audio source.</p><p>Each Recognizer instance has seven methods for recognizing speech from an audio source using various APIs. These are:</p><p>recognize_bing()</p><p>recognize_google()</p><p>recognize_google_cloud()</p><p>recognize_houndify()</p><p>recognize_ibm()</p><p>recognize_sphinx()</p><p>recognize_wit()</p><p>All seven recognize_*() methods of the Recognizer class require an audio_data argument. In each case, audio_data must be an instance of SpeechRecognition’s AudioData class.</p><p><strong>Installation</strong></p><p>To install SpeechRecognition -</p><pre>pip install SpeechRecognition</pre><h4>Import libraries and packages</h4><p>Import all the required libraries and packages for building the voice assistance.</p><pre>import pyttsx3<br>import speech_recognition as sr        <br>import datetime  <br>import wikipedia<br>import webbrowser<br>import os<br>import random</pre><p>Import datetime module for manipulating dates and times required further in our program. In order to extract data from Wikipedia, we must import the Python Wikipedia library, which wraps the official Wikipedia API. Python’s Wikipedia module is required to fetch a variety of information from the Wikipedia website. The webbrowser module provides a high-level interface which allows displaying Web-based documents to users.</p><h4>Create Functions</h4><pre>engine = pyttsx3.init(&#39;sapi5&#39;)</pre><pre><strong># returns list of voices</strong><br>voices = engine.getProperty(&#39;voices&#39;)</pre><pre><strong># </strong><strong>voice id can be male(0) or female(1)</strong>  <br>engine.setProperty(&#39;voice&#39;, voices[0].id)</pre><p>For windows, ‘sapi5&#39; is an API for voice of male and female from pyttsx3 library. There are total 3 different types of voices and you can choose one for your voice assistance as shown above in the code.</p><pre><strong>def speak(audio):</strong>    <br>engine.say(audio)    <br>engine.runAndWait() </pre><pre><strong>def wishMe(): </strong>   <br>  hour = int(datetime.datetime.now().hour)    <br>  if (hour &gt;= 0 and hour &lt; 12):        <br>      speak(&#39;Good Morning!&#39;)   <br>  elif (hour &gt;= 12 and hour &lt; 18):       <br>      speak(&#39;Good Afternoon!&#39;)    <br>  else:        <br>      speak(&#39;Good Evening!&#39;)  <br>   <br>  speak(&#39;I am Jarvis, I am working for Prajakta. Please tell me how may I help you&#39;)</pre><p>In the above code the speak() function is written to play the audio which we choosed earlier. The wishMe() fuction uses datetime module and it is written to wish the user by the voice assistant ‘Good Morning’ or ‘Good Afternoon’ or Good Evening’ according to the time mentioned in the conditions.</p><pre><strong>def takeCommand():</strong>    <br><strong># It takes microphone input from the user and returns string output, microphone input requires PyAudio    <br># pip install pyaudio</strong> <br>    <br>r = sr.Recognizer()<br><strong># use the default microphone as the audio source    </strong><br>with sr.Microphone() as source:        <br>  print(&#39;Listening...&#39;)</pre><pre><strong># It can have a 1 second gap while speaking  </strong>     <br>  r.pause_threshold = 1 </pre><pre><strong># minimum audio energy for recording   </strong>    <br>  r.energy_threshold = 300 # minimum audio energy for recording        <br>  r.adjust_for_ambient_noise(source, duration = 1)        <br>  audio = r.listen(source)   #instance of AudioData   <br>      <br>try:           <br>  print(&#39;Recognizing...&#39;)           <br>  query = r.recognize_google(audio, language=&#39;en-US&#39;)<br>  print(f&#39;User Said: {query}\n&#39;)<br>except Exception as e:            <br>  print(e)            <br>  print(&#39;Say that again please...&#39;)            <br>  query = takeCommand().lower()            <br>  return query;        <br>return query;</pre><p>As the user has to speak or voice out the commands to the virtual assistant for doing the task, we will require microphone as input. Till the time user is speaking the voice assistant will be listening. The <strong>pause_threshold</strong> is used to set the time or gap while the user is speaking. The <strong>energy_threshold</strong> represents the energy level threshold for sounds. Values below this threshold are considered silence, and values above this threshold are considered speech. The minimum energy_threshold is 300 and it can be changed. To avoid the background noise while user is speaking <strong>r.adjust_for_ambient_noise </strong>(source, duration = 1) is used.</p><p>Try and except block is used to answer the query of the user which is not understood or not accepted by the virtual assistant.</p><h4>The main program</h4><pre><strong>if __name__ == &quot;__main__&quot;:</strong>    <br>  wishMe()    <br>  while True:        <br>    query = takeCommand().lower()  <br>     <br><strong>#Simple commands to give answer for user&#39;s query </strong><br>    if &#39;your name&#39; in query:            <br>      speak(&#39;My name is Jarvis&#39;)        <br>    elif &#39;how are you&#39; in query:            <br>      speak(&#39;I am fine&#39;)        <br>    elif &#39;who are you&#39; in query:            <br>      speak(&#39;I am Jarvis working for my friend Waqar Bhai&#39;)        <br>    elif &#39;Prajakta&#39; in query:            <br>      speak(&#39;Prajakta is a good person. She was born on 16th March  1995. She loves coding and dancing. She is my friend and I am working for her.&#39;)</pre><pre><strong>#If user want some information from wikipedia</strong><br>    elif &#39;wikipedia&#39; in query:           <br>      speak(&#39;Searching Wikipedia...&#39;)            <br>      query = query.replace(&#39;wikipedia&#39;, &#39;&#39;)            <br>      results = wikipedia.summary(query, sentences=2)<br>      speak(&#39;According to wikipedia&#39;)                       <br>      print(results)            <br>      speak(results)</pre><pre><strong>#Open google for user</strong><br>    elif &#39;open google&#39; in query:<br>      webbrowser.open(&quot;google.com&quot;) </pre><pre><strong>#Play songs for user</strong>               <br>    elif &#39;play song&#39; in query:            <br>      music_dir = &#39;Give path for songs&#39; <br># returns the list of files inside that directory           <br>      songs = os.listdir(music_dir)  <br># generating random index to play random song           <br>      os.startfile(                <br>        os.path.join(music_dir, songs[random.randrange(0, 3)]))  <br><strong>#Calculator for user</strong>     <br>    elif (&#39;add&#39; or &#39;addition&#39;) in query:            <br>      speak(&quot;enter first number&quot;)            <br>      num1 = float(input(&quot;Enter First No: &quot;))<br>      speak(&quot;enter second number&quot;)                          <br>      num2 = float(input(&quot;Enter Second No: &quot;))<br>      print(f&quot;Ans = {num1+num2}&quot;)                            <br>      speak(f&quot;The addition of {num1} and {num2} is {num1+num2}&quot;)<br>    elif (&#39;subtract&#39; or &#39;subtraction&#39;) in query:                         <br>      speak(&quot;enter first number&quot;)            <br>      num1 = float(input(&quot;Enter First No: &quot;))<br>      speak(&quot;enter second number&quot;)                         <br>      num2 = float(input(&quot;Enter Second No: &quot;))<br>      print(f&quot;Ans = {num1-num2}&quot;)                         <br>      speak(f&quot;The subtraction of {num1} and {num2} is {num1-num2}&quot;)</pre><p>The above code is the main program where we give conditions for what our voice assistant should give us in return if the user asks some query. We will need to have the program consistently listening. We will need a continuous loop that runs forever recording the audio so, we have used while loop in the above code. And it is followed by the if else conditions for performing tasks like playing songs, giving information from Wikipedia, behaving like calculator, opening google for user.</p><p><strong>And we are done here, by building our first voice assistant!!</strong></p><p>Thank you for reading this article I hope it was helpful and interesting for you all. If you like it please encourage me by giving some claps for this article. Also, let me know where I can improve myself. Thank you!!</p><h3>Sources</h3><blockquote><a href="https://pypi.org/project/SpeechRecognition/1.5.0/">1. SpeechRecognition 1.5.0</a></blockquote><blockquote>2.<a href="https://pythonprogramminglanguage.com/text-to-speech/"> Speech Recognition in Python (Text to speech)</a></blockquote><blockquote>3.<a href="https://medium.com/@randerson112358/build-a-virtual-assistant-using-python-2b0f78e68b94"> Build A Virtual Assistant Using Python</a></blockquote><blockquote>4. <a href="https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst">Speech Recognition Library Reference</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c590f906a771" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>