QuestionAnsweringPipeline

stion argument).   topk (int, optional, defaults to 1) —
The number of answers to return (will be chosen by order of likelihood). Note that we return less than
topk answers if there are not enough options available within the context.   doc_stride (int, optional, defaults to 128) —
If the context is too long to fit with the question for the model, it will be split in several chunks
with some overlap. This argument controls the size of that overlap.   max_answer_len (int, optional, defaults to 15) —
The maximum length of predicted answers (e.g., only answers with a shorter length are considered).   max_seq_len (int, optional, defaults to 384) —
The maximum length of the total sentence (context + question) in tokens of each chunk passed to the
model. The context will be split in several chunks (using doc_stride as overlap) if needed.   max_question_len (int, optional, defaults to 64) —
The maximum length of the question after tokeniza


QuestionAnsweringPipeline

tion. It will be truncated if needed.   handle_impossible_answer (bool, optional, defaults to False) —
Whether or not we accept impossible as an answer.   align_to_words (bool, optional, defaults to True) —
Attempts to align the answer to real words. Improves quality on space separated langages. Might hurt on
non-space-separated languages (like Japanese or Chinese)   Returns
A dict or a list of dict

Each result comes as a dictionary with the following keys:
score (float) — The probability associated to the answer.
start (int) — The character start index of the answer (in the tokenized version of the input).
end (int) — The character end index of the answer (in the tokenized version of the input).
answer (str) — The answer to the question.
  Answer the question(s) given as inputs by using the context(s).  create_sample  < source > ( question: typing.Union[str, typing.List[str]] context: typing.Union[str, typing.List[str]]  ) → On


QuestionAnsweringPipeline

e or a list of SquadExample  Parameters   question (str or List[str]) — The question(s) asked.   context (str or List[str]) — The context(s) in which we will look for the answer.   Returns
One or a list of SquadExample

The corresponding SquadExample grouping question and context.
  QuestionAnsweringPipeline leverages the SquadExample internally. This helper method encapsulate all the
