|
@@ -0,0 +1,64 @@
|
|
|
|
+import json
|
|
|
|
+import os
|
|
|
|
+
|
|
|
|
+from Object.ResponseObject import ResponseObject
|
|
|
|
+from django.views import View
|
|
|
|
+
|
|
|
|
+from Object.WsParam.AIChatObject import ChatClient
|
|
|
|
+from Object.WsParam.WsParamRecognizeObject import WsParamRecognize
|
|
|
|
+from Object.WsParam.WsParamSynthesizeObject import WsParamSynthesize
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class WsParamService(View):
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ request_dict = request.GET
|
|
|
|
+ return self.validation(request, request_dict, operation)
|
|
|
|
+
|
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
|
+ request.encoding = 'utf-8'
|
|
|
|
+ operation = kwargs.get('operation')
|
|
|
|
+ request_dict = request.POST
|
|
|
|
+ return self.validation(request, request_dict, operation)
|
|
|
|
+
|
|
|
|
+ def validation(self, request, request_dict, operation):
|
|
|
|
+ language = request_dict.get('language', 'en')
|
|
|
|
+ response = ResponseObject(language)
|
|
|
|
+ if operation == 'smartReply':
|
|
|
|
+ return self.smart_reply(request, request_dict, response)
|
|
|
|
+ else:
|
|
|
|
+ return response.json(414)
|
|
|
|
+
|
|
|
|
+ def smart_reply(self, request, request_dict, response):
|
|
|
|
+ audio = request.FILES.get('audio', None)
|
|
|
|
+ system = request_dict.get('system', None)
|
|
|
|
+ audio_type = request_dict.get('audioType', 'pcm')
|
|
|
|
+ save_directory = 'static/demo_files/'
|
|
|
|
+ if not os.path.exists(save_directory):
|
|
|
|
+ os.makedirs(save_directory)
|
|
|
|
+
|
|
|
|
+ audio_path = os.path.join(save_directory, audio.name)
|
|
|
|
+ with open(audio_path, 'wb') as destination:
|
|
|
|
+ for chunk in audio.chunks():
|
|
|
|
+ destination.write(chunk)
|
|
|
|
+
|
|
|
|
+ APPID = "fcff8f4b"
|
|
|
|
+ APIKey = "037571e7285e64e8dc321fa5b937fea2"
|
|
|
|
+ APISecret = "ZTU3NWMyNTI1MTI4NTU5ZGUxMDZhNmQ5"
|
|
|
|
+ gpt_url = 'wss://spark-api.xf-yun.com/v3.5/chat'
|
|
|
|
+ domain = 'generalv3.5'
|
|
|
|
+ AudioFile = audio_path
|
|
|
|
+
|
|
|
|
+ # 传入语音 -> 转文字 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)
|