Update app.py
Browse files
app.py
CHANGED
|
@@ -12,23 +12,38 @@ def load_and_preprocess_data():
|
|
| 12 |
# Load the tokenizer
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 14 |
|
| 15 |
-
# Preprocess the dataset: tokenize
|
| 16 |
def preprocess_function(example):
|
| 17 |
-
|
|
|
|
| 18 |
example['prompt'],
|
| 19 |
truncation=True,
|
| 20 |
padding='max_length',
|
| 21 |
max_length=512
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return inputs
|
| 30 |
-
|
| 31 |
-
# Apply preprocessing to the dataset
|
| 32 |
dataset = dataset.map(preprocess_function, batched=True)
|
| 33 |
|
| 34 |
# Convert dataset to PyTorch format
|
|
|
|
| 12 |
# Load the tokenizer
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
| 14 |
|
| 15 |
+
# Preprocess the dataset: tokenize
|
| 16 |
def preprocess_function(example):
|
| 17 |
+
# Tokenize both chosen and rejected responses
|
| 18 |
+
chosen_tokenized = tokenizer(
|
| 19 |
example['prompt'],
|
| 20 |
truncation=True,
|
| 21 |
padding='max_length',
|
| 22 |
max_length=512
|
| 23 |
)
|
| 24 |
+
rejected_tokenized = tokenizer(
|
| 25 |
+
example['prompt'],
|
| 26 |
+
truncation=True,
|
| 27 |
+
padding='max_length',
|
| 28 |
+
max_length=512
|
| 29 |
)
|
| 30 |
+
|
| 31 |
+
# Combine chosen and rejected in one sequence, but you'll need to ensure they fit within max_length
|
| 32 |
+
# Here's a simplified approach; adjust based on your needs
|
| 33 |
+
inputs = {
|
| 34 |
+
'input_ids': chosen_tokenized['input_ids'] + rejected_tokenized['input_ids'],
|
| 35 |
+
'attention_mask': chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask'],
|
| 36 |
+
'labels': [1] * len(example['chosen']) + [0] * len(example['rejected'])
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# Ensure the lengths match; you might need to truncate or pad
|
| 40 |
+
max_length = 512 * 2 # Since we're combining two sequences
|
| 41 |
+
if len(inputs['input_ids']) > max_length:
|
| 42 |
+
inputs['input_ids'] = inputs['input_ids'][:max_length]
|
| 43 |
+
inputs['attention_mask'] = inputs['attention_mask'][:max_length]
|
| 44 |
+
inputs['labels'] = inputs['labels'][:max_length] # Be cautious here, might need adjustment based on actual label length
|
| 45 |
+
|
| 46 |
return inputs
|
|
|
|
|
|
|
| 47 |
dataset = dataset.map(preprocess_function, batched=True)
|
| 48 |
|
| 49 |
# Convert dataset to PyTorch format
|