-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistant.py
57 lines (48 loc) · 1.87 KB
/
assistant.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
from audio import Audio
from record import Recorder
from deepgramCommunication import DeepgramAssistant
from groqCommunication import GroqAssistant
import logging
logger = logging.getLogger(__name__)
class Assistant:
def __init__(self, config, regenerateGreeting, DEBUG=False):
self.DEBUG = DEBUG
self.greeting = config['greeting']
self.deepgramAssistant = DeepgramAssistant(voice=config['voice'])
if regenerateGreeting:
self.deepgramAssistant.speak(self.greeting, './greet.wav')
self.audio = Audio()
self.rec = Recorder(config['device'])
self.groqAssistant = GroqAssistant(self.DEBUG)
def __del__(self):
del self.audio
del self.rec
def greet(self):
self.audio.play('./greet.wav')
def record_audio(self, filename='./output.wav'):
self.rec.record(filename=filename)
return filename
def transcribe_audio(self, filename):
response = self.deepgramAssistant.listen(filename=filename)
chatMessage = response.results.channels[0].alternatives[0].transcript
if self.DEBUG:
print(chatMessage)
return chatMessage
def process_message(self, chatMessage):
assistantMessage = self.groqAssistant.chat(chatMessage)
if self.DEBUG:
print(assistantMessage)
return assistantMessage
def speak_response(self, assistantMessage):
self.deepgramAssistant.speak(assistantMessage)
self.audio.play('./talk.wav')
def mainLoop(self):
try:
self.audio.play('./assets/ready.wav')
filename = self.record_audio()
chatMessage = self.transcribe_audio(filename)
assistantMessage = self.process_message(chatMessage)
self.speak_response(assistantMessage)
except Exception as e:
logger.exception(e)
print(e)