2025-11-29 18:30:43 +08:00
|
|
|
|
import json
|
2025-10-25 16:45:02 +08:00
|
|
|
|
|
2025-11-29 18:30:43 +08:00
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def text_detection(text):
|
|
|
|
|
|
"""
|
|
|
|
|
|
百度检验文字是否违规
|
|
|
|
|
|
:param text:
|
|
|
|
|
|
:return:
|
|
|
|
|
|
"""
|
|
|
|
|
|
url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=" + get_baidu_access_token()
|
|
|
|
|
|
payload = 'text=' + text
|
|
|
|
|
|
headers = {
|
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response = requests.request("POST", url, headers=headers, data=payload)
|
|
|
|
|
|
content = str(response.text)
|
|
|
|
|
|
data = json.loads(content)
|
|
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
|
|
|
|
# 安全地获取 conclusion 字段,如果不存在则返回默认值
|
|
|
|
|
|
conclusion = data.get('conclusion')
|
|
|
|
|
|
return conclusion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_baidu_access_token():
|
|
|
|
|
|
"""
|
|
|
|
|
|
使用 AK,SK 生成鉴权签名(Access Token),百度信息获取
|
|
|
|
|
|
:return: access_token,或是None(如果错误)
|
|
|
|
|
|
"""
|
|
|
|
|
|
API_KEY = "K7T5muBpNEdx1ebfc2YTag4W"
|
|
|
|
|
|
SECRET_KEY = "U3jkLq3BljN1q9YKnbFou2BY2167HZl6"
|
|
|
|
|
|
|
|
|
|
|
|
url = "https://aip.baidubce.com/oauth/2.0/token"
|
|
|
|
|
|
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
|
|
|
|
|
|
return str(requests.post(url, params=params).json().get("access_token"))
|
|
|
|
|
|
|
|
|
|
|
|
text = "我草泥马"
|
|
|
|
|
|
result = text_detection(text)
|
|
|
|
|
|
print(result)
|