-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathclient.go
More file actions
520 lines (437 loc) · 13.1 KB
/
client.go
File metadata and controls
520 lines (437 loc) · 13.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package huggingface
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
)
const (
defaultBaseURL = "https://huggingface.co"
defaultUserAgent = "model-distribution"
)
// Client handles HuggingFace Hub API interactions
type Client struct {
httpClient *http.Client
userAgent string
token string
baseURL string
}
type LFSBatchRequest struct {
Operation string `json:"operation"`
Transfers []string `json:"transfers,omitempty"`
Objects []LFSBatchObject `json:"objects"`
}
type LFSBatchObject struct {
OID string `json:"oid"`
Size int64 `json:"size"`
}
type LFSObjectError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type LFSAction struct {
Href string `json:"href"`
Header map[string]string `json:"header,omitempty"`
}
type LFSObject struct {
OID string `json:"oid"`
Size int64 `json:"size"`
Actions map[string]LFSAction `json:"actions,omitempty"`
Error *LFSObjectError `json:"error,omitempty"`
}
type LFSBatchResponse struct {
Transfer string `json:"transfer,omitempty"`
Objects []LFSObject `json:"objects"`
}
// ClientOption configures a Client
type ClientOption func(*Client)
// WithToken sets the HuggingFace API token for authentication
func WithToken(token string) ClientOption {
return func(c *Client) {
if token != "" {
c.token = token
}
}
}
// WithUserAgent sets the User-Agent header for requests
func WithUserAgent(userAgent string) ClientOption {
return func(c *Client) {
if userAgent != "" {
c.userAgent = userAgent
}
}
}
// WithBaseURL sets a custom base URL (useful for testing)
func WithBaseURL(baseURL string) ClientOption {
return func(c *Client) {
if baseURL != "" {
c.baseURL = strings.TrimSuffix(baseURL, "/")
}
}
}
// NewClient creates a new HuggingFace Hub API client
func NewClient(opts ...ClientOption) *Client {
c := &Client{
httpClient: &http.Client{},
userAgent: defaultUserAgent,
baseURL: defaultBaseURL,
}
for _, opt := range opts {
opt(c)
}
return c
}
// ListFiles returns all files in a repository at a given revision, recursively traversing all directories
func (c *Client) ListFiles(ctx context.Context, repo, revision string) ([]RepoFile, error) {
if revision == "" {
revision = "main"
}
return c.listFilesRecursive(ctx, repo, revision, "")
}
// listFilesRecursive recursively lists all files starting from the given path
func (c *Client) listFilesRecursive(ctx context.Context, repo, revision, filePath string) ([]RepoFile, error) {
entries, err := c.ListFilesInPath(ctx, repo, revision, filePath)
if err != nil {
return nil, err
}
var allFiles []RepoFile
for _, entry := range entries {
switch entry.Type {
case "file":
allFiles = append(allFiles, entry)
case "directory":
// Recursively list files in subdirectory
subFiles, err := c.listFilesRecursive(ctx, repo, revision, entry.Path)
if err != nil {
return nil, fmt.Errorf("list files in %s: %w", entry.Path, err)
}
allFiles = append(allFiles, subFiles...)
}
}
return allFiles, nil
}
// ListFilesInPath returns files and directories at a specific path in the repository
func (c *Client) ListFilesInPath(ctx context.Context, repo, revision, filePath string) ([]RepoFile, error) {
if revision == "" {
revision = "main"
}
// HuggingFace API endpoint for listing files
endpointPath := path.Join(revision, filePath)
url := fmt.Sprintf("%s/api/models/%s/tree/%s", c.baseURL, repo, endpointPath)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("list files: %w", err)
}
defer resp.Body.Close()
if err := c.checkResponse(resp, repo); err != nil {
return nil, err
}
var files []RepoFile
if err := json.NewDecoder(resp.Body).Decode(&files); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
return files, nil
}
// DownloadFile streams a file from the repository
// Returns the reader, content length (-1 if unknown), and any error
func (c *Client) DownloadFile(ctx context.Context, repo, revision, filename string) (io.ReadCloser, int64, error) {
if revision == "" {
revision = "main"
}
// HuggingFace file download endpoint (handles LFS redirects automatically)
url := fmt.Sprintf("%s/%s/resolve/%s/%s", c.baseURL, repo, revision, filename)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, 0, fmt.Errorf("create request: %w", err)
}
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, 0, fmt.Errorf("download file: %w", err)
}
if err := c.checkResponse(resp, repo); err != nil {
resp.Body.Close()
return nil, 0, err
}
return resp.Body, resp.ContentLength, nil
}
// RepoInfo contains metadata about a HuggingFace repository
type RepoInfo struct {
LastModified time.Time `json:"lastModified"`
}
// GetRepoInfo fetches repository metadata from the HuggingFace API.
// This returns information such as the last modified timestamp, which is useful
// for producing deterministic OCI digests.
func (c *Client) GetRepoInfo(ctx context.Context, repo, revision string) (*RepoInfo, error) {
if revision == "" {
revision = "main"
}
reqURL := fmt.Sprintf("%s/api/models/%s/revision/%s", c.baseURL, repo, url.PathEscape(revision))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, http.NoBody)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("get repo info: %w", err)
}
defer resp.Body.Close()
if err := c.checkResponse(resp, repo); err != nil {
return nil, err
}
var info RepoInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
return &info, nil
}
type CommitFile struct {
RepoPath string
Content io.Reader
}
type LFSCommitFile struct {
Path string `json:"path"`
Algo string `json:"algo"`
OID string `json:"oid"`
Size int64 `json:"size"`
}
type ndjsonEntry struct {
Key string `json:"key"`
Value interface{} `json:"value"`
}
// CreateCommit creates a commit in a HuggingFace repository using the NDJSON API.
// LFS files must be pre-uploaded via LFSBatch + UploadLFSObject before calling this.
// Small files are sent inline as base64.
func (c *Client) CreateCommit(ctx context.Context, repo, message string, directFiles []CommitFile, lfsFiles []LFSCommitFile) error {
if repo == "" {
return fmt.Errorf("repository is required")
}
endpoint := fmt.Sprintf("%s/api/models/%s/commit/main", c.baseURL, escapePath(repo))
var buf bytes.Buffer
headerEntry := ndjsonEntry{
Key: "header",
Value: map[string]interface{}{
"summary": message,
"description": "",
},
}
if err := json.NewEncoder(&buf).Encode(headerEntry); err != nil {
return fmt.Errorf("encode commit header: %w", err)
}
for _, lf := range lfsFiles {
entry := ndjsonEntry{
Key: "lfsFile",
Value: map[string]interface{}{
"path": lf.Path,
"algo": lf.Algo,
"oid": lf.OID,
"size": lf.Size,
},
}
if err := json.NewEncoder(&buf).Encode(entry); err != nil {
return fmt.Errorf("encode lfs file entry %s: %w", lf.Path, err)
}
}
for _, f := range directFiles {
data, err := io.ReadAll(f.Content)
if err != nil {
return fmt.Errorf("read file %s: %w", f.RepoPath, err)
}
entry := ndjsonEntry{
Key: "file",
Value: map[string]interface{}{
"path": f.RepoPath,
"encoding": "base64",
"content": base64.StdEncoding.EncodeToString(data),
},
}
if err := json.NewEncoder(&buf).Encode(entry); err != nil {
return fmt.Errorf("encode file entry %s: %w", f.RepoPath, err)
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, &buf)
if err != nil {
return fmt.Errorf("create commit request: %w", err)
}
req.Header.Set("Content-Type", "application/x-ndjson")
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("create commit: %w", err)
}
defer resp.Body.Close()
if err := c.checkUploadResponse(resp, repo); err != nil {
return err
}
return nil
}
func (c *Client) LFSBatch(ctx context.Context, repo string, objects []LFSBatchObject) (*LFSBatchResponse, error) {
if repo == "" {
return nil, fmt.Errorf("repository is required")
}
if len(objects) == 0 {
return &LFSBatchResponse{}, nil
}
endpoint := fmt.Sprintf("%s/%s.git/info/lfs/objects/batch", c.baseURL, escapePath(repo))
reqBody := LFSBatchRequest{
Operation: "upload",
Transfers: []string{"basic"},
Objects: objects,
}
data, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("encode lfs batch request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("lfs batch: %w", err)
}
defer resp.Body.Close()
if err := c.checkResponse(resp, repo); err != nil {
return nil, err
}
var batchResp LFSBatchResponse
if err := json.NewDecoder(resp.Body).Decode(&batchResp); err != nil {
return nil, fmt.Errorf("decode lfs batch response: %w", err)
}
return &batchResp, nil
}
func (c *Client) UploadLFSObject(ctx context.Context, action LFSAction, content io.Reader, size int64) error {
if action.Href == "" {
return fmt.Errorf("upload action href is empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, action.Href, content)
if err != nil {
return fmt.Errorf("create upload request: %w", err)
}
for key, value := range action.Header {
req.Header.Set(key, value)
}
if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/octet-stream")
}
if size > 0 {
req.ContentLength = size
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("upload lfs object: %w", err)
}
defer resp.Body.Close()
if err := c.checkUploadResponse(resp, ""); err != nil {
return err
}
return nil
}
func (c *Client) VerifyLFSObject(ctx context.Context, action LFSAction, oid string, size int64) error {
if action.Href == "" {
return nil
}
data, err := json.Marshal(LFSBatchObject{OID: oid, Size: size})
if err != nil {
return fmt.Errorf("encode verify request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, action.Href, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("create verify request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
c.setHeaders(req)
for key, value := range action.Header {
req.Header.Set(key, value)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("verify lfs object: %w", err)
}
defer resp.Body.Close()
if err := c.checkUploadResponse(resp, ""); err != nil {
return err
}
return nil
}
func (c *Client) setHeaders(req *http.Request) {
req.Header.Set("User-Agent", c.userAgent)
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
}
func (c *Client) checkResponse(resp *http.Response, repo string) error {
switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusUnauthorized, http.StatusForbidden:
return &AuthError{Repo: repo, StatusCode: resp.StatusCode}
case http.StatusNotFound:
return &NotFoundError{Repo: repo}
case http.StatusTooManyRequests:
return &RateLimitError{Repo: repo}
default:
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
return fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(body))
}
}
func (c *Client) checkUploadResponse(resp *http.Response, repo string) error {
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
return nil
default:
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
switch resp.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
return &AuthError{Repo: repo, StatusCode: resp.StatusCode}
case http.StatusNotFound:
return &NotFoundError{Repo: repo}
default:
return fmt.Errorf("upload failed (status %d): %s", resp.StatusCode, string(body))
}
}
}
func escapePath(value string) string {
parts := strings.Split(value, "/")
for i, part := range parts {
parts[i] = url.PathEscape(part)
}
return strings.Join(parts, "/")
}
// AuthError indicates authentication failure
type AuthError struct {
Repo string
StatusCode int
}
func (e *AuthError) Error() string {
return fmt.Sprintf("authentication required for repository %q (status %d)", e.Repo, e.StatusCode)
}
// NotFoundError indicates the repository or file was not found
type NotFoundError struct {
Repo string
}
func (e *NotFoundError) Error() string {
return fmt.Sprintf("repository %q not found", e.Repo)
}
// RateLimitError indicates rate limiting
type RateLimitError struct {
Repo string
}
func (e *RateLimitError) Error() string {
return fmt.Sprintf("rate limited while accessing repository %q", e.Repo)
}