Browse Source

暂存1

dev_shibei_match
yinzuomei 5 years ago
parent
commit
82fa0ad9dd
  1. 11
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java
  2. 1
      epmet-gateway/src/main/resources/bootstrap.yml
  3. 97
      epmet-module/resi-group/resi-group-client/src/main/java/com/epmet/resi/group/dto/scanapicallback/VoiceScanCallBackContentFormDTO.java
  4. 19
      epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java
  5. 47
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/VoiceSceneEnum.java
  6. 29
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java
  7. 42
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/redis/ScanRedis.java
  8. 21
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java
  9. 212
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java
  10. 63
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceAsyncScanParam.java
  11. 31
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceTask.java
  12. 47
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanDetailDTO.java
  13. 51
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java
  14. 57
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResultDTO.java
  15. 40
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskDataDTO.java
  16. 22
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskResult.java
  17. 90
      epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanRequestSample.java
  18. 113
      epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanResultsRequestSample.java

11
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java

@ -10,12 +10,13 @@ package com.epmet.commons.tools.redis;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.ConvertUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -49,6 +50,10 @@ public class RedisUtils {
* 过期时长为1小时单位
*/
public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
/**
* 过期时长为4小时单位
*/
public final static long HOUR_FOUR_EXPIRE = 60 * 60 * 4L;
/**
* 过期时长为6小时单位
*/

1
epmet-gateway/src/main/resources/bootstrap.yml

@ -442,6 +442,7 @@ epmet:
# 内部认证url白名单(在白名单中的,就不会再校验登录了)
internalAuthUrlsWhiteList:
- /epmetuser/customerstaff/customerlist
- /resi/group/topic/callBackPublishTopic
# 外部应用认证,使用AccessToken等头进行认证
externalOpenUrls:

97
epmet-module/resi-group/resi-group-client/src/main/java/com/epmet/resi/group/dto/scanapicallback/VoiceScanCallBackContentFormDTO.java

@ -0,0 +1,97 @@
package com.epmet.resi.group.dto.scanapicallback;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 语音检测callBack入参
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/10 10:47
*/
@Data
public class VoiceScanCallBackContentFormDTO implements Serializable {
/**
* 错误码和HTTP状态码一致
* 200表示检测成功
* 280表示处理中需要继续轮询
* 其他表示任务失败
* 更多信息请参见公共错误码
*
* 说明 只有返回280表示任务还在进行需要继续轮询该任务的检测结果返回其他值均表示该语音检测任务已结束如果实际上该语音检测没有结束而是因为网络异常等原因异常结束您可以重新提交语音异步检测任务
*/
private Integer code;
/**
* 请求参数的响应信息
*/
private String msg;
/**
* 检测对象对应的数据ID
* 说明 如果在检测请求参数中传入了dataId则此处返回对应的dataId
*/
private String dataId;
/**
* 该检测任务的ID
*/
private String taskId;
/**
* 检测对象的URL
*/
private String url;
/**
* 检测成功code=200返回的检测结果该结果包含一个或多个元素每个元素是个结构体对应一个场景关于每个元素的结构描述请参见result
*/
private List<ResultDTO> results;
@Data
private static class ResultDTO{
/**
* 检测结果的分类取值
* normal正常文本
* spam含垃圾信息
* ad广告
* politics涉政
* terrorism暴恐
* abuse辱骂
* porn色情
* flood灌水
* contraband违禁
* meaningless无意义
* customized自定义例如命中自定义关键词
*/
private String label;
/**
* 检测场景和调用请求中的场景对应唯一取值antispam
*/
private String scene;
/**
* 建议您执行的后续操作取值
* pass结果正常无需进行其余操作
* review结果不确定需要进行人工审核
* block结果违规建议直接删除或者限制公开
*/
private String suggestion;
/**
* 置信度分数取值范围0表示置信度最低~100表示置信度最高
* 如果suggestion为pass则置信度越高表示内容正常的可能性越高如果suggestion为review或block则置信度越高表示内容违规的可能性越高
* 注意 该值仅作为参考强烈建议您不要在业务中使用建议您参考suggestion和label或者部分接口返回的sublabel结果用于内容违规判定
*/
private Float rate;
//下面还有个details 语音对应的文本详情。每一句文本对应一个元素,包含一个或者多个元素。关于每个元素的结构描述,请参见detail。
//暂时不解析了。
}
}

19
epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/topic/controller/ResiTopicController.java

@ -1,5 +1,6 @@
package com.epmet.modules.topic.controller;
import com.alibaba.fastjson.JSON;
import com.epmet.commons.tools.annotation.LoginUser;
import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.Result;
@ -11,10 +12,15 @@ import com.epmet.resi.group.dto.topic.ResiTopicDTO;
import com.epmet.resi.group.dto.topic.TopicInfoDTO;
import com.epmet.resi.group.dto.topic.form.*;
import com.epmet.resi.group.dto.topic.result.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description
@ -22,7 +28,7 @@ import java.util.List;
* @Author wangc
* @date 2020.03.31 13:03
*/
@Slf4j
@RestController
@RequestMapping("topic")
public class ResiTopicController {
@ -349,5 +355,16 @@ public class ResiTopicController {
return new Result<MyPartIssueResultDTO>().ok(topicService.selectMyPartTopic(myPartIssueFormDTO));
}
@PostMapping("callBackPublishTopic")
public Result callBackPublishTopic(@RequestParam("checksum") String checksum, @RequestParam("content") String content, HttpServletResponse response){
log.info("checksum:"+checksum);
log.info("content:"+ JSON.toJSONString(content,true));
Map<String,String> map=new HashMap<>();
map.put("checksum",checksum);
map.put("content",content);
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return new Result<>().ok(map);
}
}

47
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/VoiceSceneEnum.java

@ -0,0 +1,47 @@
package com.epmet.openapi.scan.common.enu;
import java.util.ArrayList;
import java.util.List;
/**
* 语音异步检测场景
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 10:12
*/
public enum VoiceSceneEnum {
ANTISPAM("antispam", "检测场景,取值:antispam");
private String code;
private String desc;
VoiceSceneEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public static List<String> getVoiceSceneList() {
List<String> result = new ArrayList<>();
VoiceSceneEnum[] values = VoiceSceneEnum.values();
for (VoiceSceneEnum v : values) {
result.add(v.getCode());
}
return result;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}

29
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/controller/ScanController.java

@ -5,13 +5,19 @@ import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.openapi.scan.service.impl.ScanService;
import com.epmet.openapi.scan.support.param.ImgScanParam;
import com.epmet.openapi.scan.support.param.TextScanParam;
import com.epmet.openapi.scan.support.param.VoiceAsyncScanParam;
import com.epmet.openapi.scan.support.result.ImgAsyncScanResult;
import com.epmet.openapi.scan.support.result.SyncScanResult;
import com.epmet.openapi.scan.support.result.VoiceAsyncScanResult;
import com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author jianjun liu
* @email liujianjun@yunzongnet.com
@ -52,4 +58,27 @@ public class ScanController {
public Result<ImgAsyncScanResult> ImgAsyncScan(@RequestBody ImgScanParam param) {
return null;//scanService.sendASyncImgScan(param);
}
/**
* @description 语音异步检测
* @Date 2020/12/9 9:14
**/
@PostMapping("voiceAsyncScan")
public Result<VoiceAsyncScanTaskResult> voiceAsyncScan(@RequestBody VoiceAsyncScanParam param){
ValidatorUtils.validateEntity(param);
return scanService.sendVoiceAsyncScan(param);
}
/**
* @param taskIds 要查询的异步检测任务的taskId列表数组中的元素个数不超过100个
* @author yinzuomei
* @description 语音异步检测结果查询
* @Date 2020/12/9 11:16
**/
@PostMapping("voiceResults")
public Result<List<VoiceAsyncScanResult>> voiceResults(@RequestBody List<String> taskIds){
return scanService.voiceResults(taskIds);
}
}

42
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/redis/ScanRedis.java

@ -0,0 +1,42 @@
/**
* Copyright 2018 人人开源 https://www.renren.io
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.epmet.openapi.scan.redis;
import com.epmet.commons.tools.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 用户居民端注册信息表 用户在居民端完善的个人信息
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2020-03-30
*/
@Component
public class ScanRedis {
@Autowired
private RedisUtils redisUtils;
public void test(){
redisUtils.set("yinzuomei", "尹作梅测试用",RedisUtils.MINUTE_THIRTY_EXPIRE);
}
public void setVoiceScanKey(String taskId, String seed) {
redisUtils.set(taskId, seed,RedisUtils.HOUR_FOUR_EXPIRE);
}
}

21
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanService.java

@ -3,7 +3,12 @@ package com.epmet.openapi.scan.service.impl;
import com.epmet.commons.tools.utils.Result;
import com.epmet.openapi.scan.support.param.ImgScanParam;
import com.epmet.openapi.scan.support.param.TextScanParam;
import com.epmet.openapi.scan.support.param.VoiceAsyncScanParam;
import com.epmet.openapi.scan.support.result.SyncScanResult;
import com.epmet.openapi.scan.support.result.VoiceAsyncScanResult;
import com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult;
import java.util.List;
/**
* desc:内容扫描接口
@ -27,4 +32,20 @@ public interface ScanService {
* @return
*/
public Result<SyncScanResult> sendSyncImgScan(ImgScanParam imgScanParam);
/**
* 语音异步检测
*
* @param param
* @return com.epmet.openapi.scan.support.result.VoiceAsyncScanTaskResult
*/
Result<VoiceAsyncScanTaskResult> sendVoiceAsyncScan(VoiceAsyncScanParam param);
/**
* 语音异步检测结果查询
*
* @param taskIds 要查询的异步检测任务的taskId列表数组中的元素个数不超过100个
* @return com.epmet.openapi.scan.support.result.VoiceAsyncScanResult
*/
Result<List<VoiceAsyncScanResult>> voiceResults(List<String> taskIds);
}

212
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java

@ -6,32 +6,30 @@ import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.AcsRequest;
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
import com.aliyuncs.green.model.v20180509.TextScanRequest;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.epmet.commons.tools.utils.Result;
import com.epmet.openapi.scan.common.constant.SysConstant;
import com.epmet.openapi.scan.common.enu.ImgSceneEnum;
import com.epmet.openapi.scan.common.enu.SuggestionEnum;
import com.epmet.openapi.scan.common.enu.SysResponseEnum;
import com.epmet.openapi.scan.common.enu.TextSceneEnum;
import com.epmet.openapi.scan.common.enu.*;
import com.epmet.openapi.scan.common.exception.ExecuteHttpException;
import com.epmet.openapi.scan.common.util.IAcsClientUtil;
import com.epmet.openapi.scan.support.param.ImgScanParam;
import com.epmet.openapi.scan.support.param.ImgTask;
import com.epmet.openapi.scan.support.param.TextScanParam;
import com.epmet.openapi.scan.support.param.TextTask;
import com.epmet.openapi.scan.support.result.ScanTaskResult;
import com.epmet.openapi.scan.support.result.SceneDetailResult;
import com.epmet.openapi.scan.support.result.SyncScanResult;
import com.epmet.openapi.scan.redis.ScanRedis;
import com.epmet.openapi.scan.support.param.*;
import com.epmet.openapi.scan.support.result.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;
/**
* @author jianjun liu
@ -45,7 +43,8 @@ public class ScanServiceImpl implements ScanService {
private String regionId;
@Value("${aliyun.green.bizType}")
private String bizType;
@Autowired
private ScanRedis scanRedis;
@Override
public Result<SyncScanResult> sendTextScan(TextScanParam textScanParam) {
//默认参数
@ -263,4 +262,193 @@ public class ScanServiceImpl implements ScanService {
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg());
}
}
/**
* @author yinzuomei
* @description 语音异步检测
* @Date 2020/12/9 10:02
*/
@Override
public Result<VoiceAsyncScanTaskResult> sendVoiceAsyncScan(VoiceAsyncScanParam voiceAsyncScanParam) {
//检测对象不能为空,且最多支持100个元素
List<VoiceTask> voiceTasks = voiceAsyncScanParam.getTasks();
if (CollectionUtils.isEmpty(voiceTasks) || voiceTasks.size() > SysConstant.MAX_TASK_SIZE) {
return new Result<VoiceAsyncScanTaskResult>().error(SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getCode(),
SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getMsg().concat(SysConstant.MAX_TASK_SIZE.toString()));
}
//默认参数
voiceAsyncScanParam.setScenes(VoiceSceneEnum.getVoiceSceneList());
voiceAsyncScanParam.setBizType(bizType);
// 如果是语音流检测,则修改为true。现在默认为音频文件检测false
voiceAsyncScanParam.setLive(false);
voiceAsyncScanParam.setSeed(UUID.randomUUID().toString().replace("-", ""));
VoiceAsyncScanRequest voiceAsyncScanRequest=getVoiceAsyncScanRequest();
try {
voiceAsyncScanRequest.setHttpContent(JSON.toJSONString(voiceAsyncScanParam).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON);
} catch (UnsupportedEncodingException e) {
log.error("sendVoiceAsyncScan parse param exception", e);
return new Result<VoiceAsyncScanTaskResult>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg());
}
log.info("语音异步检测入参:"+JSON.toJSONString(voiceAsyncScanParam,true));
List<VoiceAsyncScanTaskDataDTO> taskList;
try {
taskList = executeSyncVoice(voiceAsyncScanRequest);
log.info("语音异步检测返参taskList:"+JSON.toJSONString(taskList,true));
} catch (ExecuteHttpException e) {
log.error("sendVoiceAsyncScan execute Exception", e);
return new Result<VoiceAsyncScanTaskResult>().error(e.getCode(), e.getMsg());
}
//成功返回
VoiceAsyncScanTaskResult resultDto=new VoiceAsyncScanTaskResult();
resultDto.setSeed(voiceAsyncScanParam.getSeed());
resultDto.setTaskList(taskList);
if (StringUtils.isNotBlank(voiceAsyncScanParam.getCallback())) {
// 存储seed和 任务id、dataId的关系 用于回调鉴权
log.info("need to save seed and taskId、dataId的关系");
taskList.forEach(task -> {
scanRedis.setVoiceScanKey(task.getTaskId(), voiceAsyncScanParam.getSeed());
});
}
return new Result<VoiceAsyncScanTaskResult>().ok(resultDto);
}
/**
* @param
* @author yinzuomei
* @description 构造语音异步检测请求对象
* @Date 2020/12/9 10:44
**/
private VoiceAsyncScanRequest getVoiceAsyncScanRequest() {
VoiceAsyncScanRequest voiceAsyncScanRequest = new VoiceAsyncScanRequest();
// 指定api返回格式
voiceAsyncScanRequest.setAcceptFormat(FormatType.JSON);
// 指定请求方法
voiceAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
voiceAsyncScanRequest.setEncoding(SysConstant.UTF8);
voiceAsyncScanRequest.setRegionId(regionId);
voiceAsyncScanRequest.setConnectTimeout(3000);
voiceAsyncScanRequest.setReadTimeout(6000);
return voiceAsyncScanRequest;
}
/**
* @param voiceAsyncScanRequest
* @author yinzuomei
* @description 解析返参
* @Date 2020/12/9 10:44
**/
private List<VoiceAsyncScanTaskDataDTO> executeSyncVoice(AcsRequest<?> voiceAsyncScanRequest) {
try {
HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(voiceAsyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
//后面注释掉此日志
log.info("VoiceAsyncScanRequest原生接口返参:" + JSON.toJSONString(scrResponse, true));
if (HttpStatus.SC_OK == scrResponse.getInteger(SysConstant.CODE)) {
//获取data列表
JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA);
List<VoiceAsyncScanTaskDataDTO> resultList = dataResults.toJavaList(VoiceAsyncScanTaskDataDTO.class);
//成功返回
return resultList;
} else {
log.warn("executeSyncVoice detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE));
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(),
SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
}
} else {
log.warn("executeSyncVoice response status is not success. httpResponse:{}", JSON.toJSONString(httpResponse));
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(),
SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
}
} catch (Exception e) {
log.error("executeSyncVoice exception IAcsClientUtil do action exception", e);
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg());
}
}
/**
* @param taskIds 要查询的异步检测任务的taskId列表数组中的元素个数不超过100个
* @author yinzuomei
* @description 语音异步检测结果查询
* @Date 2020/12/9 11:17
**/
@Override
public Result<List<VoiceAsyncScanResult>> voiceResults(List<String> taskIds) {
//检测对象不能为空,且最多支持100个元素
if (CollectionUtils.isEmpty(taskIds) || taskIds.size() > SysConstant.MAX_TASK_SIZE) {
return new Result<List<VoiceAsyncScanResult>>().error(SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getCode(),
SysResponseEnum.SCAN_TASK_LIST_PARAM_ERROR.getMsg().concat(SysConstant.MAX_TASK_SIZE.toString()));
}
VoiceAsyncScanResultsRequest request=getVoiceAsyncScanResultsRequest();
try {
request.setHttpContent(JSON.toJSONString(taskIds).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON);
} catch (UnsupportedEncodingException e) {
log.error("getVoiceAsyncScanResult parse param exception", e);
return new Result<List<VoiceAsyncScanResult>>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg());
}
log.info("语音异步检测结果查询入参:"+JSON.toJSONString(taskIds,true));
try {
HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(request);
if (httpResponse.isSuccess()) {
JSONObject scrResponse=JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
//后面注释掉此返参
log.info("VoiceAsyncScanResultsRequest原生接口返参:"+JSON.toJSONString(scrResponse, true));
if (HttpStatus.SC_OK == scrResponse.getInteger(SysConstant.CODE)) {
//获取data列表
JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA);
List<VoiceAsyncScanResult> resultList = dataResults.toJavaList(VoiceAsyncScanResult.class);
//成功返回
return new Result<List<VoiceAsyncScanResult>>().ok(resultList);
}else{
log.warn("getVoiceAsyncScanResult detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE));
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(),
SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
}
} else {
log.warn("语音异步检测结果查询预警 getVoiceAsyncScanResult response status is not success. httpResponse:{}", JSON.toJSONString(httpResponse));
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getCode(),
SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
}
} catch (Exception e) {
log.error("executeSyncVoice exception IAcsClientUtil do action exception", e);
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg());
}
}
private VoiceAsyncScanResultsRequest getVoiceAsyncScanResultsRequest(){
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
// 指定API返回格式。
getResultsRequest.setAcceptFormat(FormatType.JSON);
// 指定请求方法。
getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST);
getResultsRequest.setEncoding(SysConstant.UTF8);
getResultsRequest.setRegionId(regionId);
/**
* 请务必设置超时时间
*/
getResultsRequest.setConnectTimeout(3000);
getResultsRequest.setReadTimeout(6000);
return getResultsRequest;
}
}

