code: def generate_user_based_dataset(
      N,
      generation_fold,
      safety_dataset,
      user_ratios,
      random_int_for_sample,
  ):
      total_number_of_samples = DATA_SIZE[generation_fold]
      # Take the first 800 samples from safety set to make it balanced between good and bad.
      standard_safe = safety_dataset["standard"][generation_fold]["1"][
          "good"
      ][:800]
      standard_unsafe = safety_dataset["standard"][generation_fold][
          "1"
      ]["bad"][:800]
      adversarial_safe = safety_dataset["adversarial"][generation_fold][
          "1"
      ]["good"][:800]
      adversarial_unsafe = safety_dataset["adversarial"][
          generation_fold
      ]["1"]["bad"][:800]
      standard_dataset_for_sample = random.sample(
          standard_safe + standard_unsafe,
          len(standard_safe + standard_unsafe),
      )
      adversarial_dataset_for_sample = random.sample(
          adversarial_safe + adversarial_unsafe,
          len(adversarial_safe + adversarial_unsafe),
      )
      user_id = 0
  
      # for helpers
      helper_utterance_num = int(
          total_number_of_samples * user_ratios["helper"]
      )
      helper_samples, user_id = sample_from_single_dataset(
          standard_dataset_for_sample,
          "standard",
          helper_utterance_num,
          user_id,
          random_int_for_sample,
      )
      helper_samples = set_user_type(helper_samples, "helper")
  
      # for master helper
      master_helper_utterance_num = int(
          total_number_of_samples * user_ratios["master_helper"]
      )
      master_helper_samples, user_id = sample_from_single_dataset(
          adversarial_dataset_for_sample,
          "adversarial",
          master_helper_utterance_num,
          user_id,
          random_int_for_sample,
      )
      master_helper_samples = set_user_type(
          master_helper_samples, "master_helper"
      )
  
      # for trolls
      troll_utterance_num = int(
          total_number_of_samples * user_ratios["troll"]
      )
      troll_samples, user_id = sample_from_single_dataset(
          standard_dataset_for_sample,
          "standard",
          troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      # flip samples for trolls
      troll_samples = flip_labels(troll_samples, N)
      troll_samples = set_user_type(troll_samples, "troll")
  
      # sample master trolls
      master_troll_utterance_num = int(
          total_number_of_samples * user_ratios["master_troll"]
      )
      master_troll_samples, user_id = sample_from_single_dataset(
          adversarial_dataset_for_sample,
          "adversarial",
          master_troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      # flip samples for master trolls
      master_troll_samples = flip_labels(master_troll_samples, N)
      master_troll_samples = set_user_type(
          master_troll_samples, "master_troll"
      )
  
      # sample safe trolls
      safe_troll_utterance_num = int(
          total_number_of_samples * user_ratios["safe_troll"]
      )
      safe_troll_samples, user_id = sample_from_both_datasets(
          standard_dataset_for_sample,
          adversarial_dataset_for_sample,
          safe_troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      safe_troll_samples = set_labels(safe_troll_samples, "__ok__")
      safe_troll_samples = set_user_type(
          safe_troll_samples, "safe_troll"
      )
  
      # sample unsafe trolls
      unsafe_troll_utterance_num = int(
          total_number_of_samples * user_ratios["unsafe_troll"]
      )
      unsafe_troll_samples, user_id = sample_from_both_datasets(
          standard_dataset_for_sample,
          adversarial_dataset_for_sample,
          unsafe_troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      unsafe_troll_samples = set_labels(
          unsafe_troll_samples, "__notok__"
      )
      unsafe_troll_samples = set_user_type(
          unsafe_troll_samples, "unsafe_troll"
      )
  
      # happy helper
      happy_helper_utterance_num = int(
          total_number_of_samples * user_ratios["happy_helper"]
      )
      happy_helper_samples, user_id = sample_from_single_dataset(
          random.sample(standard_safe, len(standard_safe)),
          "standard",
          happy_helper_utterance_num,
          user_id,
          random_int_for_sample,
      )
      happy_helper_samples = set_user_type(
          happy_helper_samples, "happy_helper"
      )
  
      # gaslight trolls
      gaslight_troll_utterance_num = int(
          total_number_of_samples * user_ratios["gaslight_troll"]
      )
      gaslight_troll_samples, user_id = sample_from_single_dataset(
          random.sample(adversarial_unsafe, len(adversarial_unsafe)),
          "adversarial",
          gaslight_troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      gaslight_troll_samples = set_labels(
          gaslight_troll_samples, "__ok__"
      )
      gaslight_troll_samples = set_user_type(
          gaslight_troll_samples, "gaslight_troll"
      )
  
      # lazy trolls
      lazy_troll_utterance_num = int(
          total_number_of_samples * user_ratios["lazy_troll"]
      )
      lazy_troll_samples, user_id = sample_from_both_datasets(
          standard_dataset_for_sample,
          adversarial_dataset_for_sample,
          lazy_troll_utterance_num,
          user_id,
          random_int_for_sample,
      )
      lazy_troll_samples = set_random_labels(lazy_troll_samples, N)
      lazy_troll_samples = set_user_type(
          lazy_troll_samples, "lazy_troll"
      )
  
      dataset = (
          helper_samples
          + master_helper_samples
          + troll_samples
          + master_troll_samples
          + safe_troll_samples
          + unsafe_troll_samples
          + happy_helper_samples
          + gaslight_troll_samples
          + lazy_troll_samples
      )
      return dataset
  
file_path: ParlAI/parlai/tasks/safety_mix/build.py
imports: import copy
  import hashlib
  import json
  import os
  import random
  import numpy
  import parlai.core.build_data
  import parlai.utils.logging
  from parlai.core.build_data import DownloadableFile
label_desc: Use image node_id 8 for calling a global function with example usage: generate_user_based_dataset(N, generation_fold, safety_dataset, user_ratios, random_int_for_sample) and returns: dataset