mirror of
https://github.com/GayPizzaSpecifications/stable-diffusion-rpc.git
synced 2025-08-05 06:21:31 +00:00
Formatting, linting, and hopefully a CI build.
This commit is contained in:
31
.github/workflows/macos.yml
vendored
Normal file
31
.github/workflows/macos.yml
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
name: macOS
|
||||||
|
on: [push]
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: macos-12
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Build Executable
|
||||||
|
run: swift build -c release --arch arm64 --arch x86_64
|
||||||
|
- name: Copy Executable
|
||||||
|
run: cp .build/apple/Products/Release/StableDiffusionServer StableDiffusionServer
|
||||||
|
- name: Archive Executable
|
||||||
|
uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: StableDiffusionServer
|
||||||
|
path: StableDiffusionServer
|
||||||
|
format:
|
||||||
|
runs-on: macos-12
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Swift Format
|
||||||
|
run: swiftformat --lint Package.swift Sources
|
||||||
|
lint:
|
||||||
|
runs-on: macos-12
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Swift Lint
|
||||||
|
run: swiftlint Package.swift Sources
|
1
.swift-version
Normal file
1
.swift-version
Normal file
@ -0,0 +1 @@
|
|||||||
|
5.6
|
4
.swiftformat
Normal file
4
.swiftformat
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
--indent 4
|
||||||
|
--disable trailingCommas
|
||||||
|
--exclude "Sources/StableDiffusionProtos/*.pb.swift"
|
||||||
|
--exclude "Sources/StableDiffusionProtos/*.grpc.swift"
|
4
.swiftlint.yml
Normal file
4
.swiftlint.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
line_length: 180
|
||||||
|
excluded:
|
||||||
|
- Sources/StableDiffusionProtos/*.pb.swift
|
||||||
|
- Sources/StableDiffusionProtos/*.grpc.swift
|
@ -16,17 +16,22 @@ let package = Package(
|
|||||||
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0")
|
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
|
.target(name: "StableDiffusionProtos", dependencies: [
|
||||||
|
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
|
||||||
|
.product(name: "GRPC", package: "grpc-swift")
|
||||||
|
]),
|
||||||
|
.target(name: "StableDiffusionCore", dependencies: [
|
||||||
|
.product(name: "StableDiffusion", package: "ml-stable-diffusion"),
|
||||||
|
.target(name: "StableDiffusionProtos")
|
||||||
|
]),
|
||||||
.executableTarget(name: "StableDiffusionServer", dependencies: [
|
.executableTarget(name: "StableDiffusionServer", dependencies: [
|
||||||
.product(name: "StableDiffusion", package: "ml-stable-diffusion"),
|
.product(name: "StableDiffusion", package: "ml-stable-diffusion"),
|
||||||
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
|
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
|
||||||
.product(name: "GRPC", package: "grpc-swift"),
|
.product(name: "GRPC", package: "grpc-swift"),
|
||||||
.target(name: "StableDiffusionProtos"),
|
.target(name: "StableDiffusionProtos"),
|
||||||
|
.target(name: "StableDiffusionCore"),
|
||||||
.product(name: "ArgumentParser", package: "swift-argument-parser")
|
.product(name: "ArgumentParser", package: "swift-argument-parser")
|
||||||
]),
|
]),
|
||||||
.target(name: "StableDiffusionProtos", dependencies: [
|
|
||||||
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
|
|
||||||
.product(name: "GRPC", package: "grpc-swift")
|
|
||||||
]),
|
|
||||||
.executableTarget(name: "TestStableDiffusionClient", dependencies: [
|
.executableTarget(name: "TestStableDiffusionClient", dependencies: [
|
||||||
.target(name: "StableDiffusionProtos"),
|
.target(name: "StableDiffusionProtos"),
|
||||||
.product(name: "GRPC", package: "grpc-swift")
|
.product(name: "GRPC", package: "grpc-swift")
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
enum SdServerError: Error {
|
public enum SdCoreError: Error {
|
||||||
case modelNotLoaded
|
case modelNotLoaded
|
||||||
case imageEncode
|
case imageEncode
|
||||||
case modelNotFound
|
case modelNotFound
|
@ -1,22 +1,22 @@
|
|||||||
import Foundation
|
|
||||||
import CoreImage
|
import CoreImage
|
||||||
|
import Foundation
|
||||||
import UniformTypeIdentifiers
|
import UniformTypeIdentifiers
|
||||||
|
|
||||||
extension CGImage {
|
extension CGImage {
|
||||||
func toPngData() throws -> Data {
|
func toPngData() throws -> Data {
|
||||||
guard let data = CFDataCreateMutable(nil, 0) else {
|
guard let data = CFDataCreateMutable(nil, 0) else {
|
||||||
throw SdServerError.imageEncode
|
throw SdCoreError.imageEncode
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else {
|
guard let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else {
|
||||||
throw SdServerError.imageEncode
|
throw SdCoreError.imageEncode
|
||||||
}
|
}
|
||||||
|
|
||||||
CGImageDestinationAddImage(destination, self, nil)
|
CGImageDestinationAddImage(destination, self, nil)
|
||||||
if CGImageDestinationFinalize(destination) {
|
if CGImageDestinationFinalize(destination) {
|
||||||
return data as Data
|
return data as Data
|
||||||
} else {
|
} else {
|
||||||
throw SdServerError.imageEncode
|
throw SdCoreError.imageEncode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ import Foundation
|
|||||||
import StableDiffusion
|
import StableDiffusion
|
||||||
import StableDiffusionProtos
|
import StableDiffusionProtos
|
||||||
|
|
||||||
actor ModelManager {
|
public actor ModelManager {
|
||||||
private var modelInfos: [String: SdModelInfo] = [:]
|
private var modelInfos: [String: SdModelInfo] = [:]
|
||||||
private var modelUrls: [String: URL] = [:]
|
private var modelUrls: [String: URL] = [:]
|
||||||
private var modelStates: [String: ModelState] = [:]
|
private var modelStates: [String: ModelState] = [:]
|
||||||
@ -13,7 +13,7 @@ actor ModelManager {
|
|||||||
self.modelBaseURL = modelBaseURL
|
self.modelBaseURL = modelBaseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func reloadModels() throws {
|
public func reloadModels() throws {
|
||||||
modelInfos.removeAll()
|
modelInfos.removeAll()
|
||||||
modelStates.removeAll()
|
modelStates.removeAll()
|
||||||
let contents = try FileManager.default.contentsOfDirectory(at: modelBaseURL.resolvingSymlinksInPath(), includingPropertiesForKeys: [.isDirectoryKey])
|
let contents = try FileManager.default.contentsOfDirectory(at: modelBaseURL.resolvingSymlinksInPath(), includingPropertiesForKeys: [.isDirectoryKey])
|
||||||
@ -25,12 +25,12 @@ actor ModelManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listModels() -> [SdModelInfo] {
|
public func listModels() -> [SdModelInfo] {
|
||||||
return Array(modelInfos.values)
|
Array(modelInfos.values)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getModelState(name: String) -> ModelState? {
|
public func getModelState(name: String) -> ModelState? {
|
||||||
return modelStates[name]
|
modelStates[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
private func addModel(url: URL) throws {
|
private func addModel(url: URL) throws {
|
@ -1,18 +1,18 @@
|
|||||||
import Foundation
|
|
||||||
import StableDiffusionProtos
|
|
||||||
import StableDiffusion
|
|
||||||
import CoreML
|
import CoreML
|
||||||
|
import Foundation
|
||||||
|
import StableDiffusion
|
||||||
|
import StableDiffusionProtos
|
||||||
|
|
||||||
actor ModelState {
|
public actor ModelState {
|
||||||
private let url: URL
|
private let url: URL
|
||||||
private var pipeline: StableDiffusionPipeline? = nil
|
private var pipeline: StableDiffusionPipeline?
|
||||||
private var tokenizer: BPETokenizer? = nil
|
private var tokenizer: BPETokenizer?
|
||||||
|
|
||||||
init(url: URL) throws {
|
public init(url: URL) throws {
|
||||||
self.url = url
|
self.url = url
|
||||||
}
|
}
|
||||||
|
|
||||||
func load() throws {
|
public func load() throws {
|
||||||
let config = MLModelConfiguration()
|
let config = MLModelConfiguration()
|
||||||
config.computeUnits = .all
|
config.computeUnits = .all
|
||||||
pipeline = try StableDiffusionPipeline(
|
pipeline = try StableDiffusionPipeline(
|
||||||
@ -27,9 +27,9 @@ actor ModelState {
|
|||||||
tokenizer = try BPETokenizer(mergesAt: mergesUrl, vocabularyAt: vocabUrl)
|
tokenizer = try BPETokenizer(mergesAt: mergesUrl, vocabularyAt: vocabUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generate(_ request: SdGenerateImagesRequest) throws -> SdGenerateImagesResponse {
|
public func generate(_ request: SdGenerateImagesRequest) throws -> SdGenerateImagesResponse {
|
||||||
guard let pipeline else {
|
guard let pipeline else {
|
||||||
throw SdServerError.modelNotLoaded
|
throw SdCoreError.modelNotLoaded
|
||||||
}
|
}
|
||||||
|
|
||||||
var pipelineConfig = StableDiffusionPipeline.Configuration(prompt: request.prompt)
|
var pipelineConfig = StableDiffusionPipeline.Configuration(prompt: request.prompt)
|
@ -1,5 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import GRPC
|
import GRPC
|
||||||
|
import StableDiffusionCore
|
||||||
import StableDiffusionProtos
|
import StableDiffusionProtos
|
||||||
|
|
||||||
class ImageGenerationServiceProvider: SdImageGenerationServiceAsyncProvider {
|
class ImageGenerationServiceProvider: SdImageGenerationServiceAsyncProvider {
|
||||||
@ -9,9 +10,9 @@ class ImageGenerationServiceProvider: SdImageGenerationServiceAsyncProvider {
|
|||||||
self.modelManager = modelManager
|
self.modelManager = modelManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateImage(request: SdGenerateImagesRequest, context: GRPCAsyncServerCallContext) async throws -> SdGenerateImagesResponse {
|
func generateImage(request: SdGenerateImagesRequest, context _: GRPCAsyncServerCallContext) async throws -> SdGenerateImagesResponse {
|
||||||
guard let state = await modelManager.getModelState(name: request.modelName) else {
|
guard let state = await modelManager.getModelState(name: request.modelName) else {
|
||||||
throw SdServerError.modelNotFound
|
throw SdCoreError.modelNotFound
|
||||||
}
|
}
|
||||||
return try await state.generate(request)
|
return try await state.generate(request)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import GRPC
|
import GRPC
|
||||||
|
import StableDiffusionCore
|
||||||
import StableDiffusionProtos
|
import StableDiffusionProtos
|
||||||
|
|
||||||
class ModelServiceProvider: SdModelServiceAsyncProvider {
|
class ModelServiceProvider: SdModelServiceAsyncProvider {
|
||||||
@ -9,21 +10,21 @@ class ModelServiceProvider: SdModelServiceAsyncProvider {
|
|||||||
self.modelManager = modelManager
|
self.modelManager = modelManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func listModels(request: SdListModelsRequest, context: GRPCAsyncServerCallContext) async throws -> SdListModelsResponse {
|
func listModels(request _: SdListModelsRequest, context _: GRPCAsyncServerCallContext) async throws -> SdListModelsResponse {
|
||||||
let models = await modelManager.listModels()
|
let models = await modelManager.listModels()
|
||||||
var response = SdListModelsResponse()
|
var response = SdListModelsResponse()
|
||||||
response.models.append(contentsOf: models)
|
response.models.append(contentsOf: models)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
func reloadModels(request: SdReloadModelsRequest, context: GRPCAsyncServerCallContext) async throws -> SdReloadModelsResponse {
|
func reloadModels(request _: SdReloadModelsRequest, context _: GRPCAsyncServerCallContext) async throws -> SdReloadModelsResponse {
|
||||||
try await modelManager.reloadModels()
|
try await modelManager.reloadModels()
|
||||||
return SdReloadModelsResponse()
|
return SdReloadModelsResponse()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadModel(request: SdLoadModelRequest, context: GRPCAsyncServerCallContext) async throws -> SdLoadModelResponse {
|
func loadModel(request: SdLoadModelRequest, context _: GRPCAsyncServerCallContext) async throws -> SdLoadModelResponse {
|
||||||
guard let state = await modelManager.getModelState(name: request.modelName) else {
|
guard let state = await modelManager.getModelState(name: request.modelName) else {
|
||||||
throw SdServerError.modelNotFound
|
throw SdCoreError.modelNotFound
|
||||||
}
|
}
|
||||||
try await state.load()
|
try await state.load()
|
||||||
return SdLoadModelResponse()
|
return SdLoadModelResponse()
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import Foundation
|
|
||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
import Foundation
|
||||||
import GRPC
|
import GRPC
|
||||||
import NIO
|
import NIO
|
||||||
|
import StableDiffusionCore
|
||||||
import System
|
import System
|
||||||
|
|
||||||
struct ServerCommand: ParsableCommand {
|
struct ServerCommand: ParsableCommand {
|
||||||
@ -35,6 +36,6 @@ struct ServerCommand: ParsableCommand {
|
|||||||
|
|
||||||
dispatchMain()
|
dispatchMain()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerCommand.main()
|
ServerCommand.main()
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import StableDiffusionProtos
|
|
||||||
import NIO
|
|
||||||
import System
|
|
||||||
import GRPC
|
import GRPC
|
||||||
|
import NIO
|
||||||
|
import StableDiffusionProtos
|
||||||
|
import System
|
||||||
|
|
||||||
let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
|
let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
|
||||||
defer {
|
defer {
|
||||||
@ -43,4 +43,5 @@ Task { @MainActor in
|
|||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatchMain()
|
dispatchMain()
|
||||||
|
Reference in New Issue
Block a user