优化,提取工具类
parent
911a83a093
commit
acf4256904
@ -0,0 +1,44 @@
|
||||
package com.supervision.paddlespeech;
|
||||
|
||||
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.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import com.supervision.paddlespeech.dto.req.AsrReqDTO;
|
||||
import com.supervision.paddlespeech.dto.res.AsrResultDTO;
|
||||
import com.supervision.paddlespeech.dto.res.PaddleSpeechResDTO;
|
||||
import com.supervision.util.SpringBeanUtil;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
public class AsrUtil {
|
||||
|
||||
private static final String ASR_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.asr");
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public static String asrTransformByBytes(byte[] bytes) {
|
||||
// 首先编码为base64编码
|
||||
String encode = Base64.encode(bytes);
|
||||
return asrTransformByBytes(encode);
|
||||
}
|
||||
|
||||
public static String asrTransformByBytes(String voiceBase64){
|
||||
// 这里调用Python的接口,将文字转换为语音
|
||||
String post = HttpUtil.post(ASR_URL, JSONUtil.toJsonStr(new AsrReqDTO(voiceBase64)));
|
||||
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("语音转换文字失败");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.supervision.paddlespeech;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import com.supervision.paddlespeech.dto.req.TtsReqDTO;
|
||||
import com.supervision.paddlespeech.dto.res.PaddleSpeechResDTO;
|
||||
import com.supervision.paddlespeech.dto.res.TtsResultDTO;
|
||||
import com.supervision.pojo.vo.ReplyVoiceResVO;
|
||||
import com.supervision.util.SpringBeanUtil;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
public class TtsUtil {
|
||||
|
||||
private static final String TTS_URL = SpringBeanUtil.getBean(Environment.class).getProperty("paddle-speech.tts");
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public static ReplyVoiceResVO ttsTransform(String str) {
|
||||
// 构建
|
||||
String post = HttpUtil.post(TTS_URL, 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("语音转换文字失败");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.paddlespeech.req;
|
||||
package com.supervision.paddlespeech.dto.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.paddlespeech.req;
|
||||
package com.supervision.paddlespeech.dto.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
package com.supervision.paddlespeech.dto.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
package com.supervision.paddlespeech.dto.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.paddlespeech.res;
|
||||
package com.supervision.paddlespeech.dto.res;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,11 +0,0 @@
|
||||
package com.supervision.pojo.rasa.train.intent;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class NluTemplate {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.supervision.rasa;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.rasa.dto.RasaReqDTO;
|
||||
import com.supervision.rasa.dto.RasaResDTO;
|
||||
import com.supervision.util.SpringBeanUtil;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RasaUtil {
|
||||
|
||||
private static final String RASA_URL = SpringBeanUtil.getBean(Environment.class).getProperty("rasa.url");
|
||||
|
||||
public static List<String> talkRasa(String question, String sessionId) {
|
||||
RasaReqDTO rasaReqDTO = new RasaReqDTO();
|
||||
rasaReqDTO.setSender(sessionId);
|
||||
rasaReqDTO.setMessage(question);
|
||||
String post = HttpUtil.post(RASA_URL, JSONUtil.toJsonStr(rasaReqDTO));
|
||||
List<RasaResDTO> list = JSONUtil.toList(post, RasaResDTO.class);
|
||||
return list.stream().map(RasaResDTO::getText).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa;
|
||||
package com.supervision.rasa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa;
|
||||
package com.supervision.rasa.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa.train.intent;
|
||||
package com.supervision.rasa.dto.train;
|
||||
|
||||
import lombok.Data;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa.train.intent;
|
||||
package com.supervision.rasa.dto.train;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa.train.intent;
|
||||
package com.supervision.rasa.dto.train;
|
||||
|
||||
import lombok.Data;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
@ -1,4 +1,4 @@
|
||||
package com.supervision.pojo.rasa.train.intent;
|
||||
package com.supervision.rasa.dto.train;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import lombok.AllArgsConstructor;
|
@ -0,0 +1,14 @@
|
||||
package com.supervision.websocket.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ActionDTO {
|
||||
|
||||
/**
|
||||
* type为枚举,分为2中类型,tool 体格检查工具 ancillary 辅助检查
|
||||
*/
|
||||
private String actionType;
|
||||
|
||||
private String actionId;
|
||||
}
|
@ -1,13 +1,37 @@
|
||||
package com.supervision.websocket.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ApiModel
|
||||
public class SocketMessageDTO {
|
||||
|
||||
@ApiModelProperty("socket链接ID")
|
||||
private String socketId;
|
||||
|
||||
@ApiModelProperty("当前用户ID")
|
||||
private String userId;
|
||||
|
||||
private String data;
|
||||
@ApiModelProperty("文字消息内容")
|
||||
private String textMessage;
|
||||
|
||||
@ApiModelProperty("语音消息内容")
|
||||
private String voiceMessage;
|
||||
|
||||
@ApiModelProperty("若有动作,这是需要执行的动作内容")
|
||||
private ActionDTO action;
|
||||
|
||||
/**
|
||||
* 后端返回给前端时使用,表示该是消息还是action动作,0消息,1动作
|
||||
*/
|
||||
@ApiModelProperty("后端返回给前端时使用,表示该是消息还是action动作,0消息,1动作")
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 表示是消息还是action动作,0语音,1文字
|
||||
*/
|
||||
@ApiModelProperty("前端到后端使用,表示是语音还是文字,0语音,1文字")
|
||||
private Integer messageType;
|
||||
}
|
||||
|
Loading…
Reference in New Issue