-
Notifications
You must be signed in to change notification settings - Fork 50
/
assistant.py
317 lines (253 loc) · 10.7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import sys
import json
import wave
import time
import pyttsx3
import torch
import requests
import soundfile
import yaml
import pygame
import pygame.locals
import numpy as np
import pyaudio
import whisper
import logging
import threading
import queue
# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
BACK_COLOR = (0,0,0)
REC_COLOR = (255,0,0)
TEXT_COLOR = (255,255,255)
REC_SIZE = 80
FONT_SIZE = 24
WIDTH = 320
HEIGHT = 240
KWIDTH = 20
KHEIGHT = 6
MAX_TEXT_LEN_DISPLAY = 32
INPUT_DEFAULT_DURATION_SECONDS = 5
INPUT_FORMAT = pyaudio.paInt16
INPUT_CHANNELS = 1
INPUT_RATE = 16000
INPUT_CHUNK = 1024
OLLAMA_REST_HEADERS = {'Content-Type': 'application/json'}
INPUT_CONFIG_PATH ="assistant.yaml"
class Assistant:
def __init__(self):
logging.info("Initializing Assistant")
self.config = self.init_config()
programIcon = pygame.image.load('assistant.png')
self.clock = pygame.time.Clock()
pygame.display.set_icon(programIcon)
pygame.display.set_caption("Assistant")
self.windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
self.font = pygame.font.SysFont(None, FONT_SIZE)
self.audio = pyaudio.PyAudio()
self.tts = pyttsx3.init("nsss");
self.tts.setProperty('rate', self.tts.getProperty('rate') - 20)
try:
self.audio.open(format=INPUT_FORMAT,
channels=INPUT_CHANNELS,
rate=INPUT_RATE,
input=True,
frames_per_buffer=INPUT_CHUNK).close()
except Exception as e:
logging.error(f"Error opening audio stream: {str(e)}")
self.wait_exit()
self.display_message(self.config.messages.loadingModel)
self.model = whisper.load_model(self.config.whisperRecognition.modelPath)
self.context = []
self.text_to_speech(self.config.conversation.greeting)
time.sleep(0.5)
self.display_message(self.config.messages.pressSpace)
def wait_exit(self):
while True:
self.display_message(self.config.messages.noAudioInput)
self.clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.locals.QUIT:
self.shutdown()
def shutdown(self):
logging.info("Shutting down Assistant")
self.audio.terminate()
pygame.quit()
sys.exit()
def init_config(self):
logging.info("Initializing configuration")
class Inst:
pass
with open('assistant.yaml', encoding='utf-8') as data:
configYaml = yaml.safe_load(data)
config = Inst()
config.messages = Inst()
config.messages.loadingModel = configYaml["messages"]["loadingModel"]
config.messages.pressSpace = configYaml["messages"]["pressSpace"]
config.messages.noAudioInput = configYaml["messages"]["noAudioInput"]
config.conversation = Inst()
config.conversation.greeting = configYaml["conversation"]["greeting"]
config.ollama = Inst()
config.ollama.url = configYaml["ollama"]["url"]
config.ollama.model = configYaml["ollama"]["model"]
config.whisperRecognition = Inst()
config.whisperRecognition.modelPath = configYaml["whisperRecognition"]["modelPath"]
config.whisperRecognition.lang = configYaml["whisperRecognition"]["lang"]
return config
def display_rec_start(self):
logging.info("Displaying recording start")
self.windowSurface.fill(BACK_COLOR)
pygame.draw.circle(self.windowSurface, REC_COLOR, (WIDTH/2, HEIGHT/2), REC_SIZE)
pygame.display.flip()
def display_sound_energy(self, energy):
logging.info(f"Displaying sound energy: {energy}")
COL_COUNT = 5
RED_CENTER = 100
FACTOR = 10
MAX_AMPLITUDE = 100
self.windowSurface.fill(BACK_COLOR)
amplitude = int(MAX_AMPLITUDE*energy)
hspace, vspace = 2*KWIDTH, int(KHEIGHT/2)
def rect_coords(x, y):
return (int(x-KWIDTH/2), int(y-KHEIGHT/2),
KWIDTH, KHEIGHT)
for i in range(-int(np.floor(COL_COUNT/2)), int(np.ceil(COL_COUNT/2))):
x, y, count = WIDTH/2+(i*hspace), HEIGHT/2, amplitude-2*abs(i)
mid = int(np.ceil(count/2))
for i in range(0, mid):
offset = i*(KHEIGHT+vspace)
pygame.draw.rect(self.windowSurface, RED_CENTER,
rect_coords(x, y+offset))
#mirror:
pygame.draw.rect(self.windowSurface, RED_CENTER,
rect_coords(x, y-offset))
pygame.display.flip()
def display_message(self, text):
logging.info(f"Displaying message: {text}")
self.windowSurface.fill(BACK_COLOR)
label = self.font.render(text
if (len(text)<MAX_TEXT_LEN_DISPLAY)
else (text[0:MAX_TEXT_LEN_DISPLAY]+"..."),
1,
TEXT_COLOR)
size = label.get_rect()[2:4]
self.windowSurface.blit(label, (WIDTH/2 - size[0]/2, HEIGHT/2 - size[1]/2))
pygame.display.flip()
def waveform_from_mic(self, key = pygame.K_SPACE) -> np.ndarray:
logging.info("Capturing waveform from microphone")
self.display_rec_start()
stream = self.audio.open(format=INPUT_FORMAT,
channels=INPUT_CHANNELS,
rate=INPUT_RATE,
input=True,
frames_per_buffer=INPUT_CHUNK)
frames = []
while True:
pygame.event.pump() # process event queue
pressed = pygame.key.get_pressed()
if pressed[key]:
data = stream.read(INPUT_CHUNK)
frames.append(data)
else:
break
stream.stop_stream()
stream.close()
return np.frombuffer(b''.join(frames), np.int16).astype(np.float32) * (1 / 32768.0)
def speech_to_text(self, waveform):
logging.info("Converting speech to text")
result_queue = queue.Queue()
def transcribe_speech():
try:
logging.info("Starting transcription")
transcript = self.model.transcribe(waveform,
language=self.config.whisperRecognition.lang,
fp16=torch.cuda.is_available())
logging.info("Transcription completed")
text = transcript["text"]
print('\nMe:\n', text.strip())
result_queue.put(text)
except Exception as e:
logging.error(f"An error occurred during transcription: {str(e)}")
result_queue.put("")
transcription_thread = threading.Thread(target=transcribe_speech)
transcription_thread.start()
transcription_thread.join()
return result_queue.get()
def ask_ollama(self, prompt, responseCallback):
logging.info(f"Asking OLLaMa with prompt: {prompt}")
full_prompt = prompt if hasattr(self, "contextSent") else (prompt)
self.contextSent = True
jsonParam = {
"model": self.config.ollama.model,
"stream": True,
"context": self.context,
"prompt": full_prompt
}
try:
response = requests.post(self.config.ollama.url,
json=jsonParam,
headers=OLLAMA_REST_HEADERS,
stream=True,
timeout=30) # Increase the timeout value
response.raise_for_status()
full_response = ""
for line in response.iter_lines():
body = json.loads(line)
token = body.get('response', '')
full_response += token
if 'error' in body:
logging.error(f"Error from OLLaMa: {body['error']}")
responseCallback("Error: " + body['error'])
return
if body.get('done', False) and 'context' in body:
self.context = body['context']
break
responseCallback(full_response.strip())
except requests.exceptions.ReadTimeout as e:
logging.error(f"ReadTimeout occurred while asking OLLaMa: {str(e)}")
responseCallback("Sorry, the request timed out. Please try again.")
except requests.exceptions.RequestException as e:
logging.error(f"An error occurred while asking OLLaMa: {str(e)}")
responseCallback("Sorry, an error occurred. Please try again.")
def text_to_speech(self, text):
logging.info(f"Converting text to speech: {text}")
print('\nAI:\n', text.strip())
def play_speech():
try:
logging.info("Initializing TTS engine")
engine = pyttsx3.init()
# Adjust the speech rate (optional)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 50) # Decrease the rate by 50 units
# Add a short delay before converting text to speech
time.sleep(0.5) # Adjust the delay as needed
logging.info("Converting text to speech")
engine.say(text)
engine.runAndWait()
logging.info("Speech playback completed")
except Exception as e:
logging.error(f"An error occurred during speech playback: {str(e)}")
speech_thread = threading.Thread(target=play_speech)
speech_thread.start()
def main():
logging.info("Starting Assistant")
pygame.init()
ass = Assistant()
push_to_talk_key = pygame.K_SPACE
quit_key = pygame.K_ESCAPE
while True:
ass.clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == push_to_talk_key:
logging.info("Push-to-talk key pressed")
speech = ass.waveform_from_mic(push_to_talk_key)
transcription = ass.speech_to_text(waveform=speech)
ass.ask_ollama(transcription, ass.text_to_speech)
time.sleep(1)
ass.display_message(ass.config.messages.pressSpace)
elif event.key == quit_key:
logging.info("Quit key pressed")
ass.shutdown()
if __name__ == "__main__":
main()