Coders Packet

Virtual Assistant in Python

By Rachit R Jindal

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):

# Listening to the input in microphone
def talk_listen():
    with sr.Microphone() as source:
        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','')
        # Current time is conveyed if 'time' word is heard
        if 'time' in str(t):
            talk("current time is "+str('%I:%M %p')))
        # Weather is conveyed if 'weather' word is heard
        if 'weather' in str(t):
            talk("Tell me the city")
            x = talk_listen()
  "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():

        # Searches the recognized text on wikipedia
            text = (str(t).lower()).replace('dbx',"")
            data = wikipedia.summary(text,1)

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

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'.

Download Complete Code


No comments yet