添加heygem接口
parent
7dbbd32c74
commit
8f94a6022f
@ -0,0 +1,130 @@
|
||||
package com.supervision.ai.service.hub.dto.common;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 响应信息主体
|
||||
*
|
||||
* @author qimaoyu
|
||||
*/
|
||||
@Data
|
||||
public class R<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String TOTAL_COUNT = "total";
|
||||
public static final String RESULT_LIST = "result";
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = 200;
|
||||
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = 500;
|
||||
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
|
||||
public static <T> R<T> ok() {
|
||||
return restResult(null, SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> judgeResult(Boolean bo, String successMessage, String failMessage) {
|
||||
if (bo) {
|
||||
return restResult(null, SUCCESS, successMessage);
|
||||
} else {
|
||||
return restResult(null, FAIL, failMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> R<T> okMsg(String msg) {
|
||||
return restResult(null, SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data) {
|
||||
return restResult(data, SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data, String msg) {
|
||||
return restResult(data, SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> fail() {
|
||||
return restResult(null, FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> fail(String msg) {
|
||||
return restResult(null, FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> fail(T data) {
|
||||
return restResult(data, FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> fail(int code, String msg) {
|
||||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static <T> R<T> restResult(T data, int code, String msg) {
|
||||
R<T> apiResult = new R<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
|
||||
public static Map<String, Object> buildDataMap(List list) {
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
if (list == null) {
|
||||
dataMap.put(TOTAL_COUNT, 0);
|
||||
dataMap.put(RESULT_LIST, new ArrayList<>());
|
||||
} else {
|
||||
dataMap.put(TOTAL_COUNT, list.size());
|
||||
dataMap.put(RESULT_LIST, list);
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
public static Map<String, Object> buildDataMap(List list, Long total) {
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put(TOTAL_COUNT, total);
|
||||
dataMap.put(RESULT_LIST, list);
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
public static Map<String, Object> buildDataMap(Set list) {
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
if (list == null) {
|
||||
dataMap.put(TOTAL_COUNT, 0);
|
||||
dataMap.put(RESULT_LIST, new ArrayList<>());
|
||||
} else {
|
||||
dataMap.put(TOTAL_COUNT, list.size());
|
||||
dataMap.put(RESULT_LIST, list);
|
||||
}
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
public static Map<String, Object> buildDataMap(Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
List<Object> resultList = new ArrayList<>();
|
||||
resultList.add(object);
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put(TOTAL_COUNT, resultList.size());
|
||||
dataMap.put(RESULT_LIST, resultList);
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* heygem专用响应信息主体
|
||||
* @param <T>
|
||||
*/
|
||||
@Data
|
||||
public class Response<T> {
|
||||
|
||||
/**
|
||||
* 10000 成功
|
||||
*/
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
private String success;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* heygem专用任务状态响应信息主体
|
||||
*/
|
||||
@Data
|
||||
public class TaskStatusResDTO {
|
||||
|
||||
private String code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private int progress;
|
||||
|
||||
private String result;
|
||||
|
||||
private String status;
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TextToAudioReqDTO {
|
||||
|
||||
/**
|
||||
*一个UUID保持唯一即可
|
||||
*/
|
||||
private String speaker;
|
||||
/**
|
||||
* 需要合成的文本内容
|
||||
*/
|
||||
private String text;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private String format = "wav";
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private double topP = 0.7;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private int max_new_tokens = 1024;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private int chunk_length = 100;
|
||||
/**
|
||||
* 固定传
|
||||
*/
|
||||
private double repetition_penalty = 1.2;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private double temperature = 0.7;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private boolean need_asr = false;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private boolean streaming = false;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private int is_fixed_seed = 0;
|
||||
/**
|
||||
* 固定传参
|
||||
*/
|
||||
private int is_norm = 0;
|
||||
/**
|
||||
* 上一步“模特训练”的返回值
|
||||
*/
|
||||
private String reference_audio;
|
||||
|
||||
/**
|
||||
* 上一步“模特训练”的返回值
|
||||
*/
|
||||
private String reference_text;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TextToAudioResDTO {
|
||||
|
||||
private String statusCode;
|
||||
|
||||
private String message;
|
||||
|
||||
private String error;
|
||||
|
||||
private byte[] audio;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TranReqDTO {
|
||||
|
||||
/**
|
||||
* wav
|
||||
*/
|
||||
private String format;
|
||||
|
||||
|
||||
private String reference_audio;
|
||||
|
||||
/**
|
||||
* 语言 en zh
|
||||
*/
|
||||
private String lang;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.supervision.ai.service.hub.dto.heygem;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class VideoGenerateReqDTO {
|
||||
|
||||
/**
|
||||
* 音频路径
|
||||
*/
|
||||
private String audio_url;
|
||||
/**
|
||||
* 视频路径
|
||||
*/
|
||||
private String video_url;
|
||||
/**
|
||||
* 唯一key
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 固定值
|
||||
*/
|
||||
private int chaofen = 0;
|
||||
/**
|
||||
* 固定值
|
||||
*/
|
||||
private int watermark_switch = 0;
|
||||
/**
|
||||
* 固定值
|
||||
*/
|
||||
private int pn = 1;
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.supervision.ai.service.hub.service;
|
||||
|
||||
import com.supervision.ai.service.hub.dto.heygem.*;
|
||||
|
||||
/**
|
||||
* Heygem接口服务
|
||||
*/
|
||||
public interface HeygemApiService {
|
||||
|
||||
/**
|
||||
* 预训练接口
|
||||
* @param tranReqDTO
|
||||
* @return
|
||||
*/
|
||||
TranResDTO preprocessAndTran(TranReqDTO tranReqDTO);
|
||||
|
||||
/**
|
||||
* 文本转音频
|
||||
* @param textToAudioReqDTO
|
||||
* @return
|
||||
*/
|
||||
TextToAudioResDTO textToAudio(TextToAudioReqDTO textToAudioReqDTO);
|
||||
|
||||
/**
|
||||
* 提交视频生成任务
|
||||
* @param videoGenerateReqDTO 视频生成任务请求
|
||||
* @return 任务提交结果
|
||||
*/
|
||||
Response<Object> submitVideoGenerateTask(VideoGenerateReqDTO videoGenerateReqDTO);
|
||||
|
||||
/**
|
||||
* 查询任务状态
|
||||
* @param taskCode 任务code
|
||||
* @return 任务状态
|
||||
*/
|
||||
Response<TaskStatusResDTO> queryTaskStatus(String taskCode);
|
||||
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
package com.supervision.ai.service.hub.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
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.ai.service.hub.dto.heygem.*;
|
||||
import com.supervision.ai.service.hub.service.HeygemApiService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class HeygemApiServiceImpl implements HeygemApiService {
|
||||
|
||||
@Value("${heygem.server.ip}")
|
||||
private String heygemServerIp;
|
||||
|
||||
@Value("${heygem.tts.port}")
|
||||
private String heygemTtsPort;
|
||||
|
||||
@Value("${heygem.f2f.port}")
|
||||
private String heygemF2fPort;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public TranResDTO preprocessAndTran(TranReqDTO tranReqDTO) {
|
||||
String url = "http://" + heygemServerIp + ":" + heygemTtsPort + "/v1/preprocess_and_tran";
|
||||
log.info("url:{}", url);
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
HttpRequest request = HttpUtil.createPost(url).body(JSONUtil.toJsonStr(tranReqDTO));
|
||||
try (HttpResponse response = request.execute()) {
|
||||
log.info("/v1/preprocess_and_tran请求耗时:{}", timer.intervalMs());
|
||||
if (!response.isOk()){
|
||||
throw new RuntimeException("请求失败");
|
||||
}
|
||||
String body = response.body();
|
||||
log.info("/v1/preprocess_and_tran接口response:{}", body);
|
||||
try {
|
||||
return objectMapper.readValue(body, new TypeReference<>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextToAudioResDTO textToAudio(TextToAudioReqDTO textToAudioReqDTO) {
|
||||
String url = "http://" + heygemServerIp + ":" + heygemTtsPort + "/v1/invoke";
|
||||
log.info("url:{}", url);
|
||||
HttpRequest request = HttpUtil.createPost(url).body(JSONUtil.toJsonStr(textToAudioReqDTO));
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
try (HttpResponse response = request.execute()) {
|
||||
log.info("/v1/invoke请求耗时:{}", timer.intervalMs());
|
||||
if (!response.isOk()&& response.header("Content-Type").equals("application/json")){
|
||||
try {
|
||||
String body = response.body();
|
||||
log.info("/v1/invoke接口response:{}", body);
|
||||
return objectMapper.readValue(response.body(), new TypeReference<>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
if (response.isOk()){
|
||||
byte[] bodyBytes = response.bodyBytes();
|
||||
TextToAudioResDTO textToAudioResDTO = new TextToAudioResDTO();
|
||||
textToAudioResDTO.setStatusCode("0");
|
||||
textToAudioResDTO.setAudio(bodyBytes);
|
||||
return textToAudioResDTO;
|
||||
}
|
||||
throw new RuntimeException("请求失败");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Response<Object> submitVideoGenerateTask(VideoGenerateReqDTO videoGenerateReqDTO) {
|
||||
|
||||
String url = "http://" + heygemServerIp + ":" + heygemF2fPort + "/easy/submit";
|
||||
log.info("url:{}", url);
|
||||
videoGenerateReqDTO.setCode(UUID.fastUUID().toString());
|
||||
HttpRequest request = HttpUtil.createPost(url).body(JSONUtil.toJsonStr(videoGenerateReqDTO));
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
try (HttpResponse response = request.execute()) {
|
||||
log.info("请求/easy/submit耗时:{}", timer.intervalMs());
|
||||
String body = response.body();
|
||||
log.info("/easy/submit接口response:{}", body);
|
||||
try {
|
||||
Response<Object> readValue = objectMapper.readValue(body, new TypeReference<>() {});
|
||||
if (readValue.getCode()==10000){
|
||||
readValue.setMsg(videoGenerateReqDTO.getCode());
|
||||
}
|
||||
return readValue;
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<TaskStatusResDTO> queryTaskStatus(String taskCode) {
|
||||
|
||||
String url = "http://" + heygemServerIp + ":" + heygemF2fPort + "/easy/query";
|
||||
log.info("url:{}", url);
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
try (HttpResponse response = HttpUtil.createGet(url).form("taskCode", taskCode).execute()) {
|
||||
log.info("请求/easy/query耗时:{}", timer.intervalMs());
|
||||
String body = response.body();
|
||||
log.info("/easy/query接口response:{}", body);
|
||||
try {
|
||||
return objectMapper.readValue(body, new TypeReference<>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue