initialize method

Future<void> initialize()

Initialize the model and tokenizer. Must call downloadModel first.

Implementation

Future<void> initialize() async {
  if (_initialized) return;

  final dir = await _modelsDir;
  final modelPath = '${dir.path}/$_modelFilename';
  final vocabPath = '${dir.path}/$_vocabFilename';

  if (!File(modelPath).existsSync() || !File(vocabPath).existsSync()) {
    throw StateError('Model files not found. Call downloadModel() first.');
  }

  // Load tokenizer with truncation and padding for fixed-length input.
  _tokenizer = WordPieceTokenizer.fromVocabFileSync(vocabPath)
    ..enableTruncation(maxLength: _maxSequenceLength)
    ..enablePadding(length: _maxSequenceLength);

  // Load ONNX session.
  final ort = OnnxRuntime();
  _session = await ort.createSession(modelPath);

  _initialized = true;
  dev.log('Model initialized', name: 'LocalEmbedding');
}