File size: 1,800 Bytes
fb57171
 
 
 
 
 
 
 
 
 
 
 
d4e6067
 
fb57171
 
 
d4e6067
 
fb57171
d4e6067
fb57171
d4e6067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb57171
 
 
 
 
 
 
 
 
 
 
 
 
b7702c8
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
# src/load_data.py

import sys
import os

# Ensure the project root (parent of src/) is in Python path
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if PROJECT_ROOT not in sys.path:
    sys.path.insert(0, PROJECT_ROOT)  # insert at the beginning

# Now we can safely import config
import config
from datasets import load_dataset, DatasetDict, Dataset
import pandas as pd

def load_babe_dataset():
    """

    Load the BABE dataset from local parquet files if available,

    otherwise from Hugging Face.

    Returns:

        dataset: DatasetDict with 'train' and 'test' splits

    """
    local_train = os.path.join(PROJECT_ROOT, "bias_module", "data", "cache", "data", "train-00000-of-00001.parquet")
    local_test = os.path.join(PROJECT_ROOT, "bias_module", "data", "cache", "data", "test-00000-of-00001.parquet")

    if os.path.exists(local_train) and os.path.exists(local_test):
        print("Loading BABE dataset from local parquet files...")
        train_df = pd.read_parquet(local_train)
        test_df = pd.read_parquet(local_test)
        
        dataset = DatasetDict({
            "train": Dataset.from_pandas(train_df),
            "test": Dataset.from_pandas(test_df)
        })
        return dataset

    print(f"Loading BABE dataset from Hugging Face ({config.DATASET_NAME})...")
    # Load the full dataset
    dataset = load_dataset(config.DATASET_NAME)

    # BABE dataset doesn't have a default validation split
    # Split the training data into train (80%) and test (20%)
    dataset = dataset["train"].train_test_split(test_size=0.2)

    return dataset

# Optional: test loading
if __name__ == "__main__":
    dataset = load_babe_dataset()
    print(dataset)
    print(dataset["train"][0])