Update app.py
Browse files
app.py
CHANGED
|
@@ -13,13 +13,13 @@ def load_and_preprocess_data():
|
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
def preprocess_function(examples):
|
| 16 |
-
max_length =
|
| 17 |
inputs = {
|
| 18 |
'input_ids': [],
|
| 19 |
'attention_mask': [],
|
| 20 |
'labels': []
|
| 21 |
}
|
| 22 |
-
for example in examples['prompt']:
|
| 23 |
chosen_tokenized = tokenizer(
|
| 24 |
example,
|
| 25 |
truncation=True,
|
|
@@ -27,7 +27,7 @@ def load_and_preprocess_data():
|
|
| 27 |
max_length=max_length
|
| 28 |
)
|
| 29 |
rejected_tokenized = tokenizer(
|
| 30 |
-
example,
|
| 31 |
truncation=True,
|
| 32 |
padding='max_length',
|
| 33 |
max_length=max_length
|
|
@@ -36,23 +36,16 @@ def load_and_preprocess_data():
|
|
| 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 |
-
#
|
| 40 |
-
if len(combined_input_ids) > 1000:
|
| 41 |
-
combined_input_ids = combined_input_ids[:1000]
|
| 42 |
-
combined_attention_mask = combined_attention_mask[:1000]
|
| 43 |
-
else:
|
| 44 |
-
padding_length = 1000 - len(combined_input_ids)
|
| 45 |
-
combined_input_ids += [tokenizer.pad_token_id] * padding_length
|
| 46 |
-
combined_attention_mask += [0] * padding_length
|
| 47 |
-
|
| 48 |
inputs['input_ids'].append(combined_input_ids)
|
| 49 |
inputs['attention_mask'].append(combined_attention_mask)
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
# Convert lists to tensors for each batch
|
| 53 |
inputs['input_ids'] = torch.tensor(inputs['input_ids'])
|
| 54 |
inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])
|
| 55 |
-
inputs['labels'] = torch.stack(inputs['labels']) # Stack along
|
| 56 |
|
| 57 |
return inputs
|
| 58 |
# Apply preprocessing to the dataset
|
|
@@ -105,7 +98,7 @@ def train_model(dataset):
|
|
| 105 |
# Gradio Interface function to predict
|
| 106 |
def predict(text):
|
| 107 |
# Tokenize the input text
|
| 108 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=
|
| 109 |
# Perform inference
|
| 110 |
outputs = model(**inputs)
|
| 111 |
prediction = torch.argmax(outputs.logits, dim=-1).item() # Get the class with the highest score
|
|
|
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
def preprocess_function(examples):
|
| 16 |
+
max_length = 4 # Now using 4 for both chosen and rejected
|
| 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 |
chosen_tokenized = tokenizer(
|
| 24 |
example,
|
| 25 |
truncation=True,
|
|
|
|
| 27 |
max_length=max_length
|
| 28 |
)
|
| 29 |
rejected_tokenized = tokenizer(
|
| 30 |
+
example,
|
| 31 |
truncation=True,
|
| 32 |
padding='max_length',
|
| 33 |
max_length=max_length
|
|
|
|
| 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 |
+
# No need for padding or truncating since we're already at max length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
inputs['input_ids'].append(combined_input_ids)
|
| 41 |
inputs['attention_mask'].append(combined_attention_mask)
|
| 42 |
+
# Adjust labels to match new length; here assuming binary classification where first half is 'chosen'
|
| 43 |
+
inputs['labels'].append(torch.tensor([1] * 2 + [0] * 2)) # 4 labels for 4 tokens
|
| 44 |
|
| 45 |
# Convert lists to tensors for each batch
|
| 46 |
inputs['input_ids'] = torch.tensor(inputs['input_ids'])
|
| 47 |
inputs['attention_mask'] = torch.tensor(inputs['attention_mask'])
|
| 48 |
+
inputs['labels'] = torch.stack(inputs['labels']) # Stack labels along batch dimension
|
| 49 |
|
| 50 |
return inputs
|
| 51 |
# Apply preprocessing to the dataset
|
|
|
|
| 98 |
# Gradio Interface function to predict
|
| 99 |
def predict(text):
|
| 100 |
# Tokenize the input text
|
| 101 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=4) # Adjust max_length to match preprocessing
|
| 102 |
# Perform inference
|
| 103 |
outputs = model(**inputs)
|
| 104 |
prediction = torch.argmax(outputs.logits, dim=-1).item() # Get the class with the highest score
|