File size: 2,022 Bytes
0f6debc
5112ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datasets, os, polars as pl

specter_dataset: datasets.Dataset = datasets.load_dataset(path = "sentence-transformers/specter", name = "triplet", split = "train") # type: ignore
specter_dataframe: pl.DataFrame = specter_dataset.to_polars() # type: ignore

train_dataframe: pl.DataFrame = specter_dataframe.group_by("anchor").agg([
    pl.col("positive").alias(name = "positive"),
    pl.col("negative").alias(name = "negative")
]).with_columns([
    pl.col("positive").list.head(n = 4),
    pl.col("negative").list.head(n = 4)
]).explode(columns = ["positive", "negative"])

val_test_dataframe: pl.DataFrame  = specter_dataframe.group_by("anchor").agg([
    pl.col("positive").alias(name="positive"),
    pl.col("negative").alias(name="negative")
]).with_columns([
    pl.col("positive").list.tail(n = -4),  # Take all elements after index 3
    pl.col("negative").list.tail(n = -4)   # Take all elements after index 3
])

# Filter out empty lists in validation set (in case some anchors had exactly 4 pairs)
val_test_dataframe = val_test_dataframe.filter(
    pl.col("positive").list.len() > 0
).explode(columns = ["positive", "negative"])

total_len: int = val_test_dataframe.height
val_size: int = int(total_len * 0.6)
val_dataframe: pl.DataFrame  = val_test_dataframe.head(n = val_size)
test_dataframe: pl.DataFrame  = val_test_dataframe.tail(n = total_len - val_size)

train_dataset: datasets.Dataset = datasets.Dataset.from_polars(df = train_dataframe)
val_dataset: datasets.Dataset = datasets.Dataset.from_polars(df = val_dataframe)
test_dataset: datasets.Dataset = datasets.Dataset.from_polars(df = test_dataframe)

train_dataset.push_to_hub(repo_id = "NothingMuch/Specter-Triplet-Split", split = "train", token = os.environ["HUGGINGFACE_TOKEN"])
val_dataset.push_to_hub(repo_id = "NothingMuch/Specter-Triplet-Split", split = "validation", token = os.environ["HUGGINGFACE_TOKEN"])
test_dataset.push_to_hub(repo_id = "NothingMuch/Specter-Triplet-Split", split = "test", token = os.environ["HUGGINGFACE_TOKEN"])