Spaces:
Sleeping
Sleeping
Update dataset.py
Browse files- dataset.py +44 -69
dataset.py
CHANGED
|
@@ -1,69 +1,44 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
class LanguageModelingDataset(torch.utils.data.Dataset):
|
| 48 |
-
"""
|
| 49 |
-
Dataset class for language modeling task. This is the dataset you will use to train your encoder for the language modeling task.
|
| 50 |
-
|
| 51 |
-
Args:
|
| 52 |
-
tokenizer (Tokenizer): The tokenizer used to encode the text.
|
| 53 |
-
text (str): The text data.
|
| 54 |
-
block_size (int): The size of each block of text.
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
def __init__(self, tokenizer, text, block_size):
|
| 58 |
-
self.tokenizer = tokenizer
|
| 59 |
-
self.data = torch.tensor(self.tokenizer.encode(text), dtype=torch.long)
|
| 60 |
-
self.block_size = block_size
|
| 61 |
-
|
| 62 |
-
def __len__(self):
|
| 63 |
-
return len(self.data) - self.block_size
|
| 64 |
-
|
| 65 |
-
def __getitem__(self, idx):
|
| 66 |
-
chunk = self.data[idx:idx + self.block_size + 1]
|
| 67 |
-
x = chunk[:-1]
|
| 68 |
-
y = chunk[1:]
|
| 69 |
-
return x, y
|
|
|
|
| 1 |
+
### DATASET.PY ###
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from torch.utils.data import Dataset
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class SpeechesClassificationDataset(Dataset):
|
| 9 |
+
"""
|
| 10 |
+
Dataset class for text classification task.
|
| 11 |
+
This the dataset you will use to train your encoder, and classifier jointly,
|
| 12 |
+
end-to-end for the text classification task.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
tokenizer (Tokenizer): The tokenizer used to encode the text.
|
| 16 |
+
file_path (str): The path to the file containing the speech classification data.
|
| 17 |
+
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def __init__(self, tokenizer, file_path):
|
| 21 |
+
self.tokenizer = tokenizer
|
| 22 |
+
self.samples = []
|
| 23 |
+
|
| 24 |
+
if not os.path.exists(file_path):
|
| 25 |
+
raise FileNotFoundError(f"The file {file_path} does not exist.")
|
| 26 |
+
|
| 27 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 28 |
+
for line in file:
|
| 29 |
+
label, text = line.strip().split('\t')
|
| 30 |
+
|
| 31 |
+
if len(text.strip()) == 0:
|
| 32 |
+
continue
|
| 33 |
+
|
| 34 |
+
self.samples.append((int(label), text))
|
| 35 |
+
|
| 36 |
+
def __len__(self):
|
| 37 |
+
return len(self.samples)
|
| 38 |
+
|
| 39 |
+
def __getitem__(self, index):
|
| 40 |
+
label, text = self.samples[index]
|
| 41 |
+
input_ids = torch.tensor(self.tokenizer.encode(text), dtype=torch.long)
|
| 42 |
+
label_tensor = torch.tensor(label, dtype=torch.long)
|
| 43 |
+
|
| 44 |
+
return input_ids, label_tensor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|