ocr 0.1.1

A pure Rust CLI OCR tool for printed text extraction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
//! Transformer-based OCR model implementation
//!
//! This module provides a TrOCR (Transformer-based OCR) model implementation
//! that can handle both text detection and recognition using transformer architectures.

use super::engine::*;
use crate::core::image::OcrImage;
use crate::core::ModelType;
use crate::utils::{OcrError, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Configuration for Transformer OCR models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformerConfig {
    /// Model architecture type
    pub architecture: TransformerArchitecture,
    /// Path to the model file
    pub model_path: String,
    /// Supported languages
    pub supported_languages: Vec<LanguageVariant>,
    /// Input image size (height, width, channels)
    pub input_shape: (u32, u32, u32),
    /// Maximum sequence length for text generation
    pub max_sequence_length: Option<usize>,
    /// Confidence threshold for predictions
    pub confidence_threshold: f32,
    /// Device to run inference on
    pub device: DeviceType,
    /// Quantization type
    pub quantization: Option<QuantizationType>,
    /// Number of attention heads
    pub num_attention_heads: usize,
    /// Number of transformer layers
    pub num_layers: usize,
    /// Hidden dimension size
    pub hidden_size: usize,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Whether to use beam search for decoding
    pub use_beam_search: bool,
    /// Beam search width
    pub beam_width: usize,
    /// Temperature for sampling
    pub temperature: f32,
    /// Whether to use greedy decoding
    pub use_greedy: bool,
}

/// Transformer architecture variants
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum TransformerArchitecture {
    /// TrOCR (Transformer-based OCR)
    TrOCR,
    /// Vision Transformer (ViT) for OCR
    VisionTransformer,
    /// Custom transformer architecture
    Custom(String),
    /// Multi-modal transformer (image + text)
    MultiModal,
    /// Encoder-decoder transformer
    EncoderDecoder,
}

/// Transformer OCR model implementation
pub struct TransformerModel {
    config: TransformerConfig,
    model_loaded: bool,
    tokenizer: Option<TransformerTokenizer>,
    encoder: Option<TransformerEncoder>,
    decoder: Option<TransformerDecoder>,
}

/// Tokenizer for transformer models
pub struct TransformerTokenizer {
    vocab: std::collections::HashMap<String, u32>,
    special_tokens: SpecialTokens,
    max_length: usize,
}

/// Special tokens for the tokenizer
#[derive(Debug, Clone)]
pub struct SpecialTokens {
    pub pad_token: String,
    pub start_token: String,
    pub end_token: String,
    pub unknown_token: String,
    pub mask_token: String,
}

/// Transformer encoder component
pub struct TransformerEncoder {
    layers: Vec<TransformerLayer>,
    embedding: EmbeddingLayer,
    position_encoding: PositionalEncoding,
}

/// Transformer decoder component
pub struct TransformerDecoder {
    layers: Vec<TransformerLayer>,
    embedding: EmbeddingLayer,
    position_encoding: PositionalEncoding,
    output_projection: OutputProjection,
}

/// Individual transformer layer
pub struct TransformerLayer {
    self_attention: MultiHeadAttention,
    cross_attention: Option<MultiHeadAttention>,
    feed_forward: FeedForwardNetwork,
    layer_norm1: LayerNorm,
    layer_norm2: LayerNorm,
    layer_norm3: Option<LayerNorm>,
}

/// Multi-head attention mechanism
pub struct MultiHeadAttention {
    num_heads: usize,
    head_dim: usize,
    query_projection: LinearLayer,
    key_projection: LinearLayer,
    value_projection: LinearLayer,
    output_projection: LinearLayer,
    dropout: Dropout,
}

/// Feed-forward network
pub struct FeedForwardNetwork {
    linear1: LinearLayer,
    linear2: LinearLayer,
    activation: Activation,
    dropout: Dropout,
}

/// Linear layer
pub struct LinearLayer {
    weight: Vec<Vec<f32>>,
    bias: Option<Vec<f32>>,
}

/// Layer normalization
pub struct LayerNorm {
    weight: Vec<f32>,
    bias: Vec<f32>,
    eps: f32,
}

/// Dropout layer
pub struct Dropout {
    rate: f32,
    training: bool,
}

/// Embedding layer
pub struct EmbeddingLayer {
    embeddings: Vec<Vec<f32>>,
    vocab_size: usize,
    hidden_size: usize,
}

/// Positional encoding
pub struct PositionalEncoding {
    encoding: Vec<Vec<f32>>,
    max_length: usize,
    hidden_size: usize,
}

/// Output projection layer
pub struct OutputProjection {
    weight: Vec<Vec<f32>>,
    bias: Option<Vec<f32>>,
}

/// Activation functions
#[derive(Debug, Clone)]
pub enum Activation {
    ReLU,
    GELU,
    Swish,
    Tanh,
}

impl TransformerModel {
    /// Create a new Transformer model
    pub fn new(config: TransformerConfig) -> Self {
        Self {
            config,
            model_loaded: false,
            tokenizer: None,
            encoder: None,
            decoder: None,
        }
    }

    /// Load the model from file
    pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();

        if !path.exists() {
            return Err(OcrError::ModelNotFound(format!(
                "Model file not found: {}",
                path.display()
            ))
            .into());
        }

        // Initialize tokenizer
        self.tokenizer = Some(TransformerTokenizer::new(
            self.config.vocab_size,
            self.config.max_sequence_length.unwrap_or(512),
        ));

        // Initialize encoder
        self.encoder = Some(TransformerEncoder::new(
            self.config.num_layers,
            self.config.hidden_size,
            self.config.num_attention_heads,
            self.config.input_shape,
        ));

        // Initialize decoder
        self.decoder = Some(TransformerDecoder::new(
            self.config.num_layers,
            self.config.hidden_size,
            self.config.num_attention_heads,
            self.config.vocab_size,
        ));

        self.model_loaded = true;
        Ok(())
    }

    /// Get the model configuration
    pub fn config(&self) -> &TransformerConfig {
        &self.config
    }

    /// Check if the model is loaded
    pub fn is_loaded(&self) -> bool {
        self.model_loaded
    }

    /// Get the model architecture
    pub fn architecture(&self) -> &TransformerArchitecture {
        &self.config.architecture
    }

    /// Get supported languages
    pub fn supported_languages(&self) -> &[LanguageVariant] {
        &self.config.supported_languages
    }

    /// Check if a language is supported
    pub fn supports_language(&self, language: &LanguageVariant) -> bool {
        self.config.supported_languages.contains(language)
    }

    /// Preprocess image for transformer input
    fn preprocess_image(&self, image: &OcrImage) -> Result<Vec<f32>> {
        // Convert image to the expected input format
        let (height, width, channels) = self.config.input_shape;

        // For now, return a placeholder - this would be implemented with actual image processing
        let input_size = (height * width * channels) as usize;
        Ok(vec![0.0; input_size])
    }

    /// Postprocess model output to text
    fn postprocess_output(&self, logits: &[f32]) -> Result<String> {
        if let Some(tokenizer) = &self.tokenizer {
            tokenizer.decode(logits)
        } else {
            Err(OcrError::ModelNotFound("Tokenizer not loaded".to_string()).into())
        }
    }
}

