This module provides utilities for working with model paths in asset bundles, file systems, and Play Asset Delivery.
assetModelPath()
Create a model path configuration for models bundled in your app’s assets.
function assetModelPath(assetPath: string): ModelPathConfig
Parameters
Path relative to assets directory (e.g., "models/sherpa-onnx-whisper-tiny").
Returns
Model path configuration with type 'asset'.
Example
import { assetModelPath, createSTT } from 'react-native-sherpa-onnx/stt';
const stt = await createSTT({
modelPath: assetModelPath('models/whisper-tiny-en'),
});
fileModelPath()
Create a model path configuration for models in the file system.
function fileModelPath(filePath: string): ModelPathConfig
Parameters
Absolute path to model directory. On iOS, use an absolute path from react-native-fs:DocumentDirectoryPath + '/models/model-name'
Returns
Model path configuration with type 'file'.
Example
import { fileModelPath, createSTT } from 'react-native-sherpa-onnx/stt';
import { DocumentDirectoryPath } from '@dr.pogodin/react-native-fs';
const modelPath = fileModelPath(
`${DocumentDirectoryPath}/models/sherpa-onnx-whisper-tiny`
);
const stt = await createSTT({ modelPath });
autoModelPath()
Create a model path configuration with automatic path type detection.
function autoModelPath(path: string): ModelPathConfig
Tries asset path first, then falls back to file system path.
Parameters
Path to check (will be tried as both asset and file path).
Returns
Model path configuration with type 'auto'.
Example
import { autoModelPath, createSTT } from 'react-native-sherpa-onnx/stt';
const stt = await createSTT({
modelPath: autoModelPath('models/whisper-tiny'),
});
resolveModelPath()
Resolve a model path configuration to a platform-specific absolute path.
function resolveModelPath(config: ModelPathConfig): Promise<string>
This handles different path types (asset, file, auto) and returns an absolute path that can be used by native code.
Parameters
Model path configuration from assetModelPath(), fileModelPath(), or autoModelPath().
Returns
Absolute path usable by native code.
Example
import { resolveModelPath, assetModelPath } from 'react-native-sherpa-onnx';
const config = assetModelPath('models/whisper-tiny');
const absolutePath = await resolveModelPath(config);
console.log(absolutePath);
// iOS: "/path/to/app.app/models/whisper-tiny"
// Android: "models/whisper-tiny" (asset path)
listAssetModels()
List all model folders in the assets/models directory.
function listAssetModels(): Promise<Array<{
folder: string;
hint: 'stt' | 'tts' | 'unknown';
}>>
Scans the platform-specific model directory and returns folder names with type hints.
Returns
Promise<Array<ModelInfo>>
Array of model information objects.Show ModelInfo properties
hint
'stt' | 'tts' | 'unknown'
Detected model type hint based on folder name patterns
Example
import { listAssetModels } from 'react-native-sherpa-onnx';
const models = await listAssetModels();
console.log('Available models:', models);
// [
// { folder: 'sherpa-onnx-streaming-zipformer-en-2023-06-26', hint: 'stt' },
// { folder: 'vits-piper-en_US-lessac-medium', hint: 'tts' }
// ]
listModelsAtPath()
List model folders under a specific filesystem path.
function listModelsAtPath(
path: string,
recursive?: boolean
): Promise<Array<{
folder: string;
hint: 'stt' | 'tts' | 'unknown';
}>>
Parameters
Absolute filesystem path to scan for models.
When true, returns relative folder paths under the base path recursively.
Returns
Promise<Array<ModelInfo>>
Example
import { listModelsAtPath } from 'react-native-sherpa-onnx';
import { DocumentDirectoryPath } from '@dr.pogodin/react-native-fs';
const modelsPath = `${DocumentDirectoryPath}/sherpa-onnx/models`;
const models = await listModelsAtPath(modelsPath, true);
for (const model of models) {
console.log(`Found: ${model.folder} (${model.hint})`);
}
getAssetPackPath()
Android Only: Get the path to models directory inside an Android asset pack delivered via Play Asset Delivery (PAD).
function getAssetPackPath(packName: string): Promise<string | null>
Parameters
Name of the asset pack (e.g., "sherpa_models").
Returns
Path to the models directory in the asset pack, or null if the pack is not available.
- Android: Full support with Play Asset Delivery
- iOS: Always returns
null
Example
import { getAssetPackPath, listModelsAtPath } from 'react-native-sherpa-onnx';
const packPath = await getAssetPackPath('sherpa_models');
if (packPath) {
console.log('Asset pack path:', packPath);
const models = await listModelsAtPath(packPath);
console.log('Models in pack:', models);
}
getDefaultModelPath()
Get the default model directory path for the current platform.
function getDefaultModelPath(): string
Returns a logical name (not an absolute path). On iOS, use absolute paths with react-native-fs instead.
Returns
- iOS:
"Documents/models"
- Android:
"models"
Example
import { getDefaultModelPath } from 'react-native-sherpa-onnx';
const defaultPath = getDefaultModelPath();
console.log(defaultPath);
// iOS: "Documents/models"
// Android: "models"
Types
ModelPathConfig
Model path configuration discriminated union.
type ModelPathConfig =
| { type: 'asset'; path: string }
| { type: 'file'; path: string }
| { type: 'auto'; path: string };
type
'asset' | 'file' | 'auto'
required
'asset': Model is bundled in app assets
'file': Model is in file system (absolute path)
'auto': Automatically detect (tries asset first, then file)
Path to the model directory.
See Also