forked from swordswind/ai_virtual_mate_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (75 loc) · 2.66 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
import signal
import pyaudio
import vosk
from main_sub import *
import keyboard as kb
say_text = ""
def refresh_preference():
global voice_switch, prefer_llm, prefer_tts
while True:
try:
voice_switch = voice_option_menu.get()
prefer_llm = api_option_menu.get()
prefer_tts = tts_option_menu.get()
except:
pass
time.sleep(0.1)
def run_chatweb():
try:
ft.app(target=main, assets_dir="data", view=ft.WEB_BROWSER, port=chatweb_port)
except:
pass
def voice_chat(msg2):
voice_output_label.config(text="")
voice_input_label.config(text=f"{username}: {msg2}")
bot_response = chat_llm(msg2)
voice_output_label.config(text=f"{mate_name}: {bot_response}")
get_tts_play_live2d(bot_response)
def voice_th():
global say_text
model = vosk.Model("data/vosk-model-small-cn-0.22")
rec = vosk.KaldiRecognizer(model, 16000)
p = pyaudio.PyAudio()
stream = None
while True:
try:
if voice_option_menu.get() == "开启":
try:
if not stream:
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True,
frames_per_buffer=8000)
data = stream.read(4000, exception_on_overflow=False)
if rec.AcceptWaveform(data):
say_text = rec.Result()[14:-3].replace(" ", "")
if len(say_text) > 1:
pg.init()
if pg.mixer.music.get_busy():
pass
else:
voice_chat(say_text)
except:
time.sleep(0.01)
else:
time.sleep(0.01)
except:
pass
def switch_voice(event=None):
if voice_switch == "开启":
voice_variable.set("关闭")
elif voice_switch == "关闭":
voice_variable.set("开启")
Thread(target=run_live2d).start()
Thread(target=run_chatweb).start()
Thread(target=voice_th).start()
Thread(target=refresh_preference).start()
try:
kb.add_hotkey(f'alt+{voice_key}', switch_voice)
except:
pass
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
with open('data/db/preference.db', 'w', encoding='utf-8') as file2:
file2.write(f'[实时语音交互]\n{voice_switch}\n\n')
file2.write(f'[对话语言模型]\n{prefer_llm}\n\n')
file2.write(f'[语音合成引擎]\n{prefer_tts}')
os.kill(os.getpid(), signal.SIGTERM)