mirror of
https://github.com/GayPizzaSpecifications/stable-diffusion-rpc.git
synced 2025-08-04 05:51:32 +00:00
Implement a Java/Kotlin client.
This commit is contained in:
30
Sources/StableDiffusionControl/StableDiffusionClient.swift
Normal file
30
Sources/StableDiffusionControl/StableDiffusionClient.swift
Normal file
@ -0,0 +1,30 @@
|
||||
import Foundation
|
||||
import GRPC
|
||||
import NIO
|
||||
import StableDiffusionCore
|
||||
import StableDiffusionProtos
|
||||
|
||||
struct StableDiffusionClient {
|
||||
let group: EventLoopGroup
|
||||
let channel: GRPCChannel
|
||||
|
||||
let modelService: SdModelServiceAsyncClient
|
||||
let imageGenerationService: SdImageGenerationServiceAsyncClient
|
||||
|
||||
init(connectionTarget: ConnectionTarget, transportSecurity: GRPCChannelPool.Configuration.TransportSecurity) throws {
|
||||
group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
|
||||
|
||||
channel = try GRPCChannelPool.with(
|
||||
target: connectionTarget,
|
||||
transportSecurity: transportSecurity,
|
||||
eventLoopGroup: group
|
||||
)
|
||||
|
||||
modelService = SdModelServiceAsyncClient(channel: channel)
|
||||
imageGenerationService = SdImageGenerationServiceAsyncClient(channel: channel)
|
||||
}
|
||||
|
||||
func close() async throws {
|
||||
try await group.shutdownGracefully()
|
||||
}
|
||||
}
|
37
Sources/StableDiffusionControl/main.swift
Normal file
37
Sources/StableDiffusionControl/main.swift
Normal file
@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
import GRPC
|
||||
import NIO
|
||||
import StableDiffusionProtos
|
||||
import System
|
||||
|
||||
let client = try StableDiffusionClient(connectionTarget: .host("127.0.0.1", port: 4546), transportSecurity: .plaintext)
|
||||
|
||||
Task { @MainActor in
|
||||
do {
|
||||
let modelListResponse = try await client.modelService.listModels(.init())
|
||||
print("Loading model...")
|
||||
let modelInfo = modelListResponse.models.first { $0.name == "anything-4.5" }!
|
||||
_ = try await client.modelService.loadModel(.with { request in
|
||||
request.modelName = modelInfo.name
|
||||
})
|
||||
print("Loaded model.")
|
||||
|
||||
print("Generating image...")
|
||||
let request = SdGenerateImagesRequest.with {
|
||||
$0.modelName = modelInfo.name
|
||||
$0.prompt = "cat"
|
||||
$0.imageCount = 1
|
||||
}
|
||||
|
||||
let response = try await client.imageGenerationService.generateImage(request)
|
||||
let image = response.images.first!
|
||||
try image.content.write(to: URL(filePath: "output.png"))
|
||||
print("Generated image to output.png")
|
||||
exit(0)
|
||||
} catch {
|
||||
print(error)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
dispatchMain()
|
Reference in New Issue
Block a user