ContentAuditService.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
  8. import com.aliyuncs.green.model.v20180509.TextScanRequest;
  9. import com.aliyuncs.http.FormatType;
  10. import com.aliyuncs.http.HttpResponse;
  11. import com.aliyuncs.http.MethodType;
  12. import com.aliyuncs.http.ProtocolType;
  13. import com.aliyuncs.profile.DefaultProfile;
  14. import com.aliyuncs.profile.IClientProfile;
  15. import com.izouma.nineth.config.AliyunProperties;
  16. import com.izouma.nineth.exception.BusinessException;
  17. import lombok.AllArgsConstructor;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.hibernate.internal.util.StringHelper;
  21. import org.springframework.stereotype.Service;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.*;
  24. @Service
  25. @Slf4j
  26. @AllArgsConstructor
  27. public class ContentAuditService {
  28. private AliyunProperties aliyunProperties;
  29. public boolean auditText(String content) {
  30. if (StringUtils.isBlank(content)) return true;
  31. IClientProfile profile = DefaultProfile.getProfile("cn-shenzhen",
  32. aliyunProperties.getAccessKeyId(), aliyunProperties.getAccessKeySecret());
  33. DefaultProfile.addEndpoint("cn-shenzhen", "Green", "green.cn-shenzhen.aliyuncs.com");
  34. IAcsClient client = new DefaultAcsClient(profile);
  35. TextScanRequest textScanRequest = new TextScanRequest();
  36. textScanRequest.setAcceptFormat(FormatType.JSON);
  37. textScanRequest.setHttpContentType(FormatType.JSON);
  38. textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
  39. textScanRequest.setEncoding("UTF-8");
  40. textScanRequest.setRegionId("cn-shenzhen");
  41. List<Map<String, Object>> tasks = new ArrayList<>();
  42. Map<String, Object> task1 = new LinkedHashMap<>();
  43. task1.put("dataId", UUID.randomUUID().toString());
  44. task1.put("content", content);
  45. tasks.add(task1);
  46. JSONObject data = new JSONObject();
  47. data.put("scenes", List.of("antispam"));
  48. data.put("tasks", tasks);
  49. textScanRequest.setHttpContent(data.toJSONString().getBytes(StandardCharsets.UTF_8), "UTF-8", FormatType.JSON);
  50. textScanRequest.setConnectTimeout(3000);
  51. textScanRequest.setReadTimeout(6000);
  52. try {
  53. HttpResponse httpResponse = client.doAction(textScanRequest);
  54. if (httpResponse.isSuccess()) {
  55. JSONObject scrResponse = JSON
  56. .parseObject(new String(httpResponse.getHttpContent(), StandardCharsets.UTF_8));
  57. log.info(JSON.toJSONString(scrResponse, true));
  58. if (200 == scrResponse.getInteger("code")) {
  59. JSONArray taskResults = scrResponse.getJSONArray("data");
  60. for (Object taskResult : taskResults) {
  61. if (200 == ((JSONObject) taskResult).getInteger("code")) {
  62. JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
  63. for (Object sceneResult : sceneResults) {
  64. String scene = ((JSONObject) sceneResult).getString("scene");
  65. String suggestion = ((JSONObject) sceneResult).getString("suggestion");
  66. // 根据scene和suggetion做相关处理。
  67. // suggestion为pass表示未命中垃圾。suggestion为block表示命中了垃圾,可以通过label字段查看命中的垃圾分类。
  68. log.info("scene = [" + scene + "]");
  69. log.info("suggestion = [" + suggestion + "]");
  70. if ("block".equals(suggestion)) {
  71. return false;
  72. }
  73. }
  74. } else {
  75. log.info("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
  76. }
  77. }
  78. } else {
  79. log.info("detect not success. code:" + scrResponse.getInteger("code"));
  80. }
  81. } else {
  82. log.info("response not success. status:" + httpResponse.getStatus());
  83. }
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. return true;
  88. }
  89. public boolean auditImage(String imageUrl) {
  90. IClientProfile profile = DefaultProfile.getProfile("cn-shenzhen",
  91. aliyunProperties.getAccessKeyId(), aliyunProperties.getAccessKeySecret());
  92. DefaultProfile
  93. .addEndpoint("cn-shenzhen", "Green", "green.cn-shenzhen.aliyuncs.com");
  94. IAcsClient client = new DefaultAcsClient(profile);
  95. ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
  96. // 指定API返回格式。
  97. imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
  98. // 指定请求方法。
  99. imageSyncScanRequest.setMethod(MethodType.POST);
  100. imageSyncScanRequest.setEncoding("utf-8");
  101. // 支持HTTP和HTTPS。
  102. imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
  103. JSONObject httpBody = new JSONObject();
  104. /**
  105. * 设置要检测的风险场景。计费依据此处传递的场景计算。
  106. * 一次请求中可以同时检测多张图片,每张图片可以同时检测多个风险场景,计费按照场景计算。
  107. * 例如,检测2张图片,场景传递porn和terrorism,计费会按照2张图片鉴黄,2张图片暴恐检测计算。
  108. * porn:表示鉴黄场景。
  109. */
  110. List<String> scenes = new ArrayList<>();
  111. scenes.add("porn");
  112. scenes.add("terrorism");
  113. scenes.add("ad");
  114. scenes.add("live");
  115. httpBody.put("scenes", scenes);
  116. /**
  117. * 设置待检测图片。一张图片对应一个task。
  118. * 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。
  119. * 通常情况下批量检测的平均响应时间比单张检测的要长。一次批量提交的图片数越多,响应时间被拉长的概率越高。
  120. * 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task。
  121. */
  122. JSONObject task = new JSONObject();
  123. task.put("dataId", UUID.randomUUID().toString());
  124. // 设置图片链接。URL中有特殊字符,需要对URL进行encode编码。
  125. task.put("url", imageUrl);
  126. task.put("time", new Date());
  127. httpBody.put("tasks", Collections.singletonList(task));
  128. imageSyncScanRequest
  129. .setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
  130. "UTF-8", FormatType.JSON);
  131. /**
  132. * 请设置超时时间。服务端全链路处理超时时间为10秒,请做相应设置。
  133. * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个ReadTimeout异常。
  134. */
  135. imageSyncScanRequest.setConnectTimeout(3000);
  136. imageSyncScanRequest.setReadTimeout(10000);
  137. HttpResponse httpResponse = null;
  138. try {
  139. httpResponse = client.doAction(imageSyncScanRequest);
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. // 服务端接收到请求,完成处理后返回的结果。
  144. if (httpResponse != null && httpResponse.isSuccess()) {
  145. JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils
  146. .newStringUtf8(httpResponse.getHttpContent()));
  147. System.out.println(JSON.toJSONString(scrResponse, true));
  148. int requestCode = scrResponse.getIntValue("code");
  149. // 每一张图片的检测结果。
  150. JSONArray taskResults = scrResponse.getJSONArray("data");
  151. if (200 == requestCode) {
  152. for (Object taskResult : taskResults) {
  153. // 单张图片的处理结果。
  154. int taskCode = ((JSONObject) taskResult).getIntValue("code");
  155. // 图片对应检测场景的处理结果。如果是多个场景,则会有每个场景的结果。
  156. JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
  157. if (200 == taskCode) {
  158. for (Object sceneResult : sceneResults) {
  159. String scene = ((JSONObject) sceneResult).getString("scene");
  160. String suggestion = ((JSONObject) sceneResult).getString("suggestion");
  161. if (StringUtils.equals(suggestion, "block")) {
  162. return false;
  163. }
  164. // 根据scene和suggestion做相关处理。
  165. // 根据不同的suggestion结果做业务上的不同处理。例如,将违规数据删除等。
  166. System.out.println("scene = [" + scene + "]");
  167. System.out.println("suggestion = [" + suggestion + "]");
  168. }
  169. return true;
  170. } else {
  171. // 单张图片处理失败, 原因视具体的情况详细分析。
  172. System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
  173. throw new BusinessException("审核失败");
  174. }
  175. }
  176. } else {
  177. /**
  178. * 表明请求整体处理失败,原因视具体的情况详细分析。
  179. */
  180. System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
  181. throw new BusinessException("审核失败");
  182. }
  183. }
  184. return false;
  185. }
  186. }