QChatGPT/pkg/qqbot/filter.py

78 lines
3.0 KiB
Python
Raw Normal View History

# 敏感词过滤模块
2022-12-11 17:17:33 +08:00
import re
2023-03-04 21:02:10 +08:00
import requests
import json
import logging
2022-12-11 17:17:33 +08:00
2023-03-04 21:02:10 +08:00
class ReplyFilter:
2022-12-11 17:17:33 +08:00
sensitive_words = []
# 默认值( 兼容性考虑 )
baidu_check = False
baidu_api_key = ""
baidu_secret_key = ""
inappropriate_message_tips = "[百度云]请珍惜机器人,当前返回内容不合规"
2022-12-11 17:17:33 +08:00
def __init__(self, sensitive_words: list):
self.sensitive_words = sensitive_words
import config
2023-03-05 01:17:23 +08:00
if hasattr(config, 'baidu_check') and hasattr(config, 'baidu_api_key') and hasattr(config, 'baidu_secret_key'):
self.baidu_check = config.baidu_check
self.baidu_api_key = config.baidu_api_key
self.baidu_secret_key = config.baidu_secret_key
self.inappropriate_message_tips = config.inappropriate_message_tips
2022-12-11 17:17:33 +08:00
def is_illegal(self, message: str) -> bool:
processed = self.process(message)
if processed != message:
return True
return False
2022-12-11 17:17:33 +08:00
def process(self, message: str) -> str:
2023-03-05 01:17:23 +08:00
# 本地关键词屏蔽
for word in self.sensitive_words:
match = re.findall(word, message)
if len(match) > 0:
for i in range(len(match)):
message = message.replace(match[i], "*" * len(match[i]))
2023-03-04 21:02:10 +08:00
# 百度云审核
2023-03-05 01:17:23 +08:00
if self.baidu_check:
# 百度云审核URL
baidu_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + \
str(requests.post("https://aip.baidubce.com/oauth/2.0/token",
params={"grant_type": "client_credentials",
"client_id": self.baidu_api_key,
"client_secret": self.baidu_secret_key}).json().get("access_token"))
2023-03-05 01:17:23 +08:00
2023-03-04 21:02:10 +08:00
# 百度云审核
payload = "text=" + message
logging.info("向百度云发送:" + payload)
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
2023-03-05 01:17:23 +08:00
if isinstance(payload, str):
payload = payload.encode('utf-8')
response = requests.request("POST", baidu_url, headers=headers, data=payload)
2023-03-04 21:02:10 +08:00
response_dict = json.loads(response.text)
2023-03-05 01:17:23 +08:00
2023-03-04 21:02:10 +08:00
if "error_code" in response_dict:
error_msg = response_dict.get("error_msg")
logging.warning(f"百度云判定出错,错误信息:{error_msg}")
2023-03-04 21:02:10 +08:00
conclusion = f"百度云判定出错,错误信息:{error_msg}\n以下是原消息:{message}"
else:
conclusion = response_dict["conclusion"]
if conclusion in ("合规"):
logging.info(f"百度云判定结果:{conclusion}")
return message
else:
logging.warning(f"百度云判定结果:{conclusion}")
conclusion = self.inappropriate_message_tips
2023-03-04 21:02:10 +08:00
# 返回百度云审核结果
return conclusion
2023-03-05 01:17:23 +08:00
2022-12-11 17:17:33 +08:00
return message