ContentSecurityObject.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, LOGGER
  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. #
  36. # def text_review(self, service, service_parameters):
  37. # """
  38. # 文本审核
  39. # @param service: 审核服务类型
  40. # @param service_parameters: 审核服务参数集
  41. # @return: bool, True: 审核通过, False: 审核不通过
  42. # """
  43. # client = self.create_client()
  44. # text_moderation_request = green_20220302_models.TextModerationRequest(service, service_parameters)
  45. # runtime = util_models.RuntimeOptions()
  46. # res = client.text_moderation_with_options(text_moderation_request, runtime)
  47. # print(res)
  48. # if res.status_code != 200:
  49. # return False
  50. # labels = res.body.data.labels
  51. # labels_list = labels.split(',')
  52. # for label in labels_list:
  53. # if label in TEXT_ILLEGAL_LABEL_LIST:
  54. # return False
  55. # return True
  56. #
  57. # def image_review(self, service, service_parameters):
  58. # """
  59. # 图片审核
  60. # @param service: 审核服务类型
  61. # @param service_parameters: 审核服务参数集
  62. # @return: bool, True: 审核通过, False: 审核不通过
  63. # """
  64. # client = self.create_client()
  65. # image_moderation_request = green_20220302_models.ImageModerationRequest(service, service_parameters)
  66. # runtime = util_models.RuntimeOptions()
  67. # res = client.image_moderation_with_options(image_moderation_request, runtime)
  68. # print(res)
  69. # LOGGER.info('图片审核响应内容:{}'.format(res))
  70. # if res.status_code != 200:
  71. # return False
  72. # # 获取审核结果
  73. # result = res.body
  74. # if result.code != 200:
  75. # return False
  76. # result_list = result.data.result
  77. # # 配到风险标签且置信分高于80时返回False
  78. # for result in result_list:
  79. # if result.label in IMAGE_ILLEGAL_LABEL_LIST:
  80. # if result.confidence >= 80:
  81. # return False
  82. # return True