Skip to content

feat: Support Baidu Content Audit Platform #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions reviewer-baidu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Baidu Reviewer

> Baidu Content Audit Platform is a service platform for intelligent auditing of multimedia content. It can be used to filter spam.

## Config

- `api_key`: Baidu App's API key
- `secret_key`: Baidu App's Secret Key

## Document

- https://cloud.baidu.com/doc/ANTIPORN/s/Vk3h6xaga
163 changes: 163 additions & 0 deletions reviewer-baidu/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package basic

import (
"embed"
"encoding/json"
"github.com/apache/incubator-answer-plugins/util"

"github.com/apache/incubator-answer-plugins/reviewer-baidu/i18n"
"github.com/apache/incubator-answer/plugin"
"github.com/lufei/baidu-golang-sdk/aip/censor"
myI18n "github.com/segmentfault/pacman/i18n"
"github.com/segmentfault/pacman/log"
)

//go:embed info.yaml
var Info embed.FS

type Reviewer struct {
Config *ReviewerConfig
}

type ReviewerConfig struct {
APIKey string `json:"api_key"`
SecretKey string `json:"secret_key"`
SpamFiltering string `json:"span_filtering"`
}

func init() {
plugin.Register(&Reviewer{
Config: &ReviewerConfig{},
})
}

func (r *Reviewer) Info() plugin.Info {
info := &util.Info{}
info.GetInfo(Info)

return plugin.Info{
Name: plugin.MakeTranslator(i18n.InfoName),
SlugName: info.SlugName,
Description: plugin.MakeTranslator(i18n.InfoDescription),
Author: info.Author,
Version: info.Version,
Link: info.Link,
}
}

func (r *Reviewer) Review(content *plugin.ReviewContent) (result *plugin.ReviewResult) {
result = &plugin.ReviewResult{Approved: true}
if len(r.Config.APIKey) == 0 {
return result
}
// If the author is admin, no need to review
if content.Author.Role > 1 {
return result
}

client := censor.NewClient(r.Config.APIKey, r.Config.SecretKey)
textCensorResult, err := client.TextCensor(content.Title+"\n"+content.Content, content.IP)
if err != nil {
log.Errorf("Request baidu to check failed: %v", err)
return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

var jsonMap map[string]interface{}
err = json.Unmarshal([]byte(textCensorResult), &jsonMap)
if err != nil {
return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

if conclusionType, ok := jsonMap["conclusionType"].(float64); ok {
if conclusionType == 1.0 {
return result
}
}

if r.Config.SpamFiltering == "delete" {
return handleReviewError(content, plugin.ReviewStatusDeleteDirectly)
}

return handleReviewError(content, plugin.ReviewStatusNeedReview)
}

func (r *Reviewer) ConfigFields() []plugin.ConfigField {
return []plugin.ConfigField{
{
Name: "api_key",
Type: plugin.ConfigTypeInput,
Title: plugin.MakeTranslator(i18n.ConfigAPIKeyLabel),
Description: plugin.MakeTranslator(i18n.ConfigAPIKeyDescription),
Required: true,
UIOptions: plugin.ConfigFieldUIOptions{
InputType: plugin.InputTypeText,
Label: plugin.MakeTranslator(i18n.ConfigAPIKeyLabel),
},
Value: r.Config.APIKey,
},
{
Name: "secret_key",
Type: plugin.ConfigTypeInput,
Title: plugin.MakeTranslator(i18n.ConfigSecretKeyTitle),
Description: plugin.MakeTranslator(i18n.ConfigSecretKeyDescription),
Required: true,
UIOptions: plugin.ConfigFieldUIOptions{
InputType: plugin.InputTypeText,
Label: plugin.MakeTranslator(i18n.ConfigSecretKeyLabel),
},
Value: r.Config.SecretKey,
},
{
Name: "span_filtering",
Type: plugin.ConfigTypeSelect,
Title: plugin.MakeTranslator(i18n.ConfigSpanFilteringTitle),
Required: false,
UIOptions: plugin.ConfigFieldUIOptions{},
Value: r.Config.SpamFiltering,
Options: []plugin.ConfigFieldOption{
{
Value: "review",
Label: plugin.MakeTranslator(i18n.ConfigSpanFilteringReview),
},
{
Value: "delete",
Label: plugin.MakeTranslator(i18n.ConfigSpanFilteringDelete),
},
},
},
}
}

func handleReviewError(content *plugin.ReviewContent, ReviewStatus plugin.ReviewStatus) *plugin.ReviewResult {
return &plugin.ReviewResult{
Approved: false,
ReviewStatus: ReviewStatus,
Reason: plugin.TranslateWithData(myI18n.Language(content.Language), i18n.CommentNeedReview, nil),
}
}

func (r *Reviewer) ConfigReceiver(config []byte) error {
c := &ReviewerConfig{}
_ = json.Unmarshal(config, c)
r.Config = c
return nil
}
48 changes: 48 additions & 0 deletions reviewer-baidu/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module github.com/apache/incubator-answer-plugins/reviewer-baidu

go 1.21.3

require (
github.com/apache/incubator-answer v1.4.0
github.com/apache/incubator-answer-plugins/util v1.0.2
github.com/lufei/baidu-golang-sdk v0.0.0-20241007032158-d85deddc0d61
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f
)

require (
github.com/LinkinStars/go-i18n/v2 v2.2.2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading