trustformers-models 0.1.1

Model implementations for TrustformeRS
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//! Model Cards for TrustformeRS
//!
//! This module provides a comprehensive model card system for documenting
//! model capabilities, limitations, ethical considerations, and usage guidelines.
//! Based on the Model Cards for Model Reporting framework by Mitchell et al.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Model card containing comprehensive model documentation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelCard {
    /// Model identification and basic information
    pub model_details: ModelDetails,
    /// Intended use cases and applications
    pub intended_use: IntendedUse,
    /// Factors affecting model performance
    pub factors: Factors,
    /// Evaluation metrics and performance data
    pub metrics: Metrics,
    /// Training and evaluation data information
    pub data: DataInfo,
    /// Ethical considerations and recommendations
    pub ethical_considerations: EthicalConsiderations,
    /// Caveats and recommendations
    pub caveats_and_recommendations: CaveatsAndRecommendations,
    /// Model card metadata
    pub metadata: ModelCardMetadata,
}

/// Model identification and basic information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelDetails {
    /// Model name
    pub name: String,
    /// Model version
    pub version: String,
    /// Model architecture (e.g., "transformer", "gpt", "bert")
    pub architecture: String,
    /// Model size information
    pub size: ModelSize,
    /// Brief description of the model
    pub description: String,
    /// Model authors/developers
    pub developers: Vec<String>,
    /// Organization/institution
    pub organization: Option<String>,
    /// Model license
    pub license: String,
    /// Publication date
    pub date: DateTime<Utc>,
    /// Related papers/publications
    pub papers: Vec<Publication>,
    /// Model repository/source
    pub repository: Option<String>,
    /// Contact information
    pub contact: Option<String>,
}

/// Model size information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelSize {
    /// Number of parameters
    pub parameters: u64,
    /// Model size on disk (in bytes)
    pub disk_size: u64,
    /// Memory requirements (in bytes)
    pub memory_requirements: u64,
    /// Inference time (milliseconds per token)
    pub inference_time_ms: Option<f64>,
}

/// Publication information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Publication {
    /// Paper title
    pub title: String,
    /// Authors
    pub authors: Vec<String>,
    /// Publication venue
    pub venue: Option<String>,
    /// Publication year
    pub year: u32,
    /// URL or DOI
    pub url: Option<String>,
}

/// Intended use cases and applications
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntendedUse {
    /// Primary use cases
    pub primary_uses: Vec<String>,
    /// Out-of-scope use cases
    pub out_of_scope_uses: Vec<String>,
    /// Target users
    pub target_users: Vec<String>,
    /// Supported languages
    pub languages: Vec<String>,
    /// Domains/fields of application
    pub domains: Vec<String>,
    /// Usage restrictions
    pub restrictions: Vec<String>,
}

/// Factors affecting model performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Factors {
    /// Relevant factors (demographic, environmental, technical)
    pub relevant_factors: Vec<Factor>,
    /// Evaluation factors
    pub evaluation_factors: Vec<String>,
}

/// Individual factor that may affect model performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Factor {
    /// Factor name
    pub name: String,
    /// Factor type (demographic, environmental, technical)
    pub factor_type: FactorType,
    /// Description of how this factor affects the model
    pub description: String,
    /// Impact level (low, medium, high)
    pub impact_level: ImpactLevel,
}

/// Types of factors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FactorType {
    /// Demographic factors (age, gender, race, etc.)
    Demographic,
    /// Environmental factors (lighting, noise, etc.)
    Environmental,
    /// Technical factors (hardware, software, etc.)
    Technical,
    /// Data factors (data quality, distribution, etc.)
    Data,
    /// Other factors
    Other(String),
}

/// Impact level of a factor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ImpactLevel {
    /// Low impact on model performance
    Low,
    /// Medium impact on model performance
    Medium,
    /// High impact on model performance
    High,
}

/// Evaluation metrics and performance data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Metrics {
    /// Model performance metrics
    pub performance_metrics: Vec<PerformanceMetric>,
    /// Decision thresholds
    pub decision_thresholds: Vec<DecisionThreshold>,
    /// Approaches to uncertainty and variability
    pub uncertainty_approaches: Vec<String>,
}

/// Individual performance metric
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetric {
    /// Metric name
    pub name: String,
    /// Metric type (accuracy, f1, perplexity, etc.)
    pub metric_type: MetricType,
    /// Metric value
    pub value: f64,
    /// Standard deviation or confidence interval
    pub uncertainty: Option<f64>,
    /// Dataset used for evaluation
    pub dataset: String,
    /// Subgroup or demographic breakdown
    pub subgroup: Option<String>,
    /// Additional context or notes
    pub notes: Option<String>,
}

/// Types of performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetricType {
    /// Accuracy metric
    Accuracy,
    /// Precision metric
    Precision,
    /// Recall metric
    Recall,
    /// F1 score
    F1Score,
    /// Perplexity (for language models)
    Perplexity,
    /// BLEU score (for translation/generation)
    BleuScore,
    /// ROUGE score (for summarization)
    RougeScore,
    /// Area under the curve
    Auc,
    /// Mean squared error
    Mse,
    /// Custom metric
    Custom(String),
}

/// Decision threshold information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecisionThreshold {
    /// Threshold name/description
    pub name: String,
    /// Threshold value
    pub value: f64,
    /// Rationale for this threshold
    pub rationale: String,
}

/// Training and evaluation data information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataInfo {
    /// Training datasets
    pub training_data: Vec<DatasetInfo>,
    /// Evaluation datasets
    pub evaluation_data: Vec<DatasetInfo>,
    /// Data preprocessing steps
    pub preprocessing: Vec<String>,
}

/// Information about a dataset
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetInfo {
    /// Dataset name
    pub name: String,
    /// Dataset description
    pub description: String,
    /// Dataset source/provider
    pub source: String,
    /// Dataset size (number of samples)
    pub size: u64,
    /// Data collection timeframe
    pub collection_timeframe: Option<String>,
    /// Data collection methodology
    pub collection_methodology: Option<String>,
    /// Known biases or limitations
    pub known_biases: Vec<String>,
    /// Data license
    pub license: Option<String>,
}

/// Ethical considerations and recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EthicalConsiderations {
    /// Sensitive use cases
    pub sensitive_use_cases: Vec<String>,
    /// Risks and harms
    pub risks_and_harms: Vec<RiskHarm>,
    /// Mitigation strategies
    pub mitigation_strategies: Vec<String>,
    /// Fairness considerations
    pub fairness_considerations: Vec<String>,
    /// Privacy considerations
    pub privacy_considerations: Vec<String>,
    /// Human oversight requirements
    pub human_oversight: Vec<String>,
}

/// Risk or harm information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskHarm {
    /// Risk/harm description
    pub description: String,
    /// Risk level (low, medium, high)
    pub risk_level: RiskLevel,
    /// Affected groups
    pub affected_groups: Vec<String>,
    /// Likelihood of occurrence
    pub likelihood: Likelihood,
    /// Potential impact
    pub potential_impact: String,
}

/// Risk level
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    /// Low risk
    Low,
    /// Medium risk
    Medium,
    /// High risk
    High,
    /// Critical risk
    Critical,
}

/// Likelihood of risk occurrence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Likelihood {
    /// Very unlikely
    VeryUnlikely,
    /// Unlikely
    Unlikely,
    /// Possible
    Possible,
    /// Likely
    Likely,
    /// Very likely
    VeryLikely,
}

/// Caveats and recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaveatsAndRecommendations {
    /// Model limitations
    pub limitations: Vec<String>,
    /// Usage recommendations
    pub recommendations: Vec<String>,
    /// Ideal assessment approaches
    pub ideal_assessment: Vec<String>,
    /// Additional information
    pub additional_information: Option<String>,
}

/// Model card metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelCardMetadata {
    /// Model card version
    pub version: String,
    /// Creation date
    pub created_date: DateTime<Utc>,
    /// Last updated date
    pub updated_date: DateTime<Utc>,
    /// Authors of the model card
    pub authors: Vec<String>,
    /// Review status
    pub review_status: ReviewStatus,
    /// Additional metadata
    pub additional_metadata: HashMap<String, String>,
}

/// Review status of the model card
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReviewStatus {
    /// Draft version
    Draft,
    /// Under review
    UnderReview,
    /// Approved
    Approved,
    /// Needs revision
    NeedsRevision,
}

/// Model card builder for easy construction
#[derive(Debug)]
pub struct ModelCardBuilder {
    card: ModelCard,
}

impl ModelCardBuilder {
    /// Create a new model card builder
    pub fn new(name: String, version: String, architecture: String) -> Self {
        let now = Utc::now();
        Self {
            card: ModelCard {
                model_details: ModelDetails {
                    name,
                    version,
                    architecture,
                    size: ModelSize {
                        parameters: 0,
                        disk_size: 0,
                        memory_requirements: 0,
                        inference_time_ms: None,
                    },
                    description: String::new(),
                    developers: Vec::new(),
                    organization: None,
                    license: "Unknown".to_string(),
                    date: now,
                    papers: Vec::new(),
                    repository: None,
                    contact: None,
                },
                intended_use: IntendedUse {
                    primary_uses: Vec::new(),
                    out_of_scope_uses: Vec::new(),
                    target_users: Vec::new(),
                    languages: Vec::new(),
                    domains: Vec::new(),
                    restrictions: Vec::new(),
                },
                factors: Factors {
                    relevant_factors: Vec::new(),
                    evaluation_factors: Vec::new(),
                },
                metrics: Metrics {
                    performance_metrics: Vec::new(),
                    decision_thresholds: Vec::new(),
                    uncertainty_approaches: Vec::new(),
                },
                data: DataInfo {
                    training_data: Vec::new(),
                    evaluation_data: Vec::new(),
                    preprocessing: Vec::new(),
                },
                ethical_considerations: EthicalConsiderations {
                    sensitive_use_cases: Vec::new(),
                    risks_and_harms: Vec::new(),
                    mitigation_strategies: Vec::new(),
                    fairness_considerations: Vec::new(),
                    privacy_considerations: Vec::new(),
                    human_oversight: Vec::new(),
                },
                caveats_and_recommendations: CaveatsAndRecommendations {
                    limitations: Vec::new(),
                    recommendations: Vec::new(),
                    ideal_assessment: Vec::new(),
                    additional_information: None,
                },
                metadata: ModelCardMetadata {
                    version: "1.0".to_string(),
                    created_date: now,
                    updated_date: now,
                    authors: Vec::new(),
                    review_status: ReviewStatus::Draft,
                    additional_metadata: HashMap::new(),
                },
            },
        }
    }

    /// Set model description
    pub fn description(mut self, description: String) -> Self {
        self.card.model_details.description = description;
        self
    }

    /// Add developer
    pub fn developer(mut self, developer: String) -> Self {
        self.card.model_details.developers.push(developer);
        self
    }

    /// Set organization
    pub fn organization(mut self, organization: String) -> Self {
        self.card.model_details.organization = Some(organization);
        self
    }

    /// Set license
    pub fn license(mut self, license: String) -> Self {
        self.card.model_details.license = license;
        self
    }

    /// Set model size
    pub fn size(mut self, parameters: u64, disk_size: u64, memory_requirements: u64) -> Self {
        self.card.model_details.size = ModelSize {
            parameters,
            disk_size,
            memory_requirements,
            inference_time_ms: None,
        };
        self
    }

    /// Add primary use case
    pub fn primary_use(mut self, use_case: String) -> Self {
        self.card.intended_use.primary_uses.push(use_case);
        self
    }

    /// Add out-of-scope use case
    pub fn out_of_scope_use(mut self, use_case: String) -> Self {
        self.card.intended_use.out_of_scope_uses.push(use_case);
        self
    }

    /// Add performance metric
    pub fn metric(mut self, metric: PerformanceMetric) -> Self {
        self.card.metrics.performance_metrics.push(metric);
        self
    }

    /// Add training dataset
    pub fn training_dataset(mut self, dataset: DatasetInfo) -> Self {
        self.card.data.training_data.push(dataset);
        self
    }

    /// Add ethical consideration
    pub fn risk(mut self, risk: RiskHarm) -> Self {
        self.card.ethical_considerations.risks_and_harms.push(risk);
        self
    }

    /// Add limitation
    pub fn limitation(mut self, limitation: String) -> Self {
        self.card.caveats_and_recommendations.limitations.push(limitation);
        self
    }

    /// Add recommendation
    pub fn recommendation(mut self, recommendation: String) -> Self {
        self.card.caveats_and_recommendations.recommendations.push(recommendation);
        self
    }

    /// Build the model card
    pub fn build(self) -> ModelCard {
        self.card
    }
}

impl ModelCard {
    /// Create a new model card builder
    pub fn builder(name: String, version: String, architecture: String) -> ModelCardBuilder {
        ModelCardBuilder::new(name, version, architecture)
    }

    /// Export model card to JSON
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Export model card to YAML
    pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(self)
    }

    /// Import model card from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }

    /// Import model card from YAML
    pub fn from_yaml(yaml: &str) -> Result<Self, serde_yaml::Error> {
        serde_yaml::from_str(yaml)
    }

    /// Generate HTML representation
    pub fn to_html(&self) -> String {
        let mut html = String::new();

        html.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
        html.push_str(&format!(
            "<title>Model Card: {}</title>\n",
            self.model_details.name
        ));
        html.push_str("<style>\n");
        html.push_str(include_str!("../assets/model_card.css"));
        html.push_str("</style>\n</head>\n<body>\n");

        // Header
        html.push_str(&format!(
            "<h1>Model Card: {}</h1>\n",
            self.model_details.name
        ));
        html.push_str(&format!(
            "<p class='version'>Version: {}</p>\n",
            self.model_details.version
        ));

        // Model Details
        html.push_str("<h2>Model Details</h2>\n");
        html.push_str(&format!(
            "<p><strong>Architecture:</strong> {}</p>\n",
            self.model_details.architecture
        ));
        html.push_str(&format!(
            "<p><strong>Parameters:</strong> {}</p>\n",
            self.model_details.size.parameters
        ));
        html.push_str(&format!(
            "<p><strong>Description:</strong> {}</p>\n",
            self.model_details.description
        ));
        html.push_str(&format!(
            "<p><strong>License:</strong> {}</p>\n",
            self.model_details.license
        ));

        // Intended Use
        html.push_str("<h2>Intended Use</h2>\n");
        if !self.intended_use.primary_uses.is_empty() {
            html.push_str("<h3>Primary Uses</h3>\n<ul>\n");
            for use_case in &self.intended_use.primary_uses {
                html.push_str(&format!("<li>{}</li>\n", use_case));
            }
            html.push_str("</ul>\n");
        }

        if !self.intended_use.out_of_scope_uses.is_empty() {
            html.push_str("<h3>Out-of-Scope Uses</h3>\n<ul>\n");
            for use_case in &self.intended_use.out_of_scope_uses {
                html.push_str(&format!("<li>{}</li>\n", use_case));
            }
            html.push_str("</ul>\n");
        }

        // Performance Metrics
        if !self.metrics.performance_metrics.is_empty() {
            html.push_str("<h2>Performance Metrics</h2>\n");
            html.push_str("<table>\n<tr><th>Metric</th><th>Value</th><th>Dataset</th></tr>\n");
            for metric in &self.metrics.performance_metrics {
                html.push_str(&format!(
                    "<tr><td>{}</td><td>{:.4}</td><td>{}</td></tr>\n",
                    metric.name, metric.value, metric.dataset
                ));
            }
            html.push_str("</table>\n");
        }

        // Ethical Considerations
        if !self.ethical_considerations.risks_and_harms.is_empty() {
            html.push_str("<h2>Ethical Considerations</h2>\n");
            html.push_str("<h3>Risks and Harms</h3>\n");
            for risk in &self.ethical_considerations.risks_and_harms {
                html.push_str(&format!(
                    "<div class='risk risk-{:?}'><strong>{:?} Risk:</strong> {}</div>\n",
                    risk.risk_level, risk.risk_level, risk.description
                ));
            }
        }

        // Limitations
        if !self.caveats_and_recommendations.limitations.is_empty() {
            html.push_str("<h2>Limitations</h2>\n<ul>\n");
            for limitation in &self.caveats_and_recommendations.limitations {
                html.push_str(&format!("<li>{}</li>\n", limitation));
            }
            html.push_str("</ul>\n");
        }

        html.push_str("</body>\n</html>");
        html
    }

    /// Validate the model card for completeness
    pub fn validate(&self) -> ValidationReport {
        let mut report = ValidationReport::new();

        // Check required fields
        if self.model_details.name.is_empty() {
            report.add_error("Model name is required".to_string());
        }

        if self.model_details.description.is_empty() {
            report.add_warning("Model description should be provided".to_string());
        }

        if self.intended_use.primary_uses.is_empty() {
            report.add_warning("Primary use cases should be specified".to_string());
        }

        if self.metrics.performance_metrics.is_empty() {
            report.add_warning("Performance metrics should be provided".to_string());
        }

        if self.ethical_considerations.risks_and_harms.is_empty() {
            report.add_warning("Ethical considerations should be documented".to_string());
        }

        report
    }
}

/// Validation report for model cards
#[derive(Debug, Clone)]
pub struct ValidationReport {
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

impl ValidationReport {
    fn new() -> Self {
        Self {
            errors: Vec::new(),
            warnings: Vec::new(),
        }
    }

    fn add_error(&mut self, error: String) {
        self.errors.push(error);
    }

    fn add_warning(&mut self, warning: String) {
        self.warnings.push(warning);
    }

    pub fn is_valid(&self) -> bool {
        self.errors.is_empty()
    }

    pub fn has_warnings(&self) -> bool {
        !self.warnings.is_empty()
    }
}

/// Predefined model card templates for common architectures
pub struct ModelCardTemplates;

impl ModelCardTemplates {
    /// Create a template for language models
    pub fn language_model(name: String, version: String) -> ModelCardBuilder {
        ModelCard::builder(name, version, "transformer".to_string())
            .primary_use("Text generation".to_string())
            .primary_use("Language understanding".to_string())
            .out_of_scope_use("Medical diagnosis".to_string())
            .out_of_scope_use("Legal advice".to_string())
            .limitation("May generate biased or inappropriate content".to_string())
            .limitation("Performance may vary across languages and domains".to_string())
            .recommendation("Use appropriate content filtering".to_string())
            .recommendation("Evaluate on your specific use case".to_string())
    }

    /// Create a template for vision models
    pub fn vision_model(name: String, version: String) -> ModelCardBuilder {
        ModelCard::builder(name, version, "cnn".to_string())
            .primary_use("Image classification".to_string())
            .primary_use("Computer vision tasks".to_string())
            .out_of_scope_use("Medical imaging diagnosis".to_string())
            .limitation("Performance may vary with image quality".to_string())
            .limitation("May exhibit bias across demographic groups".to_string())
            .recommendation("Validate on diverse test sets".to_string())
    }

