Skip to content

Commit 47d6c65

Browse files
authored
Add the canonical cached token location (#170)
* Add the canonical cached token location. I think ~/.huggingface is deprecated. * Local fromPretrained test
1 parent aeaf9e6 commit 47d6c65

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Sources/Hub/HubApi.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private extension HubApi {
5252
)
5353
}
5454
},
55+
{ try? String(contentsOf: .homeDirectory.appendingPathComponent(".cache/huggingface/token"), encoding: .utf8) },
5556
{ try? String(contentsOf: .homeDirectory.appendingPathComponent(".huggingface/token"), encoding: .utf8) }
5657
]
5758
return possibleTokens

Tests/TokenizersTests/TokenizerTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ class LlamaPostProcessorOverrideTests: XCTestCase {
140140
}
141141
}
142142

143+
class LocalFromPretrainedTests: XCTestCase {
144+
func testLocalTokenizerFromPretrained() async throws {
145+
let downloadDestination: URL = {
146+
let base = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
147+
return base.appending(component: "hf-local-pretrained-tests-downloads")
148+
}()
149+
150+
let hubApi = HubApi(downloadBase: downloadDestination)
151+
let downloadedTo = try await hubApi.snapshot(from: "pcuenq/gemma-tokenizer")
152+
153+
let tokenizer = try await AutoTokenizer.from(modelFolder: downloadedTo) as? PreTrainedTokenizer
154+
XCTAssertNotNil(tokenizer)
155+
156+
try FileManager.default.removeItem(at: downloadDestination)
157+
}
158+
}
159+
143160
class BertDiacriticsTests: XCTestCase {
144161
func testBertCased() async throws {
145162
guard let tokenizer = try await AutoTokenizer.from(pretrained: "distilbert/distilbert-base-multilingual-cased") as? PreTrainedTokenizer else {

0 commit comments

Comments
 (0)