File size: 3,630 Bytes
d1ea340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """\
@dataset{gotthatdata_stargate_2024,
  title  = {STARGATE: CIA Remote Viewing Archive},
  author = {GotThatData},
  year   = {2024},
  url    = {https://huggingface.co/datasets/GotThatData/STARGATE}
}
"""

_DESCRIPTION = """\
STARGATE is a dataset of 12,000+ declassified CIA PDFs related to remote viewing (RV), extrasensory perception (ESP), and anomalous cognition. 
This loader includes structured metadata and binary access to the original scanned PDFs.
"""

_HOMEPAGE = "https://huggingface.co/datasets/GotThatData/STARGATE"

_LICENSE = "CC-BY-4.0"

class StargatePDFConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(StargatePDFConfig, self).__init__(**kwargs)

class StargatePDFDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        StargatePDFConfig(name="default", version=VERSION, description="STARGATE raw PDFs with metadata")
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({
                "filename": datasets.Value("string"),
                "document_id": datasets.Value("string"),
                "page_count": datasets.Value("int32"),
                "image_count": datasets.Value("int32"),
                "processed_at": datasets.Value("string"),
                "ocr_status": datasets.Value("string"),
                "text_extracted": datasets.Value("bool"),
                "source": datasets.Value("string"),
                "tags": datasets.Sequence(datasets.Value("string")),
                "pdf": datasets.Value("binary"),
            }),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # Path to local or downloaded files
        archive_path = dl_manager.download_and_extract("./")  # Change if remote
        metadata_path = os.path.join(archive_path, "metadata.json")
        data_dir = os.path.join(archive_path, "data")

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"metadata_path": metadata_path, "data_dir": data_dir}
            )
        ]

    def _generate_examples(self, metadata_path, data_dir):
        logger.info(f"⏳ Loading metadata from {metadata_path}")
        with open(metadata_path, "r", encoding="utf-8") as f:
            records = json.load(f)

        for idx, record in enumerate(records):
            pdf_path = os.path.join(data_dir, record["filename"])
            if not os.path.isfile(pdf_path):
                logger.warning(f"🚫 Missing PDF: {pdf_path}")
                continue

            with open(pdf_path, "rb") as pdf_file:
                yield idx, {
                    "filename": record.get("filename"),
                    "document_id": record.get("document_id", record["filename"].replace(".pdf", "")),
                    "page_count": record.get("page_count", 0),
                    "image_count": record.get("image_count", 0),
                    "processed_at": record.get("processed_at", ""),
                    "ocr_status": record.get("ocr_status", "pending"),
                    "text_extracted": record.get("text_extracted", False),
                    "source": record.get("source", "CIA Stargate Archive"),
                    "tags": record.get("tags", []),
                    "pdf": pdf_file.read(),
                }