impl OcrModel for TransformerModel {
    fn model_type(&self) -> ModelType {
        ModelType::Transformer
    }

    fn supported_languages(&self) -> Vec<LanguageVariant> {
        self.config.supported_languages.clone()
    }

    fn supports_language(&self, language: &LanguageVariant) -> bool {
        self.supports_language(language)
    }

    fn input_shape(&self) -> (usize, usize, usize) {
        let (h, w, c) = self.config.input_shape;
        (h as usize, w as usize, c as usize)
    }

    fn config(&self) -> &ModelConfig {
        // Return a placeholder config - in practice this would be properly implemented
        // Note: This is a temporary implementation that creates a new config each time
        // In a real implementation, this would store the config as a field
        todo!("Implement proper config storage")
    }

    fn predict(&self, input: &[u8]) -> Result<RecognitionResult> {
        if !self.model_loaded {
            return Err(OcrError::ModelNotFound("Model not loaded".to_string()).into());
        }

        // For now, return a placeholder result
        // In a real implementation, this would:
        // 1. Preprocess the input image
        // 2. Run through the transformer encoder
        // 3. Generate text using the decoder
        // 4. Postprocess the output

        let result = RecognitionResult {
            text: "Transformer OCR Result".to_string(),
            confidence: 0.95,
            bounding_boxes: vec![],
            character_results: vec![],
            word_results: vec![],
            line_results: vec![],
            language: Some("en".to_string()),
            model_type: ModelType::Transformer,
            processing_time_ms: 150,
        };

        Ok(result)
    }
}

impl TransformerTokenizer {
    /// Create a new tokenizer
    pub fn new(vocab_size: usize, max_length: usize) -> Self {
        let mut vocab = std::collections::HashMap::new();

        // Add basic vocabulary
        for i in 0..vocab_size {
            vocab.insert(format!("token_{}", i), i as u32);
        }

        let special_tokens = SpecialTokens {
            pad_token: "<pad>".to_string(),
            start_token: "<s>".to_string(),
            end_token: "</s>".to_string(),
            unknown_token: "<unk>".to_string(),
            mask_token: "<mask>".to_string(),
        };

        Self {
            vocab,
            special_tokens,
            max_length,
        }
    }

