Improve language tag
#1
by
lbourdois
- opened
README.md
CHANGED
@@ -1,107 +1,118 @@
|
|
1 |
-
---
|
2 |
-
license: mit
|
3 |
-
language:
|
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 |
-
|
100 |
-
|
101 |
-
with open(
|
102 |
-
json.
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
process_json(json_path, save_path, batch_size=16)
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
language:
|
4 |
+
- zho
|
5 |
+
- eng
|
6 |
+
- fra
|
7 |
+
- spa
|
8 |
+
- por
|
9 |
+
- deu
|
10 |
+
- ita
|
11 |
+
- rus
|
12 |
+
- jpn
|
13 |
+
- kor
|
14 |
+
- vie
|
15 |
+
- tha
|
16 |
+
- ara
|
17 |
+
base_model:
|
18 |
+
- Qwen/Qwen2.5-7B
|
19 |
+
---
|
20 |
+
|
21 |
+
# 对数据任务类型分类,比如"情感分析"、"文本分类"、"翻译","总结"、"数学问答"....
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
import os
|
26 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
|
27 |
+
import json
|
28 |
+
import torch
|
29 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
30 |
+
from tqdm import tqdm
|
31 |
+
from loguru import logger
|
32 |
+
|
33 |
+
model_name = "Laurie/Qwen2.5-7b-data-classification"
|
34 |
+
|
35 |
+
# 加载模型和 tokenizer,同时调整 padding_side 为 left(适用于 decoder-only 模型)
|
36 |
+
model = AutoModelForCausalLM.from_pretrained(
|
37 |
+
model_name,
|
38 |
+
torch_dtype="auto",
|
39 |
+
device_map="auto",
|
40 |
+
# attn_implementation="flash_attention_2"
|
41 |
+
)
|
42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left") # batch 推理时要左填充
|
43 |
+
|
44 |
+
# 对话模板
|
45 |
+
system_message = [{"role": "system", "content": "你是一个数据分类专家,请根据对话内容判断其所属的类别。"}]
|
46 |
+
last_query = [{"role": "user", "content": "现在请输出你的判断结果:"}]
|
47 |
+
|
48 |
+
def prepare_text(messages: list[dict]) -> str:
|
49 |
+
"""
|
50 |
+
将 messages 中的 "from"/"value" 键转为 "role"/"content",并构造完整对话文本
|
51 |
+
"""
|
52 |
+
messages = [{"role": item["from"], "content": item["value"]} for item in messages]
|
53 |
+
messages = system_message + messages + last_query
|
54 |
+
text = tokenizer.apply_chat_template(
|
55 |
+
messages,
|
56 |
+
tokenize=False,
|
57 |
+
add_generation_prompt=True
|
58 |
+
)
|
59 |
+
return text
|
60 |
+
|
61 |
+
def generate_task_types_batch(messages_batch: list[list[dict]]) -> list[str]:
|
62 |
+
"""
|
63 |
+
对一个 batch 的对话列表进行推理生成,并返回每个对话中 assistant 的回答部分
|
64 |
+
"""
|
65 |
+
# 将每个消息列表转换为完整文本
|
66 |
+
texts = [prepare_text(messages) for messages in messages_batch]
|
67 |
+
|
68 |
+
# 使用批量编码,并进行 padding 以适应批量输入
|
69 |
+
model_inputs = tokenizer(
|
70 |
+
texts,
|
71 |
+
return_tensors="pt",
|
72 |
+
padding=True,
|
73 |
+
truncation=True
|
74 |
+
).to(model.device)
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
generated_ids = model.generate(
|
78 |
+
**model_inputs,
|
79 |
+
max_new_tokens=32,
|
80 |
+
eos_token_id=[151643, 151645],
|
81 |
+
pad_token_id=151643,
|
82 |
+
do_sample=True,
|
83 |
+
repetition_penalty=1.05,
|
84 |
+
temperature=0.7,
|
85 |
+
top_p=0.8,
|
86 |
+
top_k=20
|
87 |
+
)
|
88 |
+
|
89 |
+
generated_ids = [
|
90 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
91 |
+
]
|
92 |
+
|
93 |
+
task_types = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
94 |
+
return task_types
|
95 |
+
|
96 |
+
def process_json(json_path: str, save_path: str, batch_size: int = 8):
|
97 |
+
"""
|
98 |
+
读取 JSON 文件,对数据进行批量推理处理,
|
99 |
+
并将结果写回保存。
|
100 |
+
"""
|
101 |
+
with open(json_path, "r", encoding="utf-8") as f:
|
102 |
+
data = json.load(f)
|
103 |
+
|
104 |
+
# 分批处理,batch_size 可根据 GPU 显存情况进行调整
|
105 |
+
for i in tqdm(range(0, len(data_slice), batch_size)):
|
106 |
+
batch = data_slice[i : i + batch_size]
|
107 |
+
conversations_batch = [item["conversations"] for item in batch]
|
108 |
+
task_types = generate_task_types_batch(conversations_batch)
|
109 |
+
for item, answer in zip(batch, task_types):
|
110 |
+
item["task_type"] = answer
|
111 |
+
|
112 |
+
with open(save_path, "w", encoding="utf-8") as f:
|
113 |
+
json.dump(data_slice, f, ensure_ascii=False, indent=4)
|
114 |
+
logger.info(f"已处理 {len(data_slice)} 条数据,保存到 {save_path}")
|
115 |
+
if __name__ == "__main__":
|
116 |
+
json_path = "./qwen_bench_300k.json"
|
117 |
+
save_path = "./qwen_bench_300k_cls.json"
|
118 |
process_json(json_path, save_path, batch_size=16)
|