icon

Aug 31, 2021

Build your own Artificial Assistant

Virtual assistants are Artificial Intelligent based programs. They are a smart computer program that understands human natural languages through voice commands or text and performs tasks for the user.


Visual Studio Code is an integrated development platform for computer programming, with an emphasis on the Python programming language.

Step-1
Launch the application (VsCode) and click on the file menu, then select new project.
For this little script we are using python libraries:
SpeechRecognition is a library for performing speech recognition, with support for several engines and APIs, online and offline.
PyAudio provides Python bindings for PortAudio, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms.
Pyttsx3 is a text-to-speech conversion library in Python. 
For the robot to listen to our voice/speech
`pip install speechRecognition`
For Python bindings for PortAudio
'pip install pyaudio'
To speak out, or text to speech
`pip install pyttsx3`
For advance control on browser
`pip install pywhatkit`
To get wikipedia data
`pip install wikipedia`
To get funny jokes
`pip install pyjokes`

Step-2
Paste the below Code:
--------------------------------
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)


def talk(text):
    engine.say(text)
    engine.runAndWait()


def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'Shainee' in command:
                command = command.replace('Shainee', '')
                print(command)
    except:
        pass
    return command


def run_Alexa():
    command = take_command()
    print(command)
    if 'play' in command:
        song = command.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'tell me about' in command:
        person = command.replace('tell me about', '')
        info = wikipedia.summary(person, 1)
        print(info)
        talk(info)
    elif 'date' in command:
        talk('sorry, I have a headache')
    elif 'are you single' in command:
        talk('I am in a relationship with wifi')
    elif 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('Please say the command again.')

while True:
    run_Alexa()

Step-3
Click Play Button





No comments: