|
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
public class HuaweiCloudModerationService {
|
|
@@ -18,7 +19,13 @@ public class HuaweiCloudModerationService {
|
|
|
@Autowired
|
|
|
private HuaweiCloudConfig huaweiCloudConfig;
|
|
|
|
|
|
- public Boolean runTextModeration(String text) {
|
|
|
+ /**
|
|
|
+ * 文案审核,只要有一个不通过,都认为不通过
|
|
|
+ * @param texts
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean runTextModeration(List<String> texts) {
|
|
|
+ Boolean result = true;
|
|
|
ICredential credential = new BasicCredentials()
|
|
|
.withProjectId(huaweiCloudConfig.getProjectId())
|
|
|
.withAk(huaweiCloudConfig.getAccessKey())
|
|
@@ -32,26 +39,36 @@ public class HuaweiCloudModerationService {
|
|
|
RunTextModerationRequest request = new RunTextModerationRequest();
|
|
|
TextDetectionReq body = new TextDetectionReq();
|
|
|
TextDetectionDataReq databody = new TextDetectionDataReq();
|
|
|
- databody.withText(text);
|
|
|
body.withCategories(Arrays.asList("terrorism", "porn", "abuse", "ban"));
|
|
|
- body.withData(databody);
|
|
|
- body.withEventType("comment");
|
|
|
- request.withBody(body);
|
|
|
- try {
|
|
|
- RunTextModerationResponse runTextModerationResponse = client.runTextModeration(request);
|
|
|
- if("pass".equals(runTextModerationResponse.getResult().getSuggestion())){
|
|
|
- return true;
|
|
|
- }else {
|
|
|
- return false;
|
|
|
+ for (String text : texts) {
|
|
|
+ databody.withText(text);
|
|
|
+ body.withData(databody);
|
|
|
+ body.withEventType("comment");
|
|
|
+ request.withBody(body);
|
|
|
+ try {
|
|
|
+ RunTextModerationResponse response = client.runTextModeration(request);
|
|
|
+ if("pass".equals(response.getResult().getSuggestion())){
|
|
|
+ }else {
|
|
|
+ result = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch (ServiceResponseException e) {
|
|
|
+ // 处理异常
|
|
|
+ e.printStackTrace();
|
|
|
+ result = false;
|
|
|
+ break;
|
|
|
}
|
|
|
- } catch (ServiceResponseException e) {
|
|
|
- // 处理异常
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- public Boolean runImageModeration(String url) {
|
|
|
+ /**
|
|
|
+ * 只要有一张图片不通过,就认为不通过
|
|
|
+ * @param urls
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Boolean runImageModeration(List<String> urls) {
|
|
|
+ Boolean result = true;
|
|
|
ICredential credential = new BasicCredentials()
|
|
|
.withProjectId(huaweiCloudConfig.getProjectId())
|
|
|
.withAk(huaweiCloudConfig.getAccessKey())
|
|
@@ -65,20 +82,24 @@ public class HuaweiCloudModerationService {
|
|
|
CheckImageModerationRequest request = new CheckImageModerationRequest();
|
|
|
ImageDetectionReq body = new ImageDetectionReq();
|
|
|
body.withCategories(Arrays.asList("terrorism", "porn", "abuse", "ban"));
|
|
|
- body.withUrl(url);
|
|
|
body.withEventType("head_image");
|
|
|
- request.withBody(body);
|
|
|
- try {
|
|
|
- CheckImageModerationResponse response = client.checkImageModeration(request);
|
|
|
- if("pass".equals(response.getResult().getSuggestion())){
|
|
|
- return true;
|
|
|
- }else {
|
|
|
- return false;
|
|
|
+ for (String url : urls) {
|
|
|
+ body.withUrl(url);
|
|
|
+ request.withBody(body);
|
|
|
+ try {
|
|
|
+ CheckImageModerationResponse response = client.checkImageModeration(request);
|
|
|
+ if("pass".equals(response.getResult().getSuggestion())){
|
|
|
+ }else {
|
|
|
+ result = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch (ServiceResponseException e) {
|
|
|
+ // 处理异常
|
|
|
+ e.printStackTrace();
|
|
|
+ result = false;
|
|
|
+ break;
|
|
|
}
|
|
|
- } catch (ServiceResponseException e) {
|
|
|
- // 处理异常
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
}
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|