Parcourir la source

1.停水公告的截止时间可以不填写处理 2.新增处理在线解析语音的接口 3.其他优化

王苗苗 il y a 1 semaine
Parent
commit
afd4f833b6

+ 6 - 0
pom.xml

@@ -188,6 +188,12 @@
                 <version>${tts.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>com.alibaba.nls</groupId>
+                <artifactId>nls-sdk-transcriber</artifactId>
+                <version>2.2.1</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 

+ 12 - 1
slibra-admin/src/main/java/com/slibra/web/controller/business/TCallRecordController.java

@@ -128,7 +128,7 @@ public class TCallRecordController extends BaseController
 
 
     /**
-     * 通过通话ID,将存储的音频转换为文本,并将结果返回给前端
+     * 通过通话ID,将存储的音频转换为文本,并将结果返回给前端(调用阿里云的离线接口)
      * @return
      */
     @GetMapping(value = "/updateFile2TextById")
@@ -136,4 +136,15 @@ public class TCallRecordController extends BaseController
     {
         return R.ok(tCallRecordService.updateFile2TextById(id));
     }
+
+
+    /**
+     * 通过通话ID,将存储的音频转换为文本,并将结果返回给前端(调用阿里云的在线接口)
+     * @return
+     */
+    @GetMapping(value = "/updateFile2TextByIdOneLine")
+    public R<String> updateFile2TextByIdOneLine(Long id)
+    {
+        return R.ok(tCallRecordService.updateFile2TextByIdOneLine(id));
+    }
 }

+ 4 - 0
slibra-admin/src/main/resources/application.yml

@@ -245,3 +245,7 @@ log:
 ALIYUN_AK_ID: LTAI5tQ2HmiHCygZkt5BYrYR
 ALIYUN_AK_SECRET: KhmxTd14SUcXafpFk5yofA43FoeM99
 NLS_APP_KEY: OKt6jogp6fRjHQVp
+WS_URL: wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1
+DOMAIN: https://pbx.fuxicarbon.com/voice/
+URL_DISK_PREFIX: /root/aibot/dm/voice/
+

+ 6 - 0
slibra-common/pom.xml

@@ -172,6 +172,12 @@
             <version>5.8.16</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.alibaba.nls</groupId>
+            <artifactId>nls-sdk-transcriber</artifactId>
+            <version>2.2.1</version>
+        </dependency>
+
 
 
     </dependencies>

+ 2 - 0
slibra-system/src/main/java/com/slibra/business/service/ITCallRecordService.java

@@ -66,4 +66,6 @@ public interface ITCallRecordService
     List<HashMap<String, Object>> getAgentList();
 
     String updateFile2TextById(Long id);
+
+    String updateFile2TextByIdOneLine(Long id);
 }

+ 2 - 1
slibra-system/src/main/java/com/slibra/business/service/impl/File2Text.java

