FiendFlb commited on
Commit
7294a85
·
verified ·
1 Parent(s): 98f6088

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -12,27 +12,32 @@ 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(example):
16
- # Tokenize both chosen and rejected responses with a larger max_length
17
- max_length = 500 # Adjust so combined doesn't exceed 1000
 
 
 
 
 
 
18
  chosen_tokenized = tokenizer(
19
- example['prompt'],
20
  truncation=True,
21
  padding='max_length',
22
  max_length=max_length
23
  )
24
  rejected_tokenized = tokenizer(
25
- example['prompt'],
26
  truncation=True,
27
  padding='max_length',
28
  max_length=max_length
29
  )
30
 
31
- # Combine chosen and rejected in one sequence
32
  combined_input_ids = chosen_tokenized['input_ids'] + rejected_tokenized['input_ids']
33
  combined_attention_mask = chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask']
34
-
35
- # Ensure the combined length does not exceed 1000 tokens
36
  if len(combined_input_ids) > 1000:
37
  combined_input_ids = combined_input_ids[:1000]
38
  combined_attention_mask = combined_attention_mask[:1000]
@@ -40,17 +45,12 @@ def load_and_preprocess_data():
40
  padding_length = 1000 - len(combined_input_ids)
41
  combined_input_ids += [tokenizer.pad_token_id] * padding_length
42
  combined_attention_mask += [0] * padding_length
43
-
44
- inputs = {
45
- 'input_ids': combined_input_ids,
46
- 'attention_mask': combined_attention_mask,
47
- 'labels': [1] * len(example['chosen']) + [0] * len(example['rejected'])
48
- }
49
-
50
- # Truncate labels if they exceed the length of input_ids
51
- inputs['labels'] = inputs['labels'][:len(inputs['input_ids'])]
52
-
53
- return inputs
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]
 
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