logic for converting question(s) and context(s) to SquadExample. We currently support extractive question answering.  span_to_answer  < source > ( text: str start: int end: int  ) → Dictionary like `{‘answer’  Parameters   text (str) — The actual context to extract the answer from.   start (int) — The answer starting token index.   end (int) — The answer end token index.   Returns
Dictionary like `{‘answer’

str, ‘start’: int, ‘end’: int}`
  When decoding from token probabilities, this method maps token indexes to actual word in the initial conte


SummarizationPipeline

class transformers.SummarizationPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task


SummarizationPipeline

-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    S


SummarizationPipeline

ummarize news articles and other documents. This summarizing pipeline can currently be loaded from pipeline() using the following task identifier:
"summarization". The models that this pipeline can use are models that have been fine-tuned on a summarization task, which is
currently, ’bart-large-cnn’, ’t5-small’, ’t5-base’, ’t5-large’, ’t5-3b’, ’t5-11b’. See the up-to-date
list of available models on huggingface.co/models. For a list
of available parameters, see the following
documentation  Usage:   Copied # use bart in pytorch
summarizer = pipeline("summarization")
summarizer("An apple a day, keeps the doctor away", min_length=5, max_length=20)
# use t5 in tf
summarizer = pipeline("summarization", model="t5-base", tokenizer="t5-base", framework="tf")
summarizer("An apple a day, keeps the doctor away", min_length=5, max_length=20)  __call__  < source > ( *args **kwargs  ) → A list or a list of list of dict  Parameters   documents (str or List[str]) —
One


SummarizationPipeline

 or several articles (or one list of articles) to summarize.   return_text (bool, optional, defaults to True) —
Whether or not to include the decoded texts in the outputs   return_tensors (bool, optional, defaults to False) —
Whether or not to include the tensors of predictions (as token indices) in the outputs.   clean_up_tokenization_spaces (bool, optional, defaults to False) —
Whether or not to clean up the potential extra spaces in the text output.
generate_kwargs —
Additional keyword arguments to pass along to the generate method of the model (see the generate method
corresponding to your framework here).   Returns
A list or a list of list of dict

Each result comes as a dictionary with the following keys:
summary_text (str, present when return_text=True) — The summary of the corresponding input.
summary_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The token
ids of the summary.
  Summarize the text(s) given as inputs


TableQuestionAnsweringPipeline

class transformers.TableQuestionAnsweringPipeline  < source > ( args_parser = <transformers.pipelines.table_question_answering.TableQuestionAnsweringArgumentHandler object at 0x7fefb17fd430> *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the on


TableQuestionAnsweringPipeline

e currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU


TableQuestionAnsweringPipeline

, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    Table Question Answering pipeline using a ModelForTableQuestionAnswering. This pipeline is only available in
PyTorch.  Example:   Copied >>> from transformers import pipeline
>>> oracle = pipeline(model="google/tapas-base-finetuned-wtq")
>>> table = {
...     "Repository": ["Transformers", "Datasets", "Tokenizers"],
...     "Stars": ["36542", "4512", "3934"],
...     "Contributors": ["651", "77", "34"],
...     "Programming language": ["Python", "Python", "Rust, Python and NodeJS"],
... }
>>> oracle(query="How many stars does the transformers repository have?", table=table)
{'answer': 'AVERAGE > 36542', 'coordin


TableQuestionAnsweringPipeline

ates': [(0, 1)], 'cells': ['36542'], 'aggregator': 'AVERAGE'} Learn more about the basics of using a pipeline in the pipeline tutorial This tabular question answering pipeline can currently be loaded from pipeline() using the following task
identifier: "table-question-answering". The models that this pipeline can use are models that have been fine-tuned on a tabular question answering task.
See the up-to-date list of available models on
huggingface.co/models.  __call__  < source > ( *args **kwargs  ) → A dictionary or a list of dictionaries containing results  Parameters   table (pd.DataFrame or Dict) —
Pandas DataFrame or dictionary that will be converted to a DataFrame containing all the table values.
See above for an example of dictionary.   query (str or List[str]) —
Query or list of queries that will be sent to the model alongside the table.   sequential (bool, optional, default


TableQuestionAnsweringPipeline

s to False) —
Whether to do inference sequentially or as a batch. Batching is faster, but models like SQA require the
inference to be done sequentially to extract relations within sequences, given their conversational
nature.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TapasTruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following value


TableQuestionAnsweringPipeline

s:
True or 'drop_rows_to_fit': Truncate to a maximum length specified with the argument max_length
or to the maximum acceptable input length for the model if that argument is not provided. This will
truncate row by row, removing rows from the table.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   Returns
A dictionary or a list of dictionaries containing results

Each result is a dictionary with the following
keys:
answer (str) — The answer of the query given the table. If there is an aggregator, the answer will
be preceded by AGGREGATOR >.
coordinates (List[Tuple[int, int]]) — Coordinates of the cells of the answers.
cells (List[str]) — List of strings made up of the answer cell values.
aggregator (str) — If the model has an aggregator, this returns the aggregator.
  Answers


TableQuestionAnsweringPipeline

 queries according to a table. The pipeline accepts several types of inputs which are detailed below: pipeline(table, query) pipeline(table, [query]) pipeline(table=table, query=query) pipeline(table=table, query=[query]) pipeline({"table": table, "query": query}) pipeline({"table": table, "query": [query]}) pipeline([{"table": table, "query": query}, {"table": table, "query": query}]) The table argument should be a dict or a DataFrame built from that dict, containing the whole table:  Example:   Copied data = {
    "actors": ["brad pitt", "leonardo di caprio", "george clooney"],
    "age": ["56", "45", "59"],
    "number of movies": ["87", "53", "69"],
    "date of birth": ["7 february 1967", "10 june 1996", "28 november 1967"],
} This dictionary can be passed in as such, or can be converted to a pandas DataFrame:  Example:   Copied import pandas as pd
table = pd.DataFrame.from_dict


TextClassificationPipeline

class transformers.TextClassificationPipeline  < source > ( **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.


TextClassificationPipeline

num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.   return_all_scores (bool, optional, defaults to False) —
Whether t


TextClassificationPipeline

o return all prediction scores or just the one of the predicted class.   function_to_apply (str, optional, defaults to "default") —
The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:
"default": if the model has a single label, will apply the sigmoid function on the output. If the model
has several labels, will apply the softmax function on the output.
"sigmoid": Applies the sigmoid function on the output.
"softmax": Applies the softmax function on the output.
"none": Does not apply any function on the output.
    Text classification pipeline using any ModelForSequenceClassification. See the sequence classification
examples for more information.  Example:   Copied >>> from transformers import pipeline
>>> classifier = pipeline(model="distilbert-base-uncased-finetuned-sst-2-english")
>>> classifier("This movie is disgustingly good !")
[{'label': 'POSITIVE', 'score': 1.0}]
>>> classifier("Director tried too much.")
[{'label': 'NEGA


TextClassificationPipeline

TIVE', 'score': 0.996}] Learn more about the basics of using a pipeline in the pipeline tutorial This text classification pipeline can currently be loaded from pipeline() using the following task identifier:
"sentiment-analysis" (for classifying sequences according to positive or negative sentiments). If multiple classification labels are available (model.config.num_labels >= 2), the pipeline will run a softmax
over the results. If there is a single label, the pipeline will run a sigmoid over the result. The models that this pipeline can use are models that have been fine-tuned on a sequence classification task. See
the up-to-date list of available models on
huggingface.co/models.  __call__  < source > ( *args **kwargs  ) → A list or a list of list of dict  Parameters   args (str or List[str] or Dict[str], or List[Dict[str]]) —
One or several texts to classify. In order to use text pairs for your classification, you can send a
dictionary containing {"text", "text_pair"} keys, or a li


TextClassificationPipeline

st of those.   top_k (int, optional, defaults to 1) —
How many results to return.   function_to_apply (str, optional, defaults to "default") —
The function to apply to the model outputs in order to retrieve the scores. Accepts four different
values:
If this argument is not specified, then it will apply the following functions according to the number
of labels:
If the model has a single label, will apply the sigmoid function on the output.
If the model has several labels, will apply the softmax function on the output.
Possible values are:
"sigmoid": Applies the sigmoid function on the output.
"softmax": Applies the softmax function on the output.
"none": Does not apply any function on the output.
   Returns
A list or a list of list of dict

Each result comes as list of dictionaries with the following keys:
label (str) — The label predicted.
score (float) — The corresponding probability.
If top_k is used, one such dictionary is returned per label.
  Classify the text(s) given as inp


TextGenerationPipeline

class transformers.TextGenerationPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model,


TextGenerationPipeline

 or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_out


TextGenerationPipeline

put (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    Language generation pipeline using any ModelWithLMHead. This pipeline predicts the words that will follow a
specified text prompt.  Example:   Copied >>> from transformers import pipeline
>>> generator = pipeline(model="gpt2")
>>> generator("I can't believe you did such a ", do_sample=False)
[{'generated_text': "I can't believe you did such a icky thing to me. I'm so sorry. I'm so sorry. I'm so sorry. I'm so sorry. I'm so sorry. I'm so sorry. I'm so sorry. I"}]
>>> # These parameters will return suggestions, and only the newly created text making it easier for prompting suggestions.
>>> outputs = generator("My tart needs some", num_return_sequences=4, return_full_text=False) Learn more about the basics of using a pipeline in the


TextGenerationPipeline

pipeline tutorial. You can pass text
generation parameters to this pipeline to control stopping criteria, decoding strategy, and more. Learn more about
text generation parameters in Text generation strategies and Text
generation. This language generation pipeline can currently be loaded from pipeline() using the following task identifier:
"text-generation". The models that this pipeline can use are models that have been trained with an autoregressive language modeling
objective, which includes the uni-directional models in the library (e.g. gpt2). See the list of available models
on huggingface.co/models.  __call__  < source > ( text_inputs **kwargs  ) → A list or a list of list of dict  Parameters   args (str or List[str]) —
One or several prompts (or one list of prompts) to complete.   return_tensors (bool, optional, defaults to False) —
Whether or not to return the tensors of


TextGenerationPipeline

 predictions (as token indices) in the outputs. If set to
True, the decoded text is not returned.   return_text (bool, optional, defaults to True) —
Whether or not to return the decoded texts in the outputs.   return_full_text (bool, optional, defaults to True) —
If set to False only added text is returned, otherwise the full text is returned. Only meaningful if
return_text is set to True.   clean_up_tokenization_spaces (bool, optional, defaults to False) —
Whether or not to clean up the potential extra spaces in the text output.   prefix (str, optional) —
Prefix added to prompt.   handle_long_generation (str, optional) —
By default, this pipelines does not handle long generation (ones that exceed in one form or the other
the model maximum length). There is no perfect way to adress this (more info
:https://github.com/huggingface/transformers/issues/14033#issuecomment-948385227).


TextGenerationPipeline

 This provides common
strategies to work around that problem depending on your use case.
None : default strategy where nothing in particular happens
"hole": Truncates left of input, and leaves a gap wide enough to let generation happen (might
truncate a lot of the prompt and not suitable when generation exceed the model capacity)
generate_kwargs —
Additional keyword arguments to pass along to the generate method of the model (see the generate method
corresponding to your framework here).   Returns
A list or a list of list of dict

Returns one of the following dictionaries (cannot return a combination
of both generated_text and generated_token_ids):
generated_text (str, present when return_text=True) — The generated text.
generated_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The token
ids of the generated text.
  Complete the prompt(s) given as in


class transformers.Text2TextGenerationPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defau


lts to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.


e., pickle) or as raw text.    Pipeline for text to text generation using seq2seq models.  Example:   Copied >>> from transformers import pipeline
>>> generator = pipeline(model="mrm8488/t5-base-finetuned-question-generation-ap")
>>> generator(
...     "answer: Manuel context: Manuel has created RuPERTa-base with the support of HF-Transformers and Google"
... )
[{'generated_text': 'question: Who created the RuPERTa-base?'}] Learn more about the basics of using a pipeline in the pipeline tutorial. You can pass text
generation parameters to this pipeline to control stopping criteria, decoding strategy, and more. Learn more about
text generation parameters in Text generation strategies and Text
generation. This Text2TextGenerationPipeline pipeline can currently be loaded from pipeline() using the following task
identifier: "text2text-generation". The models that this pipeline can use are models that have been fine-tuned on a translation task.


See the
up-to-date list of available models on
huggingface.co/models. For a list of available
parameters, see the following
documentation  Usage:   Copied text2text_generator = pipeline("text2text-generation")
text2text_generator("question: What is 42 ? context: 42 is the answer to life, the universe and everything")  __call__  < source > ( *args **kwargs  ) → A list or a list of list of dict  Parameters   args (str or List[str]) —
Input text for the encoder.   return_tensors (bool, optional, defaults to False) —
Whether or not to include the tensors of predictions (as token indices) in the outputs.   return_text (bool, optional, defaults to True) —
Whether or not to include the decoded texts in the outputs.   clean_up_tokenization_spaces (bool, optional, defaults to False) —
Whether or not to clean up the potential extra spaces in the text output.   truncation (TruncationStrategy, optional, defaults to TruncationStrategy.DO_NOT_TRUNCATE) —


The truncation strategy for the tokenization within the pipeline. TruncationStrategy.DO_NOT_TRUNCATE
(default) will never truncate, but it is sometimes desirable to truncate the input to fit the model’s
max_length instead of throwing an error down the line.
generate_kwargs —
Additional keyword arguments to pass along to the generate method of the model (see the generate method
corresponding to your framework here).   Returns
A list or a list of list of dict

Each result comes as a dictionary with the following keys:
generated_text (str, present when return_text=True) — The generated text.
generated_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The token
ids of the generated text.
  Generate the output text(s) using text(s) given as inputs.  check_inputs  < source > ( input_length: int min_length: int max_length: int  )    Checks whether there might be something wrong with given input with regard to the model.


TokenClassificationPipeline

class transformers.TokenClassificationPipeline  < source > ( args_parser = <transformers.pipelines.token_classification.TokenClassificationArgumentHandler object at 0x7fefb1726880> *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to


TokenClassificationPipeline

 the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the outp


TokenClassificationPipeline

ut the pipeline should happen in a binary format (i.e., pickle) or as raw text.   ignore_labels (List[str], defaults to ["O"]) —
A list of labels to ignore.   grouped_entities (bool, optional, defaults to False) —
DEPRECATED, use aggregation_strategy instead. Whether or not to group the tokens corresponding to the
same entity together in the predictions or not.   stride (int, optional) —
If stride is provided, the pipeline is applied on all the text. The text is split into chunks of size
model_max_length. Works only with fast tokenizers and aggregation_strategy different from NONE. The
value of this argument defines the number of overlapping tokens between chunks. In other words, the model
will shift forward by tokenizer.model_max_length - stride tokens each step.   aggregation_strategy (str, optional, defaults to "none") —
The strategy to fuse (or not) tokens based on the model prediction.
“none” : Will simply not do any aggregation and simply return raw results from the m


TokenClassificationPipeline

odel
“simple” : Will attempt to group entities following the default schema. (A, B-TAG), (B, I-TAG), (C,
I-TAG), (D, B-TAG2) (E, B-TAG2) will end up being [{“word”: ABC, “entity”: “TAG”}, {“word”: “D”,
“entity”: “TAG2”}, {“word”: “E”, “entity”: “TAG2”}] Notice that two consecutive B tags will end up as
different entities. On word based languages, we might end up splitting words undesirably : Imagine
Microsoft being tagged as [{“word”: “Micro”, “entity”: “ENTERPRISE”}, {“word”: “soft”, “entity”:
“NAME”}]. Look for FIRST, MAX, AVERAGE for ways to mitigate that and disambiguate words (on languages
that support that meaning, which is basically tokens separated by a space). These mitigations will
only work on real words, “New york” might still be tagged with two different entities.
“first” : (works only on word based models) Will use the SIMPLE strategy except that words, cannot
end up with different tags. Words will simply use the tag of the first token of the word when there
i


TokenClassificationPipeline

s ambiguity.
“average” : (works only on word based models) Will use the SIMPLE strategy except that words,
cannot end up with different tags. scores will be averaged first across tokens, and then the maximum
label is applied.
“max” : (works only on word based models) Will use the SIMPLE strategy except that words, cannot
end up with different tags. Word entity will simply be the token with the maximum score.
    Named Entity Recognition pipeline using any ModelForTokenClassification. See the named entity recognition
examples for more information.  Example:   Copied >>> from transformers import pipeline
>>> token_classifier = pipeline(model="Jean-Baptiste/camembert-ner", aggregation_strategy="simple")
>>> sentence = "Je m'appelle jean-baptiste et je vis à montréal"
>>> tokens = token_classifier(sentence)
>>> tokens
[{'entity_group': 'PER', 'score': 0.9931, 'word': 'jean-baptiste', 'start': 12, 'end': 26}, {'entity_group': 'LOC', 'score': 0.998, 'word': 'montréal', 'start': 3


TokenClassificationPipeline

8, 'end': 47}]
>>> token = tokens[0]
>>> # Start and end provide an easy way to highlight words in the original text.
>>> sentence[token["start"] : token["end"]]
' jean-baptiste'
>>> # Some models use the same idea to do part of speech.
>>> syntaxer = pipeline(model="vblagoje/bert-english-uncased-finetuned-pos", aggregation_strategy="simple")
>>> syntaxer("My name is Sarah and I live in London")
[{'entity_group': 'PRON', 'score': 0.999, 'word': 'my', 'start': 0, 'end': 2}, {'entity_group': 'NOUN', 'score': 0.997, 'word': 'name', 'start': 3, 'end': 7}, {'entity_group': 'AUX', 'score': 0.994, 'word': 'is', 'start': 8, 'end': 10}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'sarah', 'start': 11, 'end': 16}, {'entity_group': 'CCONJ', 'score': 0.999, 'word': 'and', 'start': 17, 'end': 20}, {'entity_group': 'PRON', 'score': 0.999, 'word': 'i', 'start': 21, 'end': 22}, {'entity_group': 'VERB', 'score': 0.998, 'word': 'live', 'start': 23, 'end': 27}, {'entity_group': 'ADP', '


TokenClassificationPipeline

score': 0.999, 'word': 'in', 'start': 28, 'end': 30}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'london', 'start': 31, 'end': 37}] Learn more about the basics of using a pipeline in the pipeline tutorial This token recognition pipeline can currently be loaded from pipeline() using the following task identifier:
"ner" (for predicting the classes of tokens in a sequence: person, organisation, location or miscellaneous). The models that this pipeline can use are models that have been fine-tuned on a token classification task. See the
up-to-date list of available models on
huggingface.co/models.  __call__  < source > ( inputs: typing.Union[str, typing.List[str]] **kwargs  ) → A list or a list of list of dict  Parameters   inputs (str or List[str]) —
One or several texts (or one list of texts) for token classification.   Returns
A list or a list of list of dict

Each result comes as a list of dictionaries (one for each token in the
corresponding input, or each entity


TokenClassificationPipeline

if this pipeline was instantiated with an aggregation_strategy) with
the following keys:
word (str) — The token/word classified. This is obtained by decoding the selected tokens. If you
want to have the exact string in the original sentence, use start and end.
score (float) — The corresponding probability for entity.
entity (str) — The entity predicted for that token/word (it is named entity_group when
aggregation_strategy is not "none".
index (int, only present when aggregation_strategy="none") — The index of the corresponding
token in the sentence.
start (int, optional) — The index of the start of the corresponding entity in the sentence. Only
exists if the offsets are available within the tokenizer
end (int, optional) — The index of the end of the corresponding entity in the sentence. Only
exists if the offsets are available within the tokenizer
  Classify each token of the text(s) given as inputs.  aggregate_words  < source > ( entities: typing.List[dict] aggregation_st


TokenClassificationPipeline

rategy: AggregationStrategy  )    Override tokens from a given word that disagree to force agreement on word boundaries. Example: micro|soft| com|pany| B-ENT I-NAME I-ENT I-ENT will be rewritten with first strategy as microsoft|
company| B-ENT I-ENT  gather_pre_entities  < source > ( sentence: str input_ids: ndarray scores: ndarray offset_mapping: typing.Union[typing.List[typing.Tuple[int, int]], NoneType] special_tokens_mask: ndarray aggregation_strategy: AggregationStrategy  )    Fuse various numpy arrays into dicts with all the information needed for aggregation  group_entities  < source > ( entities: typing.List[dict]  )   Parameters   entities (dict) — The entities predicted by the pipeline.    Find and group together the adjacent tokens with the same entity predicted.  group_sub_entities  < source > ( entities: typing.List[dict]  )   Parameters   entities (dict) — The entities predicted by the pipeline.    Group together the adjacent tokens with the same entity predic


TranslationPipeline

class transformers.TranslationPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults t


TranslationPipeline

o "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e.


TranslationPipeline

, pickle) or as raw text.    Translates from one language to another. This translation pipeline can currently be loaded from pipeline() using the following task identifier:
"translation_xx_to_yy". The models that this pipeline can use are models that have been fine-tuned on a translation task. See the
up-to-date list of available models on huggingface.co/models.
For a list of available parameters, see the following
documentation  Usage:   Copied en_fr_translator = pipeline("translation_en_to_fr")
en_fr_translator("How old are you?")  __call__  < source > ( *args **kwargs  ) → A list or a list of list of dict  Parameters   args (str or List[str]) —
Texts to be translated.   return_tensors (bool, optional, defaults to False) —
Whether or not to include the tensors of predictions (as token indices) in the outputs.   return_text (bool, optional, defaults to True) —
Whether or not to include the decoded texts in the outputs.   clean_up_tokeni


TranslationPipeline

zation_spaces (bool, optional, defaults to False) —
Whether or not to clean up the potential extra spaces in the text output.   src_lang (str, optional) —
The language of the input. Might be required for multilingual models. Will not have any effect for
single pair translation models   tgt_lang (str, optional) —
The language of the desired output. Might be required for multilingual models. Will not have any effect
for single pair translation models
generate_kwargs —
Additional keyword arguments to pass along to the generate method of the model (see the generate method
corresponding to your framework here).   Returns
A list or a list of list of dict

Each result comes as a dictionary with the following keys:
translation_text (str, present when return_text=True) — The translation.
translation_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The
token ids of the translation.
  Translate the text(s) given as input


ZeroShotClassificationPipeline

class transformers.ZeroShotClassificationPipeline  < source > ( args_parser = <transformers.pipelines.zero_shot_classification.ZeroShotClassificationArgumentHandler object at 0x7fefb16f7430> *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
bo


ZeroShotClassificationPipeline

th frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   b


ZeroShotClassificationPipeline

inary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    NLI-based zero-shot classification pipeline using a ModelForSequenceClassification trained on NLI (natural
language inference) tasks. Equivalent of text-classification pipelines, but these models don’t require a
hardcoded number of potential classes, they can be chosen at runtime. It usually means it’s slower but it is
much more flexible. Any combination of sequences and labels can be passed and each combination will be posed as a premise/hypothesis
pair and passed to the pretrained model. Then, the logit for entailment is taken as the logit for the candidate
label being valid. Any NLI model can be used, but the id of the entailment label must be included in the model
config’s :attr:~transformers.PretrainedConfig.label2id.  Example:   Copied >>> from transformers import pipeline
>>>


ZeroShotClassificationPipeline

 oracle = pipeline(model="facebook/bart-large-mnli")
>>> oracle(
...     "I have a problem with my iphone that needs to be resolved asap!!",
...     candidate_labels=["urgent", "not urgent", "phone", "tablet", "computer"],
... )
{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'], 'scores': [0.504, 0.479, 0.013, 0.003, 0.002]}
>>> oracle(
...     "I have a problem with my iphone that needs to be resolved asap!!",
...     candidate_labels=["english", "german"],
... )
{'sequence': 'I have a problem with my iphone that needs to be resolved asap!!', 'labels': ['english', 'german'], 'scores': [0.814, 0.186]} Learn more about the basics of using a pipeline in the pipeline tutorial This NLI pipeline can currently be loaded from pipeline() using the following task identifier:
"zero-shot-classification". The models that this pipeline can use are models tha


ZeroShotClassificationPipeline

t have been fine-tuned on an NLI task. See the up-to-date list
of available models on huggingface.co/models.  __call__  < source > ( sequences: typing.Union[str, typing.List[str]] *args **kwargs  ) → A dict or a list of dict  Parameters   sequences (str or List[str]) —
The sequence(s) to classify, will be truncated if the model input is too large.   candidate_labels (str or List[str]) —
The set of possible class labels to classify each sequence into. Can be a single label, a string of
comma-separated labels, or a list of labels.   hypothesis_template (str, optional, defaults to "This example is {}.") —
The template used to turn each label into an NLI-style hypothesis. This template must include a {} or
similar syntax for the candidate label to be inserted into the template. For example, the default
template is "This example is {}." With the candidate label "sports", this would be fed into the
model like "<cls> sequence to classify <sep> Thi


ZeroShotClassificationPipeline

s example is sports . <sep>". The default template
works well in many cases, but it may be worthwhile to experiment with different templates depending on
the task setting.   multi_label (bool, optional, defaults to False) —
Whether or not multiple candidate labels can be true. If False, the scores are normalized such that
the sum of the label likelihoods for each sequence is 1. If True, the labels are considered
independent and probabilities are normalized for each candidate by doing a softmax of the entailment
score vs. the contradiction score.   Returns
A dict or a list of dict

Each result comes as a dictionary with the following keys:
sequence (str) — The sequence for which this is the output.
labels (List[str]) — The labels sorted by order of likelihood.
scores (List[float]) — The probabilities for each of the labels.
  Classify the sequence(s) given as inputs. See the ZeroShotClassificationPipeline documentation for more
informatio


DocumentQuestionAnsweringPipeline

class transformers.DocumentQuestionAnsweringPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (


DocumentQuestionAnsweringPipeline

str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a bin


DocumentQuestionAnsweringPipeline

ary format (i.e., pickle) or as raw text.    Document Question Answering pipeline using any AutoModelForDocumentQuestionAnswering. The inputs/outputs are
similar to the (extractive) question answering pipeline; however, the pipeline takes an image (and optional OCR’d
words/boxes) as input instead of text context.  Example:   Copied >>> from transformers import pipeline
>>> document_qa = pipeline(model="impira/layoutlm-document-qa")
>>> document_qa(
...     image="https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png",
...     question="What is the invoice number?",
... )
[{'score': 0.425, 'answer': 'us-001', 'start': 16, 'end': 16}] Learn more about the basics of using a pipeline in the pipeline tutorial This document question answering pipeline can currently be loaded from pipeline() using the following task
identifier: "document-question-answering". The models that this pipeline can


DocumentQuestionAnsweringPipeline

 use are models that have been fine-tuned on a document question answering task.
See the up-to-date list of available models on
huggingface.co/models.  __call__  < source > ( image: typing.Union[ForwardRef('Image.Image'), str] question: typing.Optional[str] = None word_boxes: typing.Tuple[str, typing.List[float]] = None **kwargs  ) → A dict or a list of dict  Parameters   image (str or PIL.Image) —
The pipeline handles three types of images:
A string containing a http link pointing to an image
A string containing a local path to an image
An image loaded in PIL directly
The pipeline accepts either a single image or a batch of images. If given a single image, it can be
broadcasted to multiple questions.   question (str) —
A question to ask of the document.   word_boxes (List[str, Tuple[float, float, float, float]], optional) —
A list of words and bounding boxes (normalized 0->1000). If you provide this optional input, then the
pipeline wi


DocumentQuestionAnsweringPipeline

ll use these words and boxes instead of running OCR on the image to derive them for models
that need them (e.g. LayoutLM). This allows you to reuse OCR’d results across many invocations of the
pipeline without having to re-run it each time.   top_k (int, optional, defaults to 1) —
The number of answers to return (will be chosen by order of likelihood). Note that we return less than
top_k answers if there are not enough options available within the context.   doc_stride (int, optional, defaults to 128) —
If the words in the document are too long to fit with the question for the model, it will be split in
several chunks with some overlap. This argument controls the size of that overlap.   max_answer_len (int, optional, defaults to 15) —
The maximum length of predicted answers (e.g., only answers with a shorter length are considered).   max_seq_len (int, optional, defaults to 384) —
The maximum length of the total sentence (context + quest


DocumentQuestionAnsweringPipeline

ion) in tokens of each chunk passed to the
model. The context will be split in several chunks (using doc_stride as overlap) if needed.   max_question_len (int, optional, defaults to 64) —
The maximum length of the question after tokenization. It will be truncated if needed.   handle_impossible_answer (bool, optional, defaults to False) —
Whether or not we accept impossible as an answer.   lang (str, optional) —
Language to use while running OCR. Defaults to english.   tesseract_config (str, optional) —
Additional flags to pass to tesseract while running OCR.   timeout (float, optional, defaults to None) —
The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
the call may block forever.   Returns
A dict or a list of dict

Each result comes as a dictionary with the following keys:
score (float) — The probability associated to the answer.
start (int) — The start word index of the answer (in


DocumentQuestionAnsweringPipeline

 the OCR’d version of the input or provided
word_boxes).
end (int) — The end word index of the answer (in the OCR’d version of the input or provided
word_boxes).
answer (str) — The answer to the question.
words (list[int]) — The index of each word/box pair that is in the answer
  Answer the question(s) given as inputs by using the document(s). A document is defined as an image and an
optional list of (word, box) tuples which represent the text in the document. If the word_boxes are not
provided, it will use the Tesseract OCR engine (if available) to extract the words and boxes automatically for
LayoutLM-like models which require them as input. For Donut, no OCR is run. You can invoke the pipeline several ways: pipeline(image=image, question=question) pipeline(image=image, question=question, word_boxes=word_boxes) pipeline([{"image": image, "question": question}]) pipeline([{"image": image, "question": question, "word_boxes": word_boxes}


FeatureExtractionPipeline

class transformers.FeatureExtractionPipeline  < source > ( model: typing.Union[ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] tokenizer: typing.Optional[transformers.tokenization_utils.PreTrainedTokenizer] = None feature_extractor: typing.Optional[ForwardRef('SequenceFeatureExtractor')] = None image_processor: typing.Optional[transformers.image_processing_utils.BaseImageProcessor] = None modelcard: typing.Optional[transformers.modelcard.ModelCard] = None framework: typing.Optional[str] = None task: str = '' args_parser: ArgumentHandler = None device: typing.Union[int, ForwardRef('torch.device')] = None torch_dtype: typing.Union[str, ForwardRef('torch.dtype'), NoneType] = None binary_output: bool = False **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used b


FeatureExtractionPipeline

y the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   return_tensors (bool, optional) —
If True, returns a tensor accor


FeatureExtractionPipeline

ding to the specified framework, otherwise returns a list.   task (str, defaults to "") —
A task-identifier for the pipeline.   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id.   tokenize_kwargs (dict, optional) —
Additional dictionary of keyword arguments passed along to the tokenizer.    Feature extraction pipeline using no model head. This pipeline extracts the hidden states from the base
transformer, which can be used as features in downstream tasks.  Example:   Copied >>> from transformers import pipeline
>>> extractor = pipeline(model="bert-base-uncased", task="feature-e


FeatureExtractionPipeline

xtraction")
>>> result = extractor("This is a simple test.", return_tensors=True)
>>> result.shape  # This is a tensor of shape [1, sequence_lenth, hidden_dimension] representing the input string.
torch.Size([1, 8, 768]) Learn more about the basics of using a pipeline in the pipeline tutorial This feature extraction pipeline can currently be loaded from pipeline() using the task identifier:
"feature-extraction". All models may be used for this pipeline. See a list of all models, including community-contributed models on
huggingface.co/models.  __call__  < source > ( *args **kwargs  ) → A nested list of float  Parameters   args (str or List[str]) — One or several texts (or one list of texts) to get the features of.   Returns
A nested list of float

The features computed by the model.
  Extract the features of the input(


ImageToTextPipeline

class transformers.ImageToTextPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
pro


ImageToTextPipeline

vided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if th


ImageToTextPipeline

e output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    Image To Text pipeline using a AutoModelForVision2Seq. This pipeline predicts a caption for a given image.  Example:   Copied >>> from transformers import pipeline
>>> captioner = pipeline(model="ydshieh/vit-gpt2-coco-en")
>>> captioner("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
[{'generated_text': 'two birds are standing next to each other '}] Learn more about the basics of using a pipeline in the pipeline tutorial This image to text pipeline can currently be loaded from pipeline() using the following task identifier:
“image-to-text”. See the list of available models on
huggingface.co/models.  __call__  < source > ( images: typing.Union[str, typing.List[str], ForwardRef('Image.Image'), typing.List[ForwardRef('Image.Image')]] **kwargs  ) → A list or a list of list of dict  Parameters   ima


ImageToTextPipeline

ges (str, List[str], PIL.Image or List[PIL.Image]) —
The pipeline handles three types of images:
A string containing a HTTP(s) link pointing to an image
A string containing a local path to an image
An image loaded in PIL directly
The pipeline accepts either a single image or a batch of images.   max_new_tokens (int, optional) —
The amount of maximum tokens to generate. By default it will use generate default.   generate_kwargs (Dict, optional) —
Pass it to send all of these arguments directly to generate allowing full control of this function.   timeout (float, optional, defaults to None) —
The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
the call may block forever.   Returns
A list or a list of list of dict

Each result comes as a dictionary with the following key:
generated_text (str) — The generated text.
  Assign labels to the image(s) passed as inpu


VisualQuestionAnsweringPipeline

class transformers.VisualQuestionAnsweringPipeline  < source > ( *args **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPreTrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for th


VisualQuestionAnsweringPipeline

e pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    Visual Question Answering pipeline using a Auto


VisualQuestionAnsweringPipeline

ModelForVisualQuestionAnswering. This pipeline is currently only
available in PyTorch.  Example:   Copied >>> from transformers import pipeline
>>> oracle = pipeline(model="dandelin/vilt-b32-finetuned-vqa")
>>> image_url = "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/lena.png"
>>> oracle(question="What is she wearing ?", image=image_url)
[{'score': 0.948, 'answer': 'hat'}, {'score': 0.009, 'answer': 'fedora'}, {'score': 0.003, 'answer': 'clothes'}, {'score': 0.003, 'answer': 'sun hat'}, {'score': 0.002, 'answer': 'nothing'}]
>>> oracle(question="What is she wearing ?", image=image_url, top_k=1)
[{'score': 0.948, 'answer': 'hat'}]
>>> oracle(question="Is this a person ?", image=image_url, top_k=1)
[{'score': 0.993, 'answer': 'yes'}]
>>> oracle(question="Is this a man ?", image=image_url, top_k=1)
[{'score': 0.996, 'answer': 'no'}] Learn more about the basics of using a pipeline in the pipeline tutorial This visual question answering pipeline can currently be loaded


VisualQuestionAnsweringPipeline

from pipeline() using the following task
identifiers: "visual-question-answering", "vqa". The models that this pipeline can use are models that have been fine-tuned on a visual question answering task. See
the up-to-date list of available models on
huggingface.co/models.  __call__  < source > ( image: typing.Union[ForwardRef('Image.Image'), str] question: str = None **kwargs  ) → A dictionary or a list of dictionaries containing the result. The dictionaries contain the following keys  Parameters   image (str, List[str], PIL.Image or List[PIL.Image]) —
The pipeline handles three types of images:
A string containing a http link pointing to an image
A string containing a local path to an image
An image loaded in PIL directly
The pipeline accepts either a single image or a batch of images. If given a single image, it can be
broadcasted to multiple questions.   question (str, List[str]) —
The question(s) asked. If given a single question, it can be broadcasted to multiple images.   top


VisualQuestionAnsweringPipeline

_k (int, optional, defaults to 5) —
The number of top labels that will be returned by the pipeline. If the provided number is higher than
the number of labels available in the model configuration, it will default to the number of labels.   timeout (float, optional, defaults to None) —
The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
the call may block forever.   Returns
A dictionary or a list of dictionaries containing the result. The dictionaries contain the following keys

label (str) — The label identified by the model.
score (int) — The score attributed by the model for that label.
  Answers open-ended questions about images. The pipeline accepts several types of inputs which are detailed
below: pipeline(image=image, question=question) pipeline({"image": image, "question": question}) pipeline([{"image": image, "question": question}]) pipeline([{"image": image, "question": question}, {"image": image, "question": question}])


Parent class: Pipeline

class transformers.Pipeline  < source > ( model: typing.Union[ForwardRef('PreTrainedModel'), ForwardRef('TFPreTrainedModel')] tokenizer: typing.Optional[transformers.tokenization_utils.PreTrainedTokenizer] = None feature_extractor: typing.Optional[ForwardRef('SequenceFeatureExtractor')] = None image_processor: typing.Optional[transformers.image_processing_utils.BaseImageProcessor] = None modelcard: typing.Optional[transformers.modelcard.ModelCard] = None framework: typing.Optional[str] = None task: str = '' args_parser: ArgumentHandler = None device: typing.Union[int, ForwardRef('torch.device')] = None torch_dtype: typing.Union[str, ForwardRef('torch.dtype'), NoneType] = None binary_output: bool = False **kwargs  )   Parameters   model (PreTrainedModel or TFPreTrainedModel) —
The model that will be used by the pipeline to make predictions. This needs to be a model inheriting from
PreTrainedModel for PyTorch and TFPre


Parent class: Pipeline

TrainedModel for TensorFlow.   tokenizer (PreTrainedTokenizer) —
The tokenizer that will be used by the pipeline to encode data for the model. This object inherits from
PreTrainedTokenizer.   modelcard (str or ModelCard, optional) —
Model card attributed to the model for this pipeline.   framework (str, optional) —
The framework to use, either "pt" for PyTorch or "tf" for TensorFlow. The specified framework must be
installed.
If no framework is specified, will default to the one currently installed. If no framework is specified and
both frameworks are installed, will default to the framework of the model, or to PyTorch if no model is
provided.   task (str, defaults to "") —
A task-identifier for the pipeline.   num_workers (int, optional, defaults to 8) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the number of
workers to be used.   batch_size (int, optional, defaults


Parent class: Pipeline

to 1) —
When the pipeline will use DataLoader (when passing a dataset, on GPU for a Pytorch model), the size of
the batch to use, for inference this is not always beneficial, please read Batching with
pipelines .   args_parser (ArgumentHandler, optional) —
Reference to the object in charge of parsing supplied pipeline parameters.   device (int, optional, defaults to -1) —
Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run the model on
the associated CUDA device id. You can pass native torch.device or a str too.   binary_output (bool, optional, defaults to False) —
Flag indicating if the output the pipeline should happen in a binary format (i.e., pickle) or as raw text.    The Pipeline class is the class from which all pipelines inherit. Refer to this class for methods shared across
different pipelines. Base class implementing pipelined operations. Pipeline workflow is defin


Parent class: Pipeline

ed as a sequence of the following
operations: Input -> Tokenization -> Model Inference -> Post-Processing (task dependent) -> Output Pipeline supports running on CPU or GPU through the device argument (see below). Some pipeline, like for instance FeatureExtractionPipeline ('feature-extraction') output large tensor object
as nested-lists. In order to avoid dumping such large structure as textual data we provide the binary_output
constructor argument. If set to True, the output will be stored in the pickle format.  check_model_type  < source > ( supported_models: typing.Union[typing.List[str], dict]  )   Parameters   supported_models (List[str] or dict) —
The list of models supported by the pipeline, or a dictionary with model class values.    Check if the model class is in supported by the pipeline.  device_placement  < source > ( )    Context Manager allowing tensor allocation on the user-specified device in framewor


Parent class: Pipeline

k agnostic way.  Examples:   Copied # Explicitly ask for tensor allocation on CUDA device :0
pipe = pipeline(..., device=0)
with pipe.device_placement():
    # Every framework specific tensor allocation will be done on the request device
    output = pipe(...)  ensure_tensor_on_device  < source > ( **inputs  ) → Dict[str, torch.Tensor]  Parameters   inputs (keyword arguments that should be torch.Tensor, the rest is ignored) —
The tensors to place on self.device.   Recursive on lists only. —   Returns
Dict[str, torch.Tensor]

The same as inputs but on the proper device.
  Ensure PyTorch tensors are on the specified device.  postprocess  < source > ( model_outputs: ModelOutput **postprocess_parameters: typing.Dict  )    Postprocess will receive the raw outputs of the _forward method, generally tensors, and reformat them into
something more friendly. Generally it will output a list or a dict or results (containing ju


Parent class: Pipeline

st strings and
numbers).  predict  < source > ( X  )    Scikit / Keras interface to transformers’ pipelines. This method will forward to call().  preprocess  < source > ( input_: typing.Any **preprocess_parameters: typing.Dict  )    Preprocess will take the input_ of a specific pipeline and return a dictionary of everything necessary for
_forward to run properly. It should contain at least one tensor, but might have arbitrary other items.  save_pretrained  < source > ( save_directory: str safe_serialization: bool = False  )   Parameters   save_directory (str) —
A path to the directory where to saved. It will be created if it doesn’t exist.   safe_serialization (str) —
Whether to save the model using safetensors or the traditional way for PyTorch or Tensorflow    Save the pipeline’s model and tokenizer.  transform  < source > ( X  )    Scikit / Keras interface to transformers’ pipelines. This method will forward to ca


Tokenizer

A tokenizer is in charge of preparing the inputs for a model. The library contains tokenizers for all the models. Most
of the tokenizers are available in two flavors: a full python implementation and a “Fast” implementation based on the
Rust library 🤗 Tokenizers. The “Fast” implementations allows:
a significant speed-up in particular when doing batched tokenization and additional methods to map between the original string (character and words) and the token space (e.g. getting the
index of the token comprising a given character or the span of characters corresponding to a given token).
The base classes PreTrainedTokenizer and PreTrainedTokenizerFast
implement the common methods for encoding string inputs in model inputs (see below) and instantiating/saving python and
“Fast” tokeni


Tokenizer

zers either from a local file or directory or from a pretrained tokenizer provided by the library
(downloaded from HuggingFace’s AWS S3 repository). They both rely on
PreTrainedTokenizerBase that contains the common methods, and
SpecialTokensMixin.
PreTrainedTokenizer and PreTrainedTokenizerFast thus implement the main
methods for using all the tokenizers:
Tokenizing (splitting strings in sub-word token strings), converting tokens strings to ids and back, and
encoding/decoding (i.e., tokenizing and converting to integers). Adding new tokens to the vocabulary in a way that is independent of the underlying structure (BPE, SentencePiece…). Managing special tokens (like mask, beginning-of-sentence, etc.): adding them, assigning them to attributes in the
tokenizer for easy access and m


Tokenizer

aking sure they are not split during tokenization.
BatchEncoding holds the output of the
PreTrainedTokenizerBase’s encoding methods (__call__,
encode_plus and batch_encode_plus) and is derived from a Python dictionary. When the tokenizer is a pure python
tokenizer, this class behaves just like a standard python dictionary and holds the various model inputs computed by
these methods (input_ids, attention_mask…). When the tokenizer is a “Fast” tokenizer (i.e., backed by
HuggingFace tokenizers library), this class provides in addition
several advanced alignment methods which can be used to map between the original string (character and words) and the
token space (e.g., getting the index of the token comprising a given character or the span of characters corresponding
to a given token


PreTrainedTokenizer

class transformers.PreTrainedTokenizer  < source > ( **kwargs  )   Parameters   model_max_length (int, optional) —
The maximum length (in number of tokens) for the inputs to the transformer model. When the tokenizer is
loaded with from_pretrained(), this will be set to the
value stored for the associated model in max_model_input_sizes (see above). If no value is provided, will
default to VERY_LARGE_INTEGER (int(1e30)).   padding_side (str, optional) —
The side on which the model should have padding applied. Should be selected between [‘right’, ‘left’].
Default value is picked from the class attribute of the same name.   truncation_side (str, optional) —
The side on which the model should have truncation applied. Should be selected between [‘right’, ‘left’].
Default value is picked from the class attribute of the same name.   chat_template (str, optional) —
A Jinja template string that will be used to format lists of chat messages. See
https://huggingface.co/docs/transformers/c


PreTrainedTokenizer

hat_templating for a full description.   model_input_names (List[string], optional) —
The list of inputs accepted by the forward pass of the model (like "token_type_ids" or
"attention_mask"). Default value is picked from the class attribute of the same name.   bos_token (str or tokenizers.AddedToken, optional) —
A special token representing the beginning of a sentence. Will be associated to self.bos_token and
self.bos_token_id.   eos_token (str or tokenizers.AddedToken, optional) —
A special token representing the end of a sentence. Will be associated to self.eos_token and
self.eos_token_id.   unk_token (str or tokenizers.AddedToken, optional) —
A special token representing an out-of-vocabulary token. Will be associated to self.unk_token and
self.unk_token_id.   sep_token (str or tokenizers.AddedToken, optional) —
A special token separating two different sentences in the same input (used by BERT for instance). Will be
associated to self.sep_token and self.sep_token_id.   pad_t


PreTrainedTokenizer

oken (str or tokenizers.AddedToken, optional) —
A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by
attention mechanisms or loss computation. Will be associated to self.pad_token and self.pad_token_id.   cls_token (str or tokenizers.AddedToken, optional) —
A special token representing the class of the input (used by BERT for instance). Will be associated to
self.cls_token and self.cls_token_id.   mask_token (str or tokenizers.AddedToken, optional) —
A special token representing a masked token (used by masked-language modeling pretraining objectives, like
BERT). Will be associated to self.mask_token and self.mask_token_id.   additional_special_tokens (tuple or list of str or tokenizers.AddedToken, optional) —
A tuple or a list of additional special tokens. Add them here to ensure they are skipped when decoding with
skip_special_tokens is set to True. If they are not part of the vocabulary, they will be added at the end
of th


PreTrainedTokenizer

e vocabulary.   clean_up_tokenization_spaces (bool, optional, defaults to True) —
Whether or not the model should cleanup the spaces that were added when splitting the input text during the
tokenization process.   split_special_tokens (bool, optional, defaults to False) —
Whether or not the special tokens should be split during the tokenization process. The default behavior is
to not split special tokens. This means that if <s> is the bos_token, then tokenizer.tokenize("<s>") = ['<s>]. Otherwise, if split_special_tokens=True, then tokenizer.tokenize("<s>") will be give ['<', 's', '>']. This argument is only supported for slow tokenizers for the moment.    Base class for all slow tokenizers. Inherits from PreTrainedTokenizerBase. Handle all the shared methods for tokenization and special tokens as well as methods downloading/caching/loading
pretrained tokenizers as well as adding tokens to the vocabulary. This class also contain the added tokens in a unified way on top of all t


PreTrainedTokenizer

okenizers so we don’t have to handle the
specific vocabulary augmentation methods of the various underlying dictionary structures (BPE, sentencepiece…). Class attributes (overridden by derived classes) vocab_files_names (Dict[str, str]) — A dictionary with, as keys, the __init__ keyword name of each
vocabulary file required by the model, and as associated values, the filename for saving the associated file
(string). pretrained_vocab_files_map (Dict[str, Dict[str, str]]) — A dictionary of dictionaries, with the
high-level keys being the __init__ keyword name of each vocabulary file required by the model, the
low-level being the short-cut-names of the pretrained models with, as associated values, the url to the
associated pretrained vocabulary file. max_model_input_sizes (Dict[str, Optional[int]]) — A dictionary with, as keys, the short-cut-names
of the pretrained models, and as associated values, the maximum length of the sequence inputs of this model,
or None if the model has


PreTrainedTokenizer

no maximum input size. pretrained_init_configuration (Dict[str, Dict[str, Any]]) — A dictionary with, as keys, the
short-cut-names of the pretrained models, and as associated values, a dictionary of specific arguments to
pass to the __init__ method of the tokenizer class for this pretrained model when loading the tokenizer
with the from_pretrained() method. model_input_names (List[str]) — A list of inputs expected in the forward pass of the model. padding_side (str) — The default value for the side on which the model should have padding applied.
Should be 'right' or 'left'. truncation_side (str) — The default value for the side on which the model should have truncation
applied. Should be 'right' or 'left'.  __call__  < source > ( text: typing.Union[str, typing.List[str], typing.List[typing.List[str]]] = None text_pair: typing.Union[str, typing.List[str], typing.List[typing.List[str]], NoneType] = None text_target: typing.Union[str, typing.List[str], typing.List[typing.List[str


PreTrainedTokenizer

]]] = None text_pair_target: typing.Union[str, typing.List[str], typing.List[typing.List[str]], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 is_split_into_words: bool = False pad_to_multiple_of: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_mapping: bool = False return_length: bool = False verbose: bool = True **kwargs  ) → BatchEncoding  Parameters   text (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded. Each sequence can b


PreTrainedTokenizer

e a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_pair (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_target (str, List[str], List[List[str]], optional) —
The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
you must set is_split_into_words=True (to lift the ambiguity with a batch of sequences).   text_pair_target (str, List[str], List[List[str]], optional) —
The sequence or batch of se


PreTrainedTokenizer

quences to be encoded as target texts. Each sequence can be a string or a
list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized),
you must set is_split_into_words=True (to lift the ambiguity with a batch of sequences).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following values:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length


PreTrainedTokenizer

 for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_


PreTrainedTokenizer

length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.   stride (int, optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide s


PreTrainedTokenizer

ome overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, optional) —
If set, will return tensors instead of list of python integers. Acceptable values are:
'tf': Return TensorFlow tf.constant objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return Numpy np.ndarray objects.
   return_toke


PreTrainedTokenizer

n_type_ids (bool, optional) —
Whether to return token type IDs. If left to the default, will return the token type IDs according to
the specific tokenizer’s default, defined by the return_outputs attribute.
What are token type IDs?   return_attention_mask (bool, optional) —
Whether to return the attention mask. If left to the default, will return the attention mask according
to the specific tokenizer’s default, defined by the return_outputs attribute.
What are attention masks?   return_overflowing_tokens (bool, optional, defaults to False) —
Whether or not to return overflowing token sequences. If a pair of sequences of input ids (or a batch
of pairs) is provided with truncation_strategy = longest_first or True, an error is raised instead
of returning overflowing tokens.   return_special_tokens_mask (bool, optional, defaults to False) —
Whether or not to return special tokens mask information.   return_offsets_mapping (bool, optional, defaults to False) —
Whether or not to ret


PreTrainedTokenizer

urn (char_start, char_end) for each token.
This is only available on fast tokenizers inheriting from PreTrainedTokenizerFast, if using
Python’s tokenizer, this method will raise NotImplementedError.   return_length  (bool, optional, defaults to False) —
Whether or not to return the lengths of the encoded inputs.   verbose (bool, optional, defaults to True) —
Whether or not to print more information and warnings.
**kwargs — passed to the self.tokenize() method   Returns
BatchEncoding

A BatchEncoding with the following fields:
input_ids — List of token ids to be fed to a model.
What are input IDs?
token_type_ids — List of token type ids to be fed to a model (when return_token_type_ids=True or
if “token_type_ids” is in self.model_input_names).
What are token type IDs?
attention_mask — List of indices specifying which tokens should be attended to by the model (when
return_attention_mask=True or if “attention_mask” is in self.model_input_names).
What are attention masks?
overfl


PreTrainedTokenizer

owing_tokens — List of overflowing tokens sequences (when a max_length is specified and
return_overflowing_tokens=True).
num_truncated_tokens — Number of tokens truncated (when a max_length is specified and
return_overflowing_tokens=True).
special_tokens_mask — List of 0s and 1s, with 1 specifying added special tokens and 0 specifying
regular sequence tokens (when add_special_tokens=True and return_special_tokens_mask=True).
length — The length of the inputs (when return_length=True)
  Main method to tokenize and prepare for the model one or several sequence(s) or one or several pair(s) of
sequences.  apply_chat_template  < source > ( conversation: typing.Union[typing.List[typing.Dict[str, str]], ForwardRef('Conversation')] chat_template: typing.Optional[str] = None add_generation_prompt: bool = False tokenize: bool = True padding: bool = False truncation: bool = False max_length: typing.Optional[int] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorTy


PreTrainedTokenizer

pe, NoneType] = None **tokenizer_kwargs  ) → List[int]  Parameters   conversation (Union[List[Dict[str, str]], “Conversation”]) — A Conversation object or list of dicts
with “role” and “content” keys, representing the chat history so far.   chat_template (str, optional) — A Jinja template to use for this conversion. If
this is not passed, the model’s default chat template will be used instead.   add_generation_prompt (bool, optional) — Whether to end the prompt with the token(s) that indicate
the start of an assistant message. This is useful when you want to generate a response from the model.
Note that this argument will be passed to the chat template, and so it must be supported in the
template for this argument to have any effect.   tokenize (bool, defaults to True) —
Whether to tokenize the output. If False, the output will be a string.   padding (bool, defaults to False) —
Whether to pad sequences to the maximum length. Has no effect if tokenize is False.   truncation (bo


PreTrainedTokenizer

ol, defaults to False) —
Whether to truncate sequences at the maximum length. Has no effect if tokenize is False.   max_length (int, optional) —
Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is False. If
not specified, the tokenizer’s max_length attribute will be used as a default.   return_tensors (str or TensorType, optional) —
If set, will return tensors of a particular framework. Has no effect if tokenize is False. Acceptable
values are:
'tf': Return TensorFlow tf.Tensor objects.
'pt': Return PyTorch torch.Tensor objects.
'np': Return NumPy np.ndarray objects.
'jax': Return JAX jnp.ndarray objects.
**tokenizer_kwargs — Additional kwargs to pass to the tokenizer.
   Returns
List[int]

A list of token ids representing the tokenized chat so far, including control tokens. This
output is ready to pass to the model, either directly or via methods like generate().
  Converts a Conversation object or a list of dictionaries with "role" an


PreTrainedTokenizer

d "content" keys to a list of token
ids. This method is intended for use with chat models, and will read the tokenizer’s chat_template attribute to
determine the format and control tokens to use when converting. When chat_template is None, it will fall back
to the default_chat_template specified at the class level.  batch_decode  < source > ( sequences: typing.Union[typing.List[int], typing.List[typing.List[int]], ForwardRef('np.ndarray'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor')] skip_special_tokens: bool = False clean_up_tokenization_spaces: bool = None **kwargs  ) → List[str]  Parameters   sequences (Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]) —
List of tokenized input ids. Can be obtained using the __call__ method.   skip_special_tokens (bool, optional, defaults to False) —
Whether or not to remove special tokens in the decoding.   clean_up_tokenization_spaces (bool, optional) —
Whether or not to clean up the tokenization spaces. If N


PreTrainedTokenizer

one, will default to
self.clean_up_tokenization_spaces.   kwargs (additional keyword arguments, optional) —
Will be passed to the underlying model specific decode method.   Returns
List[str]

The list of decoded sentences.
  Convert a list of lists of token ids into a list of strings by calling decode.  decode  < source > ( token_ids: typing.Union[int, typing.List[int], ForwardRef('np.ndarray'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor')] skip_special_tokens: bool = False clean_up_tokenization_spaces: bool = None **kwargs  ) → str  Parameters   token_ids (Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]) —
List of tokenized input ids. Can be obtained using the __call__ method.   skip_special_tokens (bool, optional, defaults to False) —
Whether or not to remove special tokens in the decoding.   clean_up_tokenization_spaces (bool, optional) —
Whether or not to clean up the tokenization spaces. If None, will default to
self.clean_up_tokenization_spaces.   kw


PreTrainedTokenizer

args (additional keyword arguments, optional) —
Will be passed to the underlying model specific decode method.   Returns
str

The decoded sentence.
  Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special
tokens and clean up tokenization spaces. Similar to doing self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids)).  encode  < source > ( text: typing.Union[str, typing.List[str], typing.List[int]] text_pair: typing.Union[str, typing.List[str], typing.List[int], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None stride: int = 0 return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None **kwargs  ) → List[int], torch.Tensor, tf.Tensor or np.ndarray  Parameters


PreTrainedTokenizer

text (str, List[str] or List[int]) —
The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the
tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   text_pair (str, List[str] or List[int], optional) —
Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using
the tokenize method) or a list of integers (tokenized string ids using the convert_tokens_to_ids
method).   add_special_tokens (bool, optional, defaults to True) —
Whether or not to add special tokens when encoding the sequences. This will use the underlying
PretrainedTokenizerBase.build_inputs_with_special_tokens function, which defines which tokens are
automatically added to the input ids. This is usefull if you want to add bos or eos tokens
automatically.   padding (bool, str or PaddingStrategy, optional, defaults to False) —
Activates and controls padding. Accepts the following val


PreTrainedTokenizer

ues:
True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
'max_length': Pad to a maximum length specified with the argument max_length or to the maximum
acceptable input length for the model if that argument is not provided.
False or 'do_not_pad' (default): No padding (i.e., can output a batch with sequences of different
lengths).
   truncation (bool, str or TruncationStrategy, optional, defaults to False) —
Activates and controls truncation. Accepts the following values:
True or 'longest_first': Truncate to a maximum length specified with the argument max_length or
to the maximum acceptable input length for the model if that argument is not provided. This will
truncate token by token, removing a token from the longest sequence in the pair if a pair of
sequences (or a batch of pairs) is provided.
'only_first': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input leng


PreTrainedTokenizer

th for the model if that argument is not provided. This will only
truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
'only_second': Truncate to a maximum length specified with the argument max_length or to the
maximum acceptable input length for the model if that argument is not provided. This will only
truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.
False or 'do_not_truncate' (default): No truncation (i.e., can output batch with sequence lengths
greater than the model maximum admissible input size).
   max_length (int, optional) —
Controls the maximum length to use by one of the truncation/padding parameters.
If left unset or set to None, this will use the predefined model maximum length if a maximum length
is required by one of the truncation/padding parameters. If the model has no specific maximum input
length (like XLNet) truncation/padding to a maximum length will be deactivated.


PreTrainedTokenizer

   stride (int, optional, defaults to 0) —
If set to a number along with max_length, the overflowing tokens returned when
return_overflowing_tokens=True will contain some tokens from the end of the truncated sequence
returned to provide some overlap between truncated and overflowing sequences. The value of this
argument defines the number of overlapping tokens.   is_split_into_words (bool, optional, defaults to False) —
Whether or not the input is already pre-tokenized (e.g., split into words). If set to True, the
tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace)
which it will tokenize. This is useful for NER or token classification.   pad_to_multiple_of (int, optional) —
If set will pad the sequence to a multiple of the provided value. Requires padding to be activated.
This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
>= 7.5 (Volta).   return_tensors (str or TensorType, op