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"])