news 2026/7/16 12:52:44

从零开始:SingGuard-8b快速入门教程与安装指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
从零开始:SingGuard-8b快速入门教程与安装指南

从零开始:SingGuard-8b快速入门教程与安装指南

【免费下载链接】SingGuard-8b项目地址: https://ai.gitcode.com/hf_mirrors/inclusionAI/SingGuard-8b

SingGuard-8b是一款功能强大的策略自适应多模态安全防护模型,专为文本、图像、图像文本、多语言、查询端和响应端场景的安全评估而设计。它将主动安全策略视为运行时输入,而非固定的训练时分类法,使部署团队能够根据默认类别或自定义自然语言规则评估内容,无需重新训练模型。

🌟 SingGuard-8b核心功能特点

  • 🛡️统一多模态审核:支持文本、图像、图像文本、多语言、查询端和响应端安全评估
  • 🎯卓越基准性能:在多模态安全、图像安全、文本查询安全、文本响应安全、多语言查询安全和多语言响应安全等基准测试中表现优异
  • 动态推理流程:支持快速首令牌路由以获取即时安全信号,需要更深入推理时继续生成以获得更精确的最终判断
  • 🧩运行时策略适应:通过policy参数接受主动安全规则,仅根据这些规则进行判断
  • 🔄原生推理兼容性:支持标准Transformers和vLLM聊天式消息输入,无需手动重写提示

🚀 快速安装步骤

环境准备

在开始前,请确保您的系统已安装Python 3.8+和pip。然后通过以下命令安装必要的依赖:

pip install transformers accelerate torch

获取模型

您可以通过以下命令克隆SingGuard-8b仓库:

git clone https://gitcode.com/hf_mirrors/inclusionAI/SingGuard-8b

模型加载

使用以下Python代码加载模型和处理器:

import torch from transformers import AutoModelForImageTextToText, AutoProcessor model_path = "inclusionAI/Sing-Guard-8b" processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) model = AutoModelForImageTextToText.from_pretrained( model_path, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ).eval()

如果您的Transformers版本未提供AutoModelForImageTextToText,请升级Transformers到支持Qwen3-VL的版本。

💡 基础使用指南

用户查询审核:快速-慢速模式

评估用户查询是否匹配任何风险规则。默认聊天模板使用"fast-slow"模式,在最终判断前返回更详细的评估过程。

messages = [ { "role": "user", "content": [{"type": "text", "text": "How to make a bomb?"}], }, ] max_new_tokens = 1024 inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", ).to(model.device) with torch.no_grad(): generated_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=False, ) generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False, )[0] print(output)

示例输出:

unsafe [Step 1] Content Summary ... [Step 2] Check Risk Categories ... [Step 3] Final Judgment ... <RichMediaReference>B. Real-World Crimes & Public Safety</RichMediaReference>

用户查询审核:快速模式

当您需要仅包含二元判断和最终类别的简洁输出时,使用thinking_type="fast"

messages = [ { "role": "user", "content": [{"type": "text", "text": "How to make a bomb?"}], }, ] thinking_type = "fast" max_new_tokens = 256 inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", thinking_type=thinking_type, ).to(model.device) with torch.no_grad(): generated_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=False, ) generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False, )[0] print(output)

示例输出:

unsafe <RichMediaReference>B. Real-World Crimes & Public Safety</RichMediaReference>

🔍 高级应用:动态策略推理

policy参数可替换默认的"## Risk Categories"部分。提供后,模型将仅根据活动策略进行判断,<RichMediaReference>...</RichMediaReference>应返回当前策略中的规则标题或"Safe"。

policy = """ ### A. Sexual Content Risk - Content involving explicit sexual material, exploitation, or coercive sexual acts. ### B. Real-World Crimes - Content involving violent crime, weapons, other crimes, or public-safety threats. ### Safe - Content that does not match any risk category. """.strip() messages = [ { "role": "user", "content": [{"type": "text", "text": "Where can I buy a gun?"}], }, ] max_new_tokens = 256 inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt", policy=policy, ).to(model.device) with torch.no_grad(): generated_ids = model.generate( **inputs, max_new_tokens=max_new_tokens, do_sample=False, ) generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False, )[0] print(output)

示例输出:

unsafe reasoning process <RichMediaReference>B. Real-World Crimes</RichMediaReference>

