Views
No views yet
| Metric | Value |
|---|---|
| Average Perplexity | 14.20 |
| Training Time | 13.71 minutes |
| Training Samples | 1000 |
| Evaluation Samples | 100 |
| Training Epochs | 3 |
| GPU Used | NVIDIA T4 (Google Colab) |
q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_projpip install transformers torch peft accelerate bitsandbytes1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2import torch
3
4# Configure 4-bit quantization for memory efficiency
5bnb_config = BitsAndBytesConfig(
6 load_in_4bit=True,
7 bnb_4bit_quant_type="nf4",
8 bnb_4bit_compute_dtype=torch.float16,
9 bnb_4bit_use_double_quant=True
10)
11
12# Load model and tokenizer
13model_name = "ahczhg/deepseek-r1-distill-qwen-1.5b-aegis-safety-lora"
14tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
15model = AutoModelForCausalLM.from_pretrained(
16 model_name,
17 quantization_config=bnb_config,
18 device_map="auto",
19 trust_remote_code=True
20)
21
22# Example: Content safety check
23prompt = """### Instruction:
24Analyze this content for safety: 'Hello! How can I help you today?'
25
26### Response:
27"""
28
29inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
30
31with torch.no_grad():
32 outputs = model.generate(
33 **inputs,
34 max_new_tokens=128,
35 temperature=0.7,
36 do_sample=True,
37 top_p=0.95
38 )
39
40response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41print(response)1from transformers import pipeline
2import torch
3
4# Create text generation pipeline
5generator = pipeline(
6 "text-generation",
7 model="ahczhg/deepseek-r1-distill-qwen-1.5b-aegis-safety-lora",
8 torch_dtype=torch.float16,
9 device_map="auto",
10 trust_remote_code=True
11)
12
13# Generate safety analysis
14prompt = "### Instruction:\nIs this content safe? 'Let's collaborate on this project!'\n\n### Response:\n"
15
16result = generator(
17 prompt,
18 max_new_tokens=128,
19 temperature=0.7,
20 do_sample=True,
21 top_p=0.95
22)
23
24print(result[0]['generated_text'])1# Process multiple content items
2content_items = [
3 "Hello, how are you?",
4 "Let's work together!",
5 "I appreciate your help."
6]
7
8for content in content_items:
9 prompt = f"### Instruction:\nAnalyze this content for safety: '{content}'\n\n### Response:\n"
10 result = generator(prompt, max_new_tokens=128, do_sample=True, temperature=0.7)
11 print(f"Content: {content}")
12 print(f"Analysis: {result[0]['generated_text'].split('### Response:')[-1].strip()}")
13 print("-" * 80)1class ContentSafetyChecker:
2 def __init__(self, model_name="ahczhg/deepseek-r1-distill-qwen-1.5b-aegis-safety-lora"):
3 self.generator = pipeline(
4 "text-generation",
5 model=model_name,
6 torch_dtype=torch.float16,
7 device_map="auto",
8 trust_remote_code=True
9 )
10
11 def check_safety(self, content: str) -> dict:
12 prompt = f"""### Instruction:
13Analyze this content for safety and provide a classification (safe/unsafe): '{content}'
14
15### Response:
16"""
17 result = self.generator(
18 prompt,
19 max_new_tokens=128,
20 temperature=0.3, # Lower temperature for more consistent classification
21 do_sample=True
22 )
23
24 response = result[0]['generated_text'].split('### Response:')[-1].strip()
25
26 return {{
27 'content': content,
28 'analysis': response,
29 'is_safe': 'safe' in response.lower() and 'unsafe' not in response.lower()
30 }}
31
32# Usage
33checker = ContentSafetyChecker()
34result = checker.check_safety("Hello, nice to meet you!")
35print(f"Safe: {result['is_safe']}")
36print(f"Analysis: {result['analysis']}")1@misc{{deepseek_r1_distill_qwen_safety,
2 author = {{ahczhg}},
3 title = {{DeepSeek-R1-Distill-Qwen-1.5B Fine-tuned for Content Safety}},
4 year = {{2025}},
5 publisher = {{HuggingFace}},
6 howpublished = {{\url{{ahczhg/deepseek-r1-distill-qwen-1.5b-aegis-safety-lora}}}},
7 note = {{Fine-tuned on NVIDIA Aegis AI Content Safety Dataset 2.0}}
8}}
9
10@misc{{deepseek_r1,
11 title = {{DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning}},
12 author = {{DeepSeek-AI}},
13 year = {{2024}},
14 publisher = {{HuggingFace}},
15 howpublished = {{\url{{https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B}}}}
16}}