63
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceAsyncScanParam.java

@ -0,0 +1,63 @@
package com.epmet.openapi.scan.support.param;
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
/**
* 音频审查 入参
* 参考文档https://help.aliyun.com/document_detail/89630.html?spm=a2c4g.11186623.2.12.51a32dbfW6AdqV#reference-bcf-3nk-z2b
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 9:07
*/
@Data
public class VoiceAsyncScanParam implements Serializable {
private static final long serialVersionUID = 3408043673247901184L;
/**
* 不必填
* 该字段用于标识您的业务场景您可以通过内容安全控制台创建业务场景具体操作请参见自定义机审标准或者提交工单联系我们帮助您创建业务场景
*/
private String bizType;
/**
* 必填
* 检测场景取值antispam
*/
private List<String> scenes;
/**
* 不必填
* 是否为语音流例如直播流检测取值
* true表示语音流检测
* false默认表示音频文件检测
*/
private Boolean live;
/**
* 不必填
* 是否为近线检测模式 取值
* true表示近线检测模式近线检测模式下您提交的任务不保证能够实时处理但是可以排队并在24小时内开始检测
* false默认表示实时检测模式对于超过了并发路数限制的检测请求会直接拒绝
* 说明 该参数仅适用于音频文件检测不适用于语音流检测
*/
private Boolean offline;
/**
* 异步检测结果回调地址,执行异步审查内容时 必填
*/
private String callback;
/**
* 随机字符串该值用于回调通知请求中的签名使用callback时 必填
*/
private String seed;
@Valid
@NotEmpty(message = "任务列表不能为空")
private List<VoiceTask> tasks;
}

