diff --git a/cmvr-iot-admin/src/main/resources/application.yml b/cmvr-iot-admin/src/main/resources/application.yml index e1c4f5d..2fb6df3 100644 --- a/cmvr-iot-admin/src/main/resources/application.yml +++ b/cmvr-iot-admin/src/main/resources/application.yml @@ -119,3 +119,4 @@ api: speech-to-text-url: https://aiagentplatform.cmft.com/api/proxy/api/v1/run_app_workflow query-result-url: https://aiagentplatform.cmft.com/api/proxy/api/v1/query_run_app_process bge-vl: http://192.168.1.8:5000/search_similar + generate_advanced_audio: http://192.168.0.19:8000/generate_advanced_audio diff --git a/cmvr-iot-api/cmvr-iot-llm/src/main/java/com/cmvr/llm/config/APIProperties.java b/cmvr-iot-api/cmvr-iot-llm/src/main/java/com/cmvr/llm/config/APIProperties.java index d59da49..13c150f 100644 --- a/cmvr-iot-api/cmvr-iot-llm/src/main/java/com/cmvr/llm/config/APIProperties.java +++ b/cmvr-iot-api/cmvr-iot-llm/src/main/java/com/cmvr/llm/config/APIProperties.java @@ -29,4 +29,9 @@ public class APIProperties { private String queryResultUrl; private String bgeVl; + + /** + * 文本合成语音 + */ + private String generateAdvancedAudio; } diff --git a/cmvr-iot-common/src/main/java/com/cmvr/common/exception/GlobalException.java b/cmvr-iot-common/src/main/java/com/cmvr/common/exception/GlobalException.java index ece1f35..32725bf 100644 --- a/cmvr-iot-common/src/main/java/com/cmvr/common/exception/GlobalException.java +++ b/cmvr-iot-common/src/main/java/com/cmvr/common/exception/GlobalException.java @@ -1,5 +1,7 @@ package com.cmvr.common.exception; +import cn.hutool.core.util.StrUtil; + /** * 全局异常 * @@ -33,6 +35,11 @@ public class GlobalException extends RuntimeException this.message = message; } + public GlobalException(String template, Object... params) { + this.message = StrUtil.format(template, params); + } + + public String getDetailMessage() { return detailMessage; diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/enums/ActionEnum.java b/cmvr-iot-test/src/main/java/com/cmvr/test/enums/ActionEnum.java index 50ab9f0..a2b7da7 100644 --- a/cmvr-iot-test/src/main/java/com/cmvr/test/enums/ActionEnum.java +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/enums/ActionEnum.java @@ -46,6 +46,7 @@ public enum ActionEnum { // 大模型行为 // ---------------获取触控坐标--------------- TOUCH_COORDINATES("LLM", "TOUCH_COORDINATES", "获取触控坐标"), + GENERATE_ADVANCED_AUDIO("LLM", "GENERATE_ADVANCED_AUDIO", "tts语音合成"), // 语音交互 // ---------------语料--------------- diff --git a/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/llm/LLMTTSOperateService.java b/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/llm/LLMTTSOperateService.java new file mode 100644 index 0000000..937cfd9 --- /dev/null +++ b/cmvr-iot-test/src/main/java/com/cmvr/test/flow/runtime/operator/llm/LLMTTSOperateService.java @@ -0,0 +1,82 @@ +package com.cmvr.test.flow.runtime.operator.llm; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.cmvr.common.exception.GlobalException; +import com.cmvr.llm.config.APIProperties; +import com.cmvr.test.enums.ActionEnum; +import com.cmvr.test.flow.runtime.message.TaskNodeExecuteMessage; +import com.cmvr.test.flow.runtime.message.TaskNodeExecuteResult; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@Service +@RequiredArgsConstructor +public class LLMTTSOperateService implements LLMOperateService { + + private final APIProperties apiProperties; + + @Override + public boolean supports(ActionEnum action) { + return action.name().equals("GENERATE_ADVANCED_AUDIO"); + } + + @Override + public TaskNodeExecuteResult execute(TaskNodeExecuteMessage message) { + JSONObject inputParams = message.getInputParams(); + String text = inputParams.getString("text"); + + JSONObject output = new JSONObject(); + + // 1. 构建请求体 + Map body = new HashMap<>(); + body.put("text", text); + body.put("dialect", "普通话"); + body.put("timbre", ""); + body.put("language", "中文"); + body.put("tone", "中性"); + + // 2. 发送请求到语音合成服务 + HttpResponse response = HttpRequest.post(apiProperties.getGenerateAdvancedAudio()) + .header("Content-Type", "application/json") + .body(JSON.toJSONString(body)) + .execute(); + + if (!response.isOk()) { + throw new GlobalException("音频生成失败,状态码: " + response.getStatus()); + } + + // 3. 获取音频数据 + byte[] audioData = response.bodyBytes(); + + // 4. 生成本地文件路径 + String filename = System.currentTimeMillis() + "_" + text + ".wav"; + String saveDir = "C:\\Users\\11158\\Desktop\\iot"; + File dir = new File(saveDir); + if (!dir.exists() && !dir.mkdirs()) { + throw new GlobalException("音频保存目录创建失败: " + saveDir); + } + + File audioFile = FileUtil.writeBytes(audioData, new File(dir, filename)); + + log.info("音频生成成功,保存路径: {}", audioFile.getAbsolutePath()); + + // 5. 预留上传功能 + // String fileUrl = fileUploadService.upload(audioFile); + + // 先返回本地路径 + output.put("localPath", audioFile.getAbsolutePath()); + // output.put("url", fileUrl); // 上传后再返回 url + + return TaskNodeExecuteResult.success(output); + } +} \ No newline at end of file diff --git a/cmvr-iot-vi/src/main/java/com/cmvr/vi/service/impl/ViProjectServiceImpl.java b/cmvr-iot-vi/src/main/java/com/cmvr/vi/service/impl/ViProjectServiceImpl.java index 04a4d6c..4e703c7 100644 --- a/cmvr-iot-vi/src/main/java/com/cmvr/vi/service/impl/ViProjectServiceImpl.java +++ b/cmvr-iot-vi/src/main/java/com/cmvr/vi/service/impl/ViProjectServiceImpl.java @@ -138,7 +138,7 @@ public class ViProjectServiceImpl extends ServiceImpl wrapper = Wrappers.lambdaQuery(TeTaskInst.class); wrapper.eq(TeTaskInst::getTaskId, projectId)