Update app.py
Browse files
app.py
CHANGED
|
@@ -12,41 +12,46 @@ def load_and_preprocess_data():
|
|
| 12 |
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
-
|
| 16 |
-
|
| 17 |
inputs = {
|
| 18 |
'input_ids': [],
|
| 19 |
'attention_mask': [],
|
| 20 |
'labels': []
|
| 21 |
}
|
| 22 |
for example in examples['prompt']: # Adjust if 'prompt' is not the key for your text
|
|
|
|
| 23 |
chosen_tokenized = tokenizer(
|
| 24 |
example,
|
| 25 |
truncation=True,
|
| 26 |
padding='max_length',
|
| 27 |
-
max_length=
|
| 28 |
)
|
| 29 |
rejected_tokenized = tokenizer(
|
| 30 |
-
example,
|
| 31 |
truncation=True,
|
| 32 |
padding='max_length',
|
| 33 |
-
max_length=
|
| 34 |
)
|
| 35 |
|
| 36 |
combined_input_ids = chosen_tokenized['input_ids'] + rejected_tokenized['input_ids']
|
| 37 |
combined_attention_mask = chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask']
|
| 38 |
|
|
|
|
| 39 |
inputs['input_ids'].append(combined_input_ids)
|
| 40 |
inputs['attention_mask'].append(combined_attention_mask)
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Convert lists to tensors for each batch
|
| 45 |
inputs['input_ids'] = torch.tensor(inputs['input_ids'])
|
| 46 |
inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])
|
| 47 |
inputs['labels'] = torch.stack(inputs['labels']) # Stack labels along batch dimension
|
| 48 |
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
return inputs
|
| 52 |
# Apply preprocessing to the dataset
|
|
|
|
| 12 |
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
+
def preprocess_function(examples):
|
| 16 |
+
# Dynamic max length; each example will be processed to match tokenization length
|
| 17 |
inputs = {
|
| 18 |
'input_ids': [],
|
| 19 |
'attention_mask': [],
|
| 20 |
'labels': []
|
| 21 |
}
|
| 22 |
for example in examples['prompt']: # Adjust if 'prompt' is not the key for your text
|
| 23 |
+
# Tokenize chosen and rejected parts separately
|
| 24 |
chosen_tokenized = tokenizer(
|
| 25 |
example,
|
| 26 |
truncation=True,
|
| 27 |
padding='max_length',
|
| 28 |
+
max_length=4 # or adjust this based on your needs
|
| 29 |
)
|
| 30 |
rejected_tokenized = tokenizer(
|
| 31 |
+
example,
|
| 32 |
truncation=True,
|
| 33 |
padding='max_length',
|
| 34 |
+
max_length=4 # or adjust this based on your needs
|
| 35 |
)
|
| 36 |
|
| 37 |
combined_input_ids = chosen_tokenized['input_ids'] + rejected_tokenized['input_ids']
|
| 38 |
combined_attention_mask = chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask']
|
| 39 |
|
| 40 |
+
# Append inputs
|
| 41 |
inputs['input_ids'].append(combined_input_ids)
|
| 42 |
inputs['attention_mask'].append(combined_attention_mask)
|
| 43 |
+
|
| 44 |
+
# Labels should match the length of input_ids for each example
|
| 45 |
+
label_length = len(combined_input_ids)
|
| 46 |
+
inputs['labels'].append(torch.tensor([1] * (label_length // 2) + [0] * (label_length // 2)))
|
| 47 |
|
| 48 |
# Convert lists to tensors for each batch
|
| 49 |
inputs['input_ids'] = torch.tensor(inputs['input_ids'])
|
| 50 |
inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])
|
| 51 |
inputs['labels'] = torch.stack(inputs['labels']) # Stack labels along batch dimension
|
| 52 |
|
| 53 |
+
# Debugging
|
| 54 |
+
print(f"Input_ids shape: {inputs['input_ids'].shape}, Labels shape: {inputs['labels'].shape}")
|
| 55 |
|
| 56 |
return inputs
|
| 57 |
# Apply preprocessing to the dataset
|