|
@@ -5,8 +5,10 @@ from Object.ResponseObject import ResponseObject
|
|
|
from django.views import View
|
|
|
import time
|
|
|
from Object.WsParam.AIChatObject import ChatClient
|
|
|
+from Object.WsParam.AudioProcessorObject import AudioProcessor
|
|
|
from Object.WsParam.WsParamRecognizeObject import WsParamRecognize
|
|
|
from Object.WsParam.WsParamSynthesizeObject import WsParamSynthesize
|
|
|
+from django.http import FileResponse
|
|
|
|
|
|
|
|
|
class WsParamService(View):
|
|
@@ -31,50 +33,66 @@ class WsParamService(View):
|
|
|
return response.json(414)
|
|
|
|
|
|
def smart_reply(self, request, request_dict, response):
|
|
|
+ app_id = "fcff8f4b"
|
|
|
+ api_key = "037571e7285e64e8dc321fa5b937fea2"
|
|
|
+ api_secret = "ZTU3NWMyNTI1MTI4NTU5ZGUxMDZhNmQ5"
|
|
|
+ gpt_url = "wss://spark-api.xf-yun.com/v3.5/chat"
|
|
|
+ domain = "generalv3.5"
|
|
|
try:
|
|
|
- audio = request.FILES.get('audio', None)
|
|
|
system = request_dict.get('system', None)
|
|
|
+ audio = request.FILES.get('audio', None)
|
|
|
history = request_dict.get('history', None)
|
|
|
- audio_type = request_dict.get('audioType', 'pcm')
|
|
|
+ if audio is None:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
save_directory = 'static/demo_files/'
|
|
|
- if not os.path.exists(save_directory):
|
|
|
- os.makedirs(save_directory)
|
|
|
+ os.makedirs(save_directory, exist_ok=True)
|
|
|
|
|
|
- start = time.time()
|
|
|
- audio_path = os.path.join(save_directory, audio.name)
|
|
|
- with open(audio_path, 'wb') as destination:
|
|
|
+ original_audio_path = os.path.join(save_directory, audio.name)
|
|
|
+ with open(original_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
|
|
|
|
|
|
+ # 转码
|
|
|
+ pcm_audio_path = os.path.splitext(original_audio_path)[0] + '.pcm'
|
|
|
+ audio_processor = AudioProcessor()
|
|
|
+ audio_processor.convert_audio(original_audio_path, pcm_audio_path)
|
|
|
+ # 传入语音 -> 转文字 APPID, APISecret, APIKey, AudioFile
|
|
|
start = time.time()
|
|
|
- # 传入语音 -> 转文字 APPID, APISecret, APIKey
|
|
|
- wsParamRecognize = WsParamRecognize(APPID, APISecret, APIKey, AudioFile)
|
|
|
+ audio_file = pcm_audio_path
|
|
|
+ wsParamRecognize = WsParamRecognize(app_id, api_secret, api_key, audio_file)
|
|
|
query = wsParamRecognize.start()
|
|
|
end = time.time()
|
|
|
- LOGGER.info(f"********smartReply语音转文字所需时间为{end - start}秒,内容为{query}********")
|
|
|
+ LOGGER.info(f"********smart_reply语音转文字所需时间为{end - start}秒,内容为{query}********")
|
|
|
+ # 删除文件 pcm_audio_path 和 original_audio_path
|
|
|
+ os.remove(pcm_audio_path)
|
|
|
+ os.remove(original_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)
|
|
|
+ start = time.time()
|
|
|
+ chat = ChatClient(app_id, api_key, api_secret, gpt_url, domain, query, history, system)
|
|
|
answer = chat.start()
|
|
|
end = time.time()
|
|
|
LOGGER.info(f"********smartReplyAI回复所需时间为{end - start}秒,内容为{answer}********")
|
|
|
|
|
|
+ # 文字转音频 APPID, APIKey, APISecret, Text, AudioName="demo"
|
|
|
start = time.time()
|
|
|
- # 文字转编码 self, APPID, APIKey, APISecret, Text, AudioType = "pcm"
|
|
|
- wsParamSynthesize = WsParamSynthesize(APPID, APIKey, APISecret, answer, audio_type)
|
|
|
- answer_base = wsParamSynthesize.start()
|
|
|
+ audio_name = f"{os.path.splitext(audio.name)[0]}_answer"
|
|
|
+ wsParamSynthesize = WsParamSynthesize(app_id, api_key, api_secret, answer, audio_name)
|
|
|
+ wsParamSynthesize.start()
|
|
|
+ answer_audio_path = os.path.splitext(original_audio_path)[0] + '_answer.mp3'
|
|
|
+ g711a_audio_path = os.path.splitext(answer_audio_path)[0] + '.g711a'
|
|
|
+ print(answer_audio_path, g711a_audio_path)
|
|
|
+ # 如果有旧文件就删掉
|
|
|
+ if os.path.exists(g711a_audio_path):
|
|
|
+ os.remove(g711a_audio_path)
|
|
|
+ audio_processor.convert_audio(answer_audio_path, g711a_audio_path)
|
|
|
+ os.remove(answer_audio_path)
|
|
|
end = time.time()
|
|
|
- LOGGER.info(f"********文字转编码所需时间为{end - start}秒********")
|
|
|
- return response.json(0, answer_base)
|
|
|
+ LOGGER.info(f"********smartReply文字转编码所需时间为{end - start}秒********")
|
|
|
+ return FileResponse(open(g711a_audio_path, 'rb'), as_attachment=True,
|
|
|
+ filename=os.path.basename(g711a_audio_path))
|
|
|
except Exception as e:
|
|
|
LOGGER.error('*****WsParamService.smart_reply:errLine:{}, errMsg:{}'
|
|
|
.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|