Spaces:
Build error
Build error
Upload prompt.py
Browse files- competition/prompt.py +161 -0
competition/prompt.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
2 |
+
from datasets import Dataset
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from peft import get_peft_model, LoraConfig, TaskType
|
6 |
+
import evaluate
|
7 |
+
import numpy as np
|
8 |
+
from tqdm import tqdm
|
9 |
+
|
10 |
+
# Load the dataset
|
11 |
+
file_path = 'train_en.csv'
|
12 |
+
dataset = pd.read_csv(file_path)
|
13 |
+
|
14 |
+
# Map labels to expected responses
|
15 |
+
label_mapping = {
|
16 |
+
"Yes": 0,
|
17 |
+
"No": 1,
|
18 |
+
"It doesn't matter": 2,
|
19 |
+
"Unimportant": 2,
|
20 |
+
"Incorrect questioning": 3,
|
21 |
+
"Correct answers": 4
|
22 |
+
}
|
23 |
+
|
24 |
+
# Apply label mapping
|
25 |
+
dataset['label'] = dataset['label'].map(label_mapping)
|
26 |
+
|
27 |
+
# Handle NaN values: Drop rows where label is NaN
|
28 |
+
dataset = dataset.dropna(subset=['label'])
|
29 |
+
|
30 |
+
# Ensure labels are integers
|
31 |
+
dataset['label'] = dataset['label'].astype(int)
|
32 |
+
|
33 |
+
# Format puzzle, truth, text into the prompt
|
34 |
+
prompt_template = """You are the host of a situational guessing game. The rules of the game are as follows:
|
35 |
+
|
36 |
+
1. Participants will receive a riddle that describes a simple yet difficult to understand event.
|
37 |
+
2. The host knows the answer, which is the solution to the riddle.
|
38 |
+
3. Participants can ask any closed-ended questions to uncover the truth of the event.
|
39 |
+
4. For each question, the host will respond with one of the following five options based on the actual situation: Yes, No, Unimportant, Correct answer, or Incorrect questioning. The criteria for each response are as follows:
|
40 |
+
- If the riddle and answer can provide an answer to the question, respond with: Yes or No
|
41 |
+
- If the riddle and answer cannot directly or indirectly infer an answer to the question, respond with: Unimportant
|
42 |
+
- If the participant's question is not a closed-ended question or is difficult to understand, respond with: Incorrect questioning
|
43 |
+
- If the participant's question essentially reveals the truth of the answer, respond with: Correct answer
|
44 |
+
5. The response must not include any additional information, nor should any word be omitted from the options. For example, "No" cannot be abbreviated to "N".
|
45 |
+
|
46 |
+
Please strictly follow these rules when answering the participant's questions.
|
47 |
+
|
48 |
+
Riddle: {}
|
49 |
+
Answer: {}
|
50 |
+
Participant's question: {}
|
51 |
+
"""
|
52 |
+
|
53 |
+
dataset['combined_text'] = dataset.apply(
|
54 |
+
lambda row: prompt_template.format(row['puzzle'], row['truth'], row['text']),
|
55 |
+
axis=1
|
56 |
+
)
|
57 |
+
|
58 |
+
# Split the dataset into training and validation sets
|
59 |
+
train_df, val_df = train_test_split(dataset, test_size=0.2, random_state=42)
|
60 |
+
|
61 |
+
# Convert the dataframes to datasets
|
62 |
+
train_dataset = Dataset.from_pandas(train_df)
|
63 |
+
val_dataset = Dataset.from_pandas(val_df)
|
64 |
+
|
65 |
+
# Load the tokenizer and model
|
66 |
+
model_name = "meta-llama/Meta-Llama-3-8B" # Replace with the actual model name
|
67 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
68 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=5)
|
69 |
+
|
70 |
+
# Add a padding token if it's not already present
|
71 |
+
if tokenizer.pad_token is None:
|
72 |
+
tokenizer.add_special_tokens({'pad_token': tokenizer.eos_token})
|
73 |
+
model.resize_token_embeddings(len(tokenizer))
|
74 |
+
tokenizer.pad_token = tokenizer.eos_token # Set the padding token explicitly
|
75 |
+
|
76 |
+
# Ensure the padding token is set correctly in the model configuration
|
77 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
78 |
+
|
79 |
+
# Tokenize the data
|
80 |
+
def tokenize_function(examples):
|
81 |
+
return tokenizer(examples['combined_text'], truncation=True, padding='max_length', max_length=512)
|
82 |
+
|
83 |
+
train_dataset = train_dataset.map(tokenize_function, batched=True, num_proc=4) # Use multiprocessing
|
84 |
+
val_dataset = val_dataset.map(tokenize_function, batched=True, num_proc=4)
|
85 |
+
|
86 |
+
# Set the format for PyTorch
|
87 |
+
train_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
|
88 |
+
val_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'label'])
|
89 |
+
|
90 |
+
# Define LoRA configuration
|
91 |
+
lora_config = LoraConfig(
|
92 |
+
task_type=TaskType.SEQ_CLS,
|
93 |
+
r=16,
|
94 |
+
lora_alpha=16,
|
95 |
+
target_modules=["q_proj", "v_proj"],
|
96 |
+
lora_dropout=0.05,
|
97 |
+
bias="none"
|
98 |
+
)
|
99 |
+
|
100 |
+
# Apply LoRA to the model
|
101 |
+
model = get_peft_model(model, lora_config)
|
102 |
+
model.print_trainable_parameters()
|
103 |
+
|
104 |
+
# Training arguments
|
105 |
+
training_args = TrainingArguments(
|
106 |
+
output_dir='./results',
|
107 |
+
learning_rate=1e-4,
|
108 |
+
lr_scheduler_type="linear",
|
109 |
+
warmup_ratio=0.1,
|
110 |
+
max_grad_norm=0.3,
|
111 |
+
per_device_train_batch_size=8, # Increase batch size if memory allows
|
112 |
+
per_device_eval_batch_size=8,
|
113 |
+
num_train_epochs=3,
|
114 |
+
weight_decay=0.001,
|
115 |
+
evaluation_strategy="epoch",
|
116 |
+
save_strategy="epoch",
|
117 |
+
load_best_model_at_end=True,
|
118 |
+
report_to="wandb",
|
119 |
+
fp16=True,
|
120 |
+
gradient_checkpointing=True,
|
121 |
+
gradient_accumulation_steps=2, # Adjust based on memory constraints
|
122 |
+
dataloader_num_workers=4,
|
123 |
+
logging_steps=100,
|
124 |
+
save_total_limit=2,
|
125 |
+
)
|
126 |
+
|
127 |
+
def compute_metrics(eval_pred):
|
128 |
+
precision_metric = evaluate.load("precision")
|
129 |
+
recall_metric = evaluate.load("recall")
|
130 |
+
f1_metric = evaluate.load("f1")
|
131 |
+
accuracy_metric = evaluate.load("accuracy")
|
132 |
+
|
133 |
+
logits, labels = eval_pred
|
134 |
+
predictions = np.argmax(logits, axis=-1)
|
135 |
+
|
136 |
+
precision = precision_metric.compute(predictions=predictions, references=labels, average="weighted")["precision"]
|
137 |
+
recall = recall_metric.compute(predictions=predictions, references=labels, average="weighted")["recall"]
|
138 |
+
f1 = f1_metric.compute(predictions=predictions, references=labels, average="weighted")["f1"]
|
139 |
+
accuracy = accuracy_metric.compute(predictions=predictions, references=labels)["accuracy"]
|
140 |
+
|
141 |
+
return {"precision": precision, "recall": recall, "f1-score": f1, 'accuracy': accuracy}
|
142 |
+
|
143 |
+
# Initialize the Trainer
|
144 |
+
trainer = Trainer(
|
145 |
+
model=model,
|
146 |
+
args=training_args,
|
147 |
+
train_dataset=train_dataset,
|
148 |
+
eval_dataset=val_dataset,
|
149 |
+
compute_metrics=compute_metrics
|
150 |
+
)
|
151 |
+
|
152 |
+
# Train the model with progress bar
|
153 |
+
trainer.train()
|
154 |
+
|
155 |
+
# Save the model
|
156 |
+
model.save_pretrained('trained_llama_model')
|
157 |
+
tokenizer.save_pretrained('trained_llama_model')
|
158 |
+
|
159 |
+
# Evaluate the model with progress bar
|
160 |
+
eval_results = trainer.evaluate()
|
161 |
+
print(eval_results)
|