Packages

In [1]:
import speech_recognition as sr # recognise speech
import playsound # to play an audio file
from gtts import gTTS # google text to speech
import random
from time import ctime # get time details
import webbrowser # open browser
import ssl
import certifi
import time
import os # to remove created audio files
from PIL import Image
import subprocess
import pyautogui #screenshot
import pyttsx3
import bs4 as bs
import urllib.request
import watermark
In [2]:
# Package versions
%load_ext watermark
%watermark -v -iv
watermark          2.0.2
pyautogui          0.9.50
bs4                4.9.1
certifi            2020.06.20
speech_recognition 3.8.1
PIL.Image          7.1.2
CPython 3.7.7
IPython 7.16.1

Person Class

In [3]:
class Person:
    name = ''
    def set_name(self, name):
        self.name = name
In [4]:
person_obj = Person()
person_obj.name = 'Matheus'

Assistant Class

In [5]:
class Assistant:
    name = ''
    def set_name(self, name):
        self.name = name
In [6]:
assistant_obj = Assistant()
assistant_obj.name = 'Bot'

Check for Terms in Voice Data

In [7]:
def check_terms(terms):
    for term in terms:
        if term in voice_data:
            return True

Bot Speak Synthesizer

In [8]:
# Uses pyttsx3 for offline speech text-to-speech
def engine_speak_text(text):
    text = str(text)
    engine.say(text)
    engine.runAndWait()
In [9]:
r = sr.Recognizer() # initialise a recogniser
In [10]:
# Listen for audio and covert it to text
def record_audio(ask=''):
    with sr.Microphone() as source: # microphone as source
        if ask:
            engine_speak(ask)
        audio = r.listen(source, 5, 5) # listen for the audio via source
        print('Done listening')
        voice_data = ''
        try:
            voice_data = r.recognize_google(audio) # convert audio to text
        except sr.UnknownValueError: # error: recognizer does not understand
            engine_speak('Sorry, I could not understand that')
        except sr.RequestError:
            engine_speak('Sorry, the service is down') # error: recognizer is not connected
        print('>>', voice_data.lower()) # print what the user said
        return voice_data.lower()
In [11]:
def engine_speak(audio_string):
    audio_string = str(audio_string)
    tts = gTTS(text=audio_string, lang='en') # Text-to-Speech (voice)
    rand = random.randint(1, 20000000)
    audio_file = 'audio' + str(rand) + '.mp3'
    tts.save(audio_file) # save as mp3
    playsound.playsound(audio_file) # play the audio file
    print(assistant_obj.name + ':', audio_string) # Print what the app said
    os.remove(audio_file) # Remove audio file
In [12]:
def respond(voice_data):
    
    # 1: Greeting
    if check_terms(['hey', 'hi', 'hello']):
        greetings = ['How can I help you? ' + person_obj.name]
        greet = greetings[random.randint(0, len(greetings)-1)]
        engine_speak(greet)
    
    # 2: Time
    if check_terms(['what time is it', "what's the time", 'tell me the time']):
        time = ctime().split(" ")[3].split(':')[0:2]
        if time[0] == '00':
            hours = '12'
        else:
            hours = time[0]
        minutes = time[1]
        time = hours + ' hours and ' + minutes + ' minutes'
        engine_speak(time)
    
    # 3: Search Google
    if check_terms(['search for']) and 'youtube' not in voice_data:
        search_term = voice_data.split('for')[-1]
        url = "https://google.com/search?q=" + search_term
        webbrowser.get().open(url)
        engine_speak('Here is what I found for ' + search_term + ' on Google')
    
    
    # 4: Search Youtube
    if check_terms(['youtube']):
        search_term = voice_data.split('for')[-1]
        url = "https://www.youtube.com/results?search_query=" + search_term
        webbrowser.get().open(url)
        engine_speak("Here is what I found for " + search_term + " on youtube")       
    
    # 5: Weather
    if check_terms(['weather']):
        search_term = voice_data.split('for')[-1]    
        url = "https://www.google.com/search?sxsrf=ACYBGNSQwMLDByBwdVFIUCbQqya-ET7AAA%3A1578847393212&ei=oUwbXtbXDN-C4-EP-5u82AE&q=weather&oq=weather&gs_l=psy-ab.3..35i39i285i70i256j0i67l4j0i131i67j0i131j0i67l2j0.1630.4591..5475...1.2..2.322.1659.9j5j0j1......0....1..gws-wiz.....10..0i71j35i39j35i362i39._5eSPD47bv8&ved=0ahUKEwiWrJvwwP7mAhVfwTgGHfsNDxsQ4dUDCAs&uact=5"
        webbrowser.get().open(url)
        engine_speak("Here is the weather for your location")

    # 6: Rock Paper Scissors 
    if check_terms(['rock paper scissors', 'rock-paper-scissors']):
        voice_data = record_audio('Choose among rock, paper or scissors')
        moves = ['rock', 'paper', 'scissors']
        
        cmove = random.choice(moves)
        pmove = voice_data
        
        engine_speak(assistant_obj.name + ' chose ' + cmove)
        engine_speak('You chose ' + pmove)
 
        if pmove==cmove:
            engine_speak("the match is draw")
        elif pmove== "rock" and cmove== "scissors":
            engine_speak("Player wins")
        elif pmove== "rock" and cmove== "paper":
            engine_speak("Computer wins")
        elif pmove== "paper" and cmove== "rock":
            engine_speak("Player wins")
        elif pmove== "paper" and cmove== "scissors":
            engine_speak("Computer wins")
        elif pmove== "scissors" and cmove== "paper":
            engine_speak("Player wins")
        elif pmove== "scissors" and cmove== "rock":
            engine_speak("Computer wins")
        
    # 7: Coin flip
    if check_terms(['toss', 'flip', 'coin']):
        moves = ['heads', 'tails']
        cmove = random.choice(moves)
        engine_speak('The coin landed ' + cmove)
    
    # 8: Screenshot
    if check_terms(["capture", "my screen", "screenshot"]):
        myScreenshot = pyautogui.screenshot()
        myScreenshot.save('screenshot/screen.png') 
        engine_speak('I took a screenshot for you')
In [15]:
while(1):
    voice_data = record_audio('Recording') # get the voice input
    print('Done')
    print('Q:', voice_data)
    respond(voice_data)
    
    # Exit
    if check_terms(['exit', 'quit', 'goodbye']):
        engine_speak('Goodbye')
        break
Mila: Recording
Done listening
>> hello
Done
Q: hello
Mila: How can I help you? Matt
Mila: Recording
Done listening
>> search for unicorn
Done
Q: search for unicorn
Mila: Here is what I found for  unicorn on Google
Mila: Recording
Done listening
>> play rock-paper-scissors
Done
Q: play rock-paper-scissors
Mila: Choose among rock, paper or scissors
Done listening
>> rock
Mila: Mila chose rock
Mila: You chose rock
Mila: the match is draw
Mila: Recording
Done listening
>> screenshot
Done
Q: screenshot
Mila: I took a screenshot for you
Mila: Recording
Done listening
>> quit
Done
Q: quit
Mila: Goodbye

End