Skip to content

Commit

Permalink
上传彩蛋
Browse files Browse the repository at this point in the history
1、上传chatgpt彩蛋;
2、修复音频合成过长不播放问题。
  • Loading branch information
xszyou committed May 19, 2023
1 parent 524e4d0 commit 023df4d
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 47 deletions.
34 changes: 34 additions & 0 deletions ai_module/nlp_gpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from revChatGPT.V1 import Chatbot
from core.content_db import Content_Db
from utils import config_util as cfg

def question(cont):
contentdb = Content_Db()
list = contentdb.get_list('all','desc',5)
text = ''
if len(list) > 0:
relist = []
i = len(list)-1
text = '以下是我和机器人的历史对话:'
while i >= 0:
if list[i][0] == 'fay':
text = text + ' 机器人:' + list[i][2]
else:
text = text + ' 我:' + list[i][2]
i -= 1

try:
chatbot = Chatbot(config={
"access_token": cfg.key_gpt_access_token,
"conversation_id": cfg.key_gpt_conversation_id
})

prompt = text + ' 现在想咨询的问题是:'+cont
response = ""
for data in chatbot.ask(
prompt
):
response = data["message"]
return response
except:
return 'gpt当前繁忙,请稍后重试'
80 changes: 42 additions & 38 deletions core/fay_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
from core.content_db import Content_Db
from datetime import datetime
from ai_module import nlp_rasa
from ai_module import nlp_gpt
class FeiFei:
def __init__(self):
pygame.mixer.init()
pygame.init()
self.q_msg = '你叫什么名字?'
self.a_msg = 'hi,我叫菲菲,英文名是fay'
self.mood = 0.0 # 情绪值
Expand Down Expand Up @@ -516,27 +517,27 @@ def __send_audio(self, file_url, say_type):
audio_length = eyed3.load(file_url).info.time_secs #mp3音频长度
# with wave.open(file_url, 'rb') as wav_file: #wav音频长度
# audio_length = wav_file.getnframes() / float(wav_file.getframerate())
if audio_length <= config_util.config["interact"]["maxInteractTime"] or say_type == "script":
if config_util.config["interact"]["playSound"]: # 展板播放
self.__play_sound(file_url)
else:#发送音频给ue和socket
content = {'Topic': 'Unreal', 'Data': {'Key': 'audio', 'Value': os.path.abspath(file_url), 'Time': audio_length, 'Type': say_type}}
wsa_server.get_instance().add_cmd(content)
if self.deviceConnect is not None:
try:
self.deviceConnect.send(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08') # 发送音频开始标志,同时也检查设备是否在线
wavfile = open(os.path.abspath(file_url),'rb')
# if audio_length <= config_util.config["interact"]["maxInteractTime"] or say_type == "script":
if config_util.config["interact"]["playSound"]: # 展板播放
self.__play_sound(file_url)
else:#发送音频给ue和socket
content = {'Topic': 'Unreal', 'Data': {'Key': 'audio', 'Value': os.path.abspath(file_url), 'Time': audio_length, 'Type': say_type}}
wsa_server.get_instance().add_cmd(content)
if self.deviceConnect is not None:
try:
self.deviceConnect.send(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08') # 发送音频开始标志,同时也检查设备是否在线
wavfile = open(os.path.abspath(file_url),'rb')
data = wavfile.read(1024)
total = 0
while data:
total += len(data)
self.deviceConnect.send(data)
data = wavfile.read(1024)
total = 0
while data:
total += len(data)
self.deviceConnect.send(data)
data = wavfile.read(1024)
time.sleep(0.001)
self.deviceConnect.send(b'\x08\x07\x06\x05\x04\x03\x02\x01\x00')# 发送音频结束标志
util.log(1, "远程音频发送完成:{}".format(total))
except socket.error as serr:
util.log(1,"远程音频输入输出设备已经断开:{}".format(serr))
time.sleep(0.001)
self.deviceConnect.send(b'\x08\x07\x06\x05\x04\x03\x02\x01\x00')# 发送音频结束标志
util.log(1, "远程音频发送完成:{}".format(total))
except socket.error as serr:
util.log(1,"远程音频输入输出设备已经断开:{}".format(serr))



Expand Down Expand Up @@ -590,7 +591,7 @@ def __waiting_speaking(self, file_url):
self.last_interact_time = time.time()
self.speaking = False

def send_for_answer(self,msg):
def send_for_answer(self,msg,sendto):
contentdb = Content_Db()
contentdb.add_content('member','send',msg)
answer = self.__get_answer('send', msg)
Expand All @@ -603,24 +604,27 @@ def send_for_answer(self,msg):
util.log(1, '自然语言处理...')
tm = time.time()
cfg.load_config()
if cfg.key_chat_module == 'xfaiui':
text = xf_aiui.question(msg)
elif cfg.key_chat_module == 'yuan':
text = yuan_1_0.question(msg)
elif cfg.key_chat_module == 'chatgpt':
text = chatgpt.question(msg)
elif cfg.key_chat_module == 'rasa':
textlist = nlp_rasa.question(msg)
text = textlist[0]['text']
if sendto == 2:
text = nlp_gpt.question(msg)
else:
if cfg.key_chat_module == 'xfaiui':
text = xf_aiui.question(msg)
elif cfg.key_chat_module == 'yuan':
text = yuan_1_0.question(msg)
elif cfg.key_chat_module == 'chatgpt':
text = chatgpt.question(msg)
elif cfg.key_chat_module == 'rasa':
textlist = nlp_rasa.question(msg)
text = textlist[0]['text']


else:
raise RuntimeError('讯飞key、yuan key、chatgpt key都没有配置!')
util.log(1, '自然语言处理完成. 耗时: {} ms'.format(math.floor((time.time() - tm) * 1000)))
if text == '哎呀,你这么说我也不懂,详细点呗' or text == '':
util.log(1, '[!] 自然语言无语了!')
text = '哎呀,你这么说我也不懂,详细点呗'
# wsa_server.get_web_instance().add_cmd({"panelMsg": ""})
else:
raise RuntimeError('讯飞key、yuan key、chatgpt key都没有配置!')
util.log(1, '自然语言处理完成. 耗时: {} ms'.format(math.floor((time.time() - tm) * 1000)))
if text == '哎呀,你这么说我也不懂,详细点呗' or text == '':
util.log(1, '[!] 自然语言无语了!')
text = '哎呀,你这么说我也不懂,详细点呗'
# wsa_server.get_web_instance().add_cmd({"panelMsg": ""})

except BaseException as e:
print(e)
Expand Down
2 changes: 1 addition & 1 deletion gui/flask_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def api_send():
print(data)
info = json.loads(data)
feiFei = FeiFei()
text = feiFei.send_for_answer(info['msg'])
text = feiFei.send_for_answer(info['msg'],info['sendto'])
return '{"result":"successful","msg":"'+text+'"}'

@__app.route('/api/get-msg', methods=['post'])
Expand Down
9 changes: 5 additions & 4 deletions gui/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ new Vue({
methods: {
// 回车和空格键提交右侧信息
handkeyCode(e) {
if(e.keyCode === 13){
this.send()
if(e.keyCode === 13 && e.keyCode === 18){
this.send(1)
}
},
handleTabsEdit(targetName, action) {
Expand Down Expand Up @@ -452,7 +452,7 @@ new Vue({
type: 'success'
});
},
send() {
send(sendto) {
let _this = this;
let text = _this.send_msg;
if (!text) {
Expand All @@ -474,7 +474,8 @@ new Vue({
_this.send_msg = ''
let url = "http://127.0.0.1:5000/api/send";
let send_data = {
"msg": text
"msg": text,
"sendto" : sendto
};

let xhr = new XMLHttpRequest()
Expand Down
5 changes: 4 additions & 1 deletion gui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ <h2>Fay数字人助理版</h2>
<div class="input-area">
<textarea v-model="send_msg" name="text" id="textarea" placeholder="发送些内容给Fay..."></textarea>
<div class="button-area">
<button id="send-btn" @click="send()">发 送</button>
<button id="send-btn" @click="send(1)">发 送</button>
<!--删除注释打开彩蛋
<button id="send-btn" @click="send(2)" style="margin-left: 25px;">发送gpt</button>
-->
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ simhash
pytz
gevent~=22.10.1
edge_tts~=6.1.3
eyed3
eyed3
#revChatGPT 删除注释打开彩蛋
8 changes: 6 additions & 2 deletions system.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ ms_tts_key=
ms_tts_region=

# 讯飞 情绪分析 服务密钥 https://www.xfyun.cn/service/emotion-analysis/
xf_ltp_app_id=
xf_ltp_api_key=
xf_ltp_app_id=604404c8
xf_ltp_api_key=78d2db9a83cda0b355c76ea791bc74da

#NLP四选一:xfaiui、yuan、chatgpt、rasa(需启动chatglm及rasa,https://m.bilibili.com/video/BV1D14y1f7pr)
chat_module=xfaiui
Expand All @@ -37,3 +37,7 @@ chatgpt_api_key=

#ngrok内网穿透id,远程设备可以通过互联网连接Fay(非必须)http://ngrok.cc
ngrok_cc_id=

#revChatGPT对接(非必须,https://chat.openai.com登录后访问https://chat.openai.com/api/auth/session获取)
gpt_access_token=
gpt_conversation_id=
6 changes: 6 additions & 0 deletions utils/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
key_yuan_1_0_phone = None
key_chatgpt_api_key = None
key_chat_module = None
key_gpt_access_token = None
key_gpt_conversation_id = None

ASR_mode = None
local_asr_ip = None
Expand All @@ -43,6 +45,8 @@ def load_config():
global key_yuan_1_0_phone
global key_chatgpt_api_key
global key_chat_module
global key_gpt_access_token
global key_gpt_conversation_id

global ASR_mode
global local_asr_ip
Expand All @@ -64,6 +68,8 @@ def load_config():
key_yuan_1_0_phone = system_config.get('key', 'yuan_1_0_phone')
key_chatgpt_api_key = system_config.get('key', 'chatgpt_api_key')
key_chat_module = system_config.get('key', 'chat_module')
key_gpt_access_token = system_config.get('key', 'gpt_access_token')
key_gpt_conversation_id = system_config.get('key', 'gpt_conversation_id')

ASR_mode = system_config.get('key', 'ASR_mode')
local_asr_ip = system_config.get('key', 'local_asr_ip')
Expand Down

0 comments on commit 023df4d

Please sign in to comment.