diff --git a/pom.xml b/pom.xml
index ec6cd2d..aec6e8e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,11 +59,6 @@
hutool-all
5.8.26
-
- org.mortbay.jetty
- servlet-api
- 9.0.83
-
diff --git a/src/main/java/com/supervision/service/impl/ChatServiceImpl.java b/src/main/java/com/supervision/service/impl/ChatServiceImpl.java
index 71da4f5..fa63613 100644
--- a/src/main/java/com/supervision/service/impl/ChatServiceImpl.java
+++ b/src/main/java/com/supervision/service/impl/ChatServiceImpl.java
@@ -1,7 +1,6 @@
package com.supervision.service.impl;
import cn.hutool.core.codec.Base64;
-import com.alibaba.fastjson.JSON;
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.dto.robot.AnswerInfo;
import com.supervision.dto.robot.AskInfo;
@@ -16,6 +15,7 @@ import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
+import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@@ -30,42 +30,48 @@ public class ChatServiceImpl implements IChatService {
@Resource
private DifyApiUtil difyApiUtil;
- Map voiceCache = new HashMap<>();
-
+ Map voiceCache = new HashMap<>();
@Override
public RobotTalkDTO talk(MultipartFile file) {
- String talkResult = "";
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder().sessionId("111");
+
try {
- byte[] bytes = file.getBytes();
+ byte[] bytes = file.getBytes();
+ StopWatch stopWatch = new StopWatch();
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
difyChatReqVO.setUser("admin");
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
+ stopWatch.start("stt");
inputs.setQuery(AsrUtil.asrTransformByBytes(bytes));
+ stopWatch.stop();
String askUUid = UUID.randomUUID().toString();
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);
+ stopWatch.start("dify");
String response = difyApiUtil.chat(difyChatReqVO);
+ stopWatch.stop();
log.info("response:{}", response);
-
+ stopWatch.start("tts");
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(response);
- log.info("ttsResultDTO:{}", JSON.toJSONString(ttsResultDTO));
+ stopWatch.stop();
String voiceBaseId = UUID.randomUUID().toString();
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) {
throw new RuntimeException(e);
}
+
return builder.build();
}
@Override
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
- log.info("audioId:{}",audioId);
- Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(),false);
+ log.info("audioId:{}", audioId);
+ Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(), false);
}
}
diff --git a/src/main/java/com/supervision/util/DifyApiUtil.java b/src/main/java/com/supervision/util/DifyApiUtil.java
index 689d7d1..db3fc44 100644
--- a/src/main/java/com/supervision/util/DifyApiUtil.java
+++ b/src/main/java/com/supervision/util/DifyApiUtil.java
@@ -41,8 +41,8 @@ public class DifyApiUtil {
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
final HttpEntity responseEntity = response.getEntity();
String responseStr = EntityUtils.toString(responseEntity);
- log.info("DIFY返回:{}", 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();
} else {
log.info("responseEntity {}", EntityUtils.toString(response.getEntity()));
diff --git a/src/main/java/com/supervision/util/TtsUtil.java b/src/main/java/com/supervision/util/TtsUtil.java
index 872c8c3..f3a3110 100644
--- a/src/main/java/com/supervision/util/TtsUtil.java
+++ b/src/main/java/com/supervision/util/TtsUtil.java
@@ -9,8 +9,9 @@ import com.supervision.dto.paddlespeech.req.TtsReqDTO;
import com.supervision.dto.paddlespeech.res.PaddleSpeechResDTO;
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.exception.BusinessException;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
-
+@Slf4j
public class TtsUtil {
private static final String TTS_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.tts");
@@ -23,6 +24,7 @@ public class TtsUtil {
try {
PaddleSpeechResDTO response = objectMapper.readValue(post, new TypeReference>() {
});
+ log.info("response:{}", response.getResult());
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("文字转换语音失败");
}
diff --git a/src/test/java/com/supervision/SpeechDemoServiceApplicationTests.java b/src/test/java/com/supervision/SpeechDemoServiceApplicationTests.java
index 1670517..dbda08e 100644
--- a/src/test/java/com/supervision/SpeechDemoServiceApplicationTests.java
+++ b/src/test/java/com/supervision/SpeechDemoServiceApplicationTests.java
@@ -19,7 +19,7 @@ class SpeechDemoServiceApplicationTests {
@Test
void testTtsTransform() {
- TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform("你好,我是小爱同学");
+ TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform("你好,我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学我是小爱同学");
System.out.println(JSONUtil.toJsonStr(ttsResultDTO));
// https://www.toolfk.com/zh-cn/tools/base64-to-audio.html base64转音频