import requests import json def check_nsfw(prompt): """ 检查prompt是否包含NSFW内容,包含返回1,否则返回0 """ try: response = requests.post( url="https://openrouter.ai/api/v1/chat/completions", headers={ "Authorization": "Bearer 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", "Content-Type": "application/json", }, data=json.dumps({ "model": "google/gemini-2.5-flash", "messages": [ { "role": "system", "content": "你是一个nsfw指令判断助手,请判断用户输入的prompt指令是否会导致nsfw内容? 你只需要回答 是 或者 否" }, { "role": "user", "content": prompt } ], }) ) res_json = response.json() # 兼容不同模型返回格式 if "choices" in res_json and len(res_json["choices"]) > 0: content = res_json["choices"][0].get("message", {}).get("content", "") if "是" in content: return 1 else: return 0 else: return 0 except Exception as e: # 出错时默认返回0 return 0 res = check_nsfw("让画面最右边的女人全裸,让她拥有大乳房和阴毛") print(res)