Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 7 |
def load_and_preprocess_data():
|
|
@@ -47,10 +143,13 @@ def train_model(dataset):
|
|
| 47 |
output_dir='./results',
|
| 48 |
evaluation_strategy="epoch",
|
| 49 |
learning_rate=2e-5,
|
| 50 |
-
per_device_train_batch_size=
|
| 51 |
-
per_device_eval_batch_size=
|
| 52 |
num_train_epochs=3,
|
| 53 |
weight_decay=0.01,
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
class CustomTrainer(Trainer):
|
|
@@ -83,3 +182,4 @@ if __name__ == "__main__":
|
|
| 83 |
dataset, tokenizer = load_and_preprocess_data()
|
| 84 |
model = train_model(dataset)
|
| 85 |
gr.Interface(fn=predict, inputs=gr.Textbox(), outputs="text").launch()
|
|
|
|
|
|
| 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
|
| 7 |
+
# def load_and_preprocess_data():
|
| 8 |
+
# dataset = load_dataset('json', data_files='data.json')['train']
|
| 9 |
+
|
| 10 |
+
# tokenizer = LongformerTokenizer.from_pretrained('allenai/longformer-base-4096')
|
| 11 |
+
|
| 12 |
+
# def preprocess_function(examples):
|
| 13 |
+
# inputs = {
|
| 14 |
+
# 'input_ids': [],
|
| 15 |
+
# 'attention_mask': [],
|
| 16 |
+
# 'labels': []
|
| 17 |
+
# }
|
| 18 |
+
# for example in examples['prompt']:
|
| 19 |
+
# # Tokenize input
|
| 20 |
+
# tokenized = tokenizer(
|
| 21 |
+
# example,
|
| 22 |
+
# truncation=True,
|
| 23 |
+
# padding='max_length',
|
| 24 |
+
# max_length=128 # Increase max_length to suit model needs
|
| 25 |
+
# )
|
| 26 |
+
|
| 27 |
+
# inputs['input_ids'].append(tokenized['input_ids'])
|
| 28 |
+
# inputs['attention_mask'].append(tokenized['attention_mask'])
|
| 29 |
+
# inputs['labels'].append(1 if "chosen" in example else 0) # Assign binary labels
|
| 30 |
+
|
| 31 |
+
# inputs['input_ids'] = torch.tensor(inputs['input_ids'], dtype=torch.long)
|
| 32 |
+
# inputs['attention_mask'] = torch.tensor(inputs['attention_mask'], dtype=torch.long)
|
| 33 |
+
# inputs['labels'] = torch.tensor(inputs['labels'], dtype=torch.long)
|
| 34 |
+
|
| 35 |
+
# return inputs
|
| 36 |
+
|
| 37 |
+
# dataset = dataset.map(preprocess_function, batched=True)
|
| 38 |
+
# dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
|
| 39 |
+
|
| 40 |
+
# return dataset, tokenizer
|
| 41 |
+
|
| 42 |
+
# # Define and train the model
|
| 43 |
+
# def train_model(dataset):
|
| 44 |
+
# model = LongformerForSequenceClassification.from_pretrained('allenai/longformer-base-4096', num_labels=2)
|
| 45 |
+
|
| 46 |
+
# training_args = TrainingArguments(
|
| 47 |
+
# output_dir='./results',
|
| 48 |
+
# evaluation_strategy="epoch",
|
| 49 |
+
# learning_rate=2e-5,
|
| 50 |
+
# per_device_train_batch_size=4,
|
| 51 |
+
# per_device_eval_batch_size=4,
|
| 52 |
+
# num_train_epochs=3,
|
| 53 |
+
# weight_decay=0.01,
|
| 54 |
+
# )
|
| 55 |
+
|
| 56 |
+
# class CustomTrainer(Trainer):
|
| 57 |
+
# def compute_loss(self, model, inputs, return_outputs=False, **kwargs):
|
| 58 |
+
# labels = inputs.pop("labels")
|
| 59 |
+
# outputs = model(**inputs)
|
| 60 |
+
# logits = outputs.logits
|
| 61 |
+
# loss = torch.nn.CrossEntropyLoss()(logits.view(-1, model.config.num_labels), labels.view(-1))
|
| 62 |
+
# return (loss, outputs) if return_outputs else loss
|
| 63 |
+
|
| 64 |
+
# trainer = CustomTrainer(
|
| 65 |
+
# model=model,
|
| 66 |
+
# args=training_args,
|
| 67 |
+
# train_dataset=dataset,
|
| 68 |
+
# eval_dataset=dataset,
|
| 69 |
+
# )
|
| 70 |
+
|
| 71 |
+
# trainer.train()
|
| 72 |
+
# return model
|
| 73 |
+
|
| 74 |
+
# # Gradio Interface function to predict
|
| 75 |
+
# def predict(text):
|
| 76 |
+
# inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 77 |
+
# outputs = model(**inputs)
|
| 78 |
+
# prediction = torch.argmax(outputs.logits, dim=-1).item()
|
| 79 |
+
# return f"Prediction: {'chosen' if prediction == 1 else 'rejected'}"
|
| 80 |
+
|
| 81 |
+
# # Main script
|
| 82 |
+
# if __name__ == "__main__":
|
| 83 |
+
# dataset, tokenizer = load_and_preprocess_data()
|
| 84 |
+
# model = train_model(dataset)
|
| 85 |
+
# gr.Interface(fn=predict, inputs=gr.Textbox(), outputs="text").launch()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
import gradio as gr
|
| 97 |
from datasets import load_dataset
|
| 98 |
from transformers import LongformerTokenizer, LongformerForSequenceClassification, Trainer, TrainingArguments
|
| 99 |
import torch
|
| 100 |
+
from torch.utils.data import DataLoader
|
| 101 |
|
| 102 |
# Load and preprocess dataset
|
| 103 |
def load_and_preprocess_data():
|
|
|
|
| 143 |
output_dir='./results',
|
| 144 |
evaluation_strategy="epoch",
|
| 145 |
learning_rate=2e-5,
|
| 146 |
+
per_device_train_batch_size=16, # Increased batch size for faster training
|
| 147 |
+
per_device_eval_batch_size=16, # Increased eval batch size
|
| 148 |
num_train_epochs=3,
|
| 149 |
weight_decay=0.01,
|
| 150 |
+
fp16=True, # Enable mixed precision
|
| 151 |
+
dataloader_num_workers=4, # Increase to load data in parallel
|
| 152 |
+
gradient_accumulation_steps=2, # Simulate larger batch size
|
| 153 |
)
|
| 154 |
|
| 155 |
class CustomTrainer(Trainer):
|
|
|
|
| 182 |
dataset, tokenizer = load_and_preprocess_data()
|
| 183 |
model = train_model(dataset)
|
| 184 |
gr.Interface(fn=predict, inputs=gr.Textbox(), outputs="text").launch()
|
| 185 |
+
|