Update app.py
Browse files18 unmodified lines192021222324251 unmodified line272829303132331 unmodified line35363738394041421 unmodified line44454647484950515253545556575859606118 unmodified lines'attention_mask': [],'labels': []}for example in examples['prompt']: # Adjust if 'prompt' is not the key for your textchosen_tokenized = tokenizer(example,truncation=True,1 unmodified linemax_length=max_length)rejected_tokenized = tokenizer(example, # Here you might need to adjust for how you handle 'chosen' and 'rejected'truncation=True,padding='max_length',max_length=max_length1 unmodified linecombined_input_ids = chosen_tokenized['input_ids'] + rejected_tokenized['input_ids']combined_attention_mask = chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask']# Pad or truncate to ensure length is 1000if len(combined_input_ids) > 1000:combined_input_ids = combined_input_ids[:1000]combined_attention_mask = combined_attention_mask[:1000]1 unmodified linepadding_length = 1000 - len(combined_input_ids)combined_input_ids += [tokenizer.pad_token_id] * padding_lengthcombined_attention_mask += [0] * padding_lengthinputs['input_ids'].append(combined_input_ids)inputs['attention_mask'].append(combined_attention_mask)inputs['labels'].append([1] * 500 + [0] * 500) # Adjust based on your label structure# Convert lists to tensors for each batchinputs['input_ids'] = torch.tensor(inputs['input_ids'])inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])inputs['labels'] = torch.tensor(inputs['labels'])return inputs# Apply preprocessing to the datasetdataset = dataset.map(preprocess_function, batched=True, remove_columns=dataset.column_names)18 unmodified lines192021222324251 unmodified line272829303132331 unmodified line35363738394041421 unmodified line444546474849505152535455565758596018 unmodified lines'attention_mask': [],'labels': []}for example in examples['prompt']:chosen_tokenized = tokenizer(example,truncation=True,1 unmodified linemax_length=max_length)rejected_tokenized = tokenizer(example,truncation=True,padding='max_length',max_length=max_length1 unmodified linecombined_input_ids = chosen_tokenized['input_ids'] + rejected_tokenized['input_ids']combined_attention_mask = chosen_tokenized['attention_mask'] + rejected_tokenized['attention_mask']# Ensure length is 1000if len(combined_input_ids) > 1000:combined_input_ids = combined_input_ids[:1000]combined_attention_mask = combined_attention_mask[:1000]1 unmodified linepadding_length = 1000 - len(combined_input_ids)combined_input_ids += [tokenizer.pad_token_id] * padding_lengthcombined_attention_mask += [0] * padding_lengthinputs['input_ids'].append(combined_input_ids)inputs['attention_mask'].append(combined_attention_mask)inputs['labels'].append(torch.tensor([1] * 500 + [0] * 500)) # Convert to tensor immediately# Convert lists to tensors for each batchinputs['input_ids'] = torch.tensor(inputs['input_ids'])inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])inputs['labels'] = torch.stack(inputs['labels']) # Stack along a new dimensionreturn inputs# Apply preprocessing to the datasetdataset = dataset.map(preprocess_function, batched=True, remove_columns=dataset.column_names)
app.py+9-10