Fix ES6 imports

This commit is contained in:
Joshua Lochner 2023-04-24 05:28:24 +02:00
parent f25b17c91a
commit 1a90c6e553
6 changed files with 16 additions and 28 deletions

View File

@ -5,9 +5,7 @@ import { fileURLToPath } from 'url';
import { ONNX } from './backends/onnx.js'; import { ONNX } from './backends/onnx.js';
const { env: onnx_env } = ONNX; const { env: onnx_env } = ONNX;
const __dirname = path.dirname(path.dirname(fileURLToPath(import.meta.url))); const __dirname = path.dirname(fileURLToPath(import.meta.url));
// check if various APIs are available (depends on environment) // check if various APIs are available (depends on environment)
const WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self; const WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self;
@ -37,7 +35,7 @@ onnx_env.wasm.wasmPaths = RUNNING_LOCALLY
// Global variable used to control exection, with suitable defaults // Global variable used to control exection, with suitable defaults
const env = { export const env = {
// Expose environment variables of different backends, allowing users to set // Expose environment variables of different backends, allowing users to set
// these variables if they want to. // these variables if they want to.
// TODO - will be used when we add more backends // TODO - will be used when we add more backends

View File

@ -1,7 +1,7 @@
import { import {
Callable, Callable,
isString, isString,
} = require("./utils.js"); } from "./utils.js";
import { import {
softmax, softmax,
max, max,

View File

@ -1,7 +1,6 @@
import { import {
Callable, Callable,
softmax,
} from './utils.js'; } from './utils.js';
import { import {

View File

@ -1,11 +1,11 @@
// Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) // Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models)
// const path = require('file-system-cache'); // const path = require('file-system-cache');
const { env } = require('../env.js'); import { env } from '../env.js';
const fs = require('fs'); import fs from 'fs';
const { dispatchCallback } = require('../utils.js'); import { dispatchCallback } from '../utils.js';
const path = require('path'); import path from 'path';
/** /**
* @typedef {object} PretrainedOptions Options for loading a pretrained model. * @typedef {object} PretrainedOptions Options for loading a pretrained model.
@ -198,7 +198,7 @@ function isValidHttpUrl(string) {
* @param {URL|string} urlOrPath - The URL/path of the file to get. * @param {URL|string} urlOrPath - The URL/path of the file to get.
* @returns {Promise<FileResponse|Response>} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API). * @returns {Promise<FileResponse|Response>} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API).
*/ */
async function getFile(urlOrPath) { export async function getFile(urlOrPath) {
// Helper function to get a file, using either the Fetch API or FileSystem API // Helper function to get a file, using either the Fetch API or FileSystem API
if (env.useFS && !isValidHttpUrl(urlOrPath)) { if (env.useFS && !isValidHttpUrl(urlOrPath)) {
@ -321,7 +321,7 @@ class FileCache {
* @throws Will throw an error if the file is not found and `fatal` is true. * @throws Will throw an error if the file is not found and `fatal` is true.
* @returns {Promise} A Promise that resolves with the file content as a buffer. * @returns {Promise} A Promise that resolves with the file content as a buffer.
*/ */
async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) {
// Initiate file retrieval // Initiate file retrieval
dispatchCallback(options.progress_callback, { dispatchCallback(options.progress_callback, {
@ -364,11 +364,11 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
// Caching not available, or file is not cached, so we perform the request // Caching not available, or file is not cached, so we perform the request
let isURL = isValidHttpUrl(request); let isURL = isValidHttpUrl(request);
let localPath = pathJoin(env.localModelPath, request);
// If request is a valid HTTP URL, we skip the local file check. Otherwise, we // If request is a valid HTTP URL, we skip the local file check. Otherwise, we
// try to get the file locally. // try to get the file locally.
if (!isURL) { if (!isURL) {
response = await getFile(pathJoin(env.localModelPath, request)) response = await getFile(localPath);
} else if (options.local_files_only) { } else if (options.local_files_only) {
throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${request}.`) throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${request}.`)
} }
@ -380,7 +380,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
if (options.local_files_only) { if (options.local_files_only) {
// User requested local files only, but the file is not found locally. // User requested local files only, but the file is not found locally.
if (fatal) { if (fatal) {
throw Error(`\`local_files_only=true\` and file was not found locally at "${request}".`) throw Error(`\`local_files_only=true\` and file was not found locally at "${localPath}".`)
} else { } else {
// File not found, but this file is optional. // File not found, but this file is optional.
// TODO in future, cache the response? // TODO in future, cache the response?
@ -458,7 +458,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
* @returns {Promise<object>} - The JSON data parsed into a JavaScript object. * @returns {Promise<object>} - The JSON data parsed into a JavaScript object.
* @throws Will throw an error if the file is not found and `fatal` is true. * @throws Will throw an error if the file is not found and `fatal` is true.
*/ */
async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { export async function getModelJSON(modelPath, fileName, fatal = true, options = {}) {
let buffer = await getModelFile(modelPath, fileName, fatal, options); let buffer = await getModelFile(modelPath, fileName, fatal, options);
if (buffer === null) { if (buffer === null) {
// Return empty object // Return empty object
@ -547,9 +547,3 @@ function pathJoin(...parts) {
}) })
return parts.join('/'); return parts.join('/');
} }
module.exports = {
getModelFile,
getModelJSON,
getFile,
}

View File

@ -1,6 +1,6 @@
import path from 'path'; import path from 'path';
import { pipeline, env } from '..'; import { pipeline, env } from '../src/transformers.js';
import { isDeepEqual } from './test_utils.js'; import { isDeepEqual } from './test_utils.js';
const __dirname = env.__dirname; const __dirname = env.__dirname;
@ -721,7 +721,7 @@ async function image_segmentation() {
quantized: false, quantized: false,
}) })
let img = path.join(__dirname, './assets/images/cats.jpg') let img = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png';
let start = performance.now(); let start = performance.now();
let outputs = await segmenter(img); let outputs = await segmenter(img);

View File

@ -1,5 +1,5 @@
function isDeepEqual(obj1, obj2, { export function isDeepEqual(obj1, obj2, {
tol = 1e-3 tol = 1e-3
} = {}) { } = {}) {
// Get the keys of both objects // Get the keys of both objects
@ -35,6 +35,3 @@ function isDeepEqual(obj1, obj2, {
// If all key-value pairs are equal, the objects are deep equal // If all key-value pairs are equal, the objects are deep equal
return true; return true;
} }
module.exports = {
isDeepEqual
}