[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
- Only
HighConfidenceis implemented: InRust/lib/src/file.rs(FileType::convert), the score is always compared against the per-label high-confidenceconfig.threshold. medium_confidence_thresholdis not stored inRust/lib/src/model.rs:model_config.min.jsondefines"medium_confidence_threshold": 0.5alongside per-label"thresholds", andRust/gen/src/main.rsgeneratesRust/lib/src/model.rs. CurrentlyLabelConfigonly stores the per-label high-confidencethreshold: f32.
Expected Behavior (Parity with Python Magika)
Support PredictionMode (HighConfidence, MediumConfidence, BestGuess) when resolving (output_label, overwrite_reason) from (dl_label, score):
- Apply
overwrite_map:- Look up mapped
output_label(config.content_typeinCONFIG.labels). - If
output_label != dl_label, initialoverwrite_reasonisOverwriteReason::OverwriteMap.
- Look up mapped
- Apply confidence threshold based on
PredictionMode:PredictionMode::BestGuess: always keepoutput_labelandoverwrite_reasonfrom Step 1 (no minimum score check).PredictionMode::MediumConfidence: keepoutput_labelandoverwrite_reasonifscore >= medium_confidence_threshold(0.5).PredictionMode::HighConfidence: keepoutput_labelandoverwrite_reasonifscore >= config.threshold(the per-label threshold fordl_label).
- Fallback when score is below the required threshold:
- Replace
output_labelwithContentType::Txtifoutput_label.info().is_textistrue, elseContentType::Unknown. - Set
overwrite_reason = OverwriteReason::LowConfidence, except whendl_label == output_label(e.g. when rawdl_labelwas alreadytxtorunknown), in which caseoverwrite_reasonisNone.
- Replace
Proposed Rust API Changes
- Add
PredictionMode(HighConfidence,MediumConfidence,BestGuess) enum tomagika(Rust/lib). - Include
medium_confidence_threshold: f32inRust/lib/src/model.rs(viaRust/gen). - Allow configuring or passing
PredictionModeonSession/Builder(orFileType::convert) soRust/pyo3can delegatePredictionModethresholding tomagika-liband remove the Python-side thresholding workaround.
内容来源: google/magika