    /// Create a template for multimodal models
    pub fn multimodal_model(name: String, version: String) -> ModelCardBuilder {
        ModelCard::builder(name, version, "multimodal_transformer".to_string())
            .primary_use("Vision-language understanding".to_string())
            .primary_use("Multimodal reasoning".to_string())
            .out_of_scope_use("Critical decision making".to_string())
            .limitation("May have alignment issues between modalities".to_string())
            .recommendation("Test on multimodal benchmarks".to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_model_card_builder() {
        let card = ModelCard::builder(
            "TestModel".to_string(),
            "1.0".to_string(),
            "transformer".to_string(),
        )
        .description("A test model".to_string())
        .developer("Test Developer".to_string())
        .license("MIT".to_string())
        .size(1000000, 500000000, 1000000000)
        .primary_use("Testing".to_string())
        .limitation("This is just a test".to_string())
        .build();

        assert_eq!(card.model_details.name, "TestModel");
        assert_eq!(card.model_details.version, "1.0");
        assert_eq!(card.model_details.architecture, "transformer");
        assert_eq!(card.model_details.description, "A test model");
        assert_eq!(card.model_details.size.parameters, 1000000);
        assert!(card.intended_use.primary_uses.contains(&"Testing".to_string()));
        assert!(card
            .caveats_and_recommendations
            .limitations
            .contains(&"This is just a test".to_string()));
    }

    #[test]
    fn test_model_card_serialization() {
        let card = ModelCard::builder(
            "SerializationTest".to_string(),
            "1.0".to_string(),
            "transformer".to_string(),
        )
        .build();

        let json = card.to_json().expect("operation failed");
        let deserialized_card = ModelCard::from_json(&json).expect("operation failed");

        assert_eq!(
            card.model_details.name,
            deserialized_card.model_details.name
        );
        assert_eq!(
            card.model_details.version,
            deserialized_card.model_details.version
        );
    }

    #[test]
    fn test_model_card_validation() {
        let empty_card =
            ModelCard::builder(String::new(), "1.0".to_string(), "transformer".to_string()).build();

        let report = empty_card.validate();
        assert!(!report.is_valid());
        assert!(!report.errors.is_empty());

        let complete_card = ModelCard::builder(
            "CompleteModel".to_string(),
            "1.0".to_string(),
            "transformer".to_string(),
        )
        .description("A complete model".to_string())
        .primary_use("Testing".to_string())
        .build();

        let report = complete_card.validate();
        assert!(report.is_valid());
    }

    #[test]
    fn test_model_card_templates() {
        let lang_model =
            ModelCardTemplates::language_model("GPT-Test".to_string(), "1.0".to_string()).build();

        assert_eq!(lang_model.model_details.architecture, "transformer");
        assert!(lang_model.intended_use.primary_uses.contains(&"Text generation".to_string()));
        assert!(lang_model
            .intended_use
            .out_of_scope_uses
            .contains(&"Medical diagnosis".to_string()));

        let vision_model =
            ModelCardTemplates::vision_model("ResNet-Test".to_string(), "1.0".to_string()).build();

        assert_eq!(vision_model.model_details.architecture, "cnn");
        assert!(vision_model
            .intended_use
            .primary_uses
            .contains(&"Image classification".to_string()));
    }

    #[test]
    fn test_performance_metric() {
        let metric = PerformanceMetric {
            name: "Accuracy".to_string(),
            metric_type: MetricType::Accuracy,
            value: 0.95,
            uncertainty: Some(0.02),
            dataset: "Test Dataset".to_string(),
            subgroup: None,
            notes: None,
        };

        assert_eq!(metric.name, "Accuracy");
        assert_eq!(metric.value, 0.95);
        assert_eq!(metric.uncertainty, Some(0.02));
    }

    #[test]
    fn test_risk_harm() {
        let risk = RiskHarm {
            description: "Potential bias in outputs".to_string(),
            risk_level: RiskLevel::Medium,
            affected_groups: vec!["Minority groups".to_string()],
            likelihood: Likelihood::Possible,
            potential_impact: "Unfair treatment".to_string(),
        };

        assert_eq!(risk.description, "Potential bias in outputs");
        assert!(matches!(risk.risk_level, RiskLevel::Medium));
        assert!(matches!(risk.likelihood, Likelihood::Possible));
    }

    #[test]
    fn test_html_generation() {
        let card = ModelCard::builder(
            "HTMLTest".to_string(),
            "1.0".to_string(),
            "transformer".to_string(),
        )
        .description("A test model for HTML generation".to_string())
        .primary_use("Testing HTML output".to_string())
        .build();

        let html = card.to_html();
        assert!(html.contains("Model Card: HTMLTest"));
        assert!(html.contains("Version: 1.0"));
        assert!(html.contains("A test model for HTML generation"));
        assert!(html.contains("Testing HTML output"));
    }
}