|
|
@ -1,7 +1,6 @@
|
|
|
|
package com.supervision.service.impl;
|
|
|
|
package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
import com.supervision.dto.robot.AnswerInfo;
|
|
|
|
import com.supervision.dto.robot.AnswerInfo;
|
|
|
|
import com.supervision.dto.robot.AskInfo;
|
|
|
|
import com.supervision.dto.robot.AskInfo;
|
|
|
@ -16,6 +15,7 @@ import jakarta.annotation.Resource;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
@ -30,42 +30,48 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
private DifyApiUtil difyApiUtil;
|
|
|
|
private DifyApiUtil difyApiUtil;
|
|
|
|
|
|
|
|
|
|
|
|
Map<String,String> voiceCache = new HashMap<>();
|
|
|
|
Map<String, String> voiceCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public RobotTalkDTO talk(MultipartFile file) {
|
|
|
|
public RobotTalkDTO talk(MultipartFile file) {
|
|
|
|
String talkResult = "";
|
|
|
|
|
|
|
|
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder().sessionId("111");
|
|
|
|
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder().sessionId("111");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
byte[] bytes = file.getBytes();
|
|
|
|
byte[] bytes = file.getBytes();
|
|
|
|
|
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
|
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
|
|
|
|
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
|
|
|
|
difyChatReqVO.setUser("admin");
|
|
|
|
difyChatReqVO.setUser("admin");
|
|
|
|
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
|
|
|
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
|
|
|
|
|
|
|
stopWatch.start("stt");
|
|
|
|
inputs.setQuery(AsrUtil.asrTransformByBytes(bytes));
|
|
|
|
inputs.setQuery(AsrUtil.asrTransformByBytes(bytes));
|
|
|
|
|
|
|
|
stopWatch.stop();
|
|
|
|
String askUUid = UUID.randomUUID().toString();
|
|
|
|
String askUUid = UUID.randomUUID().toString();
|
|
|
|
builder.askInfo(AskInfo.builder().contentType(2).message(inputs.getQuery()).audioLength(100L).askId(askUUid).build());
|
|
|
|
builder.askInfo(AskInfo.builder().contentType(2).message(inputs.getQuery()).audioLength(100L).askId(askUUid).build());
|
|
|
|
voiceCache.put(askUUid,Base64.encode(bytes));
|
|
|
|
voiceCache.put(askUUid, Base64.encode(bytes));
|
|
|
|
difyChatReqVO.setInputs(inputs);
|
|
|
|
difyChatReqVO.setInputs(inputs);
|
|
|
|
|
|
|
|
stopWatch.start("dify");
|
|
|
|
String response = difyApiUtil.chat(difyChatReqVO);
|
|
|
|
String response = difyApiUtil.chat(difyChatReqVO);
|
|
|
|
|
|
|
|
stopWatch.stop();
|
|
|
|
log.info("response:{}", response);
|
|
|
|
log.info("response:{}", response);
|
|
|
|
|
|
|
|
stopWatch.start("tts");
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(response);
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(response);
|
|
|
|
log.info("ttsResultDTO:{}", JSON.toJSONString(ttsResultDTO));
|
|
|
|
stopWatch.stop();
|
|
|
|
String voiceBaseId = UUID.randomUUID().toString();
|
|
|
|
String voiceBaseId = UUID.randomUUID().toString();
|
|
|
|
builder.answerInfo(AnswerInfo.builder().contentType(2).message(response).voiceBaseId(voiceBaseId).voiceBase64(ttsResultDTO.getAudio()).build());
|
|
|
|
builder.answerInfo(AnswerInfo.builder().contentType(2).message(response).voiceBaseId(voiceBaseId).voiceBase64(ttsResultDTO.getAudio()).build());
|
|
|
|
voiceCache.put(voiceBaseId,ttsResultDTO.getAudio());
|
|
|
|
voiceCache.put(voiceBaseId, ttsResultDTO.getAudio());
|
|
|
|
|
|
|
|
log.info("耗时:{}", stopWatch.prettyPrint());
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
|
|
|
|
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
log.info("audioId:{}",audioId);
|
|
|
|
log.info("audioId:{}", audioId);
|
|
|
|
Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(),false);
|
|
|
|
Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|