Upload collections.py with huggingface_hub
Browse files- collections.py +41 -0
collections.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .artifact import Artifact
|
2 |
+
|
3 |
+
from abc import abstractmethod
|
4 |
+
|
5 |
+
from dataclasses import field
|
6 |
+
import random
|
7 |
+
|
8 |
+
|
9 |
+
class Collection(Artifact):
|
10 |
+
@abstractmethod
|
11 |
+
def __getitem__(self, key):
|
12 |
+
pass
|
13 |
+
|
14 |
+
|
15 |
+
class ListCollection(Collection):
|
16 |
+
items: list = field(default_factory=list)
|
17 |
+
|
18 |
+
def __getitem__(self, index):
|
19 |
+
return self.items[index]
|
20 |
+
|
21 |
+
|
22 |
+
class DictCollection(Collection):
|
23 |
+
items: dict = field(default_factory=dict)
|
24 |
+
|
25 |
+
def __getitem__(self, key):
|
26 |
+
return self.items[key]
|
27 |
+
|
28 |
+
|
29 |
+
class ItemPicker(Artifact):
|
30 |
+
item: object = None
|
31 |
+
|
32 |
+
def __call__(self, collection: Collection):
|
33 |
+
return collection[self.item]
|
34 |
+
|
35 |
+
|
36 |
+
class RandomPicker(Artifact):
|
37 |
+
def __call__(self, collection: Collection):
|
38 |
+
if isinstance(collection, ListCollection):
|
39 |
+
return random.choice(list(collection.items))
|
40 |
+
elif isinstance(collection, DictCollection):
|
41 |
+
return random.choice(list(collection.items.values()))
|