how to make a virtual assistant with python

Solutions on MaxInterview for how to make a virtual assistant with python by the best coders in the world

showing results for - "how to make a virtual assistant with python"
Gabriele
07 Jan 2021
1import speech_recognition as mic
2import pyttsx3
3import pywhatkit
4import datetime
5import wikipedia
6import pyjokes
7
8listener = mic.Recognizer()
9engine = pyttsx3.init()
10voices = engine.getProperty('voices')
11engine.setProperty('voice', voices[1].id)
12
13
14def talk(text):
15    engine.say(text)
16    engine.runAndWait()
17
18
19def take_command():
20    try:
21        with sr.Microphone() as source:
22            print('Yeah I am listening!')
23            voice = listener.listen(source)
24            command = listener.recognize_google(voice)
25            command = command.lower()
26            if 'google' in command:
27                command = command.replace('google', '')
28                print(command)
29    except:
30        pass
31    return command
32
33
34def run_alexa():
35    command = take_command()
36    print(command)
37    if 'play song' in command:
38        song = command.replace('play', '')
39        talk('playing ' + song)
40        pywhatkit.playonyt(song)
41    elif 'time' in command:
42        time = datetime.datetime.now().strftime('%I:%M %p')
43        talk('The current time is ' + time)
44    elif 'who is' in command:
45        person = command.replace('who the heck is', '')
46        info = wikipedia.summary(person, 1)
47        print(info)
48        talk(info)
49    elif 'date' in command:
50        talk('sorry, I have a headache')
51    elif 'are you single' in command:
52        talk('No, my best freind is you!')
53    elif 'joke' in command:
54        talk(pyjokes.get_joke())
55    else:
56        talk('Sorry I did not get that')
57
58
59while True:
60    run_alexa()
61    
62    #you have to install the following by the commands.Just copy and paste each text given
63    #below, you can paste it on the code editors terminal or in cmd.
64    #pip install SpeechRecognition
65    #pip install pyttsx3
66    #pip install pywhatkit
67    #pip install DateTime
68    #pip install wikipedia
69    #pip install PyAudio
70    #pip install pyjokes
71    
72