File size: 3,697 Bytes
3d6ae55
 
 
 
 
 
 
 
 
 
 
42a6121
 
3d6ae55
 
 
 
 
 
 
 
 
 
 
42a6121
 
795d591
42a6121
 
 
 
 
 
 
 
 
 
 
 
 
 
4ba73b3
42a6121
 
795d591
 
42a6121
 
 
795d591
 
 
 
 
 
 
 
 
 
42a6121
 
795d591
42a6121
795d591
42a6121
795d591
1ebadae
 
795d591
1ebadae
42a6121
795d591
 
 
 
42a6121
795d591
42a6121
 
 
 
 
 
 
795d591
 
 
 
 
42a6121
 
795d591
 
1ebadae
 
db1baea
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
base_model: unsloth/meta-llama-3.1-8b-instruct-unsloth-bnb-4bit
tags:
- text-generation-inference
- transformers
- unsloth
- llama
- trl
license: apache-2.0
language:
- en
datasets:
- Machlovi/Hatebase
---

# Uploaded  model

- **Developed by:** Machlovi
- **License:** apache-2.0
- **Finetuned from model :** unsloth/meta-llama-3.1-8b-instruct-unsloth-bnb-4bit

This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.

[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)


### **📝 Load **  
```bash
from unsloth import FastLanguageModel
from peft import PeftModel
import torch

# Load the base model
base_model_name = ""unsloth/Meta-Llama-3.1-8B-Instruct",
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=base_model_name,
    max_seq_length=2048,  # Must match fine-tuning
    load_in_4bit=True,
)

# Load the fine-tuned LoRA adapter
lora_model_name = "Machlovi/SafeLlama"
model = PeftModel.from_pretrained(model, lora_model_name)

model.eval()
```



### Safety Policies Instructions
```bash
policy=You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.
* "No Harassment": The input shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
* "No Dangerous Content": The input shall not contain or seek generation of content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide, violent protest).
* "No Hate Speech": The input shall not contain or seek generation of content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status.
* "No Sexually Explicit Information": The input shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually arousal descriptions, content aimed at causing arousal, sexual assault (ex: rape),sexual harassment (physical, verbal, or visual)). Medical or scientific terms related to human anatomy or sex education are permitted.
Does the human question violate the above principle? Your answer must start with 'Safe' or 'Unsafe'. 
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories.
```

### **Chat template 
```bash
from unsloth.chat_templates import get_chat_template

chat_template='llama-3'
tokenizer = get_chat_template(
    tokenizer,
    chat_template = chat_template,
)

messages=[{"content":policy,
           'role':"system"},
 {'content':'why even let a tranny in they arent really even human',
  'role': 'user'},

]
inputs = tokenizer.apply_chat_template(
    messages,
    tokenize = True,
    add_generation_prompt = True, # Must add for generation
    return_tensors = "pt",
).to("cuda")

```


### **📝Inference with TextStreamer**  
```bash
from transformers import TextStreamer
text_streamer = TextStreamer(tokenizer)
_= model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 10, use_cache = True)


Hate speech, personal attacks, and discrimination
```