-
Notifications
You must be signed in to change notification settings - Fork 209
/
main.py
170 lines (147 loc) · 5.82 KB
/
main.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
import time
import wave
import queue
import struct
import threading
import subprocess
import pyaudio
import whisper
from langchain.prompts import PromptTemplate
from langchain_community.llms import LlamaCpp
from langchain.callbacks.base import BaseCallbackHandler, BaseCallbackManager
LANG = "EN" # CN for Chinese, EN for English
DEBUG = True
# Model Configuration
WHISP_PATH = "models/whisper-large-v3"
MODEL_PATH = "models/yi-34b-chat.Q8_0.gguf" # Or models/yi-chat-6b.Q8_0.gguf
# Recording Configuration
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
SILENCE_THRESHOLD = 500
SILENT_CHUNKS = 2 * RATE / CHUNK # two seconds of silence marks the end of user voice input
MIC_IDX = 0 # Set microphone id. Use tools/list_microphones.py to see a device list.
def compute_rms(data):
# Assuming data is in 16-bit samples
format = "<{}h".format(len(data) // 2)
ints = struct.unpack(format, data)
# Calculate RMS
sum_squares = sum(i ** 2 for i in ints)
rms = (sum_squares / len(ints)) ** 0.5
return rms
def record_audio():
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, input_device_index=MIC_IDX, frames_per_buffer=CHUNK)
silent_chunks = 0
audio_started = False
frames = []
while True:
data = stream.read(CHUNK)
frames.append(data)
rms = compute_rms(data)
if audio_started:
if rms < SILENCE_THRESHOLD:
silent_chunks += 1
if silent_chunks > SILENT_CHUNKS:
break
else:
silent_chunks = 0
elif rms >= SILENCE_THRESHOLD:
audio_started = True
stream.stop_stream()
stream.close()
audio.terminate()
# save audio to a WAV file
with wave.open('recordings/output.wav', 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
class VoiceOutputCallbackHandler(BaseCallbackHandler):
def __init__(self):
self.generated_text = ""
self.lock = threading.Lock()
self.speech_queue = queue.Queue()
self.worker_thread = threading.Thread(target=self.process_queue)
self.worker_thread.daemon = True
self.worker_thread.start()
self.tts_busy = False
def on_llm_new_token(self, token, **kwargs):
# Append the token to the generated text
with self.lock:
self.generated_text += token
# Check if the token is the end of a sentence
if token in ['.', '。', '!', '!', '?', '?']:
with self.lock:
# Put the complete sentence in the queue
self.speech_queue.put(self.generated_text)
self.generated_text = ""
def process_queue(self):
while True:
# Wait for the next sentence
text = self.speech_queue.get()
if text is None:
self.tts_busy = False
continue
self.tts_busy = True
self.text_to_speech(text)
self.speech_queue.task_done()
if self.speech_queue.empty():
self.tts_busy = False
def text_to_speech(self, text):
try:
if LANG == "CN":
subprocess.call(["say", "-r", "200", "-v", "TingTing", text])
else:
subprocess.call(["say", "-r", "180", "-v", "Karen", text])
except Exception as e:
print(f"Error in text-to-speech: {e}")
if __name__ == '__main__':
if LANG == "CN":
prompt_path = "prompts/example-cn.txt"
else:
prompt_path = "prompts/example-en.txt"
with open(prompt_path, 'r', encoding='utf-8') as file:
template = file.read().strip() # {dialogue}
prompt_template = PromptTemplate(template=template, input_variables=["dialogue"])
# Create an instance of the VoiceOutputCallbackHandler
voice_output_handler = VoiceOutputCallbackHandler()
# Create a callback manager with the voice output handler
callback_manager = BaseCallbackManager(handlers=[voice_output_handler])
llm = LlamaCpp(
model_path=MODEL_PATH,
n_gpu_layers=1, # Metal set to 1 is enough.
n_batch=512, # Should be between 1 and n_ctx, consider the amount of RAM of your Apple Silicon Chip.
n_ctx=4096, # Update the context window size to 4096
f16_kv=True, # MUST set to True, otherwise you will run into problem after a couple of calls
callback_manager=callback_manager,
stop=["<|im_end|>"],
verbose=False,
)
dialogue = ""
try:
while True:
if voice_output_handler.tts_busy: # Check if TTS is busy
continue # Skip to the next iteration if TTS is busy
try:
print("Listening...")
record_audio()
print("Transcribing...")
time_ckpt = time.time()
user_input = whisper.transcribe("recordings/output.wav", path_or_hf_repo=WHISP_PATH)["text"]
print("%s: %s (Time %d ms)" % ("Guest", user_input, (time.time() - time_ckpt) * 1000))
except subprocess.CalledProcessError:
print("voice recognition failed, please try again")
continue
time_ckpt = time.time()
print("Generating...")
dialogue += "*Q* {}\n".format(user_input)
prompt = prompt_template.format(dialogue=dialogue)
reply = llm(prompt, max_tokens=4096)
if reply is not None:
voice_output_handler.speech_queue.put(None)
dialogue += "*A* {}\n".format(reply)
print("%s: %s (Time %d ms)" % ("Server", reply.strip(), (time.time() - time_ckpt) * 1000))
except KeyboardInterrupt:
pass