Commit
·
23d1422
1
Parent(s):
fdfba35
Update README.md
Browse files
README.md
CHANGED
@@ -2,34 +2,31 @@
|
|
2 |
license: apache-2.0
|
3 |
---
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
```python
|
7 |
-
import torch
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
import numpy as np
|
10 |
from scipy.special import softmax
|
11 |
-
# 选择模型和模型名称(例如,这里使用GPT-2模型)
|
12 |
model_name = "hkust-nlp/Deita-Quality-Scorer"
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
15 |
|
16 |
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# 生成文本
|
28 |
-
max_length = 512 # 设置生成文本的最大长度
|
29 |
-
outputs = model.generate(input_ids, max_length=512, num_return_sequences=1, return_dict_in_generate=True, output_scores=True)
|
30 |
-
logprobs_list = outputs.scores[0][0]
|
31 |
-
score_logits = []
|
32 |
-
id2score = {
|
33 |
29896: "1",
|
34 |
29906: "2",
|
35 |
29941: "3",
|
@@ -37,12 +34,22 @@ id2score = {
|
|
37 |
29945: "5",
|
38 |
29953: "6"
|
39 |
}
|
40 |
-
score_template = np.array([1,2,3,4,5,6])
|
41 |
-
for k in id2score:
|
42 |
-
|
43 |
-
score_logits = np.array(score_logits)
|
44 |
-
score_npy = softmax(score_logits, axis=0)
|
45 |
-
score_npy = score_npy * score_template
|
46 |
-
|
47 |
-
score_npy = np.sum(score_npy, axis=0)
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
license: apache-2.0
|
3 |
---
|
4 |
|
5 |
+
# Deita-Quality-Scorer
|
6 |
+
|
7 |
+
Deita-Quality-Scorer is a tool for automatically annotating the Instruction Quality of SFT data.
|
8 |
+
|
9 |
+
|
10 |
+
## Uses
|
11 |
+
|
12 |
```python
|
|
|
13 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
14 |
import numpy as np
|
15 |
from scipy.special import softmax
|
|
|
16 |
model_name = "hkust-nlp/Deita-Quality-Scorer"
|
17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
18 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
19 |
|
20 |
|
21 |
+
def infer_Quality(model, tokenizer, input_text, resp_text):
|
22 |
+
quality_template = ("You are a helpful assistant. Please identify the quality score of the Response corresponding to the Question. \n #Question#:\n{instruction}\n#Response#:\n{output} \n##Quality: ")
|
23 |
+
user_input = quality_template.format(instruction=input_text, output=resp_text)
|
24 |
+
input_ids = tokenizer.encode(user_input, return_tensors="pt")
|
25 |
+
max_length = 512
|
26 |
+
outputs = model.generate(input_ids, max_length=512, num_return_sequences=1, return_dict_in_generate=True, output_scores=True)
|
27 |
+
logprobs_list = outputs.scores[0][0]
|
28 |
+
score_logits = []
|
29 |
+
id2score = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
29896: "1",
|
31 |
29906: "2",
|
32 |
29941: "3",
|
|
|
34 |
29945: "5",
|
35 |
29953: "6"
|
36 |
}
|
37 |
+
score_template = np.array([1,2,3,4,5,6])
|
38 |
+
for k in id2score:
|
39 |
+
score_logits.append(logprobs_list[k])
|
40 |
+
score_logits = np.array(score_logits)
|
41 |
+
score_npy = softmax(score_logits, axis=0)
|
42 |
+
score_npy = score_npy * score_template
|
43 |
+
|
44 |
+
score_npy = np.sum(score_npy, axis=0)
|
45 |
+
return score_npy
|
46 |
+
|
47 |
+
input_text = "word to describe UI with helpful tooltips"
|
48 |
+
output_text = "User-friendly or intuitive UI"
|
49 |
+
quality_score = infer_quality(model, tokenizer, input_text)
|
50 |
+
|
51 |
+
print(quality_score)
|
52 |
+
|
53 |
+
|
54 |
+
```
|
55 |
+
|