Skip to content

Commit 1a90c6e

Browse files
committed
Fix ES6 imports
1 parent f25b17c commit 1a90c6e

File tree

6 files changed

+16
-28
lines changed

6 files changed

+16
-28
lines changed

src/env.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { fileURLToPath } from 'url';
55
import { ONNX } from './backends/onnx.js';
66
const { env: onnx_env } = ONNX;
77

8-
const __dirname = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
9-
10-
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
119

1210
// check if various APIs are available (depends on environment)
1311
const WEB_CACHE_AVAILABLE = typeof self !== 'undefined' && 'caches' in self;
@@ -37,7 +35,7 @@ onnx_env.wasm.wasmPaths = RUNNING_LOCALLY
3735

3836

3937
// Global variable used to control exection, with suitable defaults
40-
const env = {
38+
export const env = {
4139
// Expose environment variables of different backends, allowing users to set
4240
// these variables if they want to.
4341
// TODO - will be used when we add more backends

src/pipelines.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Callable,
33
isString,
4-
} = require("./utils.js");
4+
} from "./utils.js";
55
import {
66
softmax,
77
max,

src/processors.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
import {
33
Callable,
4-
softmax,
54
} from './utils.js';
65

76
import {

src/utils/hub.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models)
22

33
// const path = require('file-system-cache');
4-
const { env } = require('../env.js');
5-
const fs = require('fs');
4+
import { env } from '../env.js';
5+
import fs from 'fs';
66

7-
const { dispatchCallback } = require('../utils.js');
8-
const path = require('path');
7+
import { dispatchCallback } from '../utils.js';
8+
import path from 'path';
99

1010
/**
1111
* @typedef {object} PretrainedOptions Options for loading a pretrained model.
@@ -198,7 +198,7 @@ function isValidHttpUrl(string) {
198198
* @param {URL|string} urlOrPath - The URL/path of the file to get.
199199
* @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).
200200
*/
201-
async function getFile(urlOrPath) {
201+
export async function getFile(urlOrPath) {
202202
// Helper function to get a file, using either the Fetch API or FileSystem API
203203

204204
if (env.useFS && !isValidHttpUrl(urlOrPath)) {
@@ -321,7 +321,7 @@ class FileCache {
321321
* @throws Will throw an error if the file is not found and `fatal` is true.
322322
* @returns {Promise} A Promise that resolves with the file content as a buffer.
323323
*/
324-
async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) {
324+
export async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) {
325325

326326
// Initiate file retrieval
327327
dispatchCallback(options.progress_callback, {
@@ -364,11 +364,11 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
364364
// Caching not available, or file is not cached, so we perform the request
365365

366366
let isURL = isValidHttpUrl(request);
367-
367+
let localPath = pathJoin(env.localModelPath, request);
368368
// If request is a valid HTTP URL, we skip the local file check. Otherwise, we
369369
// try to get the file locally.
370370
if (!isURL) {
371-
response = await getFile(pathJoin(env.localModelPath, request))
371+
response = await getFile(localPath);
372372
} else if (options.local_files_only) {
373373
throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${request}.`)
374374
}
@@ -380,7 +380,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
380380
if (options.local_files_only) {
381381
// User requested local files only, but the file is not found locally.
382382
if (fatal) {
383-
throw Error(`\`local_files_only=true\` and file was not found locally at "${request}".`)
383+
throw Error(`\`local_files_only=true\` and file was not found locally at "${localPath}".`)
384384
} else {
385385
// File not found, but this file is optional.
386386
// TODO in future, cache the response?
@@ -458,7 +458,7 @@ async function getModelFile(path_or_repo_id, filename, fatal = true, options = {
458458
* @returns {Promise<object>} - The JSON data parsed into a JavaScript object.
459459
* @throws Will throw an error if the file is not found and `fatal` is true.
460460
*/
461-
async function getModelJSON(modelPath, fileName, fatal = true, options = {}) {
461+
export async function getModelJSON(modelPath, fileName, fatal = true, options = {}) {
462462
let buffer = await getModelFile(modelPath, fileName, fatal, options);
463463
if (buffer === null) {
464464
// Return empty object
@@ -547,9 +547,3 @@ function pathJoin(...parts) {
547547
})
548548
return parts.join('/');
549549
}
550-
551-
module.exports = {
552-
getModelFile,
553-
getModelJSON,
554-
getFile,
555-
}

tests/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import path from 'path';
3-
import { pipeline, env } from '..';
3+
import { pipeline, env } from '../src/transformers.js';
44
import { isDeepEqual } from './test_utils.js';
55

66
const __dirname = env.__dirname;
@@ -721,7 +721,7 @@ async function image_segmentation() {
721721
quantized: false,
722722
})
723723

724-
let img = path.join(__dirname, './assets/images/cats.jpg')
724+
let img = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/coco_sample.png';
725725

726726
let start = performance.now();
727727
let outputs = await segmenter(img);

tests/test_utils.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
function isDeepEqual(obj1, obj2, {
2+
export function isDeepEqual(obj1, obj2, {
33
tol = 1e-3
44
} = {}) {
55
// Get the keys of both objects
@@ -35,6 +35,3 @@ function isDeepEqual(obj1, obj2, {
3535
// If all key-value pairs are equal, the objects are deep equal
3636
return true;
3737
}
38-
module.exports = {
39-
isDeepEqual
40-
}

0 commit comments

Comments
 (0)