Skip to content

Commit

Permalink
PR feedback & minor optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
vinu-vanjari committed Mar 25, 2024
1 parent 6df531c commit 800353c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
20 changes: 5 additions & 15 deletions Sources/Hub/Hub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,19 @@ public class LanguageModelConfigurationFromHub {
) async throws -> Configurations {
let filesToDownload = ["config.json", "tokenizer_config.json", "tokenizer.json"]
let repo = Hub.Repo(id: modelName)
try await hubApi.snapshot(from: repo, matching: filesToDownload)
let downloadedModelFolder = try await hubApi.snapshot(from: repo, matching: filesToDownload)

// Note tokenizerConfig may be nil (does not exist in all models)
let modelConfig = try hubApi.configuration(from: "config.json", in: repo)
let tokenizerConfig = try? hubApi.configuration(from: "tokenizer_config.json", in: repo)
let tokenizerVocab = try hubApi.configuration(from: "tokenizer.json", in: repo)

let configs = Configurations(
modelConfig: modelConfig,
tokenizerConfig: tokenizerConfig,
tokenizerData: tokenizerVocab
)
return configs
return try await loadConfig(modelFolder: downloadedModelFolder, hubApi: hubApi)
}

func loadConfig(
modelFolder: URL,
hubApi: HubApi = .shared
) async throws -> Configurations {
// Note tokenizerConfig may be nil (does not exist in all models)
let modelConfig = try hubApi.configuration(url: modelFolder.appending(path: "config.json"))
let tokenizerConfig = try? hubApi.configuration(url: modelFolder.appending(path: "tokenizer_config.json"))
let tokenizerVocab = try hubApi.configuration(url: modelFolder.appending(path: "tokenizer.json"))
let modelConfig = try hubApi.configuration(fileURL: modelFolder.appending(path: "config.json"))
let tokenizerConfig = try? hubApi.configuration(fileURL: modelFolder.appending(path: "tokenizer_config.json"))
let tokenizerVocab = try hubApi.configuration(fileURL: modelFolder.appending(path: "tokenizer.json"))

let configs = Configurations(
modelConfig: modelConfig,
Expand Down
12 changes: 6 additions & 6 deletions Sources/Hub/HubApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ public extension HubApi {
/// Assumes the file has already been downloaded.
/// `filename` is relative to the download base.
func configuration(from filename: String, in repo: Repo) throws -> Config {
let url = localRepoLocation(repo).appending(path: filename)
return try configuration(url: url)
let fileURL = localRepoLocation(repo).appending(path: filename)
return try configuration(fileURL: fileURL)
}

/// Assumes the file has already present at local url.
/// `url` is complete local file path for given model
func configuration(url: URL) throws -> Config {
let data = try Data(contentsOf: url)
/// Assumes the file is already present at local url.
/// `fileURL` is a complete local file path for the given model
func configuration(fileURL: URL) throws -> Config {
let data = try Data(contentsOf: fileURL)
let parsed = try JSONSerialization.jsonObject(with: data, options: [])
guard let dictionary = parsed as? [String: Any] else { throw Hub.HubClientError.parse }
return Config(dictionary)
Expand Down

0 comments on commit 800353c

Please sign in to comment.