As we know Python is a suitable language for scriptwriters and developers. Let’s write a script for Voice Assistant using Python. The query for the assistant can be manipulated as per the user’s need.
Speech recognition is the process of converting audio into text. This is commonly used in voice assistants like Alexa, Siri, etc. Python provides an API called SpeechRecognition to allow us to convert audio into text for further processing. In this article, we will look at converting large or long audio files into text using the SpeechRecognition API in python.
Modules needed
- Subprocess:- This module is used to get system subprocess details used in various commands i.e Shutdown, Sleep, etc. This module comes built-in with Python.
- WolframAlpha:- It is used to compute expert-level answers using Wolfram’s algorithms, knowledgebase and AI technology. To install this module type the below command in the terminal.
pip install wolframalpha
- Pyttsx3:- This module is used for the conversion of text to speech in a program it works offline. To install this module type the below command in the terminal.
pip install pyttsx3
- Tkinter:- This module is used for building GUI and comes inbuilt with Python. This module comes built-in with Python.
- Wikipedia:- As we all know Wikipedia is a great source of knowledge just like Lazyroar we have used the Wikipedia module to get information from Wikipedia or to perform a Wikipedia search. To install this module type the below command in the terminal.
pip install wikipedia
- Speech Recognition:- Since we’re building an Application of voice assistant, one of the most important things in this is that your assistant recognizes your voice (means what you want to say/ ask). To install this module type the below command in the terminal.
pip install SpeechRecognition
- Web browser:- To perform Web Search. This module comes built-in with Python.
- Ecapture:- To capture images from your Camera. To install this module type the below command in the terminal.
pip install ecapture
- Pyjokes:- Pyjokes is used for the collection of Python Jokes over the Internet. To install this module type the below command in the terminal.
pip install pyjokes
- Datetime:- Date and Time are used to showing Date and Time. This module comes built-in with Python.
- Twilio:- Twilio is used for making calls and messages. To install this module type the below command in the terminal.
pip install twilio
- Requests: Requests is used for making GET and POST requests. To install this module type the below command in the terminal.
pip install requests
- BeautifulSoup: Beautiful Soup is a library that makes it easy to scrape information from web pages. To install this module type the below command in the terminal.
pip install beautifulsoup4
Note: You can remove some of the import files if you don’t want to get that feature as here Twilio for making calls and messages if you don’t want to use that you can simply remove that function.
Implementation
Import the below libraries.
Python3
import subprocess import wolframalpha import pyttsx3 import tkinter import json import random import operator import speech_recognition as sr import datetime import wikipedia import webbrowser import os import winshell import pyjokes import feedparser import smtplib import ctypes import time import requests import shutil from twilio.rest import Client from clint.textui import progress from ecapture import ecapture as ec from bs4 import BeautifulSoup import win32com.client as wincl from urllib.request import urlopen |
Now we will set our engine to Pyttsx3 which is used for text to speech in Python and sapi5 is a Microsoft speech application platform interface we will be using this for text to speech function.
Python3
engine = pyttsx3.init( 'sapi5' ) voices = engine.getProperty( 'voices' ) engine.setProperty( 'voice' , voices[ 1 ]. id ) |
You can change the voice Id to “0” for the Male voice while using assistant here we are using a Female voice for all text to speech
Python3
def speak(audio): engine.say(audio) engine.runAndWait() def wishMe(): hour = int (datetime.datetime.now().hour) if hour> = 0 and hour< 12 : speak( "Good Morning Sir !" ) elif hour> = 12 and hour< 18 : speak( "Good Afternoon Sir !" ) else : speak( "Good Evening Sir !" ) assname = ( "Jarvis 1 point o" ) speak( "I am your Assistant" ) speak(assname) def username(): speak( "What should i call you sir" ) uname = takeCommand() speak( "Welcome Mister" ) speak(uname) columns = shutil.get_terminal_size().columns print ( "#####################" .center(columns)) print ( "Welcome Mr." , uname.center(columns)) print ( "#####################" .center(columns)) speak( "How can i Help you, Sir" ) def takeCommand(): r = sr.Recognizer() with sr.Microphone() as source: print ( "Listening..." ) r.pause_threshold = 1 audio = r.listen(source) try : print ( "Recognizing..." ) query = r.recognize_google(audio, language = 'en-in' ) print (f "User said: {query}\n" ) except Exception as e: print (e) print ( "Unable to Recognize your voice." ) return "None" return query def sendEmail(to, content): server = smtplib.SMTP( 'smtp.gmail.com' , 587 ) server.ehlo() server.starttls() # Enable low security in gmail server.login( 'your email id' , 'your email password' ) server.sendmail( 'your email id' , to, content) server.close() |
Main Function starts here, we will now call all these functions in the main function.
Python3
if __name__ = = '__main__' : clear = lambda : os.system( 'cls' ) # This Function will clean any # command before execution of this python file clear() wishMe() username() while True : query = takeCommand().lower() # All the commands said by user will be # stored here in 'query' and will be # converted to lower case for easily # recognition of command if 'wikipedia' in query: speak( 'Searching Wikipedia...' ) query = query.replace( "wikipedia" , "") results = wikipedia.summary(query, sentences = 3 ) speak( "According to Wikipedia" ) print (results) speak(results) elif 'open youtube' in query: speak( "Here you go to Youtube\n" ) webbrowser. open ( "youtube.com" ) elif 'open google' in query: speak( "Here you go to Google\n" ) webbrowser. open ( "google.com" ) elif 'open stackoverflow' in query: speak( "Here you go to Stack Over flow.Happy coding" ) webbrowser. open ( "stackoverflow.com" ) elif 'play music' in query or "play song" in query: speak( "Here you go with music" ) # music_dir = "G:\\Song" music_dir = "C:\\Users\\GAURAV\\Music" songs = os.listdir(music_dir) print (songs) random = os.startfile(os.path.join(music_dir, songs[ 1 ])) elif 'the time' in query: strTime = datetime.datetime.now().strftime( "% H:% M:% S" ) speak(f "Sir, the time is {strTime}" ) elif 'open opera' in query: codePath = r "C:\\Users\\GAURAV\\AppData\\Local\\Programs\\Opera\\launcher.exe" os.startfile(codePath) elif 'email to gaurav' in query: try : speak( "What should I say?" ) content = takeCommand() to = "Receiver email address" sendEmail(to, content) speak( "Email has been sent !" ) except Exception as e: print (e) speak( "I am not able to send this email" ) elif 'send a mail' in query: try : speak( "What should I say?" ) content = takeCommand() speak( "whome should i send" ) to = input () sendEmail(to, content) speak( "Email has been sent !" ) except Exception as e: print (e) speak( "I am not able to send this email" ) elif 'how are you' in query: speak( "I am fine, Thank you" ) speak( "How are you, Sir" ) elif 'fine' in query or "good" in query: speak( "It's good to know that your fine" ) elif "change my name to" in query: query = query.replace( "change my name to" , "") assname = query elif "change name" in query: speak( "What would you like to call me, Sir " ) assname = takeCommand() speak( "Thanks for naming me" ) elif "what's your name" in query or "What is your name" in query: speak( "My friends call me" ) speak(assname) print ( "My friends call me" , assname) elif 'exit' in query: speak( "Thanks for giving me your time" ) exit() elif "who made you" in query or "who created you" in query: speak( "I have been created by Gaurav." ) elif 'joke' in query: speak(pyjokes.get_joke()) elif "calculate" in query: app_id = "Wolframalpha api id" client = wolframalpha.Client(app_id) indx = query.lower().split().index( 'calculate' ) query = query.split()[indx + 1 :] res = client.query( ' ' .join(query)) answer = next (res.results).text print ( "The answer is " + answer) speak( "The answer is " + answer) elif 'search' in query or 'play' in query: query = query.replace( "search" , "") query = query.replace( "play" , "") webbrowser. open (query) elif "who i am" in query: speak( "If you talk then definitely your human." ) elif "why you came to world" in query: speak( "Thanks to Gaurav. further It's a secret" ) elif 'power point presentation' in query: speak( "opening Power Point presentation" ) power = r "C:\\Users\\GAURAV\\Desktop\\Minor Project\\Presentation\\Voice Assistant.pptx" os.startfile(power) elif 'is love' in query: speak( "It is 7th sense that destroy all other senses" ) elif "who are you" in query: speak( "I am your virtual assistant created by Gaurav" ) elif 'reason for you' in query: speak( "I was created as a Minor project by Mister Gaurav " ) elif 'change background' in query: ctypes.windll.user32.SystemParametersInfoW( 20 , 0 , "Location of wallpaper" , 0 ) speak( "Background changed successfully" ) elif 'open bluestack' in query: appli = r "C:\\ProgramData\\BlueStacks\\Client\\Bluestacks.exe" os.startfile(appli) elif 'news' in query: try : jsonObj = urlopen( '''https://newsapi.org / v1 / articles?source = the-times-of-india&sortBy = top&apiKey =\\times of India Api key\\''' ) data = json.load(jsonObj) i = 1 speak( 'here are some top news from the times of india' ) print ( '''=============== TIMES OF INDIA ============''' + '\n') for item in data[ 'articles' ]: print ( str (i) + '. ' + item[ 'title' ] + '\n' ) print (item[ 'description' ] + '\n' ) speak( str (i) + '. ' + item[ 'title' ] + '\n' ) i + = 1 except Exception as e: print ( str (e)) elif 'lock window' in query: speak( "locking the device" ) ctypes.windll.user32.LockWorkStation() elif 'shutdown system' in query: speak( "Hold On a Sec ! Your system is on its way to shut down" ) subprocess.call( 'shutdown / p /f' ) elif 'empty recycle bin' in query: winshell.recycle_bin().empty(confirm = False , show_progress = False , sound = True ) speak( "Recycle Bin Recycled" ) elif "don't listen" in query or "stop listening" in query: speak( "for how much time you want to stop jarvis from listening commands" ) a = int (takeCommand()) time.sleep(a) print (a) elif "where is" in query: query = query.replace( "where is" , "") location = query speak( "User asked to Locate" ) speak(location) elif "camera" in query or "take a photo" in query: ec.capture( 0 , "Jarvis Camera " , "img.jpg" ) elif "restart" in query: subprocess.call([ "shutdown" , "/r" ]) elif "hibernate" in query or "sleep" in query: speak( "Hibernating" ) subprocess.call( "shutdown / h" ) elif "log off" in query or "sign out" in query: speak( "Make sure all the application are closed before sign-out" ) time.sleep( 5 ) subprocess.call([ "shutdown" , "/l" ]) elif "write a note" in query: speak( "What should i write, sir" ) note = takeCommand() file = open ( 'jarvis.txt' , 'w' ) speak( "Sir, Should i include date and time" ) snfm = takeCommand() if 'yes' in snfm or 'sure' in snfm: strTime = datetime.datetime.now().strftime( "% H:% M:% S" ) file .write(strTime) file .write( " :- " ) file .write(note) else : file .write(note) elif "show note" in query: speak( "Showing Notes" ) file = open ( "jarvis.txt" , "r" ) print ( file .read()) speak( file .read( 6 )) elif "update assistant" in query: speak( "After downloading file please replace this file with the downloaded one" ) url = '# url after uploading file' r = requests.get(url, stream = True ) with open ( "Voice.py" , "wb" ) as Pypdf: total_length = int (r.headers.get( 'content-length' )) for ch in progress.bar(r.iter_content(chunk_size = 2391975 ), expected_size = (total_length / 1024 ) + 1 ): if ch: Pypdf.write(ch) # NPPR9-FWDCX-D2C8J-H872K-2YT43 elif "jarvis" in query: wishMe() speak( "Jarvis 1 point o in your service Mister" ) speak(assname) elif "weather" in query: # Google Open weather website # to get API of Open weather api_key = "Api key" speak( " City name " ) print ( "City name : " ) city_name = takeCommand() complete_url = base_url + "appid =" + api_key + "&q =" + city_name response = requests.get(complete_url) x = response.json() if x[ "code" ] ! = "404" : y = x[ "main" ] current_temperature = y[ "temp" ] current_pressure = y[ "pressure" ] current_humidiy = y[ "humidity" ] z = x[ "weather" ] weather_description = z[ 0 ][ "description" ] print ( " Temperature (in kelvin unit) = " + str (current_temperature) + "\n atmospheric pressure (in hPa unit) =" + str (current_pressure) + "\n humidity (in percentage) = " + str (current_humidiy) + "\n description = " + str (weather_description)) else : speak( " City Not Found " ) elif "send message " in query: # You need to create an account on Twilio to use this service account_sid = 'Account Sid key' auth_token = 'Auth token' client = Client(account_sid, auth_token) message = client.messages \ .create( body = takeCommand(), from_ = 'Sender No' , to = 'Receiver No' ) print (message.sid) elif "wikipedia" in query: webbrowser. open ( "wikipedia.com" ) elif "Good Morning" in query: speak( "A warm" + query) speak( "How are you Mister" ) speak(assname) # most asked question from google Assistant elif "will you be my gf" in query or "will you be my bf" in query: speak( "I'm not sure about, may be you should give me some time" ) elif "how are you" in query: speak( "I'm fine, glad you me that" ) elif "i love you" in query: speak( "It's hard to understand" ) elif "what is" in query or "who is" in query: # Use the same API key # that we have generated earlier client = wolframalpha.Client( "API_ID" ) res = client.query(query) try : print ( next (res.results).text) speak ( next (res.results).text) except StopIteration: print ( "No results" ) # elif "" in query: # Command go here # For adding more commands |
Output:
Listening…
Recognizing…
User said: Gaurav
#####################
Welcome Mr. Gaurav
#####################
Listening…
Recognizing…
User said: yes
(‘As the history majors among you here today know all too well, when people in power invent their own facts and attack those who question them, it can mark the beginning of the end of a free society. That is not hyperbole. It is what authoritarian regimes throughout history have done. They attempt to control reality. Not just our laws and our rights and our budgets, but our thoughts and beliefs.’, ‘Hillary Clinton’)
Listening…
Recognizing…
User said: Gaurav in Wikipedia
Gaurav is an Indian and Nepalese male name. The name literally means pride.
== Notable people named Gaurav ==
Gaurav S Bajaj, Indian television actor
Gaurav Bhatt, Indian Music Director, singer, songwriter.
Listening…
Recognizing…
User said: open YouTube
Listening…
Recognizing…
Unable to Recognizing your voice.
Listening…
Recognizing…
User said: exit