-
Notifications
You must be signed in to change notification settings - Fork 0
/
sophia.py
437 lines (376 loc) · 15.9 KB
/
sophia.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import pyttsx3
import speech_recognition as sr
import datetime
import time
import wikipedia
import webbrowser
import os
import random
import pywhatkit
import pyjokes
from pytube import YouTube
import keyboard
import pyautogui
import requests
from PIL import Image
import requests
from bs4 import BeautifulSoup
import wolframalpha
import whatsapp
from englisttohindi.englisttohindi import EngtoHindi
from googletrans import Translator
import speedtest as speedtest
assistant = pyttsx3.init()
voices = assistant.getProperty('voices')
assistant.setProperty('voice', voices[0].id)
def speak(audio):
"""[summary]
The audio is that which you are speak take as string[str] and then speak
Args:
audio ([str]]): [none]
"""
print(f": {audio}")
print(" ")
assistant.say(audio)
assistant.runAndWait()
def takeCommand():
command = sr.Recognizer()
# with sr.Microphone(device_index=1) as source:
with sr.Microphone() as source:
print("Listening...")
command.pause_threshold = 1
audio = command.listen(source)
try:
print("Recognizing...")
query = command.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
def startup():
"""[startup] this function will start when the sophia is run every time."""
# speak("Initializing Sophia")
# speak("Starting all systems applications")
# speak("Installing and checking all drivers")
# speak("Checking the internet connection")
speak("All systems have been activated")
hour = int(datetime.datetime.now().hour)
# if hour>=0 and hour<=12:
# speak("Good Morning")
# elif hour>12 and hour<18:
# speak("Good afternoon")
# else:
# speak("Good evening")
# strTime = datetime.datetime.now().strftime('%H:%M:%S')
# speak(f"Currently it is {strTime}")
# speak("I am sophia. Online and ready sir. Please tell me how may I help you")
def TalkInHindi():
command = sr.Recognizer()
with sr.Microphone() as source:
print("Listening.....")
command.pause_threshold=1
audio=command.listen(source)
try:
print("Recognizing.....")
query=command.recognize_google(audio,language='hi')
print(f"App ke kaha : {query}")
except:
return "none"
return query.lower()
def trans():
line=TalkInHindi()
translate=Translator()
result = translate.translate(line)
Text = result.text
return Text
def SpeedTest():
speed = speedtest.Speedtest()
upload = speed.upload()
correct_Up = int(int(upload)/800000)
download = speed.download()
correct_down = int(int(download)/800000)
return f'''
Uploading Speed : {correct_Up} .
Downloading Speed : {correct_down}.
'''
def screenshot():
speak("ok done, tell me the name of file.")
f_name= takeCommand()
file_name= f_name+".png"
path = "C:\\Users\\Yogesh Singh\\Documents\\GitHub\\miniProject\\Database\\screenshot\\"+file_name
pyauto = pyautogui.screenshot()
pyauto.save(path)
os.startfile("C:\\Users\\Yogesh Singh\\Documents\\GitHub\\miniProject\\Database\\screenshot\\"+file_name)
speak("Here your screenshot")
def nasa_news(Date):
speak("Extracting data from nasa")
print("Extracting data from nasa")
api_key = "ZMGdewvFuBq4SQfZJzCuNtrzqrnaPWkabDahrbpZ"
url = "https://api.nasa.gov/planetary/apod?api_key="+str(api_key)
Params = {'date':str(Date)}
r = requests.get(url,params=Params)
Data =r.json()
Info = Data['explanation']
Title = Data['title']
print(Info)
print(Title)
Img_url = Data['url']
Img_r = requests.get(Img_url)
file_name = str(Date)+'.jpg'
with open(file_name,'wb') as f:
f.write(Img_r.content)
path1 = "C:\\Users\\Yogesh Singh\\Documents\\GitHub\\miniProject\\"+str(file_name)
path2 = "C:\\Users\\Yogesh Singh\\Documents\\GitHub\\miniProject\\Database\\Photos\\"+str(file_name)
os.rename(path1, path2)
img = Image.open(path2)
img.show()
speak(f"Title is:{Title}")
speak(f"according to nasa:{Info}")
def wolfram(query):
api_key ="K93RPX-WWEK9JLAY8"
requester = wolframalpha.Client(api_key)
requested = requester.query(query)
try:
Answer = next(requested.results).text
return Answer
except:
print("An string value is not answerable.")
speak("An string value is not answerable.")
def calculator(query):
term = str(query)
term = term.replace('calculate ','')
term = term.replace('multiply ','*')
term = term.replace('plus ','+')
term = term.replace('minus ','-')
term = term.replace('divide ','/')
term = term.replace('into ','*')
term = term.replace('sophia ','')
final = str(term)
try:
result = wolfram(final)
print(result)
speak(f"Answer is: {result}")
except:
print("An string value is not answerable.")
speak("An string value is not answerable.")
def Temp(query):
tmp = str(query)
tmp = tmp.replace("sophia ","")
tmp = tmp.replace("in ","")
tmp = tmp.replace("what is the ","")
tmp = tmp.replace("temperature ","")
tmp_query = str(tmp)
if 'outside' in tmp_query:
var1="temperature in Noida"
answer=wolfram(var1)
speak(f"{var1} is {answer}")
else:
var2="temperature in " + tmp_query
ans=wolfram(var2)
speak(f"{var2} is {ans}")
if __name__ == "__main__":
startup()
while True:
try:
query = takeCommand().lower()
# Logic for executing tasks based on query
if "hello" in query:
speak("hello sir how can i help.")
elif "how are you" in query:
speak("I am fine sir What about you")
elif "fine" in query:
speak("ok, sir")
elif "bye" in query:
speak("bye sir, you can call me any time")
break
elif "time" in query:
strTime = datetime.datetime.now().strftime('%H:%M:%S')
speak(f"Currently it is {strTime}")
elif 'wikipedia' in query:
try:
speak('Searching On Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=4)
speak("According to Wikipedia")
# print(results)
speak(results)
# read_or_not=trans()
# print(read_or_not,"Please")
# if 'yes' in read_or_not:
# speak(results)
# if 'no' in read_or_not:
# pass
except Exception as e:
speak("Can not find result")
elif 'open amazon' in query or 'shop online' in query:
webbrowser.open("https://www.amazon.com")
print("opening amazon")
speak("opening amazon")
elif 'speed test' in query :
res=SpeedTest()
speak(res)
elif 'open flipkart' in query:
webbrowser.open("https://www.flipkart.com")
print("opening flipkart")
speak("opening flipkart")
elif 'open ebay' in query:
webbrowser.open("https://www.ebay.com")
print("opening ebay")
speak("opening ebay")
elif "shutdown" in query:
speak("shutting down")
os.system('shutdown -s')
elif 'make you' in query or 'created you' in query or 'develop you' in query:
ans_m = " For your information Yogesh, Krishna and Gulsher Develop me!."
print(ans_m)
speak(ans_m)
elif "who are you" in query or "about you" in query or "your details" in query:
about = "I am Sophia. I am computer based program but i can help you lot like a your close friend. Simple try me to give simple command ! I also play video and song from web or online. ok Lets Start "
print(about)
speak(about)
elif "your name" in query or "sweat name" in query:
na_me = "Thanks for Asking my name. well my self ! Sophia."
print(na_me)
speak(na_me)
elif query == 'none':
continue
elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query :
ex_exit = 'I feeling very sweet after meeting with you but you are going! i am very sad'
speak(ex_exit)
exit()
elif 'open code' in query:
code_path = "C:\\Users\\Yogesh Singh\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
print("Openning VSCODE")
speak("Openning VSCODE")
os.startfile(code_path)
elif 'play song' in query:
music_folder = "E:\\audios"
song = os.listdir(music_folder)
all_song=len(song)
import random
rand_song = random.randint(0, all_song-1)
print(f"Now playing {rand_song}, enjoy beautiful song")
os.startfile(os.path.join(music_folder,song[rand_song]))
elif "search on youtube" in query:
link = takeCommand()
link = query.replace("search",'')
link = query.replace("on","")
link = query.replace("youtube","")
web = f"https://www.youtube.com/results?search_query={link}"
speak("Searching On YouTube")
pywhatkit.playonyt(web)
elif "how to" in query:
from pywikihow import search_wikihow
import webbrowser as web
speak("Collecting data from the internet")
link = query.replace('sophia','')
link = query.replace('how to','')
link = query.replace('what is','')
link = query.replace('what do you mean by','')
max_result = 1
how_to = search_wikihow(link,max_result)
assert len(how_to) == 1
how_to[0].print()
speak(how_to[0].summary)
# speak("Can I read this?")
# read_or_not=takeCommand()
# if 'yes' in read_or_not:
# speak(how_to[0].summary)
# if 'no' in read_or_not:
# pass
elif 'remember that' in query:
speak("What to remember")
rememberMsg =takeCommand()
# speak("You tell me to remind you that" + rememberMsg)
remeber = open("D:\\miniProject\\Database\\data.txt",'w')
remeber.write(rememberMsg)
remeber.close()
speak("Done")
elif 'what you remember' in query:
remeber = open("D:\\miniProject\\Database\\data.txt")
print(f"You tell me to remember is that {remeber.read()}")
speak(f"You tell me to remember is that {remeber.read()}")
elif 'temperature' in query:
Temp(query)
# speak("Tell me the place name")
# place_name =takeCommand()
# search=f"Temperature in {place_name}"
# url = f"https://www.google.com/search?q={search}"
# r = requests.get(url)
# data = BeautifulSoup(r.text,"html.parser")
# temperature = data.find("div",class_="BNeawe").text
# speak(f"Temperature is {temperature}")
# print(f"Temperature is {temperature}")
# speak("You want to konw another city temperature?")
# next = takeCommand()
# if 'yes' in next:
# speak("Tell me the place name")
# place_name =takeCommand()
# search=f"Temperature in {place_name}"
# url = f"https://www.google.com/search?q={search}"
# import requests
# from bs4 import BeautifulSoup
# r = requests.get(url)
# data = BeautifulSoup(r.text,"html.parser")
# temperature = data.find("div",class_="BNeawe").text
# speak(f"Temperature is {temperature}")
# print(f"Temperature is {temperature}")
# else:
# speak("No problem!")
elif 'calculate' in query:
calculator(query)
elif 'screenshot' in query:
screenshot()
elif 'nasa' in query:
speak("Tell me the complete date like year, month, date ")
speak("tell me the year")
year = takeCommand()
speak("Tell me the month like 1,2 like that")
month = takeCommand()
speak("tell me the date")
date = takeCommand()
final_date =f"{year}-{month}-{date}"
nasa_news(final_date)
elif 'joke' in query:
list_of_jokes = pyjokes.get_jokes(language="en", category="all")
print(list_of_jokes)
for i in range(0, 4):
trans = EngtoHindi(message=list_of_jokes[i])
speak(trans.convert)
# speak(list_of_jokes[i])
elif 'what is' in query:
# ser=str(query)
# ser=ser.replace('what is ','')
# print(ser)
result = wolfram(query)
speak(result)
elif 'whatsapp message' in query:
query = query.replace("sophia","")
query = query.replace("send","")
query = query.replace("whatsapp message","")
query = query.replace("to","")
name = query
if 'Krishna Niet' in name:
numb = "7991189657"
speak(f"What's The Message For {name}")
mess = takeCommand()
whatsapp.whatsapp(numb,mess)
elif 'Aayan MCA' in name:
numb = "8434998154"
speak(f"What's The Message For {name}")
mess = takeCommand()
whatsapp.whatsapp(numb,mess)
elif 'Yogesh' in name:
numb = "8850281705"
speak(f"What's The Message For {name}")
mess = takeCommand()
whatsapp.whatsapp(numb,mess)
elif 'project group' in name:
gro = "GRNrmfIs9dYEs4RRybvjUw"
speak(f"What's The Message For {name}")
mess = takeCommand()
whatsapp.Whatspp_Grp(gro,mess)
except Exception as e:
print(e)
speak("I cannot recognize ")