File size: 3,858 Bytes
4c1155b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2024 Rob Kopel.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An extension of Umar Butler's open-australian-legal-corpus dataset to include 1024 long embeddings from OpenAI's text-embedding-3-large model"""

import json
import datasets


_CITATION = """\
@misc{open-australian-legal-embeddings-openai,
    title = {Open Australian Legal Embeddings OpenAI},
    author={Rob Kopel},
    year={2024},
    version={1.0}
    url={https://huggingface.co/datasets/R0bk/open-australian-legal-embeddings-openai}
}
"""

_DESCRIPTION = """\
An extension of Umar Butler's open-australian-legal-corpus dataset to include 1024 long embeddings from OpenAI's text-embedding-3-large model.
If you wish to explore or deploy in your environment it can be used with open-australian-legal-explorer on github.
"""

_HOMEPAGE = "https://huggingface.co/datasets/R0bk/open-australian-legal-embeddings-openai"

_LICENSE = """
Please see the open-australian-legal-corpus licence [Open Australian Legal Corpus](https://huggingface.co/datasets/umarbutler/open-australian-legal-corpus/blob/main/LICENCE.md).
"""

_URLS = {
    'metadatas' : 'data/metadatas.jsonl',
    'texts' : 'data/texts.jsonl',
    'embeddings' : 'data/embeddings.jsonl',
}

class OpenAustralianLegalEmbeddingsOpenai(datasets.GeneratorBasedBuilder):
    """An extension of Umar Butler's open-australian-legal-corpus dataset to include 1024 long embeddings from OpenAI's text-embedding-3-large model"""

    VERSION = datasets.Version("1.0.0")

    DEFAULT_CONFIG_NAME = "train"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    'version_id' : datasets.Value('string'),
                    'type' : datasets.Value('string'),
                    'jurisdiction' : datasets.Value('string'),
                    'source' : datasets.Value('string'),
                    'citation' : datasets.Value('string'),
                    'url' : datasets.Value('string'),
                    'is_last_chunk' : datasets.Value('bool'),
                    'chunk_index' : datasets.Value('int'),
                    'text' : datasets.Value('string'),
                    'embedding' : [datasets.Value('float32')]
                }
            ), 
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        dl_files = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    'metadatas_path' : dl_files['metadatas'],
                    'texts_path' : dl_files['texts'],
                    'embeddings_path' : dl_files['embeddings'],
                }
            )
        ]

    def _generate_examples(self, embed_path, metas_path, texts_path):
        with open(embed_path, 'r') as embeds, \
            open(metas_path, 'r') as metas, \
            open(texts_path, 'r') as texts:

            for key, (embed, meta, text) in enumerate(zip(embeds, metas, texts)):                
                yield key, {
                    **json.loads(meta),
                    'text': json.loads(text),
                    'embedding': json.loads(embed)
                }