Commit 06c2aa48 authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent 16a414d7
...@@ -83,9 +83,6 @@ ...@@ -83,9 +83,6 @@
</profiles> </profiles>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.mortals.xhx</groupId> <groupId>com.mortals.xhx</groupId>
<artifactId>common-lib</artifactId> <artifactId>common-lib</artifactId>
......
...@@ -32,6 +32,8 @@ import java.util.concurrent.Executors; ...@@ -32,6 +32,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.mortals.xhx.base.framework.exception.ExceptionHandle.*;
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("audio") @RequestMapping("audio")
...@@ -88,33 +90,21 @@ public class ApiSendMsgController { ...@@ -88,33 +90,21 @@ public class ApiSendMsgController {
return emitter; return emitter;
} }
@RequestMapping(value = "upload", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @RequestMapping(value = "upload")
@UnAuth @UnAuth
public SseEmitter doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "") String prePath) { public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "") String prePath) {
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = new HashMap<>();
String jsonStr = ""; String jsonStr = "";
SseEmitter emitter = new SseEmitter(10 * 60 * 1000L);
try { try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!"); if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename()))) if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm文件!"); throw new AppException("只支持pcm文件!");
//if (file.getOriginalFilename()) //if (file.getOriginalFilename())
String filePath = uploadService.saveFileUpload(file, prePath, null); String filePath = uploadService.saveFileUpload(file, prePath, null);
filePath = uploadService.getFilePath(filePath); filePath = uploadService.getFilePath(filePath);
log.info("filePath==>" + filePath); log.info("filePath==>" + filePath);
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
//IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid, emitter);
IatModelMulUtil.file=filePath;
IatModelMulUtil.emitter=emitter;
IatModelMulUtil.appid=appid;
String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret); String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl); //log.info("authUrl==>" + authUrl);
...@@ -123,27 +113,28 @@ public class ApiSendMsgController { ...@@ -123,27 +113,28 @@ public class ApiSendMsgController {
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://"); String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build(); Request request = new Request.Builder().url(url).build();
WebSocket webSocket = client.newWebSocket(request, new IatModelMulUtil()); WebSocket webSocket = client.newWebSocket(request, iatModelMulUtil);
// 等待返回结果(阻塞直到收到完整数据或超时)
String result = iatModelMulUtil.getResultFuture().get(60, TimeUnit.SECONDS);
log.info("识别结果:" + result);
model.put("result", result);
// model.put("url", filePath); // model.put("url", filePath);
model.put("fileName", file.getOriginalFilename()); // model.put("fileName", file.getOriginalFilename());
// model.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS); // model.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
jsonStr = JSONObject.toJSONString(model); jsonStr = JSONObject.toJSONString(model);
} catch (AppException e) { } catch (AppException e) {
emitter.complete();
/* log.debug(e);
model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE); model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
model.put(KEY_RESULT_MSG, e.getMessage());*/ model.put(KEY_RESULT_MSG, e.getMessage());
// jsonStr = JSONObject.toJSONString(model);
return null;
} catch (Exception e) { } catch (Exception e) {
emitter.complete();
return null; model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
/* model.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE); model.put(KEY_RESULT_MSG, "文件上传失败");
model.put(KEY_RESULT_MSG, "文件上传失败");*/ jsonStr = JSONObject.toJSONString(model);
// jsonStr = JSONObject.toJSONString(model);
} }
return emitter; return jsonStr;
} }
} }
...@@ -18,12 +18,14 @@ import java.nio.charset.Charset; ...@@ -18,12 +18,14 @@ import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
/** /**
* 大模型语音识别---方言识别 * 大模型语音识别---方言识别
*/ */
@Slf4j @Slf4j
public class IatModelMulUtil extends WebSocketListener { public class IatModelMulUtil extends WebSocketListener {
private final CompletableFuture<String> future = new CompletableFuture<>();
/* private static final String hostUrl = "https://iat.cn-huabei-1.xf-yun.com/v1"; // 注意多语种识别,也支持中文音频 /* private static final String hostUrl = "https://iat.cn-huabei-1.xf-yun.com/v1"; // 注意多语种识别,也支持中文音频
private static final String appid = "3cc52607"; //在控制台-我的应用获取 private static final String appid = "3cc52607"; //在控制台-我的应用获取
private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取 private static final String apiSecret = "ZTdmMjFjMGYxYmJhN2VmYjFlMTg3N2Rk"; // 在控制台-我的应用获取
...@@ -39,14 +41,21 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -39,14 +41,21 @@ public class IatModelMulUtil extends WebSocketListener {
private static Date dateEnd = new Date(); private static Date dateEnd = new Date();
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS"); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS");
private final StringBuilder responseBuilder = new StringBuilder();
public static String file; // 识别音频位置*/ public String file; // 识别音频位置*/
public static String appid; public String appid;
public static SseEmitter emitter; // public SseEmitter emitter;
/* public IatModelMulUtil(String file, String appid) {
this.file = file;
this.appid = appid;
// this.emitter = emitter;
}
/*
public IatModelMulUtil(String file, String appid, SseEmitter emitter) { public IatModelMulUtil(String file, String appid, SseEmitter emitter) {
this.file = file; this.file = file;
this.appid = appid; this.appid = appid;
...@@ -178,12 +187,15 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -178,12 +187,15 @@ public class IatModelMulUtil extends WebSocketListener {
for (Ws ws : wsList) { for (Ws ws : wsList) {
List<Cw> cwList = ws.cw; List<Cw> cwList = ws.cw;
for (Cw cw : cwList) { for (Cw cw : cwList) {
log.info("识别结果==》{}", cw.w); // log.info("识别结果==》{}", cw.w);
try { responseBuilder.append(cw.w);
emitter.send(cw.w); /* try {
// emitter.send(cw.w);
} catch (IOException e) { } catch (IOException e) {
log.error("发送异常==》{}", e.getMessage()); log.error("发送异常==》{}", e.getMessage());
} }*/
} }
} }
} }
...@@ -196,7 +208,9 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -196,7 +208,9 @@ public class IatModelMulUtil extends WebSocketListener {
log.info("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms"); log.info("耗时:" + (dateEnd.getTime() - dateBegin.getTime()) + "ms");
// System.out.println("最终识别结果 ==》" + decodeRes); // 按照规则替换与追加出最终识别结果 // System.out.println("最终识别结果 ==》" + decodeRes); // 按照规则替换与追加出最终识别结果
log.info("本次识别sid ==》" + jsonParse.header.sid); log.info("本次识别sid ==》" + jsonParse.header.sid);
emitter.complete(); // emitter.complete();
future.complete(responseBuilder.toString());
log.info("emitter 关闭"); log.info("emitter 关闭");
webSocket.close(1000, ""); webSocket.close(1000, "");
...@@ -222,6 +236,8 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -222,6 +236,8 @@ public class IatModelMulUtil extends WebSocketListener {
// TODO Auto-generated catch block // TODO Auto-generated catch block
log.error("onFailure error:" + e.getMessage()); log.error("onFailure error:" + e.getMessage());
} }
future.completeExceptionally(t);
} }
...@@ -252,6 +268,10 @@ public class IatModelMulUtil extends WebSocketListener { ...@@ -252,6 +268,10 @@ public class IatModelMulUtil extends WebSocketListener {
return httpUrl.toString(); return httpUrl.toString();
} }
public CompletableFuture<String> getResultFuture() {
return future;
}
// 返回结果拆分与展示,仅供参考 // 返回结果拆分与展示,仅供参考
// 返回结果拆分与展示,仅供参考 // 返回结果拆分与展示,仅供参考
class JsonParse { class JsonParse {
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
"baseUrl": "http://127.0.0.1:18001/m" "baseUrl": "http://127.0.0.1:18001/m"
}, },
"test": { "test": {
"baseUrl": "http://192.168.0.98:18006" "baseUrl": "http://192.168.0.250:18006"
}, },
"prod": { "prod": {
"baseUrl": "http://robot.scsmile.cn" "baseUrl": "http://robot.scsmile.cn"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment