Overview
The model provider system allows flexible model loading from multiple sources: local paths, memory blobs, or Hugging Face Hub.
RettoWorkerModelProvider
Specifies the source for detection, classification, and recognition models.
Type Definition
pub struct RettoWorkerModelProvider {
pub det: RettoWorkerModelSource,
pub rec: RettoWorkerModelSource,
pub cls: RettoWorkerModelSource,
}
Source: retto-core/src/worker.rs:61
Fields
Source for the text detection model (PP-OCRv4 det)
Source for the text recognition model (PP-OCRv4 rec)
Source for the text classification/angle model (mobile v2.0 cls)
RettoWorkerModelSource
Enum defining where to load a model from.
Type Definition
pub enum RettoWorkerModelSource {
#[cfg(not(target_family = "wasm"))]
Path(String),
Blob(Vec<u8>),
#[cfg(feature = "hf-hub")]
HuggingFace {
repo: String,
model: String,
},
}
Source: retto-core/src/worker.rs:18
Variants
Load model from a local file pathNot available on WASM targets
Load model from an in-memory byte arrayUseful for embedded models or WASM deployments
Download model from Hugging Face HubRequires hf-hub feature flag
repo: Repository identifier (e.g., “pk5ls20/PaddleModel”)
model: Path to model file within the repo
Examples
use retto_core::prelude::*;
let source = RettoWorkerModelSource::Path(
"ch_PP-OCRv4_det_infer.onnx".to_string()
);
RettoWorkerModelProviderBuilder
Trait providing convenience methods for creating model providers with default configurations.
Type Definition
pub trait RettoWorkerModelProviderBuilder: Debug + Clone {
#[cfg(all(not(target_family = "wasm"), feature = "hf-hub"))]
fn from_hf_hub_v4_default() -> Self;
#[cfg(not(target_family = "wasm"))]
fn from_local_v4_path_default() -> Self;
fn from_local_v4_blob_default() -> Self;
fn default_provider() -> Self;
}
Source: retto-core/src/worker.rs:75
Methods
from_hf_hub_v4_default()
Creates a provider that downloads PaddleOCR v4 models from Hugging Face Hub.
fn from_hf_hub_v4_default() -> Self
Availability: Non-WASM targets with hf-hub feature
Default Repository: pk5ls20/PaddleModel
Models:
- Detection:
retto/onnx/ch_PP-OCRv4_det_infer.onnx
- Recognition:
retto/onnx/ch_PP-OCRv4_rec_infer.onnx
- Classification:
retto/onnx/ch_ppocr_mobile_v2.0_cls_infer.onnx
Example:
use retto_core::prelude::*;
let models = RettoOrtWorkerModelProvider::from_hf_hub_v4_default();
Source: retto-core/src/worker/ort_worker.rs:60
from_local_v4_path_default()
Creates a provider that loads models from local files with default filenames.
fn from_local_v4_path_default() -> Self
Availability: Non-WASM targets
Default Filenames:
- Detection:
ch_PP-OCRv4_det_infer.onnx
- Recognition:
ch_PP-OCRv4_rec_infer.onnx
- Classification:
ch_ppocr_mobile_v2.0_cls_infer.onnx
Example:
// Expects model files in current directory
let models = RettoOrtWorkerModelProvider::from_local_v4_path_default();
Source: retto-core/src/worker/ort_worker.rs:79
from_local_v4_blob_default()
Creates a provider with embedded model blobs (if compiled with download-models feature).
fn from_local_v4_blob_default() -> Self
Availability: All targets
Behavior:
- With
download-models feature: Includes embedded model files
- Without feature: Returns empty blobs (will error at runtime)
Example:
// Use embedded models
let models = RettoOrtWorkerModelProvider::from_local_v4_blob_default();
Source: retto-core/src/worker/ort_worker.rs:88
default_provider()
Selects the most appropriate provider based on compilation target and features.
fn default_provider() -> Self
Selection Logic:
- Non-WASM with
hf-hub: Uses from_hf_hub_v4_default()
- Non-WASM without
hf-hub: Uses from_local_v4_path_default()
- WASM: Uses
from_local_v4_blob_default()
Example:
let models = RettoOrtWorkerModelProvider::default_provider();
Usage Examples
Loading from Hugging Face Hub
use retto_core::prelude::*;
let config = RettoOrtWorkerConfig {
device: RettoOrtWorkerDevice::CPU,
models: RettoOrtWorkerModelProvider::from_hf_hub_v4_default(),
};
let worker = RettoOrtWorker::new(config)?;
Loading from Local Files
let models = RettoOrtWorkerModelProvider(RettoWorkerModelProvider {
det: RettoWorkerModelSource::Path("models/det.onnx".into()),
rec: RettoWorkerModelSource::Path("models/rec.onnx".into()),
cls: RettoWorkerModelSource::Path("models/cls.onnx".into()),
});
let config = RettoOrtWorkerConfig {
device: RettoOrtWorkerDevice::CPU,
models,
};
Loading from Memory
let det_bytes = include_bytes!("../models/det.onnx").to_vec();
let rec_bytes = include_bytes!("../models/rec.onnx").to_vec();
let cls_bytes = include_bytes!("../models/cls.onnx").to_vec();
let models = RettoOrtWorkerModelProvider(RettoWorkerModelProvider {
det: RettoWorkerModelSource::Blob(det_bytes),
rec: RettoWorkerModelSource::Blob(rec_bytes),
cls: RettoWorkerModelSource::Blob(cls_bytes),
});
Custom Hugging Face Repository
let models = RettoOrtWorkerModelProvider(RettoWorkerModelProvider {
det: RettoWorkerModelSource::HuggingFace {
repo: "my-org/my-models".to_string(),
model: "path/to/det_model.onnx".to_string(),
},
rec: RettoWorkerModelSource::HuggingFace {
repo: "my-org/my-models".to_string(),
model: "path/to/rec_model.onnx".to_string(),
},
cls: RettoWorkerModelSource::HuggingFace {
repo: "my-org/my-models".to_string(),
model: "path/to/cls_model.onnx".to_string(),
},
});
Models from Hugging Face Hub are automatically cached locally after the first download.
When using Path variant, ensure the file exists before creating the worker, or you’ll receive a ModelNotFoundError.