import imp
from gtts import gTTS
import speech_recognition as sr
import random
import time
import playsound
def listen_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
our_speech = r.recognize_google(audio, language="ru")
print("Вы сказали: " +our_speech)
return our_speech
except sr.UnknownValueError:
return "ошибка"
except sr.RequestError as e:
return "ошибка"
def do_this_command(message):
message = message.lower()
if "привет" in message:
say_message("Привет друг!")
elif "пока" in message:
say_message("Пока!")
exit()
else:
say_message("Не понимаю")
def say_message(message):
voice = gTTS(message, lang="ru")
file_voice_name = "_audio_"+str(time.time())+"_"+str(random.randint(0,100000))+".mp3"
voice.save(file_voice_name)
playsound.playsound(file_voice_name)
print("Голосовой ассистент: "+message)
while True:
command = listen_command()
do_this_command(command)