-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
66 lines (56 loc) · 1.79 KB
/
main.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
import os
import random
import time
import webbrowser
import playsound
import speech_recognition as sr
from gtts import gTTS
r = sr.Recognizer()
def record_audio(ask=False):
with sr.Microphone() as source:
if ask:
assistant_speak(ask)
audio = r.listen(source)
voicedata = ""
try:
voicedata = r.recognize_google(audio)
except sr.UnknownValueError:
assistant_speak("sorry i did not get that")
except sr.RequestError:
assistant_speak("sorry my voive is down")
return voicedata
def assistant_speak(audio_string):
tts = gTTS(text=audio_string, lang="en")
r = random.randint(1, 1000000)
audio_file = f"audio-{r}.mp3"
tts.save(audio_file)
playsound.playsound(audio_file)
print(audio_string)
os.remove(audio_file)
def respond(voicedata):
# name
if "what is your name" in voicedata:
assistant_speak("My name is your assistant")
# time
if "what time is it" in voicedata:
assistant_speak(time.ctime())
# google search
if "search " in voicedata:
search = record_audio("what do you want to search for")
url = "https://www.google.com/search?q=" + search
webbrowser.get().open(url)
assistant_speak("here i found for" + search)
# youtube
if "youtube videos " in voicedata:
youvideos = record_audio("Name of video")
url = "https://www.youtube.com/results?search_query={youvideos}"
webbrowser.get().open(url)
assistant_speak("Here is the video " + youvideos)
# exit
if "exit" in voicedata:
exit()
time.sleep(1)
assistant_speak("How can i help you")
while True:
voicedata = record_audio()
respond(voicedata)