docstr: Initialize an Argilla Trainer.
  
  Args:
      dataset: the dataset to be used for training.
      task: the training data to be used for training.
      framework: the framework to use for training. Currently, "transformers", "setfit", "spacy", "peft",
          "openai", "span_marker", "trl" and "sentence-transformers" are supported.
      lang: the spaCy language model to use for training, just required when `framework="spacy"`.
          Defaults to None, but it will be set to `spacy.blank("en")` if not specified.
      model: name or path to the baseline model to be used, or a transformers PreTrainedModel instance
          if the framework is "transformers", "peft" or "trl". If not specified will set to a good default
          per framework, if applicable. Defaults to None.
      tokenizer: a transformers PreTrainedTokenizer instance to tokenize the text. Only used with the
          "transformers", "peft" or "trl" frameworks.
      train_size: the size of the training set. If not specified, the entire dataset will be used for training,
          which may be an issue if `framework="spacy"` as it requires a validation set. Defaults to None.
      seed: the random seed to ensure reproducibility. Defaults to None.
      gpu_id: the GPU ID to use when training a SpaCy model. Defaults to -1, which means that the CPU
          will be used by default. GPU IDs start in 0, which stands for the default GPU in the system,
          if available.
      filter_by: A dict with key the field to filter by, and values the filters to apply. Currently only
          defined for `response_status` filters. Can be one of: draft, pending, submitted, and discarded.
          Defaults to `None` (no filter is applied).
      sort_by: A list of `SortBy` objects to sort your dataset by.
          Defaults to `None` (no filter is applied).
      max_records: the maximum number of records to use for training. Defaults to None.
      framework_kwargs: arguments for the framework's trainer. A special key (model_card_kwargs) is reserved
          for the arguments that can be passed to the model card.
      **load_kwargs: arguments for the rg.load() function.
