耗时显示

main
yaxin 2 months ago
parent 59772a9b9a
commit a1fea304da

@ -59,11 +59,6 @@
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>5.8.26</version> <version>5.8.26</version>
</dependency> </dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>9.0.83</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -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);
} }
} }

@ -41,8 +41,8 @@ public class DifyApiUtil {
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) { if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
final HttpEntity responseEntity = response.getEntity(); final HttpEntity responseEntity = response.getEntity();
String responseStr = EntityUtils.toString(responseEntity); String responseStr = EntityUtils.toString(responseEntity);
log.info("DIFY返回{}", responseStr);
JSONObject jsonObject = JSON.parseObject(responseStr); JSONObject jsonObject = JSON.parseObject(responseStr);
log.info("DIFY返回{}", jsonObject.getJSONObject("data").getJSONObject("outputs").get("answerText").toString());
return jsonObject.getJSONObject("data").getJSONObject("outputs").get("answerText").toString(); return jsonObject.getJSONObject("data").getJSONObject("outputs").get("answerText").toString();
} else { } else {
log.info("responseEntity {}", EntityUtils.toString(response.getEntity())); log.info("responseEntity {}", EntityUtils.toString(response.getEntity()));

@ -9,8 +9,9 @@ import com.supervision.dto.paddlespeech.req.TtsReqDTO;
import com.supervision.dto.paddlespeech.res.PaddleSpeechResDTO; import com.supervision.dto.paddlespeech.res.PaddleSpeechResDTO;
import com.supervision.dto.paddlespeech.res.TtsResultDTO; import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.exception.BusinessException; import com.supervision.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@Slf4j
public class TtsUtil { public class TtsUtil {
private static final String TTS_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.tts"); private static final String TTS_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.tts");
@ -23,6 +24,7 @@ public class TtsUtil {
try { try {
PaddleSpeechResDTO<TtsResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<TtsResultDTO>>() { PaddleSpeechResDTO<TtsResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<TtsResultDTO>>() {
}); });
log.info("response:{}", response.getResult());
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) { if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("文字转换语音失败"); throw new BusinessException("文字转换语音失败");
} }

@ -19,7 +19,7 @@ class SpeechDemoServiceApplicationTests {
@Test @Test
void testTtsTransform() { void testTtsTransform() {
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform("你好,我是小爱同学"); TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform("你好,我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学");
System.out.println(JSONUtil.toJsonStr(ttsResultDTO)); System.out.println(JSONUtil.toJsonStr(ttsResultDTO));
// https://www.toolfk.com/zh-cn/tools/base64-to-audio.html base64转音频 // https://www.toolfk.com/zh-cn/tools/base64-to-audio.html base64转音频

Loading…
Cancel
Save