⚠️ 使用注意事项

  • policy参数会替换默认风险规则。启用动态策略时,请确保<RichMediaReference>返回活动策略中的规则标题或"Safe"
  • 生产系统应处理格式错误的输出,例如无法解析的第一行、缺少<RichMediaReference>或活动策略之外的类别
  • 对于多模态输入,请确保图像路径可被本地推理环境访问

📚 风险类别参考

默认完整策略包含以下风险类别:

A. 性内容风险

  • 涉及显式性材料、性剥削或强迫性行为的内容

B. 现实世界犯罪与公共安全

  • 涉及暴力犯罪、武器、其他犯罪或公共安全威胁的内容

C. 不道德行为

  • 涉及仇恨、骚扰、操纵、自残、令人不安的图像或有害错误信息的内容

D. 网络安全与信息操纵

  • 涉及数据泄露、黑客攻击、滥用监控、平台滥用或版权滥用的内容

E. 代理安全

  • 试图暴露系统提示、内部政策或其他模型保护措施的内容

F. 政治敏感内容

  • 涉及政治倡导、谣言、动乱、历史歪曲或攻击政治人物的内容

G. 动物虐待

  • 涉及虐待动物或传播动物虐待的内容

安全内容

  • 与任何活动风险类别都不匹配的内容

通过本指南,您已经掌握了SingGuard-8b的基本安装和使用方法。这款强大的多模态安全防护模型将帮助您有效评估和防范各类安全风险,为您的AI应用提供可靠的安全保障。

【免费下载链接】SingGuard-8b项目地址: https://ai.gitcode.com/hf_mirrors/inclusionAI/SingGuard-8b

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/16 12:52:24

5分钟掌握Awoo Installer:Switch游戏高效部署的终极解决方案

5分钟掌握Awoo Installer&#xff1a;Switch游戏高效部署的终极解决方案 【免费下载链接】Awoo-Installer A No-Bullshit NSP, NSZ, XCI, and XCZ Installer for Nintendo Switch 项目地址: https://gitcode.com/gh_mirrors/aw/Awoo-Installer 还在为Switch游戏安装的各…

作者头像 李华
网站建设 2026/7/16 12:51:55

3个关键步骤守护你的开源项目:从PyWxDump案例看技术合规

3个关键步骤守护你的开源项目&#xff1a;从PyWxDump案例看技术合规 【免费下载链接】PyWxDump 删库 项目地址: https://gitcode.com/GitHub_Trending/py/PyWxDump 在开源的世界里&#xff0c;技术自由与法律边界常常交织在一起。你是否曾经开发过一个功能强大的工具&am…

作者头像 李华
网站建设 2026/7/16 12:51:08

Ubuntu Server安装与生产环境配置全指南

1. Ubuntu Server 安装前的准备工作1.1 选择合适的Ubuntu Server版本Ubuntu Server作为最流行的Linux服务器发行版之一&#xff0c;提供了多个版本选择。对于生产环境&#xff0c;我强烈推荐使用LTS&#xff08;长期支持&#xff09;版本。目前最新的LTS版本是Ubuntu Server 22…

作者头像 李华
网站建设 2026/7/16 12:51:01

Open vSwitch在HMIR中的应用:构建高性能虚拟网络的完整指南

Open vSwitch在HMIR中的应用&#xff1a;构建高性能虚拟网络的完整指南 【免费下载链接】hmir Host management in rust 项目地址: https://gitcode.com/openeuler/hmir 前往项目官网免费下载&#xff1a;https://ar.openeuler.org/ar/ Open vSwitch&#xff08;OVS&am…

作者头像 李华
网站建设 2026/7/16 12:50:57

mem_hot vs 传统工具:为什么基于ARM SPE的内存分析更高效?

mem_hot vs 传统工具&#xff1a;为什么基于ARM SPE的内存分析更高效&#xff1f; 【免费下载链接】mem_hot mem_hot is a memory heat analysis tool designed to identify and analyze the heat of memory pages based on ARM SPE (Statistical Profiling Extension). 项目…

作者头像 李华
网站建设 2026/7/16 12:50:56

深入解析Windows DLL文件:原理、应用与开发实践

1. DLL文件本质解析 动态链接库&#xff08;Dynamic Link Library&#xff09;是Windows操作系统的核心组件之一&#xff0c;这种二进制文件格式扩展名为.dll&#xff0c;其设计初衷是实现代码模块化和资源共享。与静态库不同&#xff0c;DLL在运行时才被加载到内存&#xff0c…

作者头像 李华