docstr_tok: ["Initialize","an","Argilla","Trainer",".","Args",":","dataset",":","the","dataset","to","be","used","for","training",".","task",":","the","training","data","to","be","used","for","training",".","framework",":","the","framework","to","use","for","training",".","Currently",",","``","transformers","''",",","``","setfit","''",",","``","spacy","''",",","``","peft","''",",","``","openai","''",",","``","span_marker","''",",","``","trl","''","and","``","sentence-transformers","''","are","supported",".","lang",":","the","spaCy","language","model","to","use","for","training",",","just","required","when","`","framework=","''","spacy","''","`",".","Defaults","to","None",",","but","it","will","be","set","to","`","spacy.blank","(","``","en","''",")","`","if","not","specified",".","model",":","name","or","path","to","the","baseline","model","to","be","used",",","or","a","transformers","PreTrainedModel","instance","if","the","framework","is","``","transformers","''",",","``","peft","''","or","``","trl","''",".","If","not","specified","will","set","to","a","good","default","per","framework",",","if","applicable",".","Defaults","to","None",".","tokenizer",":","a","transformers","PreTrainedTokenizer","instance","to","tokenize","the","text",".","Only","used","with","the","``","transformers","''",",","``","peft","''","or","``","trl","''","frameworks",".","train_size",":","the","size","of","the","training","set",".","If","not","specified",",","the","entire","dataset","will","be","used","for","training",",","which","may","be","an","issue","if","`","framework=","''","spacy","''","`","as","it","requires","a","validation","set",".","Defaults","to","None",".","seed",":","the","random","seed","to","ensure","reproducibility",".","Defaults","to","None",".","gpu_id",":","the","GPU","ID","to","use","when","training","a","SpaCy","model",".","Defaults","to","-1",",","which","means","that","the","CPU","will","be","used","by","default",".","GPU","IDs","start","in","0",",","which","stands","for","the","default","GPU","in","the","system",",","if","available",".","filter_by",":","A","dict","with","key","the","field","to","filter","by",",","and","values","the","filters","to","apply",".","Currently","only","defined","for","`","response_status","`","filters",".","Can","be","one","of",":","draft",",","pending",",","submitted",",","and","discarded",".","Defaults","to","`","None","`","(","no","filter","is","applied",")",".","sort_by",":","A","list","of","`","SortBy","`","objects","to","sort","your","dataset","by",".","Defaults","to","`","None","`","(","no","filter","is","applied",")",".","max_records",":","the","maximum","number","of","records","to","use","for","training",".","Defaults","to","None",".","framework_kwargs",":","arguments","for","the","framework","'s","trainer",".","A","special","key","(","model_card_kwargs",")","is","reserved","for","the","arguments","that","can","be","passed","to","the","model","card",".","*","*","load_kwargs",":","arguments","for","the","rg.load","(",")","function","."]
code: def __init__(
      self,
      dataset: ["FeedbackDataset", "RemoteFeedbackDataset"],
      task: TrainingTaskTypes,
      framework: Framework,
      lang: Optional["spacy.Language"] = None,
      model: Optional[Union[str, "PreTrainedModel"]] = None,
      tokenizer: Optional["PreTrainedTokenizer"] = None,
      train_size: Optional[float] = None,
      seed: Optional[int] = None,
      gpu_id: Optional[int] = -1,
      filter_by: Optional[
          Dict[
              str,
              Union[
                  "ResponseStatusFilter", List["ResponseStatusFilter"]
              ],
          ]
      ] = None,
      sort_by: Optional[List["SortBy"]] = None,
      max_records: Optional[int] = None,
      framework_kwargs: Optional[dict] = {},
  ) -> None:
      """
      Initialize an Argilla Trainer.
  
      Args:
          dataset: the dataset to be used for training.
          task: the training data to be used for training.
          framework: the framework to use for training. Currently, "transformers", "setfit", "spacy", "peft",
              "openai", "span_marker", "trl" and "sentence-transformers" are supported.
          lang: the spaCy language model to use for training, just required when `framework="spacy"`.
              Defaults to None, but it will be set to `spacy.blank("en")` if not specified.
          model: name or path to the baseline model to be used, or a transformers PreTrainedModel instance
              if the framework is "transformers", "peft" or "trl". If not specified will set to a good default
              per framework, if applicable. Defaults to None.
          tokenizer: a transformers PreTrainedTokenizer instance to tokenize the text. Only used with the
              "transformers", "peft" or "trl" frameworks.
          train_size: the size of the training set. If not specified, the entire dataset will be used for training,
              which may be an issue if `framework="spacy"` as it requires a validation set. Defaults to None.
          seed: the random seed to ensure reproducibility. Defaults to None.
          gpu_id: the GPU ID to use when training a SpaCy model. Defaults to -1, which means that the CPU
              will be used by default. GPU IDs start in 0, which stands for the default GPU in the system,
              if available.
          filter_by: A dict with key the field to filter by, and values the filters to apply. Currently only
              defined for `response_status` filters. Can be one of: draft, pending, submitted, and discarded.
              Defaults to `None` (no filter is applied).
          sort_by: A list of `SortBy` objects to sort your dataset by.
              Defaults to `None` (no filter is applied).
          max_records: the maximum number of records to use for training. Defaults to None.
          framework_kwargs: arguments for the framework's trainer. A special key (model_card_kwargs) is reserved
              for the arguments that can be passed to the model card.
          **load_kwargs: arguments for the rg.load() function.
      """
      if filter_by:
          dataset = dataset.filter_by(**filter_by)
      if sort_by:
          dataset = dataset.sort_by(sort_by)
      if max_records:
          dataset = dataset.pull(max_records=max_records)
  
      self._dataset = dataset
      self._train_size = train_size
      self._task = task
      self._seed = seed  # split is used for train-test-split and should therefore be fixed
      self._model = model
      self._tokenizer = tokenizer
  
      self._prepared_data = self._dataset.prepare_for_training(
          framework=framework,
          task=task,
          train_size=train_size,
          seed=seed,
          lang=lang,
      )
  
      if isinstance(framework, str):
          framework = Framework(framework)
  
      if tokenizer and framework not in (
          Framework.TRANSFORMERS,
          Framework.PEFT,
          Framework.TRL,
      ):
          warnings.warn(
              f"Passing a tokenizer is not supported for the {framework} framework.",
              UserWarning,
              stacklevel=2,
          )
  
      # Save the model_card arguments if given by the user
      if model_card_kwargs := framework_kwargs.pop(
          "model_card_kwargs", None
      ):
          self.model_card_kwargs = model_card_kwargs
      else:
          self.model_card_kwargs = {}
  
      if framework is Framework.SETFIT:
          if not isinstance(task, TrainingTaskForTextClassification):
              raise NotImplementedError(
                  f"{Framework.SETFIT} only supports `TextClassification` tasks."
              )
          from argilla.client.feedback.training.frameworks.setfit import (
              ArgillaSetFitTrainer,
          )
  
          self._trainer = ArgillaSetFitTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
          )
      elif framework is Framework.TRANSFORMERS:
          from argilla.client.feedback.training.frameworks.transformers import (
              ArgillaTransformersTrainer,
          )
  
          self._trainer = ArgillaTransformersTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              tokenizer=self._tokenizer,
          )
      elif framework is Framework.PEFT:
          from argilla.client.feedback.training.frameworks.peft import (
              ArgillaPeftTrainer,
          )
  
          self._trainer = ArgillaPeftTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              tokenizer=self._tokenizer,
          )
      elif framework is Framework.SPACY:
          from argilla.client.feedback.training.frameworks.spacy import (
              ArgillaSpaCyTrainer,
          )
  
          self._trainer = ArgillaSpaCyTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              gpu_id=gpu_id,
              framework_kwargs=framework_kwargs,  # freeze_tok2vec
          )
      elif framework is Framework.SPACY_TRANSFORMERS:
          from argilla.client.feedback.training.frameworks.spacy import (
              ArgillaSpaCyTransformersTrainer,
          )
  
          self._trainer = ArgillaSpaCyTransformersTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              gpu_id=gpu_id,
              framework_kwargs=framework_kwargs,  # update_transformer
          )
      elif framework is Framework.OPENAI:
          from argilla.client.feedback.training.frameworks.openai import (
              ArgillaOpenAITrainer,
          )
  
          self._trainer = ArgillaOpenAITrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
          )
      elif framework is Framework.SPAN_MARKER:
          from argilla.client.feedback.training.frameworks.span_marker import (
              ArgillaSpanMarkerTrainer,
          )
  
          self._trainer = ArgillaSpanMarkerTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
          )
      elif framework is Framework.TRL:
          from argilla.client.feedback.training.frameworks.trl import (
              ArgillaTRLTrainer,
          )
  
          self._trainer = ArgillaTRLTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              tokenizer=self._tokenizer,
          )
      elif framework is Framework.SENTENCE_TRANSFORMERS:
          from argilla.client.feedback.training.frameworks.sentence_transformers import (
              ArgillaSentenceTransformersTrainer,
          )
  
          self._trainer = ArgillaSentenceTransformersTrainer(
              dataset=self._dataset,
              task=self._task,
              prepared_data=self._prepared_data,
              seed=self._seed,
              model=self._model,
              **framework_kwargs,  # cross_encoder
          )
      else:
          raise NotImplementedError(
              f"{framework} is not a valid framework."
          )
  
      self._logger.info(self)
      self._track_trainer_usage(
          framework=framework, task=self._task.__class__.__name__
      )
  
imports: import os
  import textwrap
  import warnings
  from pathlib import Path
  from typing import TYPE_CHECKING, Dict, List, Optional, Union
  from argilla.client.feedback.schemas.records import FeedbackRecord
  from argilla.client.feedback.training.schemas.base import TrainingTaskForTextClassification, TrainingTaskTypes
  from argilla.client.models import Framework, TextClassificationRecord
  from argilla.training.base import ArgillaTrainer
  from argilla.training.base import ArgillaTrainerSkeleton
label_desc: Use image node_id 1 to create a new ArgillaTrainer object from inherited base classes: ArgillaTrainerV1 with example: obj = ArgillaTrainer(dataset, task, framework, lang, model, tokenizer, train_size, seed, gpu_id, filter_by, sort_by, max_records, framework_kwargs)