|
@@ -1,9 +1,9 @@
|
|
|
import json
|
|
|
import os
|
|
|
-
|
|
|
+from Ansjer.config import LOGGER
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from django.views import View
|
|
|
-
|
|
|
+import time
|
|
|
from Object.WsParam.AIChatObject import ChatClient
|
|
|
from Object.WsParam.WsParamRecognizeObject import WsParamRecognize
|
|
|
from Object.WsParam.WsParamSynthesizeObject import WsParamSynthesize
|
|
@@ -31,35 +31,50 @@ class WsParamService(View):
|
|
|
return response.json(414)
|
|
|
|
|
|
def smart_reply(self, request, request_dict, response):
|
|
|
- audio = request.FILES.get('audio', None)
|
|
|
- system = request_dict.get('system', None)
|
|
|
- history = request_dict.get('history', None)
|
|
|
- audio_type = request_dict.get('audioType', 'pcm')
|
|
|
- save_directory = 'static/demo_files/'
|
|
|
- if not os.path.exists(save_directory):
|
|
|
- os.makedirs(save_directory)
|
|
|
+ try:
|
|
|
+ audio = request.FILES.get('audio', None)
|
|
|
+ system = request_dict.get('system', None)
|
|
|
+ history = request_dict.get('history', None)
|
|
|
+ audio_type = request_dict.get('audioType', 'pcm')
|
|
|
+ save_directory = 'static/demo_files/'
|
|
|
+ if not os.path.exists(save_directory):
|
|
|
+ os.makedirs(save_directory)
|
|
|
+
|
|
|
+ start = time.time()
|
|
|
+ audio_path = os.path.join(save_directory, audio.name)
|
|
|
+ with open(audio_path, 'wb') as destination:
|
|
|
+ for chunk in audio.chunks():
|
|
|
+ destination.write(chunk)
|
|
|
+ end = time.time()
|
|
|
+ LOGGER.info(f"********smartReply保存文件所需时间为{end-start}秒********")
|
|
|
+ APPID = "fcff8f4b"
|
|
|
+ APIKey = "037571e7285e64e8dc321fa5b937fea2"
|
|
|
+ APISecret = "ZTU3NWMyNTI1MTI4NTU5ZGUxMDZhNmQ5"
|
|
|
+ gpt_url = 'wss://spark-api.xf-yun.com/v3.5/chat'
|
|
|
+ domain = 'generalv3.5'
|
|
|
+ AudioFile = audio_path
|
|
|
|
|
|
- audio_path = os.path.join(save_directory, audio.name)
|
|
|
- with open(audio_path, 'wb') as destination:
|
|
|
- for chunk in audio.chunks():
|
|
|
- destination.write(chunk)
|
|
|
+ start = time.time()
|
|
|
+ # 传入语音 -> 转文字 APPID, APISecret, APIKey
|
|
|
+ wsParamRecognize = WsParamRecognize(APPID, APISecret, APIKey, AudioFile)
|
|
|
+ query = wsParamRecognize.start()
|
|
|
+ end = time.time()
|
|
|
+ LOGGER.info(f"********smartReply语音转文字所需时间为{end - start}秒,内容为{query}********")
|
|
|
|
|
|
- APPID = "fcff8f4b"
|
|
|
- APIKey = "037571e7285e64e8dc321fa5b937fea2"
|
|
|
- APISecret = "ZTU3NWMyNTI1MTI4NTU5ZGUxMDZhNmQ5"
|
|
|
- gpt_url = 'wss://spark-api.xf-yun.com/v3.5/chat'
|
|
|
- domain = 'generalv3.5'
|
|
|
- AudioFile = audio_path
|
|
|
+ start = time.time()
|
|
|
+ # 大语言模型 APPID, APIKey, APISecret, gpt_url, domain, query, history=None, system=None
|
|
|
+ chat = ChatClient(APPID, APIKey, APISecret, gpt_url, domain, query, history, system)
|
|
|
+ answer = chat.start()
|
|
|
+ end = time.time()
|
|
|
+ LOGGER.info(f"********smartReplyAI回复所需时间为{end - start}秒,内容为{answer}********")
|
|
|
|
|
|
- # 传入语音 -> 转文字 APPID, APISecret, APIKey
|
|
|
- wsParamRecognize = WsParamRecognize(APPID, APISecret, APIKey, AudioFile)
|
|
|
- query = wsParamRecognize.start()
|
|
|
- print(query)
|
|
|
- # 大语言模型 APPID, APIKey, APISecret, gpt_url, domain, query, history=None, system=None
|
|
|
- chat = ChatClient(APPID, APIKey, APISecret, gpt_url, domain, query, history, system)
|
|
|
- answer = chat.start()
|
|
|
- print(answer)
|
|
|
- # 文字转编码 self, APPID, APIKey, APISecret, Text, AudioType = "pcm"
|
|
|
- wsParamSynthesize = WsParamSynthesize(APPID, APIKey, APISecret, answer, audio_type)
|
|
|
- answer_base = wsParamSynthesize.start()
|
|
|
- return response.json(0, answer_base)
|
|
|
+ start = time.time()
|
|
|
+ # 文字转编码 self, APPID, APIKey, APISecret, Text, AudioType = "pcm"
|
|
|
+ wsParamSynthesize = WsParamSynthesize(APPID, APIKey, APISecret, answer, audio_type)
|
|
|
+ answer_base = wsParamSynthesize.start()
|
|
|
+ end = time.time()
|
|
|
+ LOGGER.info(f"********文字转编码所需时间为{end - start}秒********")
|
|
|
+ return response.json(0, answer_base)
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error('*****WsParamService.smart_reply:errLine:{}, errMsg:{}'
|
|
|
+ .format(e.__traceback__.tb_lineno, repr(e)))
|