2023-04-22 14:52:27 -07:00
|
|
|
import Foundation
|
2023-04-22 15:43:22 -07:00
|
|
|
import GRPC
|
2023-04-22 14:52:27 -07:00
|
|
|
import NIO
|
2023-04-22 15:43:22 -07:00
|
|
|
import StableDiffusionProtos
|
2023-04-22 14:52:27 -07:00
|
|
|
import System
|
|
|
|
|
|
|
|
let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
|
|
|
|
defer {
|
2023-04-22 15:43:22 -07:00
|
|
|
try? group.syncShutdownGracefully()
|
2023-04-22 14:52:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let channel = try GRPCChannelPool.with(
|
2023-04-22 15:43:22 -07:00
|
|
|
target: .host("localhost", port: 4546),
|
|
|
|
transportSecurity: .plaintext,
|
|
|
|
eventLoopGroup: group
|
2023-04-22 14:52:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
let modelService = SdModelServiceAsyncClient(channel: channel)
|
|
|
|
let imageGeneratorService = SdImageGenerationServiceAsyncClient(channel: channel)
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
do {
|
|
|
|
let modelListResponse = try await modelService.listModels(.init())
|
|
|
|
print("Loading model...")
|
|
|
|
let modelInfo = modelListResponse.models.first { $0.name == "anything-4.5" }!
|
|
|
|
_ = try await modelService.loadModel(.with { request in
|
|
|
|
request.modelName = modelInfo.name
|
|
|
|
})
|
|
|
|
print("Loaded model.")
|
2023-04-22 15:43:22 -07:00
|
|
|
|
2023-04-22 14:52:27 -07:00
|
|
|
print("Generating image...")
|
|
|
|
let request = SdGenerateImagesRequest.with {
|
|
|
|
$0.modelName = modelInfo.name
|
|
|
|
$0.prompt = "cat"
|
|
|
|
$0.imageCount = 1
|
|
|
|
}
|
2023-04-22 15:43:22 -07:00
|
|
|
|
2023-04-22 14:52:27 -07:00
|
|
|
let response = try await imageGeneratorService.generateImage(request)
|
|
|
|
print("Generated image.")
|
|
|
|
print(response)
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 15:43:22 -07:00
|
|
|
|
2023-04-22 14:52:27 -07:00
|
|
|
dispatchMain()
|