-
Notifications
You must be signed in to change notification settings - Fork 2
/
luna.py
174 lines (143 loc) · 5.99 KB
/
luna.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import random
'''
**********Luna voice assistant**********
Hello. This is a voice assistant script made of python. Before run, check all modules are installed or not.
Check the requirements.txt file to install modules
You can modify this code according to your need.
Share your code with me and all
Happy coding.......
'''
engine = pyttsx3.init('sapi5') #sapi5 is a speech Application Programming Interface(API) developed by Microsoft
voices = engine.getProperty('voices')
engine.setProperty('voice', "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0") #you can you other voice according to your choice
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishme():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Good Moning")
elif hour >= 12 and hour < 18:
speak("Good afternoon")
else:
speak("Good evening")
speak("hello , I am Luna , please tell me how may i help you")
def takeCommand():
r = sr.Recognizer()
r.energy_threshold = 4000
with sr.Microphone() as source:
# print(sr.Microphone.list_microphone_names())
print("Listening............")
r.pause_threshold = 0.5
audio = r.listen(source)
# r.adjust_for_ambient_noise(source, duration=5)
r.adjust_for_ambient_noise(source)
try:
print("Recognizing........")
quary = r.recognize_google(audio, language='en-in')
print(f"user said: {quary}\n")
except Exception as e:
# print(e)
print("say again.....")
return "None"
return quary
if __name__ == '__main__':
wishme()
while True:
query = takeCommand().lower()
if 'wikipedia' in query:
try:
speak('searching wikipedia')
query = query.replace('wikipedia', "")
results = wikipedia.summary(query, sentences=2)
speak("according to wikipedia")
print(results)
speak(results)
except wikipedia.exceptions.PageError:
continue
elif "thank you" in query:
speak('You are welcome sir !')
elif "hello" in query:
print("hello sir")
speak("Hello sir !")
elif "open youtube" in query:
webbrowser.open("youtube.com")
elif "open google" in query:
webbrowser.open("google.com")
elif 'time' in query:
Time = datetime.datetime.now().strftime("%H: %M : %S")
print(Time)
speak(Time)
elif 'music' in query:
music_dir = "C:\\Users\\shuvo\\Desktop\\music" #put the directory of music file .... in case of miltiple musics, use random module
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'open cmd' in query:
cmd_Path = "C:\\Windows\\system32\\Cmd.exe" #path of cmd.exe
speak("opening cmd")
os.startfile(cmd_Path)
elif 'open browser' in query:
browser_Path = "C:/Program Files (x86)/Mozilla Firefox/firefox.exe" #path of browser
speak("opening firefox")
os.startfile(browser_Path)
elif "who are you" in query:
speak('I am Luna. Your voice assistant . I am made of python code and developed by Shuvasis Sarkar')
elif 'what is' in query:
try:
speak('searching wikipedia')
query = query.replace('what is', "")
results = wikipedia.summary(query, sentences=2) #If you want more sentences, change the value of sentences
speak("according to wikipedia")
print(results)
speak(results)
finally:
speak("cant find")
elif 'who is' in query:
try:
speak('searching wikipedia')
query = query.replace('who is', "")
results = wikipedia.summary(query, sentences=2)
speak("according to wikipedia")
print(results)
speak(results)
except wikipedia.exceptions.PageError:
continue
elif "repeat" in query: #Repeat what you said
speak('repeating')
print('repeating..........')
query = query.replace('repeat', "")
print(query)
speak(query)
elif "note" in query:
query=query.replace("note","")
new_note1=open("text_to_note.txt","a+") #text_to_note.txt will be created in current directory
speak("Writing")
print("writing.......")
new_note1.write(query +"\n")
new_note1.close()
elif "show text" in query:
f=open("text_to_note.txt","r")
contents=f.read()
speak("showing text file contents")
print("showing text file contents........")
print(contents)
elif 'date' in query:
date = datetime.datetime.now().strftime("%d: %m: %Y")
print(date)
speak(date)
elif "think" in query:
f=open("think.txt","r") #make a think.txt file in current directory and put some lines
lines= f.readlines()
read_line= random.randint(0,2) #Randomely select any line ***carefully put maximum line value***
print(lines[read_line])
speak(lines[read_line])
elif 'bye' in query: #Here I am using break statement
speak("Good Bye sir ! ")
break