Skip to content

Commit 06ebe86

Browse files
himself65xenova
andauthored
fix RawImage.fromURL when using a file:// URL (#1288)
* fix: `RawImage.fromURL` error when input file url * fix: check protocol * Fix type error --------- Co-authored-by: Joshua Lochner <[email protected]>
1 parent 6c4fab6 commit 06ebe86

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/utils/hub.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/**
33
* @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models)
4-
*
4+
*
55
* @module utils/hub
66
*/
77

@@ -19,7 +19,7 @@ import { dispatchCallback } from './core.js';
1919
export const MAX_EXTERNAL_DATA_CHUNKS = 100;
2020

2121
/**
22-
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
22+
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
2323
* @property {import('./core.js').ProgressCallback} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
2424
* @property {import('../configs.js').PretrainedConfig} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
2525
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
@@ -158,7 +158,7 @@ class FileResponse {
158158
/**
159159
* Reads the contents of the file specified by the filePath property and returns a Promise that
160160
* resolves with a parsed JavaScript object containing the file's contents.
161-
*
161+
*
162162
* @returns {Promise<Object>} A Promise that resolves with a parsed JavaScript object containing the file's contents.
163163
* @throws {Error} If the file cannot be read.
164164
*/
@@ -195,7 +195,7 @@ const REPO_ID_REGEX = /^(\b[\w\-.]+\b\/)?\b[\w\-.]{1,96}\b$/;
195195
/**
196196
* Tests whether a string is a valid Hugging Face model ID or not.
197197
* Adapted from https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170
198-
*
198+
*
199199
* @param {string} string The string to test
200200
* @returns {boolean} True if the string is a valid model ID, false otherwise.
201201
*/
@@ -214,9 +214,14 @@ function isValidHfModelId(string) {
214214
*/
215215
export async function getFile(urlOrPath) {
216216

217-
if (env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) {
218-
return new FileResponse(urlOrPath.toString());
219-
217+
if (env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) {
218+
return new FileResponse(
219+
urlOrPath instanceof URL
220+
? urlOrPath.protocol === "file:"
221+
? urlOrPath.pathname
222+
: urlOrPath.toString()
223+
: urlOrPath,
224+
);
220225
} else if (typeof process !== 'undefined' && process?.release?.name === 'node') {
221226
const IS_CI = !!process.env?.TESTING_REMOTELY;
222227
const version = env.version;
@@ -280,15 +285,15 @@ function handleError(status, remoteURL, fatal) {
280285
class FileCache {
281286
/**
282287
* Instantiate a `FileCache` object.
283-
* @param {string} path
288+
* @param {string} path
284289
*/
285290
constructor(path) {
286291
this.path = path;
287292
}
288293

289294
/**
290295
* Checks whether the given request is in the cache.
291-
* @param {string} request
296+
* @param {string} request
292297
* @returns {Promise<FileResponse | undefined>}
293298
*/
294299
async match(request) {
@@ -305,8 +310,8 @@ class FileCache {
305310

306311
/**
307312
* Adds the given response to the cache.
308-
* @param {string} request
309-
* @param {Response} response
313+
* @param {string} request
314+
* @param {Response} response
310315
* @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional.
311316
* The function to call with progress updates
312317
* @returns {Promise<void>}
@@ -364,7 +369,7 @@ class FileCache {
364369
}
365370

366371
/**
367-
*
372+
*
368373
* @param {FileCache|Cache} cache The cache to search
369374
* @param {string[]} names The names of the item to search for
370375
* @returns {Promise<FileResponse|Response|undefined>} The item from the cache, or undefined if not found.

0 commit comments

Comments
 (0)