Datasets:
Delete HVU_QA/fine_tune_qg.py
Browse files- HVU_QA/fine_tune_qg.py +0 -109
HVU_QA/fine_tune_qg.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from datasets import Dataset
|
| 3 |
-
from sklearn.model_selection import train_test_split
|
| 4 |
-
from transformers import (
|
| 5 |
-
T5Tokenizer,
|
| 6 |
-
T5ForConditionalGeneration,
|
| 7 |
-
TrainingArguments,
|
| 8 |
-
Trainer
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
def load_squad_data(file_path):
|
| 12 |
-
"""
|
| 13 |
-
Đọc dữ liệu từ file JSON theo cấu trúc đã phân tích và chuẩn bị dữ liệu cho mô hình.
|
| 14 |
-
"""
|
| 15 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 16 |
-
squad_data = json.load(f)
|
| 17 |
-
|
| 18 |
-
data = []
|
| 19 |
-
for article in squad_data["data"]:
|
| 20 |
-
for paragraph in article["paragraphs"]:
|
| 21 |
-
context = paragraph.get("context", "")
|
| 22 |
-
for qa in paragraph["qas"]:
|
| 23 |
-
if not qa.get("is_impossible", False) and qa.get("answers"):
|
| 24 |
-
answer = qa["answers"][0]["text"]
|
| 25 |
-
question = qa["question"]
|
| 26 |
-
input_text = f"answer: {answer} context: {context}"
|
| 27 |
-
data.append({"input": input_text, "target": question})
|
| 28 |
-
return data
|
| 29 |
-
|
| 30 |
-
def preprocess_function(example, tokenizer, max_input_length=512, max_target_length=64):
|
| 31 |
-
"""
|
| 32 |
-
Tiền xử lý dữ liệu, bao gồm token hóa đầu vào và đầu ra.
|
| 33 |
-
"""
|
| 34 |
-
model_inputs = tokenizer(
|
| 35 |
-
example["input"],
|
| 36 |
-
max_length=max_input_length,
|
| 37 |
-
padding="max_length",
|
| 38 |
-
truncation=True,
|
| 39 |
-
)
|
| 40 |
-
labels = tokenizer(
|
| 41 |
-
text_target=example["target"],
|
| 42 |
-
max_length=max_target_length,
|
| 43 |
-
padding="max_length",
|
| 44 |
-
truncation=True,
|
| 45 |
-
)
|
| 46 |
-
model_inputs["labels"] = labels["input_ids"]
|
| 47 |
-
return model_inputs
|
| 48 |
-
|
| 49 |
-
def main():
|
| 50 |
-
data_path = "new_data.json" # Đường dẫn đến file dữ liệu của bạn
|
| 51 |
-
output_dir = "t5-viet-qg-finetuned"
|
| 52 |
-
logs_dir = "logs"
|
| 53 |
-
model_name = "VietAI/vit5-base"
|
| 54 |
-
|
| 55 |
-
print("Tải mô hình và tokenizer...")
|
| 56 |
-
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 57 |
-
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 58 |
-
|
| 59 |
-
print("Đọc và chia dữ liệu...")
|
| 60 |
-
raw_data = load_squad_data(data_path) # Tải dữ liệu từ file
|
| 61 |
-
train_data, val_data = train_test_split(raw_data, test_size=0.2, random_state=42)
|
| 62 |
-
|
| 63 |
-
train_dataset = Dataset.from_list(train_data)
|
| 64 |
-
val_dataset = Dataset.from_list(val_data)
|
| 65 |
-
|
| 66 |
-
# Tiền xử lý dữ liệu cho train và validation
|
| 67 |
-
tokenized_train = train_dataset.map(
|
| 68 |
-
lambda x: preprocess_function(x, tokenizer),
|
| 69 |
-
batched=True,
|
| 70 |
-
remove_columns=["input", "target"]
|
| 71 |
-
)
|
| 72 |
-
tokenized_val = val_dataset.map(
|
| 73 |
-
lambda x: preprocess_function(x, tokenizer),
|
| 74 |
-
batched=True,
|
| 75 |
-
remove_columns=["input", "target"]
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
print("Cấu hình huấn luyện...")
|
| 79 |
-
training_args = TrainingArguments(
|
| 80 |
-
output_dir=output_dir,
|
| 81 |
-
overwrite_output_dir=True,
|
| 82 |
-
per_device_train_batch_size=1,
|
| 83 |
-
gradient_accumulation_steps=1,
|
| 84 |
-
num_train_epochs=3,
|
| 85 |
-
learning_rate=2e-4,
|
| 86 |
-
weight_decay=0.01,
|
| 87 |
-
warmup_steps=0,
|
| 88 |
-
logging_dir=logs_dir,
|
| 89 |
-
logging_steps=10,
|
| 90 |
-
fp16=False
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
print("Huấn luyện mô hình...")
|
| 94 |
-
trainer = Trainer(
|
| 95 |
-
model=model,
|
| 96 |
-
args=training_args,
|
| 97 |
-
train_dataset=tokenized_train,
|
| 98 |
-
eval_dataset=tokenized_val,
|
| 99 |
-
tokenizer=tokenizer,
|
| 100 |
-
)
|
| 101 |
-
trainer.train()
|
| 102 |
-
|
| 103 |
-
print("Lưu mô hình...")
|
| 104 |
-
model.save_pretrained(output_dir)
|
| 105 |
-
tokenizer.save_pretrained(output_dir)
|
| 106 |
-
print("Huấn luyện hoàn tất!")
|
| 107 |
-
|
| 108 |
-
if __name__ == "__main__":
|
| 109 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|