@@ -443,7 +443,7 @@ public extension HubApi {
443443 /// We'll probably need to support Combine as well to play well with Swift UI
444444 /// (See for example PipelineLoader in swift-coreml-diffusers)
445445 @discardableResult
446- func download( progressHandler: @escaping ( Double ) -> Void ) async throws -> URL {
446+ func download( progressHandler: @escaping ( Double , Double ? ) -> Void ) async throws -> URL {
447447 let localMetadata = try hub. readDownloadMetadata ( metadataPath: metadataDestination)
448448 let remoteMetadata = try await hub. getFileMetadata ( url: source)
449449
@@ -499,8 +499,8 @@ public extension HubApi {
499499 switch state {
500500 case . notStarted:
501501 continue
502- case let . downloading( progress) :
503- progressHandler ( progress)
502+ case let . downloading( progress, speed ) :
503+ progressHandler ( progress, speed )
504504 case let . failed( error) :
505505 throw error
506506 case . completed:
@@ -583,8 +583,12 @@ public extension HubApi {
583583 backgroundSession: useBackgroundSession
584584 )
585585
586- try await downloader. download { fractionDownloaded in
586+ try await downloader. download { fractionDownloaded, speed in
587587 fileProgress. completedUnitCount = Int64 ( 100 * fractionDownloaded)
588+ if let speed {
589+ fileProgress. setUserInfoObject ( speed, forKey: . throughputKey)
590+ progress. setUserInfoObject ( speed, forKey: . throughputKey)
591+ }
588592 progressHandler ( progress)
589593 }
590594 if Task . isCancelled {
@@ -598,6 +602,14 @@ public extension HubApi {
598602 return repoDestination
599603 }
600604
605+ /// New overloads exposing speed directly in the snapshot progress handler
606+ @discardableResult func snapshot( from repo: Repo , revision: String = " main " , matching globs: [ String ] = [ ] , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
607+ try await snapshot ( from: repo, revision: revision, matching: globs) { progress in
608+ let speed = progress. userInfo [ . throughputKey] as? Double
609+ progressHandler ( progress, speed)
610+ }
611+ }
612+
601613 @discardableResult
602614 func snapshot( from repoId: String , revision: String = " main " , matching globs: [ String ] = [ ] , progressHandler: @escaping ( Progress ) -> Void = { _ in } ) async throws -> URL {
603615 try await snapshot ( from: Repo ( id: repoId) , revision: revision, matching: globs, progressHandler: progressHandler)
@@ -612,6 +624,22 @@ public extension HubApi {
612624 func snapshot( from repoId: String , revision: String = " main " , matching glob: String , progressHandler: @escaping ( Progress ) -> Void = { _ in } ) async throws -> URL {
613625 try await snapshot ( from: Repo ( id: repoId) , revision: revision, matching: [ glob] , progressHandler: progressHandler)
614626 }
627+
628+ /// Convenience overloads for other snapshot entry points with speed
629+ @discardableResult
630+ func snapshot( from repoId: String , revision: String = " main " , matching globs: [ String ] = [ ] , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
631+ try await snapshot ( from: Repo ( id: repoId) , revision: revision, matching: globs, progressHandler: progressHandler)
632+ }
633+
634+ @discardableResult
635+ func snapshot( from repo: Repo , revision: String = " main " , matching glob: String , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
636+ try await snapshot ( from: repo, revision: revision, matching: [ glob] , progressHandler: progressHandler)
637+ }
638+
639+ @discardableResult
640+ func snapshot( from repoId: String , revision: String = " main " , matching glob: String , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
641+ try await snapshot ( from: Repo ( id: repoId) , revision: revision, matching: [ glob] , progressHandler: progressHandler)
642+ }
615643}
616644
617645/// Metadata
@@ -822,6 +850,23 @@ public extension Hub {
822850 try await HubApi . shared. snapshot ( from: Repo ( id: repoId) , matching: glob, progressHandler: progressHandler)
823851 }
824852
853+ /// Overloads exposing speed via (Progress, Double?) where Double is bytes/sec
854+ static func snapshot( from repo: Repo , matching globs: [ String ] = [ ] , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
855+ try await HubApi . shared. snapshot ( from: repo, matching: globs, progressHandler: progressHandler)
856+ }
857+
858+ static func snapshot( from repoId: String , matching globs: [ String ] = [ ] , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
859+ try await HubApi . shared. snapshot ( from: Repo ( id: repoId) , matching: globs, progressHandler: progressHandler)
860+ }
861+
862+ static func snapshot( from repo: Repo , matching glob: String , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
863+ try await HubApi . shared. snapshot ( from: repo, matching: glob, progressHandler: progressHandler)
864+ }
865+
866+ static func snapshot( from repoId: String , matching glob: String , progressHandler: @escaping ( Progress , Double ? ) -> Void ) async throws -> URL {
867+ try await HubApi . shared. snapshot ( from: Repo ( id: repoId) , matching: glob, progressHandler: progressHandler)
868+ }
869+
825870 static func whoami( token: String ) async throws -> Config {
826871 try await HubApi ( hfToken: token) . whoami ( )
827872 }
0 commit comments