Privasis-Cleaner-4B API集成教程:在Python、JavaScript等语言中的应用
Privasis-Cleaner-4B API集成教程:在Python、JavaScript等语言中的应用
【免费下载链接】Privasis-Cleaner-4B项目地址: https://ai.gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4B
Privasis-Cleaner-4B是一款轻量级文本清理模型,专为从文本中移除或抽象敏感信息而设计。本文将详细介绍如何在Python、JavaScript等主流编程语言中集成Privasis-Cleaner-4B API,帮助开发者轻松实现文本数据的隐私保护。
快速了解Privasis-Cleaner-4B
Privasis-Cleaner-4B基于Qwen3 4B Instruct模型构建,通过37K指令-输入-输出三元组进行微调,能够根据用户提供的清理指令(如移除姓名、日期、位置等敏感信息类别)处理原始文本并返回合规的清理结果。该模型适用于数据工程师、机器学习从业者和处理敏感文本的组织,可用于PII/PHI自动脱敏、隐私保护研究的预处理、内容清理以及合规管道(GDPR、HIPAA等)。
环境准备与安装
系统要求
- 操作系统:Linux
- 硬件支持:NVIDIA H100-80GB、NVIDIA A100等GPU
- 运行时引擎:Privasis-Cleaner-4B
- 依赖库:transformers、vllm、torch等
安装步骤
首先克隆仓库:
git clone https://gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4B cd Privasis-Cleaner-4B然后安装所需依赖:
pip install transformers vllm torchPython语言集成指南
使用Transformers库
Transformers库是集成Privasis-Cleaner-4B的常用方式,以下是详细步骤:
- 导入必要的库
from transformers import AutoModelForCausalLM, AutoTokenizer- 加载模型和分词器
model_id = "nvidia/Privasis-Cleaner-4B" tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")- 构建清理指令和待处理文本
instruction = "Remove all person names, exact dates, and exact locations." text = "On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up."- 构建提示词
prompt = ( f"**Sanitization Instruction:**\n{instruction}\n" "Do not output any explanation or other comment than the sanitized text.\n\n" f"**Text to sanitize:**\n{text}\n\n" "**Sanitized Text:**" )- 处理输入并生成清理结果
inputs = tokenizer.apply_chat_template( [{"role": "user", "content": prompt}], add_generation_prompt=True, enable_thinking=False, # 直接输出清理后的文本 return_tensors="pt", ).to(model.device) output = model.generate(inputs, max_new_tokens=4096, do_sample=False) response = tokenizer.decode(output[0][inputs.shape[-1]:], skip_special_tokens=True) # 移除可能的"Sanitized Text:"头部 if "Sanitized Text:" in response: response = response.split("Sanitized Text:")[-1] print(response.strip())使用vLLM服务
vLLM提供了OpenAI兼容的API服务,使用起来更加便捷:
- 启动vLLM服务
vllm serve nvidia/Privasis-Cleaner-4B --port 8000- 使用Python客户端调用API
from openai import OpenAI client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY") instruction = "Remove all person names, exact dates, and exact locations." text = "On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up." prompt = ( f"**Sanitization Instruction:**\n{instruction}\n" "Do not output any explanation or other comment than the sanitized text.\n\n" f"**Text to sanitize:**\n{text}\n\n" "**Sanitized Text:**" ) resp = client.chat.completions.create( model="nvidia/Privasis-Cleaner-4B", messages=[{"role": "user", "content": prompt}], temperature=0.0, max_tokens=4096, ) print(resp.choices[0].message.content.strip())JavaScript语言集成指南
虽然官方未直接提供JavaScript示例,但我们可以通过调用vLLM提供的OpenAI兼容API来实现集成。以下是使用Node.js的示例:
- 安装必要的依赖
npm install openai- 编写JavaScript代码
const { OpenAI } = require('openai'); const client = new OpenAI({ baseURL: 'http://localhost:8000/v1', apiKey: 'EMPTY', }); async function sanitizeText() { const instruction = "Remove all person names, exact dates, and exact locations."; const text = "On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up."; const prompt = ( `**Sanitization Instruction:**\n${instruction}\n` + "Do not output any explanation or other comment than the sanitized text.\n\n" + `**Text to sanitize:**\n${text}\n\n` + "**Sanitized Text:**" ); const response = await client.chat.completions.create({ model: "nvidia/Privasis-Cleaner-4B", messages: [{ role: "user", content: prompt }], temperature: 0.0, max_tokens: 4096, }); console.log(response.choices[0].message.content.strip()); } sanitizeText();API调用最佳实践
输入格式规范
Privasis-Cleaner-4B的输入需要遵循特定的格式,以确保模型正确理解清理指令和待处理文本:
**Sanitization Instruction:** {instruction} Do not output any explanation or other comment than the sanitized text. **Text to sanitize:** {text} **Sanitized Text:**其中,{instruction}是用户指定的清理指令,{text}是需要处理的原始文本。
常见问题解决
模型输出包含"Sanitized Text:"头部
- 解决方案:在处理响应时,检查并移除该头部,如Python示例中所示。
长文本处理
- 模型支持最长262,144 tokens的输入,对于超长文本,建议进行分段处理。
性能优化
- 使用vLLM服务可以显著提高推理速度,特别是在处理大量文本时。
总结
Privasis-Cleaner-4B作为一款高效的文本清理模型,为开发者提供了简单易用的API集成方式。通过本文介绍的方法,您可以轻松在Python、JavaScript等语言中集成该模型,实现敏感信息的自动移除,为数据隐私保护提供有力支持。无论是数据预处理、内容审核还是合规需求,Privasis-Cleaner-4B都能成为您的得力助手。
如需更多详细信息,请参考项目中的README.md文件,或查看Privasis benchmark进行模型评估。
【免费下载链接】Privasis-Cleaner-4B项目地址: https://ai.gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4B
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