31
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/param/VoiceTask.java

@ -0,0 +1,31 @@
package com.epmet.openapi.scan.support.param;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 语音异步检测 对象
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 10:16
*/
@Data
public class VoiceTask implements Serializable {
/**
* 不必填
* 要检测的数据id 非必填
* 检测对象对应的数据ID
* 由大小写英文字母数字下划线_短划线-英文句号.组成不超过128个字符可以用于唯一标识您的业务数据
* */
@NotBlank(message = "dataId不能为空")
private String dataId;
/**
* 必填
* 需要检测的音频文件或语音流的下载地址
*/
@NotBlank(message = "音频URL不能为空")
private String url;
}

47
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanDetailDTO.java

@ -0,0 +1,47 @@
package com.epmet.openapi.scan.support.result;
import lombok.Data;
import java.io.Serializable;
/**
* 语音异步检测结果查询结果返参 -data-result-detail详情
* 语音对应的文本详情每一句文本对应一个元素包含一个或者多个元素关于每个元素的结构描述
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 11:11
*/
@Data
public class VoiceAsyncScanDetailDTO implements Serializable {
private static final long serialVersionUID = -2664219492371705160L;
/**
* 句子开始的时间单位
*/
private Integer startTime;
/**
* 句子结束的时间单位
*/
private Integer endTime;
/**
* 语音转换成文本的结果
*/
private String text;
/**
* 检测结果的分类取值
* normal正常文本
* spam含垃圾信息
* ad广告
* politics涉政
* terrorism暴恐
* abuse辱骂
* porn色情
* flood灌水
* contraband违禁
* meaningless无意义
* customized自定义例如命中自定义关键词
*/
private String label;
}

