forked from GoatNote/Chatterbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.py
235 lines (190 loc) · 7.18 KB
/
shared.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
import time
import dotenv
import os
import json
import sounddevice as sd
from numpy import concatenate, float32
from scipy.io.wavfile import write
from openai import OpenAI
from assistants import assistants
from io import BytesIO
dotenv.load_dotenv()
openai_api_key = os.getenv("API_KEY")
assistant = "Brian"
print(assistant)
client = OpenAI(api_key=openai_api_key)
def record_audio(is_pressed, wait_for_press, leds=None, led_update=None):
# Sampling frequency
fs = 16000 # Hz
# init list to store all audio data
buffer = []
recording = True
def stop_recording(indata, frames, time, status):
if not is_pressed():
print("Button released.")
nonlocal recording
recording = False
raise sd.CallbackAbort
# write audio data to buffer
buffer.append(indata.copy())
print("\nReady, press and hold to record...")
wait_for_press()
if led_update != None:
led_update(leds[0], "on")
try:
with sd.InputStream(
samplerate=fs,
channels=1,
dtype=float32,
callback=stop_recording,
blocksize=int(fs * 0.1),
):
print("Recording started...")
while recording:
sd.sleep(100)
print("Recording stopped.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
audio_data = concatenate(buffer)
wav_file = BytesIO()
wav_file.name = "audio.wav" # for some dumb reason, the file name is needed for the api to determine the file type
write(wav_file, fs, audio_data)
if led_update != None:
led_update(leds[0], "on")
led_update(leds[1], "blink")
return wav_file
def transcribe_on_press(is_pressed, wait_for_press, leds=None, led_update=None):
audio_wav_file = record_audio(is_pressed, wait_for_press, leds, led_update)
transcription = client.audio.transcriptions.create(
model="whisper-1", language="en", file=audio_wav_file
)
print("\nTranscription of me: ", transcription.text)
if led_update != None:
led_update(leds[1], "on")
led_update(leds[2], "blink")
return transcription.text
def create_thread(transcription):
thread = client.beta.threads.create()
message_thread(thread, transcription)
return thread
def run_thread(thread, leds=None, led_update=None):
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistants[assistant].id,
instructions="", # Working system prompt thing
)
cont = True
rtn = ""
personality = assistant
if run.status == "completed":
print("Run completed.")
messages = client.beta.threads.messages.list(thread_id=thread.id)
response = messages.data[0].content[0].text.value
print("\nResponse: ", response)
rtn = response
elif run.status == "requires_action":
print("Run rquires action.")
tool_outputs = []
for tool in run.required_action.submit_tool_outputs.tool_calls:
print(tool)
if tool.function.name == "switch_assistant":
args = json.loads(tool.function.arguments)
print("Sign off with", args["sign_off"])
print("Switch to", args["assistant"])
personality = args["assistant"]
rtn = args["sign_off"]
cont = False
tool_outputs.append({"tool_call_id": tool.id, "output": "Bye!"})
# if tool.function.name == "make_log":
# args = json.loads(tool.function.arguments)
# print(args)
# rtn=args["assistants_log"]
# tool_outputs.append({"tool_call_id": tool.id, "output": "Thank you for making a log."})
# print("Assitant made following log:", args["assistants_log"])
# print("Writing to file.")
# print("Writen to file.")
# print("Uploading to Assistant.")
print("tool outputs:", tool_outputs)
if tool_outputs:
try:
run = client.beta.threads.runs.submit_tool_outputs_and_poll(
thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs
)
print("Tool outputs submitted successfully.")
except Exception as e:
print("Failed to submit tool outputs:", e)
else:
print("No tool outputs to submit.")
else:
print("Status:", run.status)
else:
print("Status:", run.status)
# print("rtn:",rtn)
print("continue?", cont)
print("speak with:", personality)
return rtn, cont, personality
def message_thread(thread, transcription):
try:
client.beta.threads.messages.create(
thread_id=thread.id, role="user", content=transcription
)
print("\nMessage sent.")
except Exception as e:
print("\nMessage failed:", e)
raise e
def speak(response, leds=None, led_update=None):
import pyaudio
player_stream = pyaudio.PyAudio().open(
format=pyaudio.paInt16,
channels=1,
rate=24000,
output=True,
frames_per_buffer=5000,
)
if led_update != None:
led_update(leds[2], "on")
start_time = time.time()
with client.audio.speech.with_streaming_response.create(
model="tts-1",
voice=assistants[assistant].voice,
response_format="pcm", # similar to WAV, but without a header chunk at the start.
input=response,
) as response:
print(f"Time to first byte: {int((time.time() - start_time) * 1000)}ms")
for chunk in response.iter_bytes(chunk_size=1024):
player_stream.write(chunk)
if led_update != None:
led_update(leds[0], "blink")
led_update(leds[1], "off")
led_update(leds[2], "off")
print(f"Done in {int((time.time() - start_time) * 1000)}ms.")
def run(is_pressed, wait_for_press, leds=None, led_update=None):
global assistant
if led_update != None:
led_update(leds[0], "blink")
# create flag to track the status of the user interaction
topic_running = True
same_thread = False
while topic_running == True:
if same_thread == False:
if led_update != None:
led_update(leds[0], "blink")
led_update(leds[1], "blink")
led_update(leds[2], "blink")
thread = create_thread(
"Thread starting, please say hello so the user knows who you are."
)
else:
try:
transcription = transcribe_on_press(
is_pressed, wait_for_press, leds, led_update
)
except ValueError:
print("Recording failed.")
continue
message_to_thread = message_thread(thread, transcription)
response, same_thread, personality = run_thread(thread)
speak(response, leds, led_update)
# has to be after, otherwise will speak sign-off in wrong voice
assistant = personality