You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virtual-patient/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java

105 lines
3.8 KiB
Java

2 years ago
package com.supervision.service.impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.supervision.exception.BusinessException;
import com.supervision.pojo.paddlespeech.req.AsrReqDTO;
import com.supervision.pojo.paddlespeech.req.TtsReqDTO;
import com.supervision.pojo.paddlespeech.res.AsrResultDTO;
import com.supervision.pojo.paddlespeech.res.PaddleSpeechResDTO;
import com.supervision.pojo.paddlespeech.res.TtsResultDTO;
import com.supervision.pojo.rasa.RasaReqDTO;
import com.supervision.pojo.rasa.RasaResDTO;
import com.supervision.pojo.vo.ReplyVoiceResVO;
2 years ago
import com.supervision.service.AskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
2 years ago
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
2 years ago
@Service
@RequiredArgsConstructor
2 years ago
public class AskServiceImpl implements AskService {
@Value("${paddle-speech.tts}")
private String tts;
@Value("${paddle-speech.asr}")
private String asr;
@Value("${rasa.url}")
private String rasa;
private final ObjectMapper objectMapper = new ObjectMapper();
2 years ago
@Override
public String receiveVoiceFile(MultipartFile file) throws IOException {
return asrTransform(file.getBytes());
}
@Override
public ReplyVoiceResVO replyVoice() {
String text = "测试:这是文字转语音的测试,测试是否OK";
return ttsTransform(text);
}
private String asrTransform(byte[] bytes) {
2 years ago
// 首先编码为base64编码
String encode = Base64.encode(bytes);
// 这里调用Python的接口,将文字转换为语音
String post = HttpUtil.post(asr, JSONUtil.toJsonStr(new AsrReqDTO(encode)));
try {
PaddleSpeechResDTO<AsrResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<AsrResultDTO>>() {});
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("语音转换文字失败");
}
return response.getResult().getTranscription();
}catch (Exception e){
throw new BusinessException("语音转换文字失败");
}
}
2 years ago
private ReplyVoiceResVO ttsTransform(String str) {
// 构建
String post = HttpUtil.post(tts, JSONUtil.toJsonStr(new TtsReqDTO(str)));
try {
PaddleSpeechResDTO<TtsResultDTO> response = objectMapper.readValue(post, new TypeReference<PaddleSpeechResDTO<TtsResultDTO>>() {
});
if (!response.getSuccess() || ObjectUtil.isEmpty(response.getResult())) {
throw new BusinessException("文字转换语音失败");
}
ReplyVoiceResVO resVO = new ReplyVoiceResVO();
resVO.setVoice(response.getResult().getAudio());
resVO.setText(str);
return resVO;
}catch (Exception e){
throw new BusinessException("语音转换文字失败");
}
2 years ago
}
@Override
public List<String> conversation(String question) {
RasaReqDTO rasaReqDTO = new RasaReqDTO();
rasaReqDTO.setSender("tester1");
rasaReqDTO.setMessage(question);
String post = HttpUtil.post(rasa, JSONUtil.toJsonStr(rasaReqDTO));
List<RasaResDTO> list = JSONUtil.toList(post, RasaResDTO.class);
return list.stream().map(RasaResDTO::getText).collect(Collectors.toList());
}
2 years ago
}