import random

from datasets import DatasetDict, load_dataset


ds = load_dataset("HuggingFaceM4/TextCaps", use_auth_token=True, download_mode="force_redownload")
print(f"Composition of the original dataset: {ds}")


number_of_examples_support_sets = 2048
number_of_examples_captioning_validation_query_sets = 5000
repo_id = "HuggingFaceM4/TextCaps_support_query_sets"

# Assign the train split indices to the validation support set, validation query set, and test support set
indices_train_set = list(range(0, len(ds["train"])))
random.shuffle(indices_train_set)
remaining_indices_train_set = indices_train_set

indices_validation_support_set, remaining_indices_train_set = (
    remaining_indices_train_set[:number_of_examples_support_sets],
    remaining_indices_train_set[number_of_examples_support_sets:],
)

indices_test_support_set, remaining_indices_train_set = (
    remaining_indices_train_set[:number_of_examples_support_sets],
    remaining_indices_train_set[number_of_examples_support_sets:],
)

indices_validation_query_set, remaining_indices_train_set = (
    remaining_indices_train_set[:number_of_examples_captioning_validation_query_sets],
    remaining_indices_train_set[number_of_examples_captioning_validation_query_sets:],
)

# print lengths
print(
    f"Lengths of the sets:\nvalidation query set: {len(indices_validation_query_set)}\nvalidation support set:"
    f" {len(indices_validation_support_set)}\ntest support set: {len(indices_test_support_set)}"
)

# Check that we have no overlap between the sets
print("Intersection between the sets:\n")
print(set(indices_validation_query_set).intersection(set(indices_validation_support_set)))
print(set(indices_validation_query_set).intersection(set(indices_test_support_set)))


validation_support_set = ds["train"].select(indices_validation_support_set)
test_support_set = ds["train"].select(indices_test_support_set)
server_check_support_set = ds["train"].select(indices_validation_support_set)

validation_query_set = ds["train"].select(indices_validation_query_set)
test_query_set = ds["test"]
server_check_query_set = ds["validation"]

ds_to_push = DatasetDict(
    {
        "validation_support_set": validation_support_set,
        "validation_query_set": validation_query_set,
        "test_support_set": test_support_set,
        "test_query_set": test_query_set,
        "server_check_support_set": server_check_support_set,
        "server_check_query_set": server_check_query_set,
    }
)

ds_to_push.push_to_hub(repo_id, private=True)

# Load the newly created dataset from hub
ds_final = load_dataset(repo_id, use_auth_token=True)

# Print the final composition of the dataset
print(f"Composition of the final dataset: {ds_final}")
