// // Pipeline.swift // Diffusion // // Created by Pedro Cuenca on December 2022. // See LICENSE at https://github.com/huggingface/swift-coreml-diffusers/LICENSE // import Foundation import CoreML import Combine import StableDiffusion typealias StableDiffusionProgress = StableDiffusionPipeline.Progress struct GenerationResult { var image: CGImage? var lastSeed: UInt32 var interval: TimeInterval? } class Pipeline { let pipeline: StableDiffusionPipeline var progress: StableDiffusionProgress? = nil { didSet { progressPublisher.value = progress } } lazy private(set) var progressPublisher: CurrentValueSubject = CurrentValueSubject(progress) init(_ pipeline: StableDiffusionPipeline) { self.pipeline = pipeline } func generate( prompt: String, negativePrompt: String = "", scheduler: StableDiffusionScheduler, numInferenceSteps stepCount: Int = 50, seed: UInt32? = nil, guidanceScale: Float = 7.5, disableSafety: Bool = false ) throws -> GenerationResult { let beginDate = Date() print("Generating...") let theSeed = seed ?? UInt32.random(in: 0..