@@ -163,7 +163,8 @@ public class File2Text {
 //        String fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
 //        String fileLink = "https://static.fuxicarbon.com/test/234527-I-0168-15765398611-S.wav";
 //        String fileLink = "http://192.168.100.159/voice/005cad2d-3626-477c-8b95-07b68f339630.wav";
-        String fileLink = "https://static.fuxicarbon.com/userupload/ttt.wav";
+//        String fileLink = "https://static.fuxicarbon.com/userupload/ttt.wav";
+        String fileLink = "https://static.fuxicarbon.com/test/output.wav";
 //        String fileLink = "https://www.w3school.com.cn/i/audio/song.mp3";
         File2Text demo = new File2Text(accessKeyId, accessKeySecret);
         // 第一步:提交录音文件识别请求,获取任务ID用于后续的识别结果轮询。

+ 213 - 0
slibra-system/src/main/java/com/slibra/business/service/impl/SpeechTranscriberDemo.java

@@ -0,0 +1,213 @@
+package com.slibra.business.service.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.nls.client.AccessToken;
+import com.alibaba.nls.client.protocol.InputFormatEnum;
+import com.alibaba.nls.client.protocol.NlsClient;
+import com.alibaba.nls.client.protocol.SampleRateEnum;
+import com.alibaba.nls.client.protocol.asr.SpeechTranscriber;
+import com.alibaba.nls.client.protocol.asr.SpeechTranscriberListener;
+import com.alibaba.nls.client.protocol.asr.SpeechTranscriberResponse;
+import com.qiniu.util.Json;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+/**
+ * 此示例演示了:
+ * ASR实时识别API调用。
+ * 动态获取token。获取Token具体操作,请参见:https://help.aliyun.com/document_detail/450514.html
+ * 通过本地模拟实时流发送。
+ * 识别耗时计算。
+ */
+public class SpeechTranscriberDemo {
+    private String appKey;
+    private NlsClient client;
+    private static final Logger logger = LoggerFactory.getLogger(SpeechTranscriberDemo.class);
+
+    //最终返回的结果
+    public static final List<String> result = new ArrayList<>();
+
+    public SpeechTranscriberDemo(String appKey, String id, String secret, String url) {
+        this.appKey = appKey;
+        //应用全局创建一个NlsClient实例,默认服务地址为阿里云线上服务地址。
+        //获取token,实际使用时注意在accessToken.getExpireTime()过期前再次获取。
+        AccessToken accessToken = new AccessToken(id, secret);
+        try {
+            accessToken.apply();
+            System.out.println("get token: " + ", expire time: " + accessToken.getExpireTime());
+            if(url.isEmpty()) {
+                client = new NlsClient(accessToken.getToken());
+            }else {
+                client = new NlsClient(url, accessToken.getToken());
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static SpeechTranscriberListener getTranscriberListener() {
+        SpeechTranscriberListener listener = new SpeechTranscriberListener() {
+            //识别出中间结果。仅当setEnableIntermediateResult为true时,才会返回该消息。
+            @Override
+            public void onTranscriptionResultChange(SpeechTranscriberResponse response) {
+                System.out.println("task_id: " + response.getTaskId() +
+                        ", name: " + response.getName() +
+                        //状态码“20000000”表示正常识别。
+                        ", status: " + response.getStatus() +
+                        //句子编号,从1开始递增。
+                        ", index: " + response.getTransSentenceIndex() +
+                        //当前的识别结果。
+                        ", result: " + response.getTransSentenceText() +
+                        //当前已处理的音频时长,单位为毫秒。
+                        ", time: " + response.getTransSentenceTime());
+            }
+
+            @Override
+            public void onTranscriberStart(SpeechTranscriberResponse response) {
+                //task_id是调用方和服务端通信的唯一标识,遇到问题时,需要提供此task_id。
+                System.out.println("task_id: " + response.getTaskId() + ", name: " + response.getName() + ", status: " + response.getStatus());
+            }
+
+            @Override
+            public void onSentenceBegin(SpeechTranscriberResponse response) {
+                System.out.println("task_id: " + response.getTaskId() + ", name: " + response.getName() + ", status: " + response.getStatus());
+
+            }
+
+            //识别出一句话。服务端会智能断句,当识别到一句话结束时会返回此消息。
+            @Override
+            public void onSentenceEnd(SpeechTranscriberResponse response) {
+                /*System.out.println("a:task_id: " + response.getTaskId() +
+                        ", name: " + response.getName() +
+                        //状态码“20000000”表示正常识别。
+                        ", status: " + response.getStatus() +
+                        //句子编号,从1开始递增。
+                        ", index: " + response.getTransSentenceIndex() +
+                        //当前的识别结果。
+                        ", result: " + response.getTransSentenceText() +
+                        //置信度
+                        ", confidence: " + response.getConfidence() +
+                        //开始时间
+                        ", begin_time: " + response.getSentenceBeginTime() +
+                        //当前已处理的音频时长,单位为毫秒。
+                        ", time: " + response.getTransSentenceTime());*/
+//                logger.info("返回的结果是{}", JSON.toJSONString(response));
+                result.add(response.getTransSentenceText());
+//                logger.info("返回的结果是{}", JSON.toJSONString(response.getTransSentenceText()));
+            }
+
+            //识别完毕
+            @Override
+            public void onTranscriptionComplete(SpeechTranscriberResponse response) {
+                System.out.println("task_id: " + response.getTaskId() + ", name: " + response.getName() + ", status: " + response.getStatus());
+            }
+
+            @Override
+            public void onFail(SpeechTranscriberResponse response) {
+                //task_id是调用方和服务端通信的唯一标识,遇到问题时,需要提供此task_id。
+                System.out.println("task_id: " + response.getTaskId() +  ", status: " + response.getStatus() + ", status_text: " + response.getStatusText());
+            }
+        };
+
+        return listener;
+    }
+
+    //根据二进制数据大小计算对应的同等语音长度。
+    //sampleRate:支持8000或16000。
+    public static int getSleepDelta(int dataSize, int sampleRate) {
+        // 仅支持16位采样。
+        int sampleBytes = 16;
+        // 仅支持单通道。
+        int soundChannel = 1;
+        return (dataSize * 10 * 8000) / (160 * sampleRate);
+    }
+
+    public void process(String filepath) {
+        SpeechTranscriber transcriber = null;
+        try {
+            //创建实例、建立连接。
+            transcriber = new SpeechTranscriber(client, getTranscriberListener());
+            transcriber.setAppKey(appKey);
+            //输入音频编码方式。
+            transcriber.setFormat(InputFormatEnum.PCM);
+            //输入音频采样率。
+            transcriber.setSampleRate(SampleRateEnum.SAMPLE_RATE_16K);
+            //是否返回中间识别结果。
+            transcriber.setEnableIntermediateResult(false);
+            //是否生成并返回标点符号。
+            transcriber.setEnablePunctuation(true);
+            //是否将返回结果规整化,比如将一百返回为100。
+            transcriber.setEnableITN(false);
+
+            //设置vad断句参数。默认值:800ms,有效值:200ms~6000ms。
+            //transcriber.addCustomedParam("max_sentence_silence", 600);
+            //设置是否语义断句。
+            //transcriber.addCustomedParam("enable_semantic_sentence_detection",false);
+            //设置是否开启过滤语气词,即声音顺滑。
+            //transcriber.addCustomedParam("disfluency",true);
+            //设置是否开启词模式。
+            //transcriber.addCustomedParam("enable_words",true);
+            //设置vad噪音阈值参数,参数取值为-1~+1,如-0.9、-0.8、0.2、0.9。
+            //取值越趋于-1,判定为语音的概率越大,亦即有可能更多噪声被当成语音被误识别。
+            //取值越趋于+1,判定为噪音的越多,亦即有可能更多语音段被当成噪音被拒绝识别。
+            //该参数属高级参数,调整需慎重和重点测试。
+            //transcriber.addCustomedParam("speech_noise_threshold",0.3);
+            //设置训练后的定制语言模型id。
+            //transcriber.addCustomedParam("customization_id","你的定制语言模型id");
+            //设置训练后的定制热词id。
+            //transcriber.addCustomedParam("vocabulary_id","你的定制热词id");
+
+            //此方法将以上参数设置序列化为JSON发送给服务端,并等待服务端确认。
+            transcriber.start();
+
+            File file = new File(filepath);
+            FileInputStream fis = new FileInputStream(file);
+            byte[] b = new byte[3200];
+            int len;
+            while ((len = fis.read(b)) > 0) {
+                logger.info("send data pack length: " + len);
+                transcriber.send(b, len);
+                //本案例用读取本地文件的形式模拟实时获取语音流并发送的,因为读取速度较快,这里需要设置sleep。
+                //如果实时获取语音则无需设置sleep, 如果是8k采样率语音第二个参数设置为8000。
+                int deltaSleep = getSleepDelta(len, 16000);
+                Thread.sleep(deltaSleep);
+            }
+
+            //通知服务端语音数据发送完毕,等待服务端处理完成。
+            long now = System.currentTimeMillis();
+            logger.info("ASR wait for complete");
+            transcriber.stop();
+            logger.info("ASR latency : " + (System.currentTimeMillis() - now) + " ms");
+        } catch (Exception e) {
+            System.err.println(e.getMessage());
+        } finally {
+            if (null != transcriber) {
+                transcriber.close();
+            }
+        }
+    }
+
+    public void shutdown() {
+        client.shutdown();
+    }
+
+    public static void main(String[] args) throws Exception {
+        String appKey = "OKt6jogp6fRjHQVp";
+        String id = "LTAI5tQ2HmiHCygZkt5BYrYR";
+        String secret = "KhmxTd14SUcXafpFk5yofA43FoeM99";
+        String url = System.getenv().getOrDefault("NLS_GATEWAY_URL", "wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1");
+
+        //本案例使用本地文件模拟发送实时流数据。您在实际使用时,可以实时采集或接收语音流并发送到ASR服务端。
+//        String filepath = "/Users/wangmiaomiao/Downloads/ttt.wav";
+        String filepath = "/Users/wangmiaomiao/Downloads/output.wav";
+        SpeechTranscriberDemo demo = new SpeechTranscriberDemo(appKey, id, secret, url);
+        demo.process(filepath);
+        System.out.println(JSON.toJSONString(result));
+        demo.shutdown();
+    }
+}

+ 42 - 12
slibra-system/src/main/java/com/slibra/business/service/impl/TCallRecordServiceImpl.java

@@ -10,14 +10,11 @@ import com.slibra.business.domain.TCutOffWater;
 import com.slibra.business.domain.TUserInfo;
 import com.slibra.business.mapper.TCutOffWaterMapper;
 import com.slibra.business.mapper.TUserInfoMapper;
-import com.slibra.business.res.Ele;
 import com.slibra.common.exception.ServiceException;
 import com.slibra.common.utils.DateUtils;
 import com.slibra.common.utils.SecurityUtils;
 import com.slibra.common.utils.StringUtils;
 import lombok.extern.slf4j.Slf4j;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -55,6 +52,15 @@ public class TCallRecordServiceImpl implements ITCallRecordService
 
     @Value("${NLS_APP_KEY}")
     private String appKey;
+
+    @Value("${WS_URL}")
+    private String wsUrl;
+
+    @Value("${DOMAIN}")
+    private String domain;
+
+    @Value("${URL_DISK_PREFIX}")
+    private String urlDiskPrefix;
     /**
      * 查询通话记录
      * 
@@ -183,16 +189,9 @@ public class TCallRecordServiceImpl implements ITCallRecordService
 
     @Override
     public String updateFile2TextById(Long id) {
-        //查询数据
+        //查询数据 校验数据
         TCallRecord tCallRecord = this.tCallRecordMapper.selectTCallRecordById(id);
-        if(Objects.isNull(tCallRecord))
-            throw new ServiceException("请输入正确的ID");
-        Long hasParsed = tCallRecord.getHasParsed();
-        if(!Objects.isNull(hasParsed) && 1== hasParsed)
-            throw new ServiceException("此次通话记录已经做过转换了,无需重复转换");
-        String url = tCallRecord.getUrl();
-        if(StringUtils.isBlank(url))
-            throw new ServiceException("此次通话记录,未记录录音文件,无法转换");
+        String url = checkParamsAndGetUrl(tCallRecord);
         /*final String accessKeyId = "LTAI5tQ2HmiHCygZkt5BYrYR";
         final String accessKeySecret = "KhmxTd14SUcXafpFk5yofA43FoeM99";
         final String appKey = "OKt6jogp6fRjHQVp";*/
@@ -233,4 +232,35 @@ public class TCallRecordServiceImpl implements ITCallRecordService
             throw new ServiceException("录音文件识别结果查询失败!");
         }
     }
+
+
+    @Override
+    public String updateFile2TextByIdOneLine(Long id) {
+        //查询数据 校验数据
+        TCallRecord tCallRecord = this.tCallRecordMapper.selectTCallRecordById(id);
+        String url = checkParamsAndGetUrl(tCallRecord);
+
+        SpeechTranscriberDemo demo = new SpeechTranscriberDemo(appKey, accessKeyId, accessKeySecret, wsUrl);
+        demo.process(url.replace(domain, urlDiskPrefix));//假设是带http的,替换URL  示例:将https://pbx.fuxicarbon.com/voice/005cad2d-3626-477c-8b95-07b68f339630.wav 变化成 /root/aibot/dm/voice/005cad2d-3626-477c-8b95-07b68f339630.wav
+        demo.shutdown();
+        return JSON.toJSONString(SpeechTranscriberDemo.result);
+    }
+
+
+    /**
+     * 校验数据
+     * @param tCallRecord
+     * @return
+     */
+    private static String checkParamsAndGetUrl(TCallRecord tCallRecord) {
+        if(Objects.isNull(tCallRecord))
+            throw new ServiceException("请输入正确的ID");
+        Long hasParsed = tCallRecord.getHasParsed();
+        if(!Objects.isNull(hasParsed) && 1== hasParsed)
+            throw new ServiceException("此次通话记录已经做过转换了,无需重复转换");
+        String url = tCallRecord.getUrl();
+        if(StringUtils.isBlank(url))
+            throw new ServiceException("此次通话记录,未记录录音文件,无法转换");
+        return url;
+    }
 }

+ 1 - 0
slibra-system/src/main/resources/mapper/business/TCutOffWaterMapper.xml

@@ -139,6 +139,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         INNER JOIN t_neighborhood c ON c.id = b.neighborhood_id
         AND c.del_flag = 0
         WHERE c.`name` LIKE concat('%', #{asrText}, '%')
+        AND NOW() &gt;= time_begin AND (time_end is NULL or NOW() &lt; time_end)
     </select>
     
     <select id="selectgetExtraListByPumpingStationId" resultType="com.slibra.business.res.CutOffExtraRes">