ContentSecurityObject.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # @Author : Rocky
  2. # @File : ContentSecurityObject.py
  3. # @Time : 2023/8/18 11:02
  4. from alibabacloud_green20220302.client import Client as Green20220302Client
  5. from alibabacloud_tea_openapi import models as open_api_models
  6. from alibabacloud_green20220302 import models as green_20220302_models
  7. from alibabacloud_tea_util import models as util_models
  8. from Ansjer.config import ALI_ACCESS_KEY_ID, ALI_ACCESS_KEY_SECRET
  9. TEXT_ILLEGAL_LABEL_LIST = ['political_content', 'profanity', 'contraband', 'sexual_content', 'violence',
  10. 'negative_content', 'religion', 'cyberbullying']
  11. IMAGE_ILLEGAL_LABEL_LIST = ['pornographic_adultContent', 'sexual_suggestiveContent', 'political_politicalFigure_1',
  12. 'violent_blood', 'contraband_drug']
  13. class ContentSecurity:
  14. """
  15. 阿里云内容安全服务类
  16. """
  17. def __init__(self):
  18. pass
  19. @staticmethod
  20. def create_client() -> Green20220302Client:
  21. """
  22. 使用AK&SK初始化账号Client
  23. @return: Client
  24. @throws Exception
  25. """
  26. config = open_api_models.Config(
  27. # 必填,您的 AccessKey ID,
  28. access_key_id=ALI_ACCESS_KEY_ID,
  29. # 必填,您的 AccessKey Secret,
  30. access_key_secret=ALI_ACCESS_KEY_SECRET
  31. )
  32. # Endpoint 请参考 https://api.aliyun.com/product/Green
  33. config.endpoint = f'green-cip.cn-shanghai.aliyuncs.com'
  34. return Green20220302Client(config)
  35. def text_review(self, service, service_parameters):
  36. """
  37. 文本审核
  38. @param service: 审核服务类型
  39. @param service_parameters: 审核服务参数集
  40. @return: bool, True: 审核通过, False: 审核不通过
  41. """
  42. client = self.create_client()
  43. text_moderation_request = green_20220302_models.TextModerationRequest(service, service_parameters)
  44. runtime = util_models.RuntimeOptions()
  45. res = client.text_moderation_with_options(text_moderation_request, runtime)
  46. print(res)
  47. if res.status_code != 200:
  48. return False
  49. labels = res.body.data.labels
  50. labels_list = labels.split(',')
  51. for label in labels_list:
  52. if label in TEXT_ILLEGAL_LABEL_LIST:
  53. return False
  54. return True
  55. def image_review(self, service, service_parameters):
  56. """
  57. 图片审核
  58. @param service: 审核服务类型
  59. @param service_parameters: 审核服务参数集
  60. @return: bool, True: 审核通过, False: 审核不通过
  61. """
  62. client = self.create_client()
  63. image_moderation_request = green_20220302_models.ImageModerationRequest(service, service_parameters)
  64. runtime = util_models.RuntimeOptions()
  65. res = client.image_moderation_with_options(image_moderation_request, runtime)
  66. print(res)
  67. if res.status_code != 200:
  68. return False
  69. # 获取审核结果
  70. result = res.body
  71. if result.code != 200:
  72. return False
  73. result_list = result.data.result
  74. # 配到风险标签且置信分高于80时返回False
  75. for result in result_list:
  76. if result.label in IMAGE_ILLEGAL_LABEL_LIST:
  77. if result.confidence >= 80:
  78. return False
  79. return True