8
8
import Foundation
9
9
10
10
public struct HubApi {
11
- let endpoint = " https://huggingface.co/api "
12
11
var downloadBase : URL
13
- var hfToken : String ? = nil
12
+ var hfToken : String ?
13
+ var endpoint : String
14
14
15
15
public typealias RepoType = Hub . RepoType
16
16
public typealias Repo = Hub . Repo
17
17
18
- public init ( downloadBase: URL ? = nil , hfToken: String ? = nil ) {
18
+ public init ( downloadBase: URL ? = nil , hfToken: String ? = nil , endpoint : String = " https://huggingface.co " ) {
19
19
if downloadBase == nil {
20
20
let documents = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . first!
21
21
self . downloadBase = documents. appending ( component: " huggingface " )
22
22
} else {
23
23
self . downloadBase = downloadBase!
24
24
}
25
25
self . hfToken = hfToken
26
+ self . endpoint = endpoint
26
27
}
27
28
28
29
static let shared = HubApi ( )
@@ -51,15 +52,15 @@ public extension HubApi {
51
52
switch response. statusCode {
52
53
case 200 ..< 300 : break
53
54
case 400 ..< 500 : throw Hub . HubClientError. authorizationRequired
54
- default : throw Hub . HubClientError. httpStatusCode ( response. statusCode)
55
+ default : throw Hub . HubClientError. httpStatusCode ( response. statusCode)
55
56
}
56
57
57
58
return ( data, response)
58
59
}
59
60
60
61
func getFilenames( from repo: Repo , matching globs: [ String ] = [ ] ) async throws -> [ String ] {
61
62
// Read repo info and only parse "siblings"
62
- let url = URL ( string: " \( endpoint) / \( repo. type) / \( repo. id) " ) !
63
+ let url = URL ( string: " \( endpoint) /api/ \( repo. type) / \( repo. id) " ) !
63
64
let ( data, _) = try await httpGet ( for: url)
64
65
let response = try JSONDecoder ( ) . decode ( SiblingsResponse . self, from: data)
65
66
let filenames = response. siblings. map { $0. rfilename }
@@ -103,7 +104,7 @@ public extension HubApi {
103
104
func whoami( ) async throws -> Config {
104
105
guard hfToken != nil else { throw Hub . HubClientError. authorizationRequired }
105
106
106
- let url = URL ( string: " \( endpoint) /whoami-v2 " ) !
107
+ let url = URL ( string: " \( endpoint) /api/ whoami-v2 " ) !
107
108
let ( data, _) = try await httpGet ( for: url)
108
109
109
110
let parsed = try JSONSerialization . jsonObject ( with: data, options: [ ] )
@@ -123,15 +124,16 @@ public extension HubApi {
123
124
let repoDestination : URL
124
125
let relativeFilename : String
125
126
let hfToken : String ?
127
+ let endpoint : String ?
126
128
127
129
var source : URL {
128
130
// https://huggingface.co/coreml-projects/Llama-2-7b-chat-coreml/resolve/main/tokenizer.json?download=true
129
- var url = URL ( string: " https://huggingface.co " ) !
131
+ var url = URL ( string: endpoint ?? " https://huggingface.co " ) !
130
132
if repo. type != . models {
131
133
url = url. appending ( component: repo. type. rawValue)
132
134
}
133
135
url = url. appending ( path: repo. id)
134
- url = url. appending ( path: " resolve/main " ) // TODO: revisions
136
+ url = url. appending ( path: " resolve/main " ) // TODO: revisions
135
137
url = url. appending ( path: relativeFilename)
136
138
return url
137
139
}
@@ -177,7 +179,7 @@ public extension HubApi {
177
179
let repoDestination = localRepoLocation ( repo)
178
180
for filename in filenames {
179
181
let fileProgress = Progress ( totalUnitCount: 100 , parent: progress, pendingUnitCount: 1 )
180
- let downloader = HubFileDownloader ( repo: repo, repoDestination: repoDestination, relativeFilename: filename, hfToken: hfToken)
182
+ let downloader = HubFileDownloader ( repo: repo, repoDestination: repoDestination, relativeFilename: filename, hfToken: hfToken, endpoint : endpoint )
181
183
try await downloader. download { fractionDownloaded in
182
184
fileProgress. completedUnitCount = Int64 ( 100 * fractionDownloaded)
183
185
progressHandler ( progress)
@@ -243,9 +245,8 @@ public extension Hub {
243
245
}
244
246
}
245
247
246
- public extension Array < String > {
248
+ public extension [ String ] {
247
249
func matching( glob: String ) -> [ String ] {
248
- self . filter { fnmatch ( glob, $0, 0 ) == 0 }
250
+ filter { fnmatch ( glob, $0, 0 ) == 0 }
249
251
}
250
252
}
251
-
0 commit comments