Commit c740db49 authored by 赵啸非's avatar 赵啸非

添加音频识别

parent f8ab5faa
...@@ -7,7 +7,11 @@ import com.mortals.framework.service.ILogService; ...@@ -7,7 +7,11 @@ import com.mortals.framework.service.ILogService;
import com.mortals.framework.service.impl.FileLogServiceImpl; import com.mortals.framework.service.impl.FileLogServiceImpl;
import com.mortals.xhx.base.system.upload.service.UploadService; import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.common.utils.IatModelMulMain; import com.mortals.xhx.common.utils.IatModelMulMain;
import com.mortals.xhx.common.utils.IatModelMulUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -49,39 +53,11 @@ public class ApiSendMsgController { ...@@ -49,39 +53,11 @@ public class ApiSendMsgController {
@GetMapping(value = "/events/create", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @GetMapping(value = "/events/create", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamEventsCreate() { public Flux<String> streamEventsCreate() {
/* return Flux.push(sink -> {
new Thread(() -> {
try {
for (int i = 1; i <= 1; i++) {
sink.next("Message " + i);
Thread.sleep(1000);
}
sink.complete();
} catch (InterruptedException e) {
sink.error(e);
}
}).start();
});*/
/* return Flux.create(sink -> {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
sink.next("Message " + System.currentTimeMillis());
}, 0, 1, TimeUnit.SECONDS);
});*/
return Flux.create(sink -> { return Flux.create(sink -> {
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
new Thread(() -> { // 需要异步执行,否则会阻塞 new Thread(() -> { // 需要异步执行,否则会阻塞
sink.next("Message "); sink.next("Message ");
// 每秒发送一条数据 // 每秒发送一条数据
}).start(); }).start();
try { try {
...@@ -92,9 +68,6 @@ public class ApiSendMsgController { ...@@ -92,9 +68,6 @@ public class ApiSendMsgController {
} }
sink.complete(); // 结束流 sink.complete(); // 结束流
}); });
//return null;
} }
@GetMapping("/emitter") @GetMapping("/emitter")
...@@ -113,17 +86,27 @@ public class ApiSendMsgController { ...@@ -113,17 +86,27 @@ public class ApiSendMsgController {
return emitter; return emitter;
} }
@RequestMapping(value = "upload") @RequestMapping(value = "upload", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath", defaultValue = "") String prePath) { @UnAuth
public SseEmitter 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(30000L);
try { try {
String filePath = uploadService.saveFileUpload(file, prePath, null); String filePath = uploadService.saveFileUpload(file, prePath, null);
//将音频文件传给ai大模型 filePath = uploadService.getFilePath(filePath);
// IatModelMulMain.getAuthUrl() IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid, emitter);
String authUrl = iatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl);
OkHttpClient client = new OkHttpClient.Builder().build();
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build();
WebSocket webSocket = client.newWebSocket(request, iatModelMulUtil);
// model.put("url", filePath); // model.put("url", filePath);
model.put("fileName", file.getOriginalFilename()); model.put("fileName", file.getOriginalFilename());
...@@ -140,7 +123,7 @@ public class ApiSendMsgController { ...@@ -140,7 +123,7 @@ public class ApiSendMsgController {
model.put(KEY_RESULT_MSG, "文件上传失败");*/ model.put(KEY_RESULT_MSG, "文件上传失败");*/
jsonStr = JSONObject.toJSONString(model); jsonStr = JSONObject.toJSONString(model);
} }
return jsonStr; return emitter;
} }
} }
...@@ -114,7 +114,7 @@ public class IatModelMulMain extends WebSocketListener { ...@@ -114,7 +114,7 @@ public class IatModelMulMain extends WebSocketListener {
"}"; "}";
webSocket.send(json); webSocket.send(json);
// System.err.println(json); // System.err.println(json);
System.out.println("中间帧音频发送中..."+seq); // System.out.println("中间帧音频发送中..."+seq);
break; break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束 case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
json = "{\n" + json = "{\n" +
...@@ -155,7 +155,7 @@ public class IatModelMulMain extends WebSocketListener { ...@@ -155,7 +155,7 @@ public class IatModelMulMain extends WebSocketListener {
@Override @Override
public void onMessage(WebSocket webSocket, String text) { public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text); super.onMessage(webSocket, text);
System.out.println(text); //System.out.println(text);
JsonParse jsonParse = gson.fromJson(text, JsonParse.class); JsonParse jsonParse = gson.fromJson(text, JsonParse.class);
if (jsonParse != null) { if (jsonParse != null) {
if (jsonParse.header.code != 0) { if (jsonParse.header.code != 0) {
......
...@@ -33,6 +33,17 @@ Content-Disposition: form-data; name="file"; filename="file.zip" ...@@ -33,6 +33,17 @@ Content-Disposition: form-data; name="file"; filename="file.zip"
< ./file.zip < ./file.zip
--WebAppBoundary-- --WebAppBoundary--
###上传音频文件
POST {{baseUrl}}/audio/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="test.pcm"
< ./test.pcm
--WebAppBoundary--
###测试链接数据库 ###测试链接数据库
POST {{baseUrl}}/m/db/connect POST {{baseUrl}}/m/db/connect
Content-Type: application/json Content-Type: application/json
......
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