-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (44 loc) · 1.44 KB
/
main.py
File metadata and controls
61 lines (44 loc) · 1.44 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
import random
import playsound
from gtts import gTTS
import speech_recognition as sr
def listen():
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Скажите команду:")
audio = r.listen(source)
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
speech = r.recognize_google(audio, language="ru")
print("Вы сказали: ", speech)
return speech
except sr.UnknownValueError:
return 'Error'
except sr.RequestError as e:
return 'Error'
def say(text):
voice = gTTS(text, lang = "ru")
unique_filename = "audio_" + str(random.randint(0, 100000)) + ".mp3"
voice.save(unique_filename)
playsound.playsound(unique_filename)
print("Assistant:", text)
def handle_message(message):
message = message.lower()
if "привет" in message:
say("Привет-привет!")
elif "прощай" in message:
finish()
else:
say("Я такой команды не знаю")
def finish():
print("Пока")
exit()
if __name__ == '__main__':
print('Test')
while True:
command = listen()
handle_message(command)