-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathtext_generation_pool.py
47 lines (38 loc) · 1.21 KB
/
text_generation_pool.py
1
2
3
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
import random
from abc import abstractclassmethod
from dataclasses import dataclass
from typing import Any, List, Dict
@dataclass(init=True)
class Sample:
id: str
prompt_or_input_text: str
references: List[str]
meta_data: Dict[str, Any] = None
class TextGenPool:
def __init__(self, samples: List[Sample]):
self._samples = samples
def __len__(self):
return len(self._samples)
def __getitem__(self, ix: int) -> Sample:
if ix >= len(self):
raise StopIteration
sample = self._samples[ix]
return sample, 1.0
def sample(self) -> Sample:
random_sample = random.choice(self._samples)
return random_sample
@abstractclassmethod
def prepare(cls, **args) -> 'TextGenPool':
"""
A factory method to instantiate data pool
"""
raise NotImplementedError
def split(self, split_ratios: List[float]) -> List['TextGenPool']:
start_ix = 0
pools = []
for ratio in split_ratios:
count = int(len(self) * ratio)
end_ix = start_ix + count
pools.append(type(self)(self._samples[start_ix: end_ix]))
start_ix = end_ix
return pools