content_service.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
  5. //region "github.com/huaweicloud/huaweicloud-review_service-go-v3/core/region"
  6. moderation "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3"
  7. "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3/model"
  8. moderationRegion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/moderation/v3/region"
  9. "youngee_b_api/app/vo"
  10. )
  11. type ContentService struct{}
  12. // 内容审核
  13. func (s ContentService) CheckContent(contentCheckParam *vo.ContentCheckParam) (*string, error) {
  14. // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
  15. // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
  16. ak := "CNN8MCCSJFGHL1MXCJRF"
  17. sk := "91arFGHyO7RrzXnZIFSPcLSC6y4P4MTKtkq3qLIy"
  18. projectId := "CNN8MCCSJFGHL1MXCJRF"
  19. eventType := "article"
  20. auth := basic.NewCredentialsBuilder().
  21. WithAk(ak).
  22. WithSk(sk).
  23. WithProjectId(projectId).
  24. Build()
  25. client := moderation.NewModerationClient(
  26. moderation.ModerationClientBuilder().
  27. WithRegion(moderationRegion.ValueOf("cn-north-4")). //把xxx替换成服务所在的区域,例如北京四:cn-north-4。
  28. WithCredential(auth).
  29. Build())
  30. //request := &model.RunTextModerationRequest{}
  31. //request.EnterpriseProjectId = &projectId
  32. //databody := &model.TextDetectionDataReq{
  33. // Text: contentCheckParam.Summary,
  34. //}
  35. //request.Body = &model.TextDetectionReq{
  36. // Data: databody,
  37. // EventType: &eventType,
  38. //}
  39. //response, err := client.RunTextModeration(request)
  40. //if err == nil {
  41. // fmt.Printf("%+v\n", response)
  42. //} else {
  43. // fmt.Println(err)
  44. //}
  45. request := &model.CheckImageModerationRequest{}
  46. var listCategoriesbody = []string{
  47. "porn", "terrorism",
  48. }
  49. eventType = "head_image"
  50. urlImageDetectionReq := "https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1oXRBY.img?w=640&h=360&m=6"
  51. request.Body = &model.ImageDetectionReq{
  52. Url: &urlImageDetectionReq,
  53. Categories: &listCategoriesbody,
  54. EventType: &eventType,
  55. }
  56. response, err := client.CheckImageModeration(request)
  57. if err == nil {
  58. fmt.Printf("%+v\n", response)
  59. } else {
  60. fmt.Println(err)
  61. }
  62. result := response.Result
  63. fmt.Print(result)
  64. return nil, nil
  65. }