The virtual assistant named 'DBX' makes use of speech_recognition, pyttsx3, and pywhatkit python modules for listening, converting speech-to-text, and search for text on the web.
This virtual assistant python project mainly uses speech_recognition, pyttsx3, and pywhatkit modules for listening, converting speech-to-text, and search for text on the web. By default, the virtual assistant is named 'dbx' which can be modified to the desired name.
import speech_recognition as sr import pyttsx3 from datetime import datetime import pywhatkit import os import wikipedia import pyjokes
Other modules include DateTime to fetch the current time and date, Wikipedia for extracting data of text, pyjokes to create some humor in the assistant commands, and os for opening and extracting stored data from the files.
Recognition and Speech function:
# Creating functions for voice recognition and listening speech = sr.Recognizer() engine = pyttsx3.init() # Text-to-Voice conversion def talk(text): engine.say(text) engine.runAndWait() # Listening to the input in microphone def talk_listen(): with sr.Microphone() as source: print("Listening........") voice = speech.listen(source) speech_data = speech.recognize_google(voice) # Returns the string of the recognized voice return speech_data
The above code contains the speech-to-text conversion function named talk_listen, and text-to-speech conversion function named talk. The 'speech' sis the recognizer function is used to listen and convert the speech to the string while the 'engine' variable is the function the converts the input text to the speech. The talk_speech function returns the text extracted from the voice in the form of a string.
Commands response of assistant:
# Contains the different function for the vitual assistant def commands(): # Listens to the input voice t = talk_listen() # This virtual assistant is named as dbx # If dbx is recognised then assistant work if 'dbx' in str(t).lower(): # Play the music if 'play' word is heard if 'play' in str(t): song = str(t.lower()).replace('dbx play','') talk("playing"+song) pywhatkit.playonyt(song) # Current time is conveyed if 'time' word is heard if 'time' in str(t): talk("current time is "+str(datetime.now().strftime('%I:%M %p'))) # Weather is conveyed if 'weather' word is heard if 'weather' in str(t): talk("Tell me the city") x = talk_listen() pywhatkit.search("weather in "+x) # These two conditions are just for adding humour if 'are you single' in str(t).lower(): talk("No! I'm in relationship with your complier") if 'joke' in str(t).lower(): talk(pyjokes.get_joke()) # Searches the recognized text on wikipedia else: text = (str(t).lower()).replace('dbx',"") data = wikipedia.summary(text,1) talk(data) else: return
This part contains the operation the assistant can perform and the function can be integrated by maintaining a database and using NLP(Natural Language Processing) to extract more info to perform a wide range of operations. The above code performs the following operations:
1) Plays the video song on youtube if the word 'play' is recognized in the speech
2) Tells the current time using the DateTime python module in [%H:%M %p] format
3) Provides the weather information of the asked city if the speech contains the word 'weather' as one of its parts
4) Cracks a joke using pyjokes module.
5) Search for the text on Wikipedia for providing the information based on the asked data.
All the operations are converted to speech form using the talk function which was created earlier in the packet.
Running the assistant:
# The assistant runs infinitely while True: # Repeatedly calling command function commands()
The while loop runs infinitely for the assistant to keep the assistant in an active state, but the assistant operates only if the speech contains the name of the assistant 'dbx'.
Submitted by Rachit R Jindal (rachit99)
Download packets of source code on Coders Packet