Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
-
from transformers import
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# Load and preprocess dataset
|
|
@@ -9,22 +9,22 @@ def load_and_preprocess_data():
|
|
| 9 |
dataset = load_dataset('json', data_files='data.json')['train'] # Assuming single split
|
| 10 |
|
| 11 |
# Load the tokenizer
|
| 12 |
-
tokenizer =
|
| 13 |
|
| 14 |
# Preprocess the dataset: tokenize inputs
|
| 15 |
def preprocess_function(example):
|
| 16 |
-
# Tokenize both chosen and rejected responses
|
| 17 |
chosen_tokenized = tokenizer(
|
| 18 |
example['prompt'],
|
| 19 |
truncation=True,
|
| 20 |
padding='max_length',
|
| 21 |
-
max_length=
|
| 22 |
)
|
| 23 |
rejected_tokenized = tokenizer(
|
| 24 |
example['prompt'],
|
| 25 |
truncation=True,
|
| 26 |
padding='max_length',
|
| 27 |
-
max_length=
|
| 28 |
)
|
| 29 |
|
| 30 |
# Combine chosen and rejected in one sequence
|
|
@@ -35,7 +35,7 @@ def load_and_preprocess_data():
|
|
| 35 |
}
|
| 36 |
|
| 37 |
# Ensure the lengths match; adjust if necessary
|
| 38 |
-
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]
|
|
@@ -53,18 +53,18 @@ def load_and_preprocess_data():
|
|
| 53 |
|
| 54 |
# Define and train the model
|
| 55 |
def train_model(dataset):
|
| 56 |
-
# Load the model
|
| 57 |
-
model =
|
| 58 |
|
| 59 |
# Define training arguments
|
| 60 |
training_args = TrainingArguments(
|
| 61 |
output_dir='./results', # Output directory for model checkpoints
|
| 62 |
-
evaluation_strategy="epoch",
|
| 63 |
-
learning_rate=2e-5,
|
| 64 |
-
per_device_train_batch_size=
|
| 65 |
-
per_device_eval_batch_size=
|
| 66 |
-
num_train_epochs=3,
|
| 67 |
-
weight_decay=0.01,
|
| 68 |
)
|
| 69 |
|
| 70 |
# Initialize the Trainer
|
|
@@ -83,7 +83,7 @@ def train_model(dataset):
|
|
| 83 |
# Gradio Interface function to predict
|
| 84 |
def predict(text):
|
| 85 |
# Tokenize the input text
|
| 86 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=
|
| 87 |
# Perform inference
|
| 88 |
outputs = model(**inputs)
|
| 89 |
prediction = torch.argmax(outputs.logits, dim=-1).item() # Get the class with the highest score
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset
|
| 3 |
+
from transformers import LongformerTokenizer, LongformerForSequenceClassification, Trainer, TrainingArguments
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# Load and preprocess dataset
|
|
|
|
| 9 |
dataset = load_dataset('json', data_files='data.json')['train'] # Assuming single split
|
| 10 |
|
| 11 |
# Load the tokenizer
|
| 12 |
+
tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 13 |
|
| 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
|
|
|
|
| 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]
|
|
|
|
| 53 |
|
| 54 |
# Define and train the model
|
| 55 |
def train_model(dataset):
|
| 56 |
+
# Load the Longformer model for sequence classification
|
| 57 |
+
model = LongformerForSequenceClassification.from_pretrained('allenai/longformer-base-4096', num_labels=2)
|
| 58 |
|
| 59 |
# Define training arguments
|
| 60 |
training_args = TrainingArguments(
|
| 61 |
output_dir='./results', # Output directory for model checkpoints
|
| 62 |
+
evaluation_strategy="epoch", # Evaluation at the end of each epoch
|
| 63 |
+
learning_rate=2e-5, # Learning rate
|
| 64 |
+
per_device_train_batch_size=4, # Reduced batch size due to longer sequences
|
| 65 |
+
per_device_eval_batch_size=4, # Reduced batch size for evaluation
|
| 66 |
+
num_train_epochs=3, # Number of training epochs
|
| 67 |
+
weight_decay=0.01, # Strength of weight decay
|
| 68 |
)
|
| 69 |
|
| 70 |
# Initialize the Trainer
|
|
|
|
| 83 |
# Gradio Interface function to predict
|
| 84 |
def predict(text):
|
| 85 |
# Tokenize the input text
|
| 86 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=4096)
|
| 87 |
# Perform inference
|
| 88 |
outputs = model(**inputs)
|
| 89 |
prediction = torch.argmax(outputs.logits, dim=-1).item() # Get the class with the highest score
|