Skip to content

Commit 7bdd198

Browse files
authored
fix(downloader): Rewrite full https HF URI with HF_ENDPOINT (#9107)
Signed-off-by: Richard Palethorpe <io@richiejp.com>
1 parent b296e3d commit 7bdd198

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

pkg/downloader/huggingface.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ var ErrUnsafeFilesFound = errors.New("unsafe files found")
2323

2424
func HuggingFaceScan(uri URI) (*HuggingFaceScanResult, error) {
2525
cleanParts := strings.Split(uri.ResolveURL(), "/")
26-
if len(cleanParts) <= 4 || cleanParts[2] != "huggingface.co" && cleanParts[2] != HF_ENDPOINT {
26+
// cleanParts[2] is the hostname from the URL (e.g. "huggingface.co" or "hf-mirror.com").
27+
// Extract the hostname from HF_ENDPOINT for comparison, since HF_ENDPOINT includes the scheme.
28+
hfHost := strings.TrimPrefix(strings.TrimPrefix(HF_ENDPOINT, "https://"), "http://")
29+
if len(cleanParts) <= 4 || (cleanParts[2] != "huggingface.co" && cleanParts[2] != hfHost) {
2730
return nil, ErrNonHuggingFaceFile
2831
}
2932
results, err := http.Get(fmt.Sprintf("%s/api/models/%s/%s/scan", HF_ENDPOINT, cleanParts[3], cleanParts[4]))

pkg/downloader/uri.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ func (s URI) ResolveURL() string {
261261
return fmt.Sprintf("%s/%s/%s/resolve/%s/%s", HF_ENDPOINT, owner, repo, branch, filepath)
262262
}
263263

264+
// If a HuggingFace mirror is configured, rewrite direct https://huggingface.co/ URLs
265+
// to use the mirror. This ensures gallery entries with hardcoded URLs also benefit
266+
// from the mirror setting.
267+
if HF_ENDPOINT != "https://huggingface.co" && strings.HasPrefix(string(s), "https://huggingface.co/") {
268+
return HF_ENDPOINT + strings.TrimPrefix(string(s), "https://huggingface.co")
269+
}
270+
264271
return string(s)
265272
}
266273

pkg/downloader/uri_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,42 @@ var _ = Describe("Gallery API tests", func() {
4949
).ToNot(HaveOccurred())
5050
})
5151
})
52+
53+
Context("HuggingFace mirror", func() {
54+
var originalEndpoint string
55+
56+
BeforeEach(func() {
57+
originalEndpoint = HF_ENDPOINT
58+
})
59+
60+
AfterEach(func() {
61+
HF_ENDPOINT = originalEndpoint
62+
})
63+
64+
It("rewrites direct https://huggingface.co URLs when mirror is set", func() {
65+
HF_ENDPOINT = "https://hf-mirror.com"
66+
uri := URI("https://huggingface.co/TheBloke/model-GGUF/resolve/main/model.Q4_K_M.gguf")
67+
Expect(uri.ResolveURL()).To(Equal("https://hf-mirror.com/TheBloke/model-GGUF/resolve/main/model.Q4_K_M.gguf"))
68+
})
69+
70+
It("does not rewrite direct https://huggingface.co URLs when no mirror is set", func() {
71+
HF_ENDPOINT = "https://huggingface.co"
72+
uri := URI("https://huggingface.co/TheBloke/model-GGUF/resolve/main/model.Q4_K_M.gguf")
73+
Expect(uri.ResolveURL()).To(Equal("https://huggingface.co/TheBloke/model-GGUF/resolve/main/model.Q4_K_M.gguf"))
74+
})
75+
76+
It("rewrites hf:// URIs when mirror is set", func() {
77+
HF_ENDPOINT = "https://hf-mirror.com"
78+
uri := URI("hf://TheBloke/model-GGUF/model.Q4_K_M.gguf")
79+
Expect(uri.ResolveURL()).To(Equal("https://hf-mirror.com/TheBloke/model-GGUF/resolve/main/model.Q4_K_M.gguf"))
80+
})
81+
82+
It("does not rewrite non-huggingface URLs", func() {
83+
HF_ENDPOINT = "https://hf-mirror.com"
84+
uri := URI("https://example.com/some/file.gguf")
85+
Expect(uri.ResolveURL()).To(Equal("https://example.com/some/file.gguf"))
86+
})
87+
})
5288
})
5389

5490
var _ = Describe("ContentLength", func() {

0 commit comments

Comments
 (0)