Start work on more clean client.

This commit is contained in:
2023-04-22 16:32:54 -07:00
parent 1c0fbe02db
commit 4430bdcdd7
9 changed files with 69 additions and 26 deletions

View File

@ -15,6 +15,7 @@ public actor ModelManager {
public func reloadModels() throws {
modelInfos.removeAll()
modelUrls.removeAll()
modelStates.removeAll()
let contents = try FileManager.default.contentsOfDirectory(at: modelBaseURL.resolvingSymlinksInPath(), includingPropertiesForKeys: [.isDirectoryKey])
for subdirectoryURL in contents {
@ -29,6 +30,22 @@ public actor ModelManager {
Array(modelInfos.values)
}
public func createModelState(name: String) throws -> ModelState {
let state = modelStates[name]
let url = modelUrls[name]
guard let url else {
throw SdCoreError.modelNotFound
}
if state == nil {
let state = ModelState(url: url)
modelStates[name] = state
return state
} else {
return state!
}
}
public func getModelState(name: String) -> ModelState? {
modelStates[name]
}
@ -40,7 +57,6 @@ public actor ModelManager {
info.attention = attention ?? "unknown"
modelInfos[info.name] = info
modelUrls[info.name] = url
modelStates[info.name] = try ModelState(url: url)
}
private func getModelAttention(_ url: URL) -> String? {

View File

@ -8,13 +8,13 @@ public actor ModelState {
private var pipeline: StableDiffusionPipeline?
private var tokenizer: BPETokenizer?
public init(url: URL) throws {
public init(url: URL) {
self.url = url
}
public func load() throws {
let config = MLModelConfiguration()
config.computeUnits = .all
config.computeUnits = .cpuAndGPU
pipeline = try StableDiffusionPipeline(
resourcesAt: url,
controlNet: [],
@ -25,6 +25,7 @@ public actor ModelState {
let mergesUrl = url.appending(component: "merges.txt")
let vocabUrl = url.appending(component: "vocab.json")
tokenizer = try BPETokenizer(mergesAt: mergesUrl, vocabularyAt: vocabUrl)
try pipeline?.loadResources()
}
public func generate(_ request: SdGenerateImagesRequest) throws -> SdGenerateImagesResponse {