forked from zhangbincheng1997/chatbot-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (67 loc) · 2.19 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
1. 唤醒 √
2. 响应 √
3. 输入 √
4. 理解 √
5. 反馈 √
"""
from baidu import Baidu
from tuling import TuLing
from netease import NetEase
from snowboy import snowboydecoder
import util
import signal
import os
class ChatBot:
def __init__(self):
self.interrupted = False
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, self.signal_handler)
print('Listening... Press Ctrl+C to exit')
self.baidu = Baidu()
self.tuling = TuLing()
self.netease = NetEase()
def signal_handler(self, signal, frame):
self.interrupted = True
def interrupt_callback(self):
return self.interrupted
def chat(self):
# ding
util.audio_play('snowboy/resources/ding.wav')
# 录制音频
util.audio_record('audio/audio.wav')
# 语音识别
response = self.baidu.asr('audio/audio.wav')
# dong
util.audio_play('snowboy/resources/dong.wav')
if response['err_no'] == 0:
question = response['result'][0]
print('Q: ' + question)
# 比较粗糙的实现......
if question.find('播放') == -1:
# 智能问答
answer = self.tuling.tuling(question)
print('A: ' + answer)
# 语音合成
self.baidu.synthesis(answer, 'audio/audio.mp3')
# 播放音频
util.audio_play('audio/audio.mp3')
else:
# 下载歌曲
song_name = question[2:]
self.netease.download_song(song_name, 'audio/song.mp3')
# 播放音频
util.audio_play('audio/song.mp3')
else:
print('%d: %s' % (response['err_no'], response['err_msg']))
if __name__ == '__main__':
model = 'snowboy/resources/xiaoqi.pmdl'
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
chatbot = ChatBot()
# main loop
detector.start(detected_callback=chatbot.chat,
interrupt_check=chatbot.interrupt_callback,
sleep_time=0.03)
detector.terminate()