forked from VMIJUNV/linux-gpt-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
110 lines (88 loc) · 2.94 KB
/
demo.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
import snowboydecoder
import sys
import signal
import getsound
import os
import time
import tts
import asr
import gpt
interrupted = False
model =['resources/javs.pmdl','resources/new.pmdl']
os.close(sys.stderr.fileno())
callbacks = [lambda: callbacks1(),
lambda: callbacks2()]
def callbacks1():
msg=[{"role": "system", "content": "你说话的语气萌萌哒,用词十分可爱"}]
detector.terminate()
print("检测成功!")
os.system('aplay -D default sound/hi.wav')
try:
getsound.rec()
qu=asr.getasr('sound/question.wav')
print('问:'+qu)
msg.append({"role": "user", "content": qu})
if qu!='':
an=gpt.getgpt(msg)
print('答:'+an)
tts.gettts(an)
os.system('aplay -D default sound/result.wav')
except:
os.system('aplay -D default sound/error.wav')
print("发生错误")
os.system('aplay -D default sound/bye.wav')
print("结束对话!")
print('检测中...')
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)
def callbacks2():
msg=[{"role": "system", "content": "你说话的语气萌萌哒,用词十分可爱"}]
detector.terminate()
print("检测成功!")
os.system('aplay -D default sound/start.wav')
try:
getsound.rec()
qu=asr.getasr('sound/question.wav')
print('输入:'+qu)
if '我爱你' in qu:
os.system('aplay -D default sound/right.wav')
for i in range(5):
getsound.rec()
qu=asr.getasr('sound/question.wav')
print('问:'+qu)
msg.append({"role": "user", "content": qu})
if '再见' in qu: break
if qu=='':break
an=gpt.getgpt(msg)
msg.append({"role": "assistant", "content": an})
print('答:'+an)
tts.gettts(an)
os.system('aplay -D default sound/result.wav')
else:
os.system('aplay -D default sound/wrong.wav')
print("密码错误")
except:
os.system('aplay -D default sound/error.wav')
print("发生错误")
os.system('aplay -D default sound/bye.wav')
print("结束对话!")
print('检测中...')
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)
def signal_handler(signal, frame):
global interrupted
interrupted = True
def interrupt_callback():
global interrupted
return interrupted
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
# main loop
print('检测中...')
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)
detector.terminate()