Skip to content

fix: RawImage.fromURL error when input file url #1288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/utils/hub.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models)
*
*
* @module utils/hub
*/

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

/**
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
* @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.
* @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:
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
Expand Down Expand Up @@ -158,7 +158,7 @@ class FileResponse {
/**
* Reads the contents of the file specified by the filePath property and returns a Promise that
* resolves with a parsed JavaScript object containing the file's contents.
*
*
* @returns {Promise<Object>} A Promise that resolves with a parsed JavaScript object containing the file's contents.
* @throws {Error} If the file cannot be read.
*/
Expand Down Expand Up @@ -195,7 +195,7 @@ const REPO_ID_REGEX = /^(\b[\w\-.]+\b\/)?\b[\w\-.]{1,96}\b$/;
/**
* Tests whether a string is a valid Hugging Face model ID or not.
* Adapted from https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170
*
*
* @param {string} string The string to test
* @returns {boolean} True if the string is a valid model ID, false otherwise.
*/
Expand All @@ -214,9 +214,14 @@ function isValidHfModelId(string) {
*/
export async function getFile(urlOrPath) {

if (env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) {
return new FileResponse(urlOrPath.toString());

if (env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) {
return new FileResponse(
urlOrPath instanceof URL
? urlOrPath.protocol === "file:"
? urlOrPath.pathname
: urlOrPath.toString()
: urlOrPath,
);
} else if (typeof process !== 'undefined' && process?.release?.name === 'node') {
const IS_CI = !!process.env?.TESTING_REMOTELY;
const version = env.version;
Expand Down Expand Up @@ -280,15 +285,15 @@ function handleError(status, remoteURL, fatal) {
class FileCache {
/**
* Instantiate a `FileCache` object.
* @param {string} path
* @param {string} path
*/
constructor(path) {
this.path = path;
}

/**
* Checks whether the given request is in the cache.
* @param {string} request
* @param {string} request
* @returns {Promise<FileResponse | undefined>}
*/
async match(request) {
Expand All @@ -305,8 +310,8 @@ class FileCache {

/**
* Adds the given response to the cache.
* @param {string} request
* @param {Response} response
* @param {string} request
* @param {Response} response
* @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional.
* The function to call with progress updates
* @returns {Promise<void>}
Expand Down Expand Up @@ -364,7 +369,7 @@ class FileCache {
}

/**
*
*
* @param {FileCache|Cache} cache The cache to search
* @param {string[]} names The names of the item to search for
* @returns {Promise<FileResponse|Response|undefined>} The item from the cache, or undefined if not found.
Expand Down