-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (76 loc) · 2.82 KB
/
main.py
File metadata and controls
99 lines (76 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import speech_recognition as sr
import webbrowser
import pyttsx3
import musicLibrary
from gtts import gTTS
import pygame
import time
import os
from client import aiProcess
recognaizer = sr.Recognizer()
engine = pyttsx3.init()
# I added just for study purpose
def speak(text: str): #Not using due to bad unser interface
engine.say(text)
engine.runAndWait()
# New speak fucntion
def old_speak(text: str ):
tts = gTTS(text)
tts.save('tempaudio.mp3')
pygame.mixer.init()
pygame.mixer.music.load("tempaudio.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(1)
pygame.mixer.music.unload() # Optional but good practice
os.remove("tempaudio.mp3")
def processCommand(command):
if "open google" in command.lower():
webbrowser.open("https://google.com")
elif "open facebook" in command.lower():
webbrowser.open("https://facebook.com")
elif"open youtube" in command.lower():
webbrowser.open("https://youtube.com")
elif"open linkedin" in command.lower():
webbrowser.open("https://in.linkedin.com")
elif command.lower().startswith("play"):
song = command.lower().split(" ")[1]
link = musicLibrary.music[song]
webbrowser.open(link)
else:
output = aiProcess(command)
speak(output)
if __name__ == "__main__" :
speak("Initializing your personal assistant AD....")
while True:
# Say hello to wake me up
# Obtain audio from the Microphone
r = sr.Recognizer()
r.energy_threshold = 300
print("Recognizing....")
# Recognize speech using google
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening....")
audio = r.listen(source)
word = r.recognize_google(audio)
if(word.lower() == "hello"):
speak("Yeah")
#Listen for command
constt = True
while constt:
with sr.Microphone() as source:
print("Ad is now Active....")
audio = r.listen(source)
command = r.recognize_google(audio)
if command.lower() == ["bye", "exit", "quit", "goodbye", "see you", "stop", "see you later"]:
speak("Have a nice day!")
exit()
processCommand(command)
else:
constt = False
else:
speak("I did not understand")
except Exception as e:
print("Error; {0}".format(e))