From 24c4efc1284a2010b10edb5899be92093b2d6708 Mon Sep 17 00:00:00 2001 From: taiyi Date: Sat, 29 Nov 2025 18:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=AA=8C=E8=AF=81=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 7076366..044a643 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,43 @@ -text = "```markdown你好的" +import json -if "```markdown" in text: - text = text.replace("```markdown", "") -print(text) \ No newline at end of file +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) \ No newline at end of file