Repository: xenova/transformers.js
File: packages/transformers/src/env.js (JavaScript)

Code before:
 * env.logLevel = LogLevel.INFO;
 *
 * // Disable all logging
 * env.logLevel = LogLevel.NONE;
 *
 * @readonly
 * @enum {number}
 * @property {0} NONE - No logging output
 * @property {1} ERROR - Only error messages (value: 1)
 * @property {2} WARNING - Errors and warnings (value: 2)
 * @property {3} INFO - Errors, warnings, and info messages (value: 3)
 * @property {4} DEBUG - All messages including debug output (value: 4)
 */
export const LogLevel = Object.freeze({
    /** No logging output (value: 0) */
    NONE: 0,
    /** Only error messages (value: 1) */
    ERROR: 1,
    /** Errors and warnings (value: 2) */
    WARNING: 2,
    /** Errors, warnings, and info messages (value: 3) */
    INFO: 3,
    /** All messages including debug output (value: 4) */
    DEBUG: 4,
});

/**
 * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js.
 * @typedef {Object} TransformersEnvironment
 * @property {string} version This version of Transformers.js.
 * @property {{onnx: Partial<import('onnxruntime-common').Env>}} backends Expose environment variables of different backends,
 * allowing users to set these variables if they want to.
 * @property {number} logLevel The logging level. Use LogLevel enum values. Defaults to LogLevel.WARNING.
 * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`.
 * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc.
 * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub.
 * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models.
 * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise.
 * If set to `false`, it will skip the local file check and try to load the model from the remote host.
 * @property {string} localModelPath Path to load local models from. Defaults to `/models/`.
 * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available.
 * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available.
 * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available.
 * @property {string|null} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
 * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
 * @property {import('./utils/cache.js').CacheInterface|null} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
 * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache.
 * @property {boolean} useWasmCache Whether to pre-load and cache WASM binaries for ONNX Runtime. Defaults to `true` when cache is available.
 * This can improve performance by avoiding repeated downloads of WASM files. Note: Only the WASM binary is cached.
 * The MJS loader file still requires network access unless you use a Service Worker.
 * @property {string} cacheKey The cache key to use for storing models and WASM binaries. Defaults to 'transformers-cache'.

Review comment (bug):
Probably a nit because this won't be visible externally, but we should probably align with how the python logging library does it: https://docs.python.org/3/library/logging.html#logging-levels

(which I think is standard across many other libraries I've seen... and for onnxruntime)

<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Level</p></th>
<th class="head"><p>Numeric value</p></th>
<th class="head"><p>What it means / When to use it</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.NOTSET">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">NOTSET</span></span><a class="headerlink" href="#logging.NOTSET" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>0</p></td>
<td><p>When set on a logger, indicates that
ancestor loggers are to be consulted
to determine the effective level.
If that still resolves to
<code class="xref py py-const docutils literal notranslate"><span class="pre">NOTSET</span></code>, then all events
are logged. When set on a handler,
all events are handled.</p></td>
</tr>
<tr class="row-odd"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.DEBUG">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">DEBUG</span></span><a class="headerlink" href="#logging.DEBUG" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>10</p></td>
<td><p>Detailed information, typically only
of interest to a developer trying to
diagnose a problem.</p></td>
</tr>
<tr class="row-even"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.INFO">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">INFO</span></span><a class="headerlink" href="#logging.INFO" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>20</p></td>
<td><p>Confirmation that things are working
as expected.</p></td>
</tr>
<tr class="row-odd"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.WARNING">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">WARNING</span></span><a class="headerlink" href="#logging.WARNING" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>30</p></td>
<td><p>An indication that something
unexpected happened, or that a
problem might occur in the near
future (e.g. ‘disk space low’). The
software is still working as
expected.</p></td>
</tr>
<tr class="row-even"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.ERROR">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">ERROR</span></span><a class="headerlink" href="#logging.ERROR" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>40</p></td>
<td><p>Due to a more serious problem, the
software has not been able to
perform some function.</p></td>
</tr>
<tr class="row-odd"><td><dl class="py data">
<dt class="sig sig-object py" id="logging.CRITICAL">
<span class="sig-prename descclassname"><span class="pre">logging.</span></span><span class="sig-name descname"><span class="pre">CRITICAL</span></span><a class="headerlink" href="#logging.CRITICAL" title="Link to this definition">¶</a></dt>
<dd></dd></dl>

</td>
<td><p>50</p></td>
<td><p>A serious error, indicating that the
program itself may be unable to
continue running.</p></td>
</tr>
</tbody>
</table>

Code after:
 * env.logLevel = LogLevel.INFO;
 *
 * // Disable all logging
 * env.logLevel = LogLevel.NONE;
 *
 */
export const LogLevel = Object.freeze({
    /** All messages including debug output (value: 10) */
    DEBUG: 10,
    /** Errors, warnings, and info messages (value: 20) */
    INFO: 20,
    /** Errors and warnings (value: 30) */
    WARNING: 30,
    /** Only error messages (value: 40) */
    ERROR: 40,
    /** No logging output (value: 50) */
    NONE: 50,
});

/**
 * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js.
 * @typedef {Object} TransformersEnvironment
 * @property {string} version This version of Transformers.js.
 * @property {{onnx: Partial<import('onnxruntime-common').Env>}} backends Expose environment variables of different backends,
 * allowing users to set these variables if they want to.
 * @property {number} logLevel The logging level. Use LogLevel enum values. Defaults to LogLevel.ERROR.
 * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`.
 * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc.
 * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub.
 * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models.
 * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise.
 * If set to `false`, it will skip the local file check and try to load the model from the remote host.
 * @property {string} localModelPath Path to load local models from. Defaults to `/models/`.
 * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available.
 * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available.
 * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available.
 * @property {string|null} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`.
 * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`.
 * @property {import('./utils/cache.js').CacheInterface|null} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which
 * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache.
 * @property {boolean} useWasmCache Whether to pre-load and cache WASM binaries for ONNX Runtime. Defaults to `true` when cache is available.
 * This can improve performance by avoiding repeated downloads of WASM files. Note: Only the WASM binary is cached.
 * The MJS loader file still requires network access unless you use a Service Worker.
 * @property {string} cacheKey The cache key to use for storing models and WASM binaries. Defaults to 'transformers-cache'.
 */

/** @type {TransformersEnvironment} */
export const env = {
    version: VERSION,

    /////////////////// Backends settings ///////////////////