Fix download gated HF models (#3004)

This commit is contained in:
Christian Byrne
2025-03-13 10:54:40 -07:00
committed by GitHub
parent 8b69b280fa
commit a7d3a74daa
2 changed files with 37 additions and 3 deletions

View File

@@ -357,3 +357,30 @@ export const isCivitaiModelUrl = (url: string): boolean => {
/^\/api\/v1\/models-versions\/(\d+)$/.test(pathname)
)
}
/**
* Converts a Hugging Face download URL to a repository page URL
* @param url The download URL to convert
* @returns The repository page URL or the original URL if conversion fails
* @example
* downloadUrlToHfRepoUrl(
* 'https://huggingface.co/bfl/FLUX.1/resolve/main/flux1-canny-dev.safetensors?download=true'
* ) // https://huggingface.co/bfl/FLUX.1
*/
export const downloadUrlToHfRepoUrl = (url: string): string => {
try {
const urlObj = new URL(url)
const pathname = urlObj.pathname
// Use regex to match everything before /resolve/ or /blob/
const regex = /^(.*?)(?:\/resolve\/|\/blob\/|$)/
const repoPathMatch = regex.exec(pathname)
// Extract the repository path and remove leading slash if present
const repoPath = repoPathMatch?.[1]?.replace(/^\//, '') || ''
return `https://huggingface.co/${repoPath}`
} catch (error) {
return url
}
}