Make iOS/Catalyst generation use GenerationContext.

pull/12/head
Pedro Cuenca 2 years ago
parent e14e0119cb
commit f92d53ae53

@ -11,15 +11,9 @@ import Combine
import StableDiffusion import StableDiffusion
// TODO: bind to UI controls // TODO: bind to UI controls
let scheduler = StableDiffusionScheduler.dpmSolverMultistepScheduler
let steps = 25 let steps = 25
let seed: UInt32? = nil let seed: UInt32? = nil
func generate(pipeline: Pipeline?, prompt: String) async -> (CGImage, TimeInterval)? {
guard let pipeline = pipeline else { return nil }
return try? pipeline.generate(prompt: prompt, scheduler: scheduler, numInferenceSteps: steps, seed: seed)
}
/// Presents "Share" + "Save" buttons on Mac; just "Share" on iOS/iPadOS. /// Presents "Share" + "Save" buttons on Mac; just "Share" on iOS/iPadOS.
/// This is because I didn't find a way for "Share" to show a Save option when running on macOS. /// This is because I didn't find a way for "Share" to show a Save option when running on macOS.
struct ShareButtons: View { struct ShareButtons: View {
@ -103,18 +97,15 @@ struct TextToImage: View {
@EnvironmentObject var context: GenerationContext @EnvironmentObject var context: GenerationContext
@State private var prompt = "Labrador in the style of Vermeer" @State private var prompt = "Labrador in the style of Vermeer"
@State private var image: CGImage? = nil
@State private var state: GenerationState = .startup
@State private var progressSubscriber: Cancellable?
func submit() { func submit() {
if case .running = state { return } if case .running = context.state { return }
Task { Task {
state = .running(nil) context.state = .running(nil)
let interval: TimeInterval? let interval: TimeInterval?
(image, interval) = await generate(pipeline: context.pipeline, prompt: prompt) ?? (nil, nil) let image: CGImage?
state = .complete(prompt, image, interval) (image, interval) = await context.generate(prompt: prompt, steps: steps, seed: seed) ?? (nil, nil)
context.state = .complete(prompt, image, interval)
} }
} }
@ -132,16 +123,10 @@ struct TextToImage: View {
.padding() .padding()
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
} }
ImageWithPlaceholder(state: $state) ImageWithPlaceholder(state: $context.state)
.scaledToFit() .scaledToFit()
Spacer() Spacer()
} }
.padding() .padding()
.onAppear {
progressSubscriber = context.pipeline!.progressPublisher.sink { progress in
guard let progress = progress else { return }
state = .running(progress)
}
}
} }
} }

Loading…
Cancel
Save