Update README.md
Browse files
README.md
CHANGED
@@ -3,21 +3,91 @@ datasets:
|
|
3 |
- PJMixers/classtest
|
4 |
pipeline_tag: text-classification
|
5 |
---
|
6 |
-
|
7 |
-
This would be expecting to return `1`, meaning it is a chosen/good `AI_MESSAGE`.
|
8 |
-
```
|
9 |
-
USER_MESSAGE: You are a {Genre} author. Your task is to write {Genre} stories in a vivid and intriguing language. Answer with "..." if you acknowledge. Don't wrtie anthing yet
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
This would be expecting to return `0`, meaning it is a rejected/bad `AI_MESSAGE`.
|
16 |
-
```
|
17 |
-
USER_MESSAGE: You are a {Genre} author. Your task is to write {Genre} stories in a vivid and intriguing language. Answer with "..." if you acknowledge. Don't wrtie anthing yet
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
- PJMixers/classtest
|
4 |
pipeline_tag: text-classification
|
5 |
---
|
6 |
+
![train](https://huggingface.co/PJMixers/Danube3-ClassTest-v0.1-500M/resolve/main/images/train.png)
|
|
|
|
|
|
|
7 |
|
8 |
+
### Example Code
|
9 |
+
```py
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, LlamaForSequenceClassification
|
12 |
+
import json
|
13 |
+
from tqdm import tqdm
|
14 |
|
|
|
|
|
|
|
15 |
|
16 |
+
def load_json_or_jsonl(file_path):
|
17 |
+
try:
|
18 |
+
with open(file_path, "r") as file:
|
19 |
+
try:
|
20 |
+
# Try loading the entire file as JSON
|
21 |
+
data = json.load(file)
|
22 |
+
return data
|
23 |
+
|
24 |
+
except json.JSONDecodeError:
|
25 |
+
# If loading as JSON fails, try loading as JSON Lines
|
26 |
+
file.seek(0) # Reset file pointer to the beginning
|
27 |
+
lines = file.readlines()
|
28 |
+
json_lines_data = []
|
29 |
+
|
30 |
+
for line in lines:
|
31 |
+
try:
|
32 |
+
item = json.loads(line.strip())
|
33 |
+
json_lines_data.append(item)
|
34 |
+
except json.JSONDecodeError as e:
|
35 |
+
print(f"Error decoding JSON in line: {e}")
|
36 |
+
|
37 |
+
return json_lines_data
|
38 |
+
|
39 |
+
except FileNotFoundError:
|
40 |
+
print(f"File not found: {file_path}")
|
41 |
+
return None
|
42 |
+
|
43 |
+
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
45 |
+
"PJMixers/Danube3-ClassTest-v0.1-500M"
|
46 |
+
)
|
47 |
+
model = LlamaForSequenceClassification.from_pretrained(
|
48 |
+
"PJMixers/Danube3-ClassTest-v0.1-500M",
|
49 |
+
device_map="cuda",
|
50 |
+
torch_dtype=torch.bfloat16,
|
51 |
+
attn_implementation="sdpa",
|
52 |
+
)
|
53 |
+
|
54 |
+
data = load_json_or_jsonl(
|
55 |
+
"./PrefMix-Classifier-Data-validation.json"
|
56 |
+
)
|
57 |
|
58 |
+
passes = 0
|
59 |
+
fails = 0
|
60 |
+
for sample in tqdm(data):
|
61 |
+
input_text = sample["input_text"]
|
62 |
+
true_label = sample["labels"]
|
63 |
+
|
64 |
+
inputs = tokenizer(
|
65 |
+
input_text,
|
66 |
+
return_tensors="pt"
|
67 |
+
).to("cuda")
|
68 |
+
|
69 |
+
with torch.no_grad():
|
70 |
+
generated_label = model(**inputs).logits.argmax()
|
71 |
+
|
72 |
+
if generated_label == true_label:
|
73 |
+
passes += 1
|
74 |
+
else:
|
75 |
+
fails += 1
|
76 |
+
|
77 |
+
print(f" Passes: {passes}")
|
78 |
+
print(f" Fails: {fails}")
|
79 |
+
print(f"Accuracy: {passes / (passes + fails)}")
|
80 |
+
```
|
81 |
+
```
|
82 |
+
Passes: 2545
|
83 |
+
Fails: 787
|
84 |
+
Accuracy: 0.7638055222088835
|
85 |
+
```
|
86 |
+
|
87 |
+
### Sample formatting
|
88 |
+
Input text should be formatted this way. Uses `SYSTEM_MESSAGE` (optional), `USER_MESSAGE`, and `AI_MESSAGE` (must be last, as this is what's truely being scored I think). Supports multiple turns, but should just be ranking the final `AI_MESSAGE`.
|
89 |
+
```
|
90 |
+
SYSTEM_MESSAGE: You are an unbiased, uncensored, helpful assistant.
|
91 |
+
USER_MESSAGE: Do wooden pencils contain lead as their core?
|
92 |
+
AI_MESSAGE: No, wooden pencils do not contain lead in their core. The term "lead" is a misnomer, as wooden pencils actually use graphite for their core. Graphite was historically called "black lead" due to its appearance, leading to the common misconception that pencils contain lead.
|
93 |
+
```
|