Spaces:
Runtime error
Runtime error
Update train.py
Browse files
train.py
CHANGED
|
@@ -1,58 +1,70 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
from peft import LoraConfig, get_peft_model
|
| 4 |
-
from datasets import load_dataset
|
| 5 |
|
| 6 |
-
# 加载
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# 加载模型
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
lora_alpha=16,
|
| 17 |
-
lora_dropout=0.1,
|
| 18 |
-
bias="none",
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
{"instruction": "粉丝通跨店版的费用是多少?", "output": "粉丝通跨店版按月付费,500元/月,仅提供增值税普通电子发票。"},
|
| 27 |
-
{"instruction": "如何充值粉丝通软件的红包?", "output": "商家可以灵活充值红包,每个红包最低0.1元,具体总额根据拉新目标决定。"},
|
| 28 |
-
{"instruction": "红包的扣费机制是怎样的?", "output": "红包在用户实际使用后才会扣款,未使用到期会自动退回商家公户。"},
|
| 29 |
-
# 你可以继续添加数据...
|
| 30 |
-
]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
# 设置训练参数
|
| 38 |
training_args = TrainingArguments(
|
| 39 |
-
output_dir=
|
| 40 |
evaluation_strategy="epoch",
|
| 41 |
-
learning_rate=
|
| 42 |
-
per_device_train_batch_size=
|
| 43 |
-
per_device_eval_batch_size=2,
|
| 44 |
num_train_epochs=3,
|
| 45 |
weight_decay=0.01,
|
| 46 |
-
|
| 47 |
-
save_total_limit=2,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
trainer = Trainer(
|
| 52 |
-
model=
|
| 53 |
args=training_args,
|
| 54 |
-
train_dataset=
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
# 开始训练
|
| 58 |
trainer.train()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, Trainer, TrainingArguments, AutoTokenizer
|
| 4 |
+
from datasets import Dataset
|
| 5 |
+
from huggingface_hub import HfApi, HfFolder
|
| 6 |
from peft import LoraConfig, get_peft_model
|
|
|
|
| 7 |
|
| 8 |
+
# 从环境变量加载 Hugging Face token
|
| 9 |
+
hf_token = os.getenv('HF_TOKEN') # 假设你将 token 设置为环境变量
|
| 10 |
+
if hf_token:
|
| 11 |
+
HfFolder.save_token(hf_token)
|
| 12 |
+
else:
|
| 13 |
+
raise ValueError("Hugging Face token 未设置")
|
| 14 |
|
| 15 |
+
# 加载基础模型(例如:DeepSeek-R1)
|
| 16 |
+
model_name = "DeepSeek-R1" # 你可以根据需要调整基础模型
|
| 17 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 18 |
+
|
| 19 |
+
# 加载 tokenizer
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
|
| 22 |
+
# 读取数据文件
|
| 23 |
+
with open('data.json', 'r', encoding='utf-8') as f:
|
| 24 |
+
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# 将数据转换为 Dataset 格式
|
| 27 |
+
def preprocess_data(example):
|
| 28 |
+
return tokenizer(example['instruction'], truncation=True, padding="max_length", max_length=128)
|
| 29 |
|
| 30 |
+
dataset = Dataset.from_dict(data)
|
| 31 |
+
dataset = dataset.map(preprocess_data, batched=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# LoRA 配置(根据需要调整)
|
| 34 |
+
lora_config = LoraConfig(
|
| 35 |
+
r=8,
|
| 36 |
+
lora_alpha=32,
|
| 37 |
+
lora_dropout=0.1,
|
| 38 |
+
target_modules=["q_proj", "v_proj"], # 根据你的模型结构调整
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
# 获取 LoRA 微调模型
|
| 42 |
+
peft_model = get_peft_model(model, lora_config)
|
| 43 |
|
| 44 |
# 设置训练参数
|
| 45 |
training_args = TrainingArguments(
|
| 46 |
+
output_dir="./output",
|
| 47 |
evaluation_strategy="epoch",
|
| 48 |
+
learning_rate=2e-5,
|
| 49 |
+
per_device_train_batch_size=8,
|
|
|
|
| 50 |
num_train_epochs=3,
|
| 51 |
weight_decay=0.01,
|
| 52 |
+
logging_dir="./logs",
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# 训练器
|
| 56 |
trainer = Trainer(
|
| 57 |
+
model=peft_model,
|
| 58 |
args=training_args,
|
| 59 |
+
train_dataset=dataset,
|
| 60 |
+
eval_dataset=dataset, # 你可以根据需要设置验证集
|
| 61 |
)
|
| 62 |
|
| 63 |
# 开始训练
|
| 64 |
trainer.train()
|
| 65 |
+
|
| 66 |
+
# 保存模型
|
| 67 |
+
model.save_pretrained("./fst-nnn")
|
| 68 |
+
tokenizer.save_pretrained("./fst-nnn")
|
| 69 |
+
|
| 70 |
+
# 上传到 Hugging Face
|