Skip to content

Commit 26707b8

Browse files
committed
preserve original repo file structure for metadata files
1 parent b4e1c49 commit 26707b8

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

Sources/Hub/HubApi.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ public extension HubApi {
248248
try FileManager.default.createDirectory(at: metadataDestination, withIntermediateDirectories: true, attributes: nil)
249249
}
250250

251-
func readDownloadMetadata(localDir: URL, filename: String) throws -> FileMetadata? {
252-
let metadataPath = localDir.appending(path: filename)
251+
func readDownloadMetadata(localDir: URL, filePath: String) throws -> FileMetadata? {
252+
let metadataPath = localDir.appending(path: filePath)
253253
if FileManager.default.fileExists(atPath: metadataPath.path) {
254254
let contents = try String(contentsOf: metadataPath, encoding: .utf8)
255255
let lines = contents.components(separatedBy: .newlines)
@@ -281,11 +281,12 @@ public extension HubApi {
281281
return regex?.firstMatch(in: hash, options: [], range: range) != nil
282282
}
283283

284-
func writeDownloadMetadata(commitHash: String, etag: String, metadataFileName: String) throws {
284+
func writeDownloadMetadata(commitHash: String, etag: String, metadataRelativePath: String) throws {
285285
let metadataContent = "\(commitHash)\n\(etag)\n\(Date().timeIntervalSince1970)\n"
286-
let metadataPath = metadataDestination.appending(component: metadataFileName)
286+
let metadataPath = metadataDestination.appending(component: metadataRelativePath)
287287

288288
do {
289+
try FileManager.default.createDirectory(at: metadataPath.deletingLastPathComponent(), withIntermediateDirectories: true)
289290
try metadataContent.write(to: metadataPath, atomically: true, encoding: .utf8)
290291
} catch {
291292
throw EnvironmentError.invalidMetadataError("Failed to write metadata file \(metadataPath)")
@@ -330,10 +331,10 @@ public extension HubApi {
330331
// (See for example PipelineLoader in swift-coreml-diffusers)
331332
@discardableResult
332333
func download(progressHandler: @escaping (Double) -> Void) async throws -> URL {
333-
var metadataFileName = (source.lastPathComponent as NSString).deletingPathExtension
334-
metadataFileName += ".metadata"
335-
336-
let localMetadata = try readDownloadMetadata(localDir: metadataDestination, filename: metadataFileName)
334+
var metadataRelativePath = (relativeFilename as NSString).deletingPathExtension
335+
metadataRelativePath += ".metadata"
336+
337+
let localMetadata = try readDownloadMetadata(localDir: metadataDestination, filePath: metadataRelativePath)
337338
let remoteMetadata = try await HubApi.shared.getFileMetadata(url: source)
338339

339340
let localCommitHash = localMetadata?.commitHash ?? ""
@@ -356,7 +357,7 @@ public extension HubApi {
356357
if downloaded {
357358
// etag matches => update metadata and return file
358359
if localMetadata?.etag == remoteEtag {
359-
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataFileName: metadataFileName)
360+
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataRelativePath: metadataRelativePath)
360361
return destination
361362
}
362363

@@ -367,7 +368,7 @@ public extension HubApi {
367368
if localMetadata != nil && isValidSHA256(remoteEtag) {
368369
let fileHash = try computeFileHash(file: destination)
369370
if fileHash == remoteEtag {
370-
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataFileName: metadataFileName)
371+
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataRelativePath: metadataRelativePath)
371372
return destination
372373
}
373374
}
@@ -387,7 +388,7 @@ public extension HubApi {
387388
try downloader.waitUntilDone()
388389
}
389390

390-
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataFileName: metadataFileName)
391+
try writeDownloadMetadata(commitHash: remoteCommitHash, etag: remoteEtag, metadataRelativePath: metadataRelativePath)
391392

392393
return destination
393394
}

Tests/HubTests/HubApiTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ class SnapshotDownloadTests: XCTestCase {
303303
".cache/huggingface/config.metadata",
304304
".cache/huggingface/tokenizer.metadata",
305305
".cache/huggingface/tokenizer_config.metadata",
306-
".cache/huggingface/Manifest.metadata",
307-
".cache/huggingface/FeatureDescriptions.metadata",
308-
".cache/huggingface/Metadata.metadata",
306+
".cache/huggingface/llama-2-7b-chat.mlpackage/Manifest.metadata",
307+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/FeatureDescriptions.metadata",
308+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/Metadata.metadata",
309309
])
310310
)
311311
}
@@ -348,9 +348,9 @@ class SnapshotDownloadTests: XCTestCase {
348348
".cache/huggingface/config.metadata",
349349
".cache/huggingface/tokenizer.metadata",
350350
".cache/huggingface/tokenizer_config.metadata",
351-
".cache/huggingface/Manifest.metadata",
352-
".cache/huggingface/FeatureDescriptions.metadata",
353-
".cache/huggingface/Metadata.metadata",
351+
".cache/huggingface/llama-2-7b-chat.mlpackage/Manifest.metadata",
352+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/FeatureDescriptions.metadata",
353+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/Metadata.metadata",
354354
])
355355
)
356356

@@ -405,9 +405,9 @@ class SnapshotDownloadTests: XCTestCase {
405405
".cache/huggingface/config.metadata",
406406
".cache/huggingface/tokenizer.metadata",
407407
".cache/huggingface/tokenizer_config.metadata",
408-
".cache/huggingface/Manifest.metadata",
409-
".cache/huggingface/FeatureDescriptions.metadata",
410-
".cache/huggingface/Metadata.metadata",
408+
".cache/huggingface/llama-2-7b-chat.mlpackage/Manifest.metadata",
409+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/FeatureDescriptions.metadata",
410+
".cache/huggingface/llama-2-7b-chat.mlpackage/Data/com.apple.CoreML/Metadata.metadata",
411411
])
412412
)
413413

0 commit comments

Comments
 (0)