From 5b97fd2e6f5c700a3cddccee86dfa8d7cda41fc3 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:33:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0baidu=5Faip=5Fjudge?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/baidu_aip_judge.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 addons/baidu_aip_judge.py diff --git a/addons/baidu_aip_judge.py b/addons/baidu_aip_judge.py new file mode 100644 index 00000000..2b80cb14 --- /dev/null +++ b/addons/baidu_aip_judge.py @@ -0,0 +1,26 @@ +from aip import AipContentCensor + +class BaiduJudge: + def __init__(self, baidu_configs) -> None: + if 'app_id' in baidu_configs and 'api_key' in baidu_configs and 'secret_key' in baidu_configs: + self.app_id = str(baidu_configs['app_id']) + self.api_key = baidu_configs['api_key'] + self.secret_key = baidu_configs['secret_key'] + self.client = AipContentCensor(self.app_id, self.api_key, self.secret_key) + else: + raise ValueError("Baidu configs error! 请填写百度内容审核服务相关配置!") + def judge(self, text): + res = self.client.textCensorUserDefined(text) + if 'conclusionType' not in res: + return False, "百度审核服务未知错误" + if res['conclusionType'] == 1: + return True, "合规" + else: + if 'data' not in res: + return False, "百度审核服务未知错误" + count = len(res['data']) + info = f"百度审核服务发现 {count} 处违规:\n" + for i in res['data']: + info += f"{i['msg']};\n" + info += "\n判断结果:"+res['conclusion'] + return False, info \ No newline at end of file