1 Star 0 Fork 129

wu/infrastructure

forked from openEuler/infrastructure 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
moderation.py 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
wu 提交于 2023-12-07 10:53 . add moderation.py.
import json
import logging
import requests
import redis
from pilot.configs.model_config import config_parser
logger = logging.getLogger("model_worker")
class Moderation:
def __init__(self) -> None:
self.configs = config_parser
self.token_url = self.configs.get('moderation', 'token_url')
self.moderation_url = self.configs.get('moderation', 'moderation_url')
self.redis_host = self.configs.get('redis', 'redis_host')
self.redis_port = self.configs.getint('redis', 'redis_port')
self.redis_db = self.configs.getint('redis', 'redis_db')
self.redis_password = self.configs.get('redis', 'redis_password')
self.redis = redis.Redis(host=self.redis_host, port=self.redis_host, db=self.redis_db, password=self.redis_password)
def get_token(self, token_data):
token = self.redis.get('moderation_token')
if token:
return token
headers = {
"Content-Type": "application/json;charset=utf-8",
}
response = requests.post(url=self.token_url, data=json.dumps(token_data), headers=headers)
token = response.headers.get("X-Subject-Token")
created_at = '2023-13-01'
self.redis.set('moderation_token', token, 36000)
logger.info('...refresh token succeed...')
return token
def check_text(self, text):
token = self.get_token(self.username, self.password, self.domain, self.project_id)
body = {
"categories": ["terrorism"],
"items": [{
"text": text,
"type": "content"
}
]
}
_header = {
"Content-Type": 'application/json',
'X-Auth-Token': token
}
reslut = requests.post(url=self.moderation_url, data=json.dumps(body), headers=_header)
if reslut.json().get('result').get('suggestion') == 'pass':
return True
return False
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Shell
1
https://gitee.com/kaede10/infrastructure.git
git@gitee.com:kaede10/infrastructure.git
kaede10
infrastructure
infrastructure
master

搜索帮助