FiendFlb commited on
Commit
3940c81
·
verified ·
1 Parent(s): 03e8a0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
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 inputs
16
  def preprocess_function(example):
17
- inputs = tokenizer(
 
18
  example['prompt'],
19
  truncation=True,
20
  padding='max_length',
21
  max_length=512
22
  )
23
- # Assume binary classification: 1 for chosen, 0 for rejected
24
- inputs['labels'] = [1] * len(example['chosen']) + [0] * len(example['rejected'])
25
- inputs['input_ids'] = (
26
- inputs['input_ids'][:len(example['chosen'])] +
27
- inputs['input_ids'][len(example['chosen']):]
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