到ai智能工牌的录音后上传到minio并保存数据库的逻辑
This commit is contained in:
96
src/test/java/com/rj/audio/SenseVoiceAsrTest.java
Normal file
96
src/test/java/com/rj/audio/SenseVoiceAsrTest.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package com.rj.audio;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class SenseVoiceAsrTest {
|
||||
// Xinference集群地址(替换为你的实际地址)
|
||||
private static final String XINFERENCE_ASR_URL = "http://101.35.52.237:19997/v1/audio/transcriptions";
|
||||
// 已部署的SenseVoiceSmall模型名称(必须与部署时一致)
|
||||
private static final String MODEL_NAME = "SenseVoiceSmall";
|
||||
// 本地测试音频文件路径(需是模型支持的格式,如.wav)
|
||||
private static final String AUDIO_FILE_PATH = "D:\\bCard\\code\\Langchain4j-rj\\output_longyingling.mp3"; // 替换为你的音频文件路径
|
||||
// private static final String AUDIO_FILE_PATH = "http://101.35.52.237:19005/car/20251027_141728_de21b339944c4e43867afc903a511905.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20251102%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251102T012236Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=9ceb9ea5361da2dbf830e5c0ad68c1a0cb73573cf039148225f53457d83e1b47"; // 替换为你的音频文件路径
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1. 初始化RestTemplate
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
try {
|
||||
// 2. 读取音频文件为字节数组
|
||||
byte[] audioData = Files.readAllBytes(Paths.get(AUDIO_FILE_PATH));
|
||||
|
||||
// 3. 构造multipart/form-data请求体
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
// 添加模型参数
|
||||
body.add("model", MODEL_NAME);
|
||||
// 添加音频文件(key固定为"file",文件名可自定义)
|
||||
body.add("file", new ByteArrayResource(audioData) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "output_longyingling.mp3"; // 必须指定文件名(含扩展名)
|
||||
}
|
||||
});
|
||||
// 可选参数:指定语言(如中文"zh-CN",根据模型支持添加)
|
||||
body.add("language", "zh-CN");
|
||||
|
||||
// 4. 设置请求头(multipart/form-data)
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
|
||||
|
||||
// 5. 发送POST请求,获取转录结果
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||
XINFERENCE_ASR_URL,
|
||||
request,
|
||||
String.class
|
||||
);
|
||||
/**
|
||||
* {
|
||||
* "text": "转录文本",
|
||||
* "audio_duration": 13.114, # 音频时长(秒)
|
||||
* "transcription_time": 0.337, # 转录耗时(秒)
|
||||
* "segment_count": 1, # 分段数
|
||||
* "char_count": 50 # 字数(新增)
|
||||
* }
|
||||
*/
|
||||
System.out.println("音频识别结果:" + response.toString());
|
||||
// 6. 处理响应(解析JSON获取文本)
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
String responseBody = response.getBody();
|
||||
// 解析JSON:Xinference返回格式通常为 {"text": "识别结果文本"}
|
||||
// 实际项目中建议使用Jackson/Gson解析,这里简化处理
|
||||
String transcription = extractTextFromJson(responseBody);
|
||||
System.out.println("音频识别结果:" + transcription);
|
||||
} else {
|
||||
System.out.println("请求失败,状态码:" + response.getStatusCode());
|
||||
System.out.println("错误信息:" + response.getBody());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取音频文件失败:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.err.println("API调用失败:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 简易JSON解析(提取"text"字段)
|
||||
private static String extractTextFromJson(String json) {
|
||||
if (json == null || !json.contains("\"text\":")) {
|
||||
return "解析失败,响应:" + json;
|
||||
}
|
||||
int start = json.indexOf("\"text\":") + 7;
|
||||
int end = json.indexOf("}", start);
|
||||
return json.substring(start, end).replace("\"", "").trim();
|
||||
}
|
||||
}
|
||||
59
src/test/java/com/rj/audio/SenseVoiceByXinferenceTest.java
Normal file
59
src/test/java/com/rj/audio/SenseVoiceByXinferenceTest.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.rj.audio;
|
||||
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SenseVoiceByXinferenceTest {
|
||||
// Xinference 集群地址(替换为你的实际地址)
|
||||
private static final String XINFERENCE_URL = "http://192.168.1.39:9997/v1/audio/speech";
|
||||
// 已部署的模型名称
|
||||
private static final String MODEL_NAME = "SenseVoiceSmall";
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1. 初始化 RestTemplate
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
// 2. 构造请求体:指定模型、输入文本
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("model", MODEL_NAME);
|
||||
requestBody.put("input", "这是一段测试SenseVoiceSmall模型的语音合成文本。");
|
||||
// 可选参数:调整语音风格、语速等(根据模型支持的参数添加)
|
||||
requestBody.put("voice", "default"); // 语音风格
|
||||
requestBody.put("speed", 1.0); // 语速(1.0为正常)
|
||||
|
||||
// 3. 设置请求头为 JSON 格式
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
|
||||
|
||||
try {
|
||||
// 4. 发送 POST 请求,获取音频二进制数据
|
||||
ResponseEntity<byte[]> response = restTemplate.postForEntity(
|
||||
XINFERENCE_URL,
|
||||
request,
|
||||
byte[].class
|
||||
);
|
||||
|
||||
// 5. 处理响应:保存音频到本地
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
byte[] audioData = response.getBody();
|
||||
if (audioData != null) {
|
||||
String outputPath = "sense_voice_test.wav";
|
||||
try (FileOutputStream fos = new FileOutputStream(new File(outputPath))) {
|
||||
fos.write(audioData);
|
||||
System.out.println("语音合成成功,已保存到:" + outputPath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("请求失败,状态码:" + response.getStatusCode());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,6 +255,8 @@ public class FaceDetectImageCountTest {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -196,6 +196,8 @@ public class TtsRequestLogShortUrlTest {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -174,6 +174,8 @@ public class VideoSynthesisTempUrlTest {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -149,6 +149,8 @@ public class VideoSynthesisVideoNameTest {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user