Handles lifecycle of ML models on device: download, import, validation, listing, deletion, and model instantiation.
filesDir/models/
suspend fun downloadModelInternal(
context: Context,
modelInfo: ModelInfo,
onProgress: ((Int) -> Unit)? = null
): Result<File>Downloads a model into internal storage.
- Streams file from network to local storage
- Reports progress (0–100) if available
- Uses temporary file then atomic rename
- Returns final model file
fun downloadModelExternal(context: Context, url: String)Opens external browser for model download URL.
suspend fun importModel(context: Context, modelInfo: ModelInfo, uri: Uri)Imports a model from a URI into internal storage.
- Copies URI content into model directory
- Replaces existing model if present
fun modelExists(context: Context, model: ModelName): BooleanChecks whether a model is fully available locally.
fun deleteModel(context: Context, modelInfo: ModelInfo): BooleanDeletes a model from internal storage.
fun listModels(context: Context, type: ModelType? = null): List<ModelName>Returns installed models, optionally filtered by type.
fun getModelFile(context: Context, modelInfo: ModelInfo): FileResolves model path:
filesDir/models/<modelInfo.path>
Core network download utility.
- Streams HTTP response to local file
- Writes to temp file first
- Reports progress if content length is known
- Replaces final file atomically
fun getTextEmbedder(context: Context, modelName: ModelName): TextEmbeddingProviderReturns initialized text embedding model.
Supported models:
ALL_MINILM_L6_V2CLIP_VIT_B_32_TEXT
Throws ModelNotDownloaded if missing.
fun getImageEmbedder(context: Context, modelName: ModelName): ImageEmbeddingProviderSupported models:
DINOV2_SMALLCLIP_VIT_B_32_IMAGEINCEPTION_RESNET_V1
Throws ModelNotDownloaded if missing.
fun getObjectDetector(context: Context, modelName: ModelName): DetectorProviderSupported models:
ULTRA_LIGHT_FACE_DETECTOR
Throws ModelNotDownloaded if missing.
- Internal storage only (
filesDir/models) - Atomic file replacement for safety
- Progress-aware streaming downloads
- Model validation enforced before instantiation
- Strict model-type matching for provider factories