-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfile_from_remote.go
153 lines (137 loc) · 4.01 KB
/
file_from_remote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gguf_parser
import (
"context"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/gpustack/gguf-parser-go/util/httpx"
"github.com/gpustack/gguf-parser-go/util/osx"
)
// ParseGGUFFileFromHuggingFace parses a GGUF file from Hugging Face(https://huggingface.co/),
// and returns a GGUFFile, or an error if any.
func ParseGGUFFileFromHuggingFace(ctx context.Context, repo, file string, opts ...GGUFReadOption) (*GGUFFile, error) {
ep := osx.Getenv("HF_ENDPOINT", "https://huggingface.co")
return ParseGGUFFileRemote(ctx, fmt.Sprintf("%s/%s/resolve/main/%s", ep, repo, file), opts...)
}
// ParseGGUFFileFromModelScope parses a GGUF file from Model Scope(https://modelscope.cn/),
// and returns a GGUFFile, or an error if any.
func ParseGGUFFileFromModelScope(ctx context.Context, repo, file string, opts ...GGUFReadOption) (*GGUFFile, error) {
ep := osx.Getenv("MS_ENDPOINT", "https://modelscope.cn")
opts = append(opts[:len(opts):len(opts)], SkipRangeDownloadDetection())
return ParseGGUFFileRemote(ctx, fmt.Sprintf("%s/models/%s/resolve/master/%s", ep, repo, file), opts...)
}
// ParseGGUFFileRemote parses a GGUF file from a remote BlobURL,
// and returns a GGUFFile, or an error if any.
func ParseGGUFFileRemote(ctx context.Context, url string, opts ...GGUFReadOption) (gf *GGUFFile, err error) {
var o _GGUFReadOptions
for _, opt := range opts {
opt(&o)
}
// Cache.
{
if o.CachePath != "" {
o.CachePath = filepath.Join(o.CachePath, "remote")
if o.SkipLargeMetadata {
o.CachePath = filepath.Join(o.CachePath, "brief")
}
}
c := GGUFFileCache(o.CachePath)
// Get from cache.
if gf, err = c.Get(url, o.CacheExpiration); err == nil {
return gf, nil
}
// Put to cache.
defer func() {
if err == nil {
_ = c.Put(url, gf)
}
}()
}
cli := httpx.Client(
httpx.ClientOptions().
WithUserAgent("gguf-parser-go").
If(o.Debug,
func(x *httpx.ClientOption) *httpx.ClientOption {
return x.WithDebug()
},
).
If(o.BearerAuthToken != "",
func(x *httpx.ClientOption) *httpx.ClientOption {
return x.WithBearerAuth(o.BearerAuthToken)
},
).
WithTimeout(0).
WithTransport(
httpx.TransportOptions().
WithoutKeepalive().
TimeoutForDial(5*time.Second).
TimeoutForTLSHandshake(5*time.Second).
TimeoutForResponseHeader(5*time.Second).
If(o.SkipProxy,
func(x *httpx.TransportOption) *httpx.TransportOption {
return x.WithoutProxy()
},
).
If(o.ProxyURL != nil,
func(x *httpx.TransportOption) *httpx.TransportOption {
return x.WithProxy(http.ProxyURL(o.ProxyURL))
},
).
If(o.SkipTLSVerification || !strings.HasPrefix(url, "https://"),
func(x *httpx.TransportOption) *httpx.TransportOption {
return x.WithoutInsecureVerify()
},
).
If(o.SkipDNSCache,
func(x *httpx.TransportOption) *httpx.TransportOption {
return x.WithoutDNSCache()
},
),
),
)
return parseGGUFFileFromRemote(ctx, cli, url, o)
}
func parseGGUFFileFromRemote(ctx context.Context, cli *http.Client, url string, o _GGUFReadOptions) (*GGUFFile, error) {
var urls []string
{
rs := CompleteShardGGUFFilename(url)
if rs != nil {
urls = rs
} else {
urls = []string{url}
}
}
fs := make([]_GGUFFileReadSeeker, 0, len(urls))
defer func() {
for i := range fs {
osx.Close(fs[i])
}
}()
for i := range urls {
req, err := httpx.NewGetRequestWithContext(ctx, urls[i])
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
sf, err := httpx.OpenSeekerFile(cli, req,
httpx.SeekerFileOptions().
WithBufferSize(o.BufferSize).
If(o.SkipRangeDownloadDetection,
func(x *httpx.SeekerFileOption) *httpx.SeekerFileOption {
return x.WithoutRangeDownloadDetect()
},
),
)
if err != nil {
return nil, fmt.Errorf("open http file: %w", err)
}
fs = append(fs, _GGUFFileReadSeeker{
Closer: sf,
ReadSeeker: io.NewSectionReader(sf, 0, sf.Len()),
Size: sf.Len(),
})
}
return parseGGUFFile(fs, o)
}