DonutModel class

The complete Donut model combining Swin Transformer encoder and BART decoder.

Usage:

final config = DonutConfig.base();
final model = DonutModel(config);
await model.loadWeights('path/to/weights');

final image = DonutImageUtils.loadAndPreprocess(imageBytes, config);
final result = model.inference(
  imageTensor: image,
  prompt: '<s_cord-v2>',
);
print(result.json);

Constructors

DonutModel(DonutConfig config)
Creates a new Donut model with the given configuration.
DonutModel.cordV2()
Create a pre-configured model for CORD receipt parsing.
factory
DonutModel.docvqa()
Create a pre-configured model for document VQA.
factory
DonutModel.rvlcdip()
Create a pre-configured model for document classification (RVL-CDIP).
factory

Properties

config DonutConfig
Model configuration.
final
decoder BartDecoder
Text decoder (BART).
getter/setter pair
encoder SwinEncoder
Visual encoder (Swin Transformer).
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
isReady bool
Whether the model has loaded weights and is ready for inference.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tokenizer DonutTokenizer?
Tokenizer for encoding/decoding text.
getter/setter pair

Methods

addTaskTokens(List<Map<String, dynamic>> labels) → void
Add task-specific special tokens for a dataset.
decode({required Tensor encoderOutput, required List<int> promptTokens, int? maxLength, int? eosTokenId}) List<int>
Run the decoder to generate tokens given encoder output.
encode(Tensor imageTensor) Tensor
Run the encoder on an image tensor.
inference({required Tensor imageTensor, required String prompt, int? maxLength}) DonutResult
Run full inference: image → structured JSON output.
inferenceFromBytes({required List<int> imageBytes, required String prompt, int? maxLength}) DonutResult
Run inference on raw image bytes.
loadTokenizerFromFile(String path) → void
Load a tokenizer from a HuggingFace tokenizer.json file.
loadWeights(String directory) Future<void>
Load model weights from a directory containing safetensors or weight files.
loadWeightsFromMap(Map<String, Tensor> weights) → void
Load weights from a map of tensor name → Tensor.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
preparePrompt(String task, {String? question}) String
Prepare task-specific prompt tokens with special tokens added.
randomInit({double scale = 0.02, int? seed}) → void
Initialize all model weights with small random values.
setTokenizer(DonutTokenizer tok) → void
Set the tokenizer.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

json2token(dynamic obj, {bool updateSpecialTokens = true, DonutTokenizer? tokenizer}) String
Convert a JSON object to a Donut token sequence.
token2json(String tokenStr) → dynamic
Convert a Donut token sequence back to a JSON object.