添加heygem接口

master
xueqingkun 1 month ago
parent 7dbbd32c74
commit 8f94a6022f

@ -67,22 +67,20 @@
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.15.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

@ -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,17 @@
package com.supervision.ai.service.hub.dto.heygem;
import lombok.Data;
@Data
public class TranResDTO {
/**
* -1 0
*/
private int code;
private String msg;
private String reference_audio_text;
private String asr_format_audio_url;
}

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

@ -16,3 +16,11 @@ mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
heygem:
server:
ip: 192.168.10.96
tts:
port: 18180
f2f:
port: 8383

@ -0,0 +1,66 @@
package com.supervision.ai.service.hub;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.supervision.ai.service.hub.dto.heygem.*;
import com.supervision.ai.service.hub.service.HeygemApiService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@Slf4j
@SpringBootTest
public class HeygemApiServiceTest {
@Autowired
private HeygemApiService heygemApiService;
@Test
public void preprocessAndTranTest() {
TranReqDTO tranReqDTO = new TranReqDTO();
tranReqDTO.setFormat("wav");
tranReqDTO.setLang("zh");
tranReqDTO.setReference_audio("origin_audio/format_20250325102100472.wav");
TranResDTO test = heygemApiService.preprocessAndTran(tranReqDTO);
log.info("preprocessAndTranTest:{}", JSONUtil.toJsonStr(test));
// {"code":0,"msg":"success","reference_audio_text":"爸爸鱼竿女主我爱爸爸爱办法而我爱不如果播放奥特曼二就别把爱来。嗯我看18.5 啊75 ,为啥不是真的想得爱的,是是空与佛,你在干什么?徒儿不是就是穷,不是穷。","asr_format_audio_url":"/code/sessions/20250328/4c8a1e68fdc24e2f99480770b8a5a3d1/format_denoise_format_20250325102100472_part0.wav"}
}
@Test
public void textToAudioTest() {
TextToAudioReqDTO textToAudioReqDTO = new TextToAudioReqDTO();
textToAudioReqDTO.setSpeaker("zs");
textToAudioReqDTO.setText("你好,我是伏小羲");
textToAudioReqDTO.setReference_audio("/code/sessions/20250328/4c8a1e68fdc24e2f99480770b8a5a3d1/format_denoise_format_20250325102100472_part0.wav");
textToAudioReqDTO.setReference_text("爸爸鱼竿女主我爱爸爸爱办法而我爱不如果播放奥特曼二就别把爱来。嗯我看18.5 啊75 ,为啥不是真的想得爱的,是是空与佛,你在干什么?徒儿不是就是穷,不是穷。");
TextToAudioResDTO textToAudioResDTO = heygemApiService.textToAudio(textToAudioReqDTO);
if (StrUtil.equals("0",textToAudioResDTO.getStatusCode())){
String videoPath = "F:\\tmp\\heyGem\\test.wav";
log.info("textToAudioTest:正确响应回音频数据...音频保存位置:{}",videoPath);
FileUtil.writeBytes(textToAudioResDTO.getAudio(), videoPath);
}else {
log.info("textToAudioTest:错误响应回音频数据,详情:{}",JSONUtil.toJsonStr(textToAudioResDTO));
}
}
@Test
public void submitVideoGenerateTaskTest() {
VideoGenerateReqDTO videoGenerateReqDTO = new VideoGenerateReqDTO();
videoGenerateReqDTO.setAudio_url("476154b5-2603-498f-aa6e-28fcbe7472b8_format.wav");// 存放在tmp目录下
videoGenerateReqDTO.setVideo_url("外国男主播.mp4");
videoGenerateReqDTO.setCode("123456789");
Response<Object> objectResponse = heygemApiService.submitVideoGenerateTask(videoGenerateReqDTO);
log.info("submitVideoGenerateTaskTest:{}", JSONUtil.toJsonStr(objectResponse));
}
@Test
public void getTaskStatusTest() {
Response<TaskStatusResDTO> taskStatusResDTOResponse = heygemApiService.queryTaskStatus("123456789");
log.info("getTaskStatusTest:{}", JSONUtil.toJsonStr(taskStatusResDTOResponse));
}
}
Loading…
Cancel
Save