From f6c60cf574973928f1a93d4bd056a5464ef8498d Mon Sep 17 00:00:00 2001 From: Leufolium Date: Mon, 29 Apr 2024 14:17:29 +0800 Subject: [PATCH] by Robin at 20240429 --- library/contentaudit/textaudit/textaudit.go | 45 +++++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/library/contentaudit/textaudit/textaudit.go b/library/contentaudit/textaudit/textaudit.go index 195ac85e..459e090b 100644 --- a/library/contentaudit/textaudit/textaudit.go +++ b/library/contentaudit/textaudit/textaudit.go @@ -44,7 +44,7 @@ func Run(batchId string) (successNum int, failNum int, err error) { ctrlBlock := NewTextAuditTaskBatchControlBlock(textaudittasks, batchId) // 2.创建请求 - req, err := createScanTextRequest(ctrlBlock) + reqs, err := createScanTextRequest(ctrlBlock) if err != nil { logger.Info("Create Scan TextRequest fail: %v", err) handleBatchError(ctrlBlock, err) @@ -55,21 +55,27 @@ func Run(batchId string) (successNum int, failNum int, err error) { runtime := &teautils.RuntimeOptions{ ConnectTimeout: tea.Int(30000), } - _result, err := defaultTextAuditClient.ScanTextWithOptions(req, runtime) - if err != nil { - logger.Error("ScanTextWithOptions fail : %v", err) - handleBatchError(ctrlBlock, err) - return + + results := make([]*textaudit.ScanTextResponseBodyDataElements, 0) + for _, req := range reqs { + var _result *textaudit.ScanTextResponse + _result, err = defaultTextAuditClient.ScanTextWithOptions(req, runtime) + if err != nil { + logger.Error("ScanTextWithOptions fail : %v", err) + handleBatchError(ctrlBlock, err) + return + } + results = append(results, _result.Body.Data.Elements...) } // 4.处理应答 - err = handleScanTextResponse(ctrlBlock, _result) + err = handleScanTextResponseBodyDataElements(ctrlBlock, results) successNum = len(textaudittasks) return } -func createScanTextRequest(ctrlBlock *TextAuditTaskBatchControlBlock) (request *textaudit.ScanTextRequest, err error) { +func createScanTextRequest(ctrlBlock *TextAuditTaskBatchControlBlock) (requests []*textaudit.ScanTextRequest, err error) { reqTasks := make([]*textaudit.ScanTextRequestTasks, 0) for _, taskCtrlBlock := range ctrlBlock.TaskCtrlBlocks { reqTasks = append(reqTasks, &textaudit.ScanTextRequestTasks{ @@ -84,16 +90,29 @@ func createScanTextRequest(ctrlBlock *TextAuditTaskBatchControlBlock) (request * } } - request = &textaudit.ScanTextRequest{ - Labels: scanLabels, - Tasks: reqTasks, + // 阿里云一次最多审10条,将待审图片按10条拆分 + requests = make([]*textaudit.ScanTextRequest, 0) + i := 0 + for ; i < len(reqTasks)/10; i++ { + request := &textaudit.ScanTextRequest{ + Labels: scanLabels, + Tasks: reqTasks[i*10 : (i+1)*10], + } + requests = append(requests, request) } + if i*10 < len(reqTasks) { + request := &textaudit.ScanTextRequest{ + Labels: scanLabels, + Tasks: reqTasks[i*10 : (i+1)*10], + } + requests = append(requests, request) + } + logger.Info("本次打包:%v", reqTasks) return } -func handleScanTextResponse(ctrlBlock *TextAuditTaskBatchControlBlock, resp *textaudit.ScanTextResponse) (err error) { - results := resp.Body.Data.Elements +func handleScanTextResponseBodyDataElements(ctrlBlock *TextAuditTaskBatchControlBlock, results []*textaudit.ScanTextResponseBodyDataElements) (err error) { taskCtrlBlocks := ctrlBlock.TaskCtrlBlocks actionMap := ctrlBlock.ActionMap for i, result := range results {