FiendFlb commited on
Commit
fb5819f
·
verified ·
1 Parent(s): 3dca0ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -13,42 +13,39 @@ def load_and_preprocess_data():
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}")
 
13
 
14
  # Preprocess the dataset: tokenize inputs
15
  def preprocess_function(examples):
 
16
  inputs = {
17
  'input_ids': [],
18
  'attention_mask': [],
19
  'labels': []
20
  }
21
+ for example in examples['prompt']:
22
  # Tokenize chosen and rejected parts separately
23
  chosen_tokenized = tokenizer(
24
  example,
25
  truncation=True,
26
  padding='max_length',
27
+ max_length=4
28
  )
29
  rejected_tokenized = tokenizer(
30
  example,
31
  truncation=True,
32
  padding='max_length',
33
+ max_length=4
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
+ label_length = len(combined_input_ids)
40
+ # Ensure labels match the exact length of input_ids
41
  inputs['input_ids'].append(combined_input_ids)
42
  inputs['attention_mask'].append(combined_attention_mask)
43
+ inputs['labels'].append(torch.tensor([1] * (label_length // 2) + [0] * (label_length - label_length // 2)))
 
 
 
44
 
45
  # Convert lists to tensors for each batch
46
+ inputs['input_ids'] = torch.tensor(inputs['input_ids'], dtype=torch.long)
47
+ inputs['attention_mask'] = torch.tensor(inputs['attention_mask'], dtype=torch.long)
48
+ inputs['labels'] = torch.stack(inputs['labels'])
49
 
50
  # Debugging
51
  print(f"Input_ids shape: {inputs['input_ids'].shape}, Labels shape: {inputs['labels'].shape}")