Update app.py
Browse files
app.py
CHANGED
|
@@ -12,45 +12,45 @@ def load_and_preprocess_data():
|
|
| 12 |
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
-
def preprocess_function(examples):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
# Apply preprocessing to the dataset
|
| 55 |
dataset = dataset.map(preprocess_function, batched=True)
|
| 56 |
|
|
|
|
| 12 |
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
+
def preprocess_function(examples):
|
| 16 |
+
# Assuming 'examples' is a batch of examples
|
| 17 |
+
max_length = 500 # Half of 1000 for each segment
|
| 18 |
+
inputs = {
|
| 19 |
+
'input_ids': [],
|
| 20 |
+
'attention_mask': [],
|
| 21 |
+
'labels': []
|
| 22 |
+
}
|
| 23 |
+
for example in examples['prompt']: # Adjust if 'prompt' is not the key for your text
|
| 24 |
+
chosen_tokenized = tokenizer(
|
| 25 |
+
example,
|
| 26 |
+
truncation=True,
|
| 27 |
+
padding='max_length',
|
| 28 |
+
max_length=max_length
|
| 29 |
+
)
|
| 30 |
+
rejected_tokenized = tokenizer(
|
| 31 |
+
example, # Here you might need to adjust for how you handle 'chosen' and 'rejected'
|
| 32 |
+
truncation=True,
|
| 33 |
+
padding='max_length',
|
| 34 |
+
max_length=max_length
|
| 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 |
+
# Pad or truncate to ensure length is 1000
|
| 41 |
+
if len(combined_input_ids) > 1000:
|
| 42 |
+
combined_input_ids = combined_input_ids[:1000]
|
| 43 |
+
combined_attention_mask = combined_attention_mask[:1000]
|
| 44 |
+
else:
|
| 45 |
+
padding_length = 1000 - len(combined_input_ids)
|
| 46 |
+
combined_input_ids += [tokenizer.pad_token_id] * padding_length
|
| 47 |
+
combined_attention_mask += [0] * padding_length
|
| 48 |
+
|
| 49 |
+
inputs['input_ids'].append(combined_input_ids)
|
| 50 |
+
inputs['attention_mask'].append(combined_attention_mask)
|
| 51 |
+
inputs['labels'].append([1] * 500 + [0] * 500) # Adjust based on your label structure
|
| 52 |
+
|
| 53 |
+
return inputs
|
| 54 |
# Apply preprocessing to the dataset
|
| 55 |
dataset = dataset.map(preprocess_function, batched=True)
|
| 56 |
|