FiendFlb commited on
Commit
98f6088
·
verified ·
1 Parent(s): 3f72ea7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -14,35 +14,43 @@ def load_and_preprocess_data():
14
  # Preprocess the dataset: tokenize inputs
15
  def preprocess_function(example):
16
  # Tokenize both chosen and rejected responses with a larger max_length
 
17
  chosen_tokenized = tokenizer(
18
  example['prompt'],
19
  truncation=True,
20
  padding='max_length',
21
- max_length=2000 # Adjust based on your longest example but within model limits
22
  )
23
  rejected_tokenized = tokenizer(
24
  example['prompt'],
25
  truncation=True,
26
  padding='max_length',
27
- max_length=2000 # Adjust based on your longest example but within model limits
28
  )
29
 
30
  # Combine chosen and rejected in one sequence
 
 
 
 
 
 
 
 
 
 
 
 
31
  inputs = {
32
- 'input_ids': chosen_tokenized['input_ids'] + rejected_tokenized['input_ids'],
33
- 'attention_mask': chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask'],
34
  'labels': [1] * len(example['chosen']) + [0] * len(example['rejected'])
35
  }
36
 
37
- # Ensure the lengths match; adjust if necessary
38
- max_length = 4096 # Longformer's max length
39
- if len(inputs['input_ids']) > max_length:
40
- inputs['input_ids'] = inputs['input_ids'][:max_length]
41
- inputs['attention_mask'] = inputs['attention_mask'][:max_length]
42
- inputs['labels'] = inputs['labels'][:max_length]
43
-
44
  return inputs
45
-
46
  # Apply preprocessing to the dataset
47
  dataset = dataset.map(preprocess_function, batched=True)
48
 
 
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]
39
+ else:
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