Upload dataset.py with huggingface_hub
Browse files- dataset.py +73 -73
dataset.py
CHANGED
|
@@ -1,73 +1,73 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import torch
|
| 3 |
-
from typing import Final, Dict
|
| 4 |
-
from torch.utils.data import Dataset
|
| 5 |
-
|
| 6 |
-
class FriendsDataset(Dataset):
|
| 7 |
-
""" Dataset class for Friends transcript dialogue data (.json).
|
| 8 |
-
Args:
|
| 9 |
-
data: the .json corpus file (path name)
|
| 10 |
-
tokenizer: custom GPT-2 tokenizer
|
| 11 |
-
maxt: maximum token length, default 128
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
# hard code speakers
|
| 15 |
-
SPEAKER_LOOKUP: Final[Dict[str, int]] = {"ROSS": 0, "MONICA": 1, "CHANDLER": 2,
|
| 16 |
-
"JOEY": 3, "RACHEL": 4, "PHOEBE": 5,
|
| 17 |
-
"GUNTHER": 6, "JANICE": 7, "RICHARD": 8,
|
| 18 |
-
"CAROL": 9, "SUSAN": 10, "MIKE": 11, "OTHER": 12}
|
| 19 |
-
|
| 20 |
-
def __init__(self, data, tokenizer, maxt = 128):
|
| 21 |
-
self.sequences = []
|
| 22 |
-
self.responders = []
|
| 23 |
-
self.tokenizer = tokenizer
|
| 24 |
-
self.maxt = maxt
|
| 25 |
-
self.pad_id = tokenizer.pad_token_id
|
| 26 |
-
|
| 27 |
-
# import corpus
|
| 28 |
-
with open(data, 'r', encoding = 'utf-8') as f:
|
| 29 |
-
corpus = json.load(f)
|
| 30 |
-
|
| 31 |
-
def format_turn(turn: dict[str, str]) -> str:
|
| 32 |
-
""" Context Format Helper
|
| 33 |
-
"""
|
| 34 |
-
speaker = turn['speaker'].upper()
|
| 35 |
-
return f"<SPEAKER={speaker}> {turn['text']}", speaker
|
| 36 |
-
|
| 37 |
-
# unravel scenes and construct input sequence tensors
|
| 38 |
-
for scene in corpus:
|
| 39 |
-
turns = scene['turns']
|
| 40 |
-
n = len(turns)
|
| 41 |
-
|
| 42 |
-
# Context Windows (1,2,3)
|
| 43 |
-
for i in range(n):
|
| 44 |
-
for window in range(1, 4):
|
| 45 |
-
|
| 46 |
-
if i - window < 0: continue
|
| 47 |
-
|
| 48 |
-
context = turns[i - window:i]
|
| 49 |
-
response = turns[i]
|
| 50 |
-
|
| 51 |
-
# build sequence
|
| 52 |
-
context_str = "\n".join(format_turn(t)[0] for t in context)
|
| 53 |
-
response_str, responder = format_turn(response)
|
| 54 |
-
sequence = ("<CONTEXT>\n" + context_str + \
|
| 55 |
-
"\n</CONTEXT>\n<RESPONSE>\n" + \
|
| 56 |
-
response_str + "\n<EOT>")
|
| 57 |
-
self.sequences.append(sequence)
|
| 58 |
-
self.responders.append(self.SPEAKER_LOOKUP.get(
|
| 59 |
-
responder, self.SPEAKER_LOOKUP["OTHER"]))
|
| 60 |
-
|
| 61 |
-
def __len__(self): return len(self.sequences)
|
| 62 |
-
def __getitem__(self, index):
|
| 63 |
-
""" Tokenization on the fly
|
| 64 |
-
"""
|
| 65 |
-
encoded = self.tokenizer(self.sequences[index], add_special_tokens = False,
|
| 66 |
-
max_length = self.maxt,
|
| 67 |
-
truncation = True,
|
| 68 |
-
padding = 'max_length',
|
| 69 |
-
return_tensors = 'pt')
|
| 70 |
-
|
| 71 |
-
return {'input_ids': encoded['input_ids'].squeeze(0),
|
| 72 |
-
'attention_mask': encoded['attention_mask'].squeeze(0),
|
| 73 |
-
'responder': torch.tensor(self.responders[index], dtype = torch.long)}
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import torch
|
| 3 |
+
from typing import Final, Dict
|
| 4 |
+
from torch.utils.data import Dataset
|
| 5 |
+
|
| 6 |
+
class FriendsDataset(Dataset):
|
| 7 |
+
""" Dataset class for Friends transcript dialogue data (.json).
|
| 8 |
+
Args:
|
| 9 |
+
data: the .json corpus file (path name)
|
| 10 |
+
tokenizer: custom GPT-2 tokenizer
|
| 11 |
+
maxt: maximum token length, default 128
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
# hard code speakers
|
| 15 |
+
SPEAKER_LOOKUP: Final[Dict[str, int]] = {"ROSS": 0, "MONICA": 1, "CHANDLER": 2,
|
| 16 |
+
"JOEY": 3, "RACHEL": 4, "PHOEBE": 5,
|
| 17 |
+
"GUNTHER": 6, "JANICE": 7, "RICHARD": 8,
|
| 18 |
+
"CAROL": 9, "SUSAN": 10, "MIKE": 11, "OTHER": 12}
|
| 19 |
+
|
| 20 |
+
def __init__(self, data, tokenizer, maxt = 128):
|
| 21 |
+
self.sequences = []
|
| 22 |
+
self.responders = []
|
| 23 |
+
self.tokenizer = tokenizer
|
| 24 |
+
self.maxt = maxt
|
| 25 |
+
self.pad_id = tokenizer.pad_token_id
|
| 26 |
+
|
| 27 |
+
# import corpus
|
| 28 |
+
with open(data, 'r', encoding = 'utf-8') as f:
|
| 29 |
+
corpus = json.load(f)
|
| 30 |
+
|
| 31 |
+
def format_turn(turn: dict[str, str]) -> str:
|
| 32 |
+
""" Context Format Helper
|
| 33 |
+
"""
|
| 34 |
+
speaker = turn['speaker'].upper()
|
| 35 |
+
return f"<SPEAKER={speaker}> {turn['text']}", speaker
|
| 36 |
+
|
| 37 |
+
# unravel scenes and construct input sequence tensors
|
| 38 |
+
for scene in corpus:
|
| 39 |
+
turns = scene['turns']
|
| 40 |
+
n = len(turns)
|
| 41 |
+
|
| 42 |
+
# Context Windows (1,2,3)
|
| 43 |
+
for i in range(n):
|
| 44 |
+
for window in range(1, 4):
|
| 45 |
+
|
| 46 |
+
if i - window < 0: continue
|
| 47 |
+
|
| 48 |
+
context = turns[i - window:i]
|
| 49 |
+
response = turns[i]
|
| 50 |
+
|
| 51 |
+
# build sequence
|
| 52 |
+
context_str = "\n".join(format_turn(t)[0] for t in context)
|
| 53 |
+
response_str, responder = format_turn(response)
|
| 54 |
+
sequence = ("<CONTEXT>\n" + context_str + \
|
| 55 |
+
"\n</CONTEXT>\n<RESPONSE>\n" + \
|
| 56 |
+
response_str + "\n<EOT>")
|
| 57 |
+
self.sequences.append(sequence)
|
| 58 |
+
self.responders.append(self.SPEAKER_LOOKUP.get(
|
| 59 |
+
responder, self.SPEAKER_LOOKUP["OTHER"]))
|
| 60 |
+
|
| 61 |
+
def __len__(self): return len(self.sequences)
|
| 62 |
+
def __getitem__(self, index):
|
| 63 |
+
""" Tokenization on the fly
|
| 64 |
+
"""
|
| 65 |
+
encoded = self.tokenizer(self.sequences[index], add_special_tokens = False,
|
| 66 |
+
max_length = self.maxt,
|
| 67 |
+
truncation = True,
|
| 68 |
+
padding = 'max_length',
|
| 69 |
+
return_tensors = 'pt')
|
| 70 |
+
|
| 71 |
+
return {'input_ids': encoded['input_ids'].squeeze(0),
|
| 72 |
+
'attention_mask': encoded['attention_mask'].squeeze(0),
|
| 73 |
+
'responder': torch.tensor(self.responders[index], dtype = torch.long)}
|