    /// Tokenize text
    pub fn encode(&self, text: &str) -> Result<Vec<u32>> {
        let mut tokens = Vec::new();

        // Add start token
        tokens.push(0); // Assuming 0 is start token ID

        // Simple word-level tokenization
        for word in text.split_whitespace() {
            if let Some(&token_id) = self.vocab.get(word) {
                tokens.push(token_id);
            } else {
                tokens.push(1); // Unknown token ID
            }
        }

        // Add end token
        tokens.push(2); // Assuming 2 is end token ID

        // Pad or truncate to max_length
        if tokens.len() > self.max_length {
            tokens.truncate(self.max_length);
        } else {
            while tokens.len() < self.max_length {
                tokens.push(0); // Pad token ID
            }
        }

        Ok(tokens)
    }

    /// Decode tokens to text
    pub fn decode(&self, tokens: &[f32]) -> Result<String> {
        // Convert logits to token IDs (simplified)
        let token_ids: Vec<u32> = tokens
            .chunks(self.vocab.len())
            .map(|chunk| {
                chunk
                    .iter()
                    .enumerate()
                    .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
                    .map(|(i, _)| i as u32)
                    .unwrap_or(0)
            })
            .collect();

        // Convert token IDs to text
        let mut text = String::new();
        for &token_id in &token_ids {
            if token_id == 0 || token_id == 2 {
                // Skip pad and end tokens
                continue;
            }

            if let Some((token, _)) = self.vocab.iter().find(|&(_, &id)| id == token_id) {
                if !text.is_empty() {
                    text.push(' ');
                }
                text.push_str(token);
            }
        }

        Ok(text)
    }
}

impl TransformerEncoder {
    /// Create a new encoder
    pub fn new(
        num_layers: usize,
        hidden_size: usize,
        num_attention_heads: usize,
        input_shape: (u32, u32, u32),
    ) -> Self {
        let layers = (0..num_layers)
            .map(|_| TransformerLayer::new(hidden_size, num_attention_heads))
            .collect();

        let embedding = EmbeddingLayer::new(input_shape, hidden_size);
        let position_encoding = PositionalEncoding::new(512, hidden_size);

        Self {
            layers,
            embedding,
            position_encoding,
        }
    }

    /// Encode input features
    pub fn encode(&self, input: &[f32]) -> Result<Vec<f32>> {
        // Apply embedding
        let mut features = self.embedding.forward(input)?;

        // Apply positional encoding
        features = self.position_encoding.add_positional_encoding(&features)?;

        // Apply transformer layers
        for layer in &self.layers {
            features = layer.forward(&features, None)?;
        }

        Ok(features)
    }
}

impl TransformerDecoder {
    /// Create a new decoder
    pub fn new(
        num_layers: usize,
        hidden_size: usize,
        num_attention_heads: usize,
        vocab_size: usize,
    ) -> Self {
        let layers = (0..num_layers)
            .map(|_| TransformerLayer::new(hidden_size, num_attention_heads))
            .collect();

        let embedding = EmbeddingLayer::new((1, 1, hidden_size as u32), hidden_size);
        let position_encoding = PositionalEncoding::new(512, hidden_size);
        let output_projection = OutputProjection::new(hidden_size, vocab_size);

        Self {
            layers,
            embedding,
            position_encoding,
            output_projection,
        }
    }

    /// Decode features to text
    pub fn decode(&self, encoder_output: &[f32], max_length: usize) -> Result<Vec<f32>> {
        let mut output = Vec::new();
        let mut hidden_state = vec![0.0; self.embedding.hidden_size];

        for _ in 0..max_length {
            // Apply embedding and positional encoding
            let embedded = self.embedding.forward(&hidden_state)?;
            let positioned = self.position_encoding.add_positional_encoding(&embedded)?;

            // Apply transformer layers
            let mut features = positioned;
            for layer in &self.layers {
                features = layer.forward(&features, Some(encoder_output))?;
            }

            // Apply output projection
            let logits = self.output_projection.forward(&features)?;
            output.extend_from_slice(&logits);

            // Update hidden state for next iteration
            hidden_state = features;
        }

        Ok(output)
    }
}

// Implement the various layer types with placeholder implementations
impl TransformerLayer {
    fn new(hidden_size: usize, num_attention_heads: usize) -> Self {
        Self {
            self_attention: MultiHeadAttention::new(hidden_size, num_attention_heads),
            cross_attention: Some(MultiHeadAttention::new(hidden_size, num_attention_heads)),
            feed_forward: FeedForwardNetwork::new(hidden_size),
            layer_norm1: LayerNorm::new(hidden_size),
            layer_norm2: LayerNorm::new(hidden_size),
            layer_norm3: Some(LayerNorm::new(hidden_size)),
        }
    }

    fn forward(&self, input: &[f32], encoder_output: Option<&[f32]>) -> Result<Vec<f32>> {
        // Self-attention
        let mut output = self.self_attention.forward(input, input, input)?;
        output = self.layer_norm1.normalize(&output)?;

        // Cross-attention (if encoder output provided)
        if let Some(encoder_out) = encoder_output {
            let cross_output = self.cross_attention.as_ref().unwrap().forward(
                &output,
                encoder_out,
                encoder_out,
            )?;
            output = self.layer_norm2.normalize(&cross_output)?;
        }

        // Feed-forward
        let ff_output = self.feed_forward.forward(&output)?;
        output = self.layer_norm3.as_ref().unwrap().normalize(&ff_output)?;

        Ok(output)
    }
}

impl MultiHeadAttention {
    fn new(hidden_size: usize, num_heads: usize) -> Self {
        let head_dim = hidden_size / num_heads;
        Self {
            num_heads,
            head_dim,
            query_projection: LinearLayer::new(hidden_size, hidden_size),
            key_projection: LinearLayer::new(hidden_size, hidden_size),
            value_projection: LinearLayer::new(hidden_size, hidden_size),
            output_projection: LinearLayer::new(hidden_size, hidden_size),
            dropout: Dropout::new(0.1),
        }
    }

    fn forward(&self, query: &[f32], key: &[f32], value: &[f32]) -> Result<Vec<f32>> {
        // Simplified attention implementation
        let q = self.query_projection.forward(query)?;
        let k = self.key_projection.forward(key)?;
        let v = self.value_projection.forward(value)?;

        // Compute attention scores (simplified)
        let attention_scores = self.compute_attention_scores(&q, &k)?;
        let attention_output = self.apply_attention(&attention_scores, &v)?;

        self.output_projection.forward(&attention_output)
    }

    fn compute_attention_scores(&self, query: &[f32], key: &[f32]) -> Result<Vec<f32>> {
        // Simplified attention score computation
        let mut scores = vec![0.0; query.len()];
        for i in 0..query.len() {
            scores[i] = query[i] * key[i % key.len()];
        }
        Ok(scores)
    }

    fn apply_attention(&self, scores: &[f32], value: &[f32]) -> Result<Vec<f32>> {
        // Simplified attention application
        let mut output = vec![0.0; value.len()];
        for i in 0..value.len() {
            output[i] = scores[i % scores.len()] * value[i];
        }
        Ok(output)
    }
}

impl FeedForwardNetwork {
    fn new(hidden_size: usize) -> Self {
        Self {
            linear1: LinearLayer::new(hidden_size, hidden_size * 4),
            linear2: LinearLayer::new(hidden_size * 4, hidden_size),
            activation: Activation::GELU,
            dropout: Dropout::new(0.1),
        }
    }

    fn forward(&self, input: &[f32]) -> Result<Vec<f32>> {
        let mut output = self.linear1.forward(input)?;
        output = self.apply_activation(&output)?;
        output = self.dropout.apply(&output)?;
        output = self.linear2.forward(&output)?;
        Ok(output)
    }

    fn apply_activation(&self, input: &[f32]) -> Result<Vec<f32>> {
        match self.activation {
            Activation::ReLU => Ok(input.iter().map(|&x| x.max(0.0)).collect()),
            Activation::GELU => Ok(input
                .iter()
                .map(|&x| 0.5 * x * (1.0 + (x * 0.79788456).tanh()))
                .collect()),
            Activation::Swish => Ok(input
                .iter()
                .map(|&x| x * (1.0 + (-x).exp()).recip())
                .collect()),
            Activation::Tanh => Ok(input.iter().map(|&x| x.tanh()).collect()),
        }
    }
}

impl LinearLayer {
    fn new(input_size: usize, output_size: usize) -> Self {
        Self {
            weight: vec![vec![0.0; input_size]; output_size],
            bias: Some(vec![0.0; output_size]),
        }
    }

    fn forward(&self, input: &[f32]) -> Result<Vec<f32>> {
        let mut output = vec![0.0; self.weight.len()];

        for (i, row) in self.weight.iter().enumerate() {
            for (j, &weight) in row.iter().enumerate() {
                if j < input.len() {
                    output[i] += weight * input[j];
                }
            }
            if let Some(ref bias) = self.bias {
                if i < bias.len() {
                    output[i] += bias[i];
                }
            }
        }

        Ok(output)
    }
}

impl LayerNorm {
    fn new(size: usize) -> Self {
        Self {
            weight: vec![1.0; size],
            bias: vec![0.0; size],
            eps: 1e-5,
        }
    }

    fn normalize(&self, input: &[f32]) -> Result<Vec<f32>> {
        let mean = input.iter().sum::<f32>() / input.len() as f32;
        let variance = input.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / input.len() as f32;
        let std = (variance + self.eps).sqrt();

        let mut output = vec![0.0; input.len()];
        for i in 0..input.len() {
            let normalized = (input[i] - mean) / std;
            output[i] =
                normalized * self.weight[i % self.weight.len()] + self.bias[i % self.bias.len()];
        }

        Ok(output)
    }
}

impl Dropout {
    fn new(rate: f32) -> Self {
        Self {
            rate,
            training: true,
        }
    }

    fn apply(&self, input: &[f32]) -> Result<Vec<f32>> {
        if self.training {
            Ok(input
                .iter()
                .map(|&x| {
                    if fastrand::f32() < self.rate {
                        0.0
                    } else {
                        x / (1.0 - self.rate)
                    }
                })
                .collect())
        } else {
            Ok(input.to_vec())
        }
    }
}

impl EmbeddingLayer {
    fn new(input_shape: (u32, u32, u32), hidden_size: usize) -> Self {
        let vocab_size = (input_shape.0 * input_shape.1 * input_shape.2) as usize;
        Self {
            embeddings: vec![vec![0.0; hidden_size]; vocab_size],
            vocab_size,
            hidden_size,
        }
    }

    fn forward(&self, input: &[f32]) -> Result<Vec<f32>> {
        // Simplified embedding lookup
        let mut output = vec![0.0; self.hidden_size];
        for (i, &value) in input.iter().enumerate() {
            let idx = (value as usize) % self.embeddings.len();
            for j in 0..self.hidden_size {
                if j < self.embeddings[idx].len() {
                    output[j] += self.embeddings[idx][j];
                }
            }
        }
        Ok(output)
    }
}

impl PositionalEncoding {
    fn new(max_length: usize, hidden_size: usize) -> Self {
        let mut encoding = vec![vec![0.0; hidden_size]; max_length];

        for pos in 0..max_length {
            for i in 0..hidden_size {
                if i % 2 == 0 {
                    encoding[pos][i] = (pos as f32
                        / 10000.0_f32.powf(2.0 * (i / 2) as f32 / hidden_size as f32))
                    .sin();
                } else {
                    encoding[pos][i] = (pos as f32
                        / 10000.0_f32.powf(2.0 * (i / 2) as f32 / hidden_size as f32))
                    .cos();
                }
            }
        }

        Self {
            encoding,
            max_length,
            hidden_size,
        }
    }

    fn add_positional_encoding(&self, input: &[f32]) -> Result<Vec<f32>> {
        let mut output = input.to_vec();
        for i in 0..input.len().min(self.hidden_size) {
            if i < self.encoding[0].len() {
                output[i] += self.encoding[0][i]; // Simplified: use first position
            }
        }
        Ok(output)
    }
}

impl OutputProjection {
    fn new(input_size: usize, output_size: usize) -> Self {
        Self {
            weight: vec![vec![0.0; input_size]; output_size],
            bias: Some(vec![0.0; output_size]),
        }
    }

    fn forward(&self, input: &[f32]) -> Result<Vec<f32>> {
        let mut output = vec![0.0; self.weight.len()];

        for (i, row) in self.weight.iter().enumerate() {
            for (j, &weight) in row.iter().enumerate() {
                if j < input.len() {
                    output[i] += weight * input[j];
                }
            }
            if let Some(ref bias) = self.bias {
                if i < bias.len() {
                    output[i] += bias[i];
                }
            }
        }

        Ok(output)
    }
}

/// Builder for Transformer models
pub struct TransformerModelBuilder {
    config: Option<TransformerConfig>,
}

impl TransformerModelBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self { config: None }
    }

    /// Set the model configuration
    pub fn with_config(mut self, config: TransformerConfig) -> Self {
        self.config = Some(config);
        self
    }

    /// Build the model
    pub fn build(self) -> Result<TransformerModel> {
        let config = self
            .config
            .ok_or_else(|| OcrError::ModelNotFound("Configuration not provided".to_string()))?;

        Ok(TransformerModel::new(config))
    }
}

impl Default for TransformerModelBuilder {
    fn default() -> Self {
        Self::new()
    }
}