Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,51 @@
|
|
1 |
-
import
|
2 |
-
from transformers import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration, AdamW
|
3 |
+
from torch.utils.data import DataLoader, Dataset
|
4 |
+
from tqdm import tqdm
|
5 |
+
|
6 |
+
# Define your dataset class
|
7 |
+
class SpiderDataset(Dataset):
|
8 |
+
def __init__(self, encodings, labels):
|
9 |
+
self.encodings = encodings
|
10 |
+
self.labels = labels
|
11 |
+
|
12 |
+
def __getitem__(self, idx):
|
13 |
+
return {'input_ids': self.encodings[idx], 'labels': self.labels[idx]}
|
14 |
+
|
15 |
+
def __len__(self):
|
16 |
+
return len(self.encodings)
|
17 |
+
|
18 |
+
# Load your preprocessed Spider dataset
|
19 |
+
train_encodings = # Your preprocessed input encodings for training (e.g., a list of input IDs)
|
20 |
+
train_labels = # Your preprocessed labels for training (e.g., a list of label IDs)
|
21 |
+
|
22 |
+
# Create a PyTorch dataset and dataloader
|
23 |
+
train_dataset = SpiderDataset(train_encodings, train_labels)
|
24 |
+
train_loader = DataLoader(train_dataset, batch_size=8, shuffle=True)
|
25 |
+
|
26 |
+
# Load the pre-trained T5 model
|
27 |
+
model = T5ForConditionalGeneration.from_pretrained('t5-base')
|
28 |
+
tokenizer = T5Tokenizer.from_pretrained('t5-base')
|
29 |
+
|
30 |
+
# Move the model to the GPU if available
|
31 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
32 |
+
model.to(device)
|
33 |
+
|
34 |
+
# Set up the optimizer
|
35 |
+
optimizer = AdamW(model.parameters(), lr=5e-5)
|
36 |
+
|
37 |
+
# Fine-tune the model
|
38 |
+
model.train()
|
39 |
+
for epoch in range(3): # Number of epochs
|
40 |
+
for batch in tqdm(train_loader):
|
41 |
+
optimizer.zero_grad()
|
42 |
+
input_ids = batch['input_ids'].to(device)
|
43 |
+
labels = batch['labels'].to(device)
|
44 |
+
outputs = model(input_ids=input_ids, labels=labels)
|
45 |
+
loss = outputs.loss
|
46 |
+
loss.backward()
|
47 |
+
optimizer.step()
|
48 |
+
|
49 |
+
# Save the fine-tuned model
|
50 |
+
model.save_pretrained('your_model_directory')
|
51 |
+
tokenizer.save_pretrained('your_model_directory')
|