DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Virtual Assistant using python
VISHAK
VISHAK

Posted on

Virtual Assistant using python

Python for Artificial Intelligence !!

Python modules plays a very important role in AI based solutions that defines a functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.

Used Modules;
1) Speech recognition

2) pyttsx3
3) pyaudio
4) wikipedia
5) datetime

Speech recognition
It allows computers to understand human language. Speech recognition is a machine's ability to listen to spoken words and identify them. You can then use speech recognition in Python to convert the spoken words into text, make a query or give a reply.

pyttsx3
pyttsx3 is a text-to-speech conversion library in Python. it is a very easy to use tool which converts the entered text into speech.

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

pywhatkit
Module used for playing songs from youtube.Function pywhatkit.playonyt(), opens the YouTube in your default browser and plays the video you mentioned in the function. If you pass the topic name as parameter, it plays the random video on that topic. On passing the URL of the video as the parameter, it open that exact video.

Syntax: pywhatkit.playonyt(β€œsearch name”)

wikipedia
Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia. All the datas from the wikipedia can be used here by text or by audio output.

datetime
Used to return current date, that can be using in many properties as a timestamp if needed.

import speech_recognition as sr
import pyttsx3
import pyaudio
import wikipedia
import pywhatkit
from datetime import datetime

now = datetime.now()
current_time = now.strftime("%H"  "%M")
cam = ["who", "when", "why", "what", "where"]


listener = sr.Recognizer()
on = pyttsx3.init()
newVoiceRate = 129
on.setProperty("rate", newVoiceRate)
voices = on.getProperty('voices')
on.setProperty('voice', voices[1].id)
on.say("hello vishak what can i do for you")
on.runAndWait()
def talk(lm):
    on.say(lm)
    on.runAndWait()
def take():
    try:
        with sr.Microphone() as source:
            print("listening...")
            voice = listener.listen(source)
            com = listener.recognize_google(voice)

    except:
        pass
    return com
def vk():
    command = take()
    if 'happy' in command:
        talk('yes iam happy')
    elif 'what is your name' in command:
        talk('my name is vk 3.7')
    elif 'who created you' in command:
        talk('vishak created me')

    elif 'play' in command:
        song = command.replace('play', '')
        talk('playing' + song)
        pywhatkit.playonyt(song)

    elif 'hai' in command:
        talk('hehehe')

    elif 'how are you' in command:
        talk('iam fine thank you')
    elif 'what is' in command:
        dd = command.replace('what is', '')
        woc = wikipedia.summary(dd, 1)
        talk(woc)

    elif 'time' in command:
        time = datetime.now().strftime('%I:%M %p')
        talk('the time is' + time)
    elif 'can you talk faster' in command:
        new = 180
        on.setProperty("rate", new)
        talk('yes i can talk faster is this fast ok for you vishak')

    elif 'can you speak other languages' in command:
        talk('sorry i dont know other languages,but i can learn')

    elif 'get' in command:
        nic = command.replace('what is', '')
        wac = wikipedia.summary(nic, 1)
        talk(wac)
    elif 'can you change your voice' in command:
        voices = on.getProperty('voices')
        on.setProperty('voice', voices[0].id)
        talk('okay,what about my new voice')
    elif 'wow cool' in command:
        talk('thank you')


    elif 'then tell me something' in command:
        talk('can you please me more romantic vishak,it will be better')



    elif 'who is ' in command:
        who = command.replace('who is', '')
        ohw = wikipedia.summary(who, 1)
        talk(ohw)


while True: vk()
Enter fullscreen mode Exit fullscreen mode

You can edit the elif function and add appropriate text as needed(question) and put down the reply in the talk block.

Create your own piece of code!
Enjoy <3

Top comments (0)

Let's hear from your organization

Create an Organization and start sharing content with the community on DEV.