-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
97 lines (83 loc) · 2.59 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
import sys
sys.path.append('.')
sys.path.append('./service')
from datetime import datetime
from playsound import playsound
from queue import Queue
from threading import Thread
import time
from service.asr import load_whisper, transcribe_audio
from service.record import record_audio
from service.llm import chat_stream
from service.tts import to_speech_wav
from service.util import split_sentences
load_whisper()
input_path = './temp/'
print('')
print('======================================')
print('')
# 音频队列
audio_queue = Queue()
def audio_player():
'''
顺序播放队列中的音频
'''
while True:
task = audio_queue.get()
# None 表示结束信号
if task is None:
audio_queue.queue.clear()
break
try:
playsound(task)
# 让每句话之间有间隔
time.sleep(0.5)
except Exception as e:
print(f'无法播放: {e}')
# 开始对话
pre_message = ''
while True:
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
input_speech = input_path + timestamp + '.wav'
record_audio(input_speech)
input, language = transcribe_audio(input_speech)
print('用户: ' + input)
print()
# 开启后台音频播放线程
player_thread = Thread(target=audio_player)
player_thread.start()
# 调用大模型对话
message_queue = Queue()
chat_thread = Thread(target=chat_stream, args=(input, message_queue))
chat_thread.start()
sentence_buffer = ''
print('埃癸斯: ', end='')
while True:
content = message_queue.get()
if content == None:
break
print(content, end='')
sentence_buffer += content
# 检测并处理完整句子
# 不要一次性整段做TTS 会很慢 发现一句就生成一句
sentences = split_sentences(sentence_buffer)
if sentences:
for sentence in sentences[:-1]:
if sentence:
speech_file = to_speech_wav(sentence, language)
audio_queue.put(speech_file)
# 保留未完成的句子
sentence_buffer = sentences[-1]
# 处理剩余缓冲区内容
rest = sentence_buffer.strip()
if rest:
speech_file = to_speech_wav(rest, language)
audio_queue.put(speech_file)
pre_message += rest
# 等待对话结束
chat_thread.join()
# 等待音频播放结束
audio_queue.put(None)
player_thread.join()
print()
print()