51
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java

@ -0,0 +1,51 @@
package com.epmet.openapi.scan.support.result;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 语音异步检测结果查询结果返参
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 10:56
*/
@Data
public class VoiceAsyncScanResult implements Serializable {
private static final long serialVersionUID = 8702129292884498023L;
/**
* 错误码和HTTP状态码一致
* 200表示检测成功
* 280表示处理中需要继续轮询
* 其他表示任务失败
* 更多信息请参见公共错误码
*/
private String code;
/**
* 错误描述信息
*/
private String msg;
/**
* 检测对象对应的数据ID
*/
private String dataId;
/**
* 检测任务的ID
*/
private String taskId;
/**
* 暂时没用所以返回忽略
*/
@JsonIgnore
private String url;
/**
* 检测成功code=200返回的检测结果该结果包含一个或多个元素每个元素是个结构体对应一个场景关于每个元素的结构描述请参见result
*/
private List<VoiceAsyncScanResultDTO> results;
}

57
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResultDTO.java

@ -0,0 +1,57 @@
package com.epmet.openapi.scan.support.result;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 语音异步检测结果查询结果返参 -data-result详情
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 11:07
*/
@Data
public class VoiceAsyncScanResultDTO implements Serializable {
private static final long serialVersionUID = 4602226363259983753L;
/**
* 检测场景和调用请求中的场景对应取值antispam
*/
private String scene;
/**
* 检测结果的分类取值
* normal正常文本
* spam含垃圾信息
* ad广告
* politics涉政
* terrorism暴恐
* abuse辱骂
* porn色情
* flood灌水
* contraband违禁
* meaningless无意义
* customized自定义例如命中自定义关键词
*/
private String label;
/**
* 建议您执行的后续操作取值
* pass结果正常无需进行其余操作
* review结果不确定需要进行人工审核
* block结果违规建议直接删除或者限制公开
*/
private String suggestion;
/**
* 置信度分数取值范围0表示置信度最低~100表示置信度最高
* 如果suggestion为pass则置信度越高表示内容正常的可能性越高如果suggestion为review或block则置信度越高表示内容违规的可能性越高
* 注意 该值仅作为参考强烈建议您不要在业务中使用建议您参考suggestion和label或者部分接口返回的sublabel结果用于内容违规判定
*/
private Float rate;
/**
* 语音对应的文本详情每一句文本对应一个元素包含一个或者多个元素关于每个元素的结构描述
*/
private List<VoiceAsyncScanDetailDTO> details;
}

