File size: 2,442 Bytes
59953e2
84c740d
 
 
 
 
 
 
 
 
59953e2
 
 
 
84c740d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6de56f
84c740d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461a045
84c740d
 
 
59953e2
461a045
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pandas as pd
import datasets
from sklearn.model_selection import train_test_split

_CITATION = "N/A"
_DESCRIPTION = "Embeddings for the jokes in Jester jokes dataset" 
_HOMEPAGE = "N/A"
_LICENSE = "apache-2.0"

_URLS = {
        "mistral": "./jester-salesforce-sfr-embedding-mistral.parquet",
        "instructor-xl": "./jester-hkunlp-instructor-xl.parquet",
        "all-MiniLM-L6-v2": "./jester-sentence-transformers-all-MiniLM-L6-v2.parquet",
        "all-mpnet-base-v2": "./jester-sentence-transformers-all-mpnet-base-v2.parquet",
}


_DIMS = {
        "mistral": 4096,
        "instructor-xl": 768,
        "all-MiniLM-L6-v2": 384,
        "all-mpnet-base-v2": 768,
}


class JesterEmbedding(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("0.0.1")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="mistral", version=VERSION, description="SFR-Embedding by Salesforce Research."),
        datasets.BuilderConfig(name="instructor-xl", version=VERSION, description="Instructor embedding"),
        datasets.BuilderConfig(name="all-MiniLM-L6-v2", version=VERSION, description="All-round model embedding tuned for many use-cases"),
        datasets.BuilderConfig(name="all-mpnet-base-v2", version=VERSION, description="All-round model embedding tuned for many use-cases"),
    ]

    DEFAULT_CONFIG_NAME = "mistral"  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        features = datasets.Features({"x": datasets.Array2D(shape=(1, _DIMS[self.config.name]), dtype="float32")})
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        urls = _URLS[self.config.name]
        data_dir = dl_manager.download_and_extract(urls)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": data_dir,
                    "split": "train",
                },
            )
        ]

    def _generate_examples(self, filepath, split):
        embeddings = pd.read_parquet(filepath).values
        for _id, x in enumerate(embeddings):
            yield _id, {"x": x.reshape(1, -1)}