#1479·magika

[magika-lib] 支持 PredictionMode (HighConfidence、MediumConfidence、BestGuess)

作者: reyammer创建于 2026年9月18日更新于 2026年9月18日

Context

In the Python pyo3 bindings (Rust/pyo3 + Python/), we are replacing the legacy Python/ONNX Runtime inference and post-processing pipeline with magika-lib (Rust/lib).

Currently, magika-lib already computes (ContentType, OverwriteReason) in FileType::convert, but only supports HIGH_CONFIDENCE prediction mode (Rust/lib/src/lib.rs:147: // we only support high-confidence).

Current State in Rust/lib

  1. Only HighConfidence is implemented: In Rust/lib/src/file.rs (FileType::convert), the score is always compared against the per-label high-confidence config.threshold.
  2. medium_confidence_threshold is not stored in Rust/lib/src/model.rs: model_config.min.json defines "medium_confidence_threshold": 0.5 alongside per-label "thresholds", and Rust/gen/src/main.rs generates Rust/lib/src/model.rs. Currently LabelConfig only stores the per-label high-confidence threshold: f32.

Expected Behavior (Parity with Python Magika)

Support PredictionMode (HighConfidence, MediumConfidence, BestGuess) when resolving (output_label, overwrite_reason) from (dl_label, score):

  1. Apply overwrite_map:
    • Look up mapped output_label (config.content_type in CONFIG.labels).
    • If output_label != dl_label, initial overwrite_reason is OverwriteReason::OverwriteMap.
  2. Apply confidence threshold based on PredictionMode:
    • PredictionMode::BestGuess: always keep output_label and overwrite_reason from Step 1 (no minimum score check).
    • PredictionMode::MediumConfidence: keep output_label and overwrite_reason if score >= medium_confidence_threshold (0.5).
    • PredictionMode::HighConfidence: keep output_label and overwrite_reason if score >= config.threshold (the per-label threshold for dl_label).
  3. Fallback when score is below the required threshold:
    • Replace output_label with ContentType::Txt if output_label.info().is_text is true, else ContentType::Unknown.
    • Set overwrite_reason = OverwriteReason::LowConfidence, except when dl_label == output_label (e.g. when raw dl_label was already txt or unknown), in which case overwrite_reason is None.

Proposed Rust API Changes

  1. Add PredictionMode (HighConfidence, MediumConfidence, BestGuess) enum to magika (Rust/lib).
  2. Include medium_confidence_threshold: f32 in Rust/lib/src/model.rs (via Rust/gen).
  3. Allow configuring or passing PredictionMode on Session / Builder (or FileType::convert) so Rust/pyo3 can delegate PredictionMode thresholding to magika-lib and remove the Python-side thresholding workaround.