40
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskDataDTO.java

@ -0,0 +1,40 @@
package com.epmet.openapi.scan.support.result;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import java.io.Serializable;
/**
* 描述一下
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 16:38
*/
@Data
public class VoiceAsyncScanTaskDataDTO implements Serializable {
/**
* 错误码和HTTP状态码一致
* 更多信息请参见公共错误码
*/
private String code;
/**
* 错误描述信息
*/
private String msg;
/**
* 检测对象对应的数据ID
*/
private String dataId;
/**
* 检测任务的ID
*/
private String taskId;
/**
* 暂时没用所以返回忽略
*/
@JsonIgnore
private String url;
}

22
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanTaskResult.java

@ -0,0 +1,22 @@
package com.epmet.openapi.scan.support.result;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 音频审查返参用于接收taskId
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/9 9:23
*/
@Data
public class VoiceAsyncScanTaskResult implements Serializable {
private static final long serialVersionUID = 8702129292884498023L;
/**
* 随机字符串该值用于回调通知请求中的签名
*/
private String seed;
private List<VoiceAsyncScanTaskDataDTO> taskList;
}

90
epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanRequestSample.java

@ -0,0 +1,90 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
/**
* 提交语音异步检测任务 demo
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/8 21:51
*/
public class VoiceAsyncScanRequestSample extends BaseSample {
public static void main(String[] args) throws Exception {
//请替换成您自己的AccessKey ID、AccessKey Secret。
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai",
"LTAI4G6Fv6uTzQbpsayATHq4", //您自己的AccessKey ID
"QevMw1RYCwQUG3RSMPq1J6EAfmSblo");//您自己的AccessKey Secret
final IAcsClient client = new DefaultAcsClient(profile);
VoiceAsyncScanRequest asyncScanRequest = new VoiceAsyncScanRequest(); //class different vs common
asyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
asyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
asyncScanRequest.setRegionId("cn-shanghai");
asyncScanRequest.setConnectTimeout(3000);
asyncScanRequest.setReadTimeout(6000);
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
// 请将下面的地址修改为要检测的语音文件的地址。
task1.put("dataId","voice1");
task1.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/6480bd6be9f14a458162218cea84dfa5.aac");
Map<String, Object> task2 = new LinkedHashMap<String, Object>();
task2.put("dataId","voice2");
task2.put("url", "https://elink-esua-epdc.oss-cn-qingdao.aliyuncs.com/epmet/test/20201208/b566d94fd7114ffb9a203e80c22ebb96.aac");
tasks.add(task1);
tasks.add(task2);
JSONObject data = new JSONObject();
System.out.println("==========Task count:" + tasks.size());
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);
// 如果是语音流检测,则修改为true。
data.put("live", false);
asyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
System.out.println("接口入参:"+JSON.toJSONString(data, true));
try {
HttpResponse httpResponse = client.doAction(asyncScanRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println("接口返参:"+JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONArray taskResults = scrResponse.getJSONArray("data");
for (Object taskResult : taskResults) {
Integer code = ((JSONObject) taskResult).getInteger("code");
if (200 == code) {
final String taskId = ((JSONObject) taskResult).getString("taskId");
System.out.println("submit async task success, taskId = [" + taskId + "]");
} else {
System.out.println("task process fail: " + code);
}
}
} else {
System.out.println("detect not success. code: " + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status: " + httpResponse.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

113
epmet-openapi/epmet-openapi-scan/src/test/java/VoiceAsyncScanResultsRequestSample.java

@ -0,0 +1,113 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.*;
/**
* 查询异步语音检测结果
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/8 22:08
*/
public class VoiceAsyncScanResultsRequestSample extends BaseSample {
public static void main(String[] args) throws Exception {
// 请替换成您自己的AccessKey ID、AccessKey Secret。
IClientProfile profile = DefaultProfile
.getProfile("cn-shanghai",
"LTAI4G6Fv6uTzQbpsayATHq4",
"QevMw1RYCwQUG3RSMPq1J6EAfmSblo");
final IAcsClient client = new DefaultAcsClient(profile);
//提交语音异步检测任务后返回的taskId
pollingScanResult(client, Arrays.asList("vc_f_7WZZ01BCH0q6ZnM3g1OKGG-1tAaZ7", "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi"));
// pollingScanResult(client, "vc_f_6AKaBxy4HdG5bruQ0JwweV-1tAaJi");
}
public static void pollingScanResult(IAcsClient client, List<String> taskIdList) throws InterruptedException {
int failCount = 0;
boolean stop = false;
do {
// 设置每10秒查询一次。
Thread.sleep(10 * 1000);
JSONObject scanResult = getScanResult(client, taskIdList);
System.out.println("接口完整返参:"+JSON.toJSONString(scanResult, true));
if (scanResult == null || 200 != scanResult.getInteger("code")) {
failCount++;
System.out.println("请求失败,get result fail, failCount=" + failCount);
if (scanResult != null) {
System.out.println("请求失败,错误信息errorMsg:" + scanResult.getString("msg"));
}
if (failCount > 20) {
break;
}
continue;
}
JSONArray taskResults = scanResult.getJSONArray("data");
if (taskResults.isEmpty()) {
System.out.println("请求成功,but data is empty");
break;
}
System.out.println("data.size=" + taskResults.size());
for (Object taskResult : taskResults) {
JSONObject result = (JSONObject) taskResult;
Integer code = result.getInteger("code");
String taskId = result.getString("taskId");
if (280 == code) {
System.out.println("taskId=" + taskId + ": processing status: " + result.getString("msg"));
} else if (200 == code) {
System.out.println("taskId=" + taskId + "请求成功,返参:" + JSON.toJSONString(taskResult, true));
stop = true;
} else {
System.out.println("taskId=" + taskId + "请求失败,返参:" + JSON.toJSONString(taskResult, true));
stop = true;
}
}
} while (!stop);
}
private static JSONObject getScanResult(IAcsClient client, List<String> taskIdList) {
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
getResultsRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
getResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
getResultsRequest.setEncoding("utf-8");
getResultsRequest.setRegionId("cn-shanghai");
List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
for (String taskId : taskIdList) {
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("taskId", taskId);
tasks.add(task1);
}
/**
* 请务必设置超时时间
*/
getResultsRequest.setConnectTimeout(3000);
getResultsRequest.setReadTimeout(6000);
try {
getResultsRequest.setHttpContent(JSON.toJSONString(tasks).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
HttpResponse httpResponse = client.doAction(getResultsRequest);
if (httpResponse.isSuccess()) {
return JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
} else {
System.out.println("response not success. status: " + httpResponse.getStatus());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Loading…
Cancel
Save