123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- # @Author : Rocky
- # @File : ContentSecurityObject.py
- # @Time : 2023/8/18 11:02
- from alibabacloud_green20220302.client import Client as Green20220302Client
- from alibabacloud_tea_openapi import models as open_api_models
- from alibabacloud_green20220302 import models as green_20220302_models
- from alibabacloud_tea_util import models as util_models
- from Ansjer.config import ALI_ACCESS_KEY_ID, ALI_ACCESS_KEY_SECRET
- ILLEGAL_LABEL_LIST = ['political_content', 'profanity', 'contraband', 'sexual_content', 'violence', 'negative_content',
- 'religion', 'cyberbullying']
- class ContentSecurity:
- """
- 阿里云内容安全服务类
- """
- def __init__(self):
- pass
- @staticmethod
- def create_client() -> Green20220302Client:
- """
- 使用AK&SK初始化账号Client
- @return: Client
- @throws Exception
- """
- config = open_api_models.Config(
- # 必填,您的 AccessKey ID,
- access_key_id=ALI_ACCESS_KEY_ID,
- # 必填,您的 AccessKey Secret,
- access_key_secret=ALI_ACCESS_KEY_SECRET
- )
- # Endpoint 请参考 https://api.aliyun.com/product/Green
- config.endpoint = f'green-cip.cn-shanghai.aliyuncs.com'
- return Green20220302Client(config)
- def text_review(self, service, service_parameters):
- """
- @param service: 审核服务类型
- @param service_parameters: 审核服务参数集
- @return: bool, True: 审核通过, False: 审核不通过
- """
- client = self.create_client()
- text_moderation_request = green_20220302_models.TextModerationRequest(service, service_parameters)
- runtime = util_models.RuntimeOptions()
- res = client.text_moderation_with_options(text_moderation_request, runtime)
- print(res)
- if res.status_code != 200:
- return False
- labels = res.body.data.labels
- labels_list = labels.split(',')
- for label in labels_list:
- if label in ILLEGAL_LABEL_LIST:
- return False
- return True
|