mirror of
https://github.com/MarkParker5/STARK.git
synced 2024-11-24 08:12:13 +02:00
fix crashes in voice assistant
This commit is contained in:
parent
b5edc30a09
commit
9ddc85c765
@ -31,31 +31,31 @@ class VoiceAssistant(Control):
|
||||
return cls.instance
|
||||
|
||||
def start(self):
|
||||
listener.listen_noise()
|
||||
self.listener.listen_noise()
|
||||
os.system('clear')
|
||||
|
||||
while True:
|
||||
if voids >= 3:
|
||||
voids = 0
|
||||
if self.voids >= 3:
|
||||
self.voids = 0
|
||||
if config.double_clap_activation:
|
||||
print('\nSleep (-_-)zzZZ\n')
|
||||
sleep()
|
||||
print('\nYou: ', end='')
|
||||
speech = listener.listen()
|
||||
speech = self.listener.listen()
|
||||
print(speech.get('text') or '', end='')
|
||||
while True:
|
||||
if speech['status'] == 'error':
|
||||
break
|
||||
if speech['status'] == 'void':
|
||||
voids += 1
|
||||
self.voids += 1
|
||||
break
|
||||
text = speech['text']
|
||||
cmd, params = Command.reg_find(text).values()
|
||||
try: response = cmd.start(params)
|
||||
except: break
|
||||
reply(response)
|
||||
check_threads()
|
||||
report()
|
||||
self.reply(response)
|
||||
self.check_threads()
|
||||
self.report()
|
||||
if response.callback:
|
||||
speech = recognize(response.callback, {})
|
||||
else:
|
||||
@ -63,20 +63,20 @@ class VoiceAssistant(Control):
|
||||
|
||||
def recognize(self, callback, params):
|
||||
print('\nYou: ', end='')
|
||||
speech = listener.listen()
|
||||
speech = self.listener.listen()
|
||||
if speech['status'] in ['error', 'void']:
|
||||
return speech
|
||||
text = speech['text']
|
||||
print(text, end='')
|
||||
while True:
|
||||
check_threads()
|
||||
self.check_threads()
|
||||
if not callback: break
|
||||
try:
|
||||
if response := callback.answer(text):
|
||||
reply(response)
|
||||
self.reply(response)
|
||||
except:
|
||||
break
|
||||
memory.insert(0, {
|
||||
self.memory.insert(0, {
|
||||
'text': text,
|
||||
'cmd': cmd,
|
||||
'response': response,
|
||||
@ -86,64 +86,59 @@ class VoiceAssistant(Control):
|
||||
return speech
|
||||
|
||||
def report(self):
|
||||
global reports
|
||||
for response in reports:
|
||||
for response in self.reports:
|
||||
if response.voice:
|
||||
voice.generate(response.voice).speak()
|
||||
self.voice.generate(response.voice).speak()
|
||||
time.sleep(2)
|
||||
reports = []
|
||||
self.reports = []
|
||||
|
||||
def reply(self, response):
|
||||
if response.text: # print answer
|
||||
print('\nArchie: '+response.text)
|
||||
if response.voice: # say answer
|
||||
voice.generate(response.voice).speak()
|
||||
self.voice.generate(response.voice).speak()
|
||||
if response.thread: # add background thread to stack
|
||||
threads.append(response.thread)
|
||||
self.threads.append(response.thread)
|
||||
|
||||
def check_threads(self):
|
||||
for thread in threads:
|
||||
for thread in self.threads:
|
||||
if not thread['finish_event'].is_set(): continue
|
||||
response = thread['thread'].join()
|
||||
reply(response)
|
||||
self.reply(response)
|
||||
if response.callback:
|
||||
if response.callback.quiet:
|
||||
response.callback.start()
|
||||
else:
|
||||
for _ in range(3):
|
||||
print('\nYou: ', end='')
|
||||
speech = listener.listen()
|
||||
speech = self.listener.listen()
|
||||
if speech['status'] == 'ok':
|
||||
print(speech['text'], '\n')
|
||||
new_response = response.callback.answer(speech['text'])
|
||||
reply(new_response)
|
||||
self.reply(new_response)
|
||||
break
|
||||
else:
|
||||
reports.append(response)
|
||||
self.reports.append(response)
|
||||
thread['finish_event'].clear()
|
||||
del thread
|
||||
|
||||
# check double clap from arduino microphone module
|
||||
def checkClap(self, channel):
|
||||
global lastClapTime
|
||||
global doubleClap
|
||||
now = time.time()
|
||||
delta = now - lastClapTime
|
||||
delta = now - self.lastClapTime
|
||||
if 0.1 < delta < 0.6:
|
||||
doubleClap = True
|
||||
self.doubleClap = True
|
||||
else:
|
||||
lastClapTime = now
|
||||
self.lastClapTime = now
|
||||
|
||||
# waiting for double clap
|
||||
def sleep(self):
|
||||
global lastClapTime
|
||||
lastClapTime = 0
|
||||
global doubleClap
|
||||
while not doubleClap:
|
||||
check_threads()
|
||||
self.lastClapTime = 0
|
||||
while not self.doubleClap:
|
||||
self.check_threads()
|
||||
time.sleep(1)
|
||||
else:
|
||||
doubleClap = False
|
||||
self.doubleClap = False
|
||||
|
||||
if __name__ == '__main__':
|
||||
VoiceAssistant().start()
|
||||
|
@ -1,5 +1,5 @@
|
||||
from bs4 import BeautifulSoup as BS
|
||||
from ..Command import Command
|
||||
from ..Command import Command, Response
|
||||
import wikipedia as wiki
|
||||
import requests
|
||||
import random
|
||||
@ -69,10 +69,6 @@ class QA(Command):
|
||||
try: search = this.googleSearch(query)
|
||||
except: search = ''
|
||||
voice = text = search or random.choice(['Не совсем понимаю, о чём вы.', 'Вот эта последняя фраза мне не ясна.', 'А вот это не совсем понятно.', 'Можете сказать то же самое другими словами?', 'Вот сейчас я совсем вас не понимаю.', 'Попробуйте выразить свою мысль по-другому',])
|
||||
return {
|
||||
'type': 'simple',
|
||||
'text': text,
|
||||
'voice': voice,
|
||||
}
|
||||
return Response(text = text, voice = voice)
|
||||
|
||||
Command.QA = QA('QA', {}, [])
|
||||
|
@ -72,8 +72,11 @@ class Engine:
|
||||
path = f'{dir}/{Engine.transliterate(text)[:100]}.mp3'
|
||||
if( os.path.exists(path) ): return Speech(text, this._name, path, standart)
|
||||
synthesis_input = texttospeech.SynthesisInput(text=text)
|
||||
response = this._client.synthesize_speech(input = synthesis_input, voice = this._voice, audio_config = this._audio_config)
|
||||
if not os.path.exists(dir): os.makedirs(dir)
|
||||
with open(path, 'wb') as out:
|
||||
out.write(response.audio_content)
|
||||
try:
|
||||
response = this._client.synthesize_speech(input = synthesis_input, voice = this._voice, audio_config = this._audio_config)
|
||||
if not os.path.exists(dir): os.makedirs(dir)
|
||||
with open(path, 'wb') as out:
|
||||
out.write(response.audio_content)
|
||||
except:
|
||||
print("[ERROR] TTS Error: google cloud tts response error. Check Cloud Platform Console")
|
||||
return Speech(text, this._name, path, standart)
|
||||
|
Loading…
Reference in New Issue
Block a user