7
7
8
8
import Foundation
9
9
10
- public struct Hub { }
10
+ public struct Hub { }
11
11
12
12
public extension Hub {
13
13
enum HubClientError : LocalizedError {
@@ -25,28 +25,28 @@ public extension Hub {
25
25
26
26
public var errorDescription : String ? {
27
27
switch self {
28
- case . authorizationRequired:
29
- return String ( localized: " Authentication required. Please provide a valid Hugging Face token. " )
30
- case . httpStatusCode( let code) :
31
- return String ( localized: " HTTP error with status code: \( code) " )
32
- case . parse:
33
- return String ( localized: " Failed to parse server response. " )
34
- case . unexpectedError:
35
- return String ( localized: " An unexpected error occurred. " )
36
- case . downloadError( let message) :
37
- return String ( localized: " Download failed: \( message) " )
38
- case . fileNotFound( let filename) :
39
- return String ( localized: " File not found: \( filename) " )
40
- case . networkError( let error) :
41
- return String ( localized: " Network error: \( error. localizedDescription) " )
42
- case . resourceNotFound( let resource) :
43
- return String ( localized: " Resource not found: \( resource) " )
44
- case . configurationMissing( let file) :
45
- return String ( localized: " Required configuration file missing: \( file) " )
46
- case . fileSystemError( let error) :
47
- return String ( localized: " File system error: \( error. localizedDescription) " )
48
- case . parseError( let message) :
49
- return String ( localized: " Parse error: \( message) " )
28
+ case . authorizationRequired:
29
+ String ( localized: " Authentication required. Please provide a valid Hugging Face token. " )
30
+ case let . httpStatusCode( code) :
31
+ String ( localized: " HTTP error with status code: \( code) " )
32
+ case . parse:
33
+ String ( localized: " Failed to parse server response. " )
34
+ case . unexpectedError:
35
+ String ( localized: " An unexpected error occurred. " )
36
+ case let . downloadError( message) :
37
+ String ( localized: " Download failed: \( message) " )
38
+ case let . fileNotFound( filename) :
39
+ String ( localized: " File not found: \( filename) " )
40
+ case let . networkError( error) :
41
+ String ( localized: " Network error: \( error. localizedDescription) " )
42
+ case let . resourceNotFound( resource) :
43
+ String ( localized: " Resource not found: \( resource) " )
44
+ case let . configurationMissing( file) :
45
+ String ( localized: " Required configuration file missing: \( file) " )
46
+ case let . fileSystemError( error) :
47
+ String ( localized: " File system error: \( error. localizedDescription) " )
48
+ case let . parseError( message) :
49
+ String ( localized: " Parse error: \( message) " )
50
50
}
51
51
}
52
52
}
@@ -79,7 +79,7 @@ public struct Config {
79
79
}
80
80
81
81
func camelCase( _ string: String ) -> String {
82
- return string
82
+ string
83
83
. split ( separator: " _ " )
84
84
. enumerated ( )
85
85
. map { $0. offset == 0 ? $0. element. lowercased ( ) : $0. element. capitalized }
@@ -108,7 +108,6 @@ public struct Config {
108
108
return result
109
109
}
110
110
111
-
112
111
public subscript( dynamicMember member: String ) -> Config ? {
113
112
let key = ( dictionary [ member as NSString ] != nil ? member : uncamelCase ( member) ) as NSString
114
113
if let value = dictionary [ key] as? [ NSString : Any ] {
@@ -120,17 +119,17 @@ public struct Config {
120
119
}
121
120
122
121
public var value : Any ? {
123
- return dictionary [ " value " ]
122
+ dictionary [ " value " ]
124
123
}
125
124
126
125
public var intValue : Int ? { value as? Int }
127
126
public var boolValue : Bool ? { value as? Bool }
128
127
public var stringValue : String ? { value as? String }
129
128
130
- // Instead of doing this we could provide custom classes and decode to them
129
+ /// Instead of doing this we could provide custom classes and decode to them
131
130
public var arrayValue : [ Config ] ? {
132
131
guard let list = value as? [ Any ] else { return nil }
133
- return list. map { Config ( $0 as! [ NSString : Any ] ) }
132
+ return list. map { Config ( $0 as! [ NSString : Any ] ) }
134
133
}
135
134
136
135
/// Tuple of token identifier and string value
@@ -144,23 +143,23 @@ public class LanguageModelConfigurationFromHub {
144
143
var tokenizerData : Config
145
144
}
146
145
147
- private var configPromise : Task < Configurations , Error > ? = nil
146
+ private var configPromise : Task < Configurations , Error > ?
148
147
149
148
public init (
150
149
modelName: String ,
151
150
hubApi: HubApi = . shared
152
151
) {
153
- self . configPromise = Task . init {
154
- return try await self . loadConfig ( modelName: modelName, hubApi: hubApi)
152
+ configPromise = Task . init {
153
+ try await self . loadConfig ( modelName: modelName, hubApi: hubApi)
155
154
}
156
155
}
157
156
158
157
public init (
159
158
modelFolder: URL ,
160
159
hubApi: HubApi = . shared
161
160
) {
162
- self . configPromise = Task {
163
- return try await self . loadConfig ( modelFolder: modelFolder, hubApi: hubApi)
161
+ configPromise = Task {
162
+ try await self . loadConfig ( modelFolder: modelFolder, hubApi: hubApi)
164
163
}
165
164
}
166
165
@@ -221,12 +220,12 @@ public class LanguageModelConfigurationFromHub {
221
220
// Convert generic errors to more specific ones
222
221
if let urlError = error as? URLError {
223
222
switch urlError. code {
224
- case . notConnectedToInternet, . networkConnectionLost:
225
- throw Hub . HubClientError. networkError ( urlError)
226
- case . resourceUnavailable:
227
- throw Hub . HubClientError. resourceNotFound ( modelName)
228
- default :
229
- throw Hub . HubClientError. networkError ( urlError)
223
+ case . notConnectedToInternet, . networkConnectionLost:
224
+ throw Hub . HubClientError. networkError ( urlError)
225
+ case . resourceUnavailable:
226
+ throw Hub . HubClientError. resourceNotFound ( modelName)
227
+ default :
228
+ throw Hub . HubClientError. networkError ( urlError)
230
229
}
231
230
} else {
232
231
throw error
@@ -265,7 +264,8 @@ public class LanguageModelConfigurationFromHub {
265
264
let chatTemplateURL = modelFolder. appending ( path: " chat_template.json " )
266
265
if FileManager . default. fileExists ( atPath: chatTemplateURL. path) ,
267
266
let chatTemplateConfig = try ? hubApi. configuration ( fileURL: chatTemplateURL) ,
268
- let chatTemplate = chatTemplateConfig. chatTemplate? . stringValue {
267
+ let chatTemplate = chatTemplateConfig. chatTemplate? . stringValue
268
+ {
269
269
// Create or update tokenizer config with chat template
270
270
if var configDict = tokenizerConfig? . dictionary {
271
271
configDict [ " chat_template " ] = chatTemplate
@@ -284,7 +284,7 @@ public class LanguageModelConfigurationFromHub {
284
284
throw error
285
285
} catch {
286
286
if let nsError = error as NSError ? {
287
- if nsError. domain == NSCocoaErrorDomain && nsError. code == NSFileReadNoSuchFileError {
287
+ if nsError. domain == NSCocoaErrorDomain, nsError. code == NSFileReadNoSuchFileError {
288
288
throw Hub . HubClientError. fileSystemError ( error)
289
289
} else if nsError. domain == " NSJSONSerialization " {
290
290
throw Hub . HubClientError. parseError ( " Invalid JSON format: \( nsError. localizedDescription) " )
0 commit comments