Simple display of time interval.

pull/6/head
Pedro Cuenca 2 years ago
parent a660d47fa3
commit da83fdfff1

@ -29,7 +29,7 @@ class Pipeline {
self.pipeline = pipeline self.pipeline = pipeline
} }
func generate(prompt: String, scheduler: StableDiffusionScheduler, numInferenceSteps stepCount: Int = 50, seed: UInt32? = nil) throws -> CGImage { func generate(prompt: String, scheduler: StableDiffusionScheduler, numInferenceSteps stepCount: Int = 50, seed: UInt32? = nil) throws -> (CGImage, TimeInterval) {
let beginDate = Date() let beginDate = Date()
print("Generating...") print("Generating...")
let theSeed = seed ?? UInt32.random(in: 0..<UInt32.max) let theSeed = seed ?? UInt32.random(in: 0..<UInt32.max)
@ -43,11 +43,12 @@ class Pipeline {
handleProgress(progress) handleProgress(progress)
return true return true
} }
print("Got images: \(images) in \(Date().timeIntervalSince(beginDate))") let interval = Date().timeIntervalSince(beginDate)
print("Got images: \(images) in \(interval)")
// unwrap the 1 image we asked for // unwrap the 1 image we asked for
guard let image = images.compactMap({ $0 }).first else { throw "Generation failed" } guard let image = images.compactMap({ $0 }).first else { throw "Generation failed" }
return image return (image, interval)
} }
func handleProgress(_ progress: StableDiffusionPipeline.Progress) { func handleProgress(_ progress: StableDiffusionPipeline.Progress) {

@ -15,7 +15,7 @@ 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? { func generate(pipeline: Pipeline?, prompt: String) async -> (CGImage, TimeInterval)? {
guard let pipeline = pipeline else { return nil } guard let pipeline = pipeline else { return nil }
return try? pipeline.generate(prompt: prompt, scheduler: scheduler, numInferenceSteps: steps, seed: seed) return try? pipeline.generate(prompt: prompt, scheduler: scheduler, numInferenceSteps: steps, seed: seed)
} }
@ -23,7 +23,7 @@ func generate(pipeline: Pipeline?, prompt: String) async -> CGImage? {
enum GenerationState { enum GenerationState {
case startup case startup
case running(StableDiffusionProgress?) case running(StableDiffusionProgress?)
case idle(String) case idle(String, TimeInterval?)
} }
/// Presents "Share" + "Save" buttons on Mac; just "Share" on iOS/iPadOS. /// Presents "Share" + "Save" buttons on Mac; just "Share" on iOS/iPadOS.
@ -82,7 +82,7 @@ struct ImageWithPlaceholder: View {
let fraction = Double(step) / Double(progress.stepCount) let fraction = Double(step) / Double(progress.stepCount)
let label = "Step \(step) of \(progress.stepCount)" let label = "Step \(step) of \(progress.stepCount)"
return AnyView(ProgressView(label, value: fraction, total: 1).padding()) return AnyView(ProgressView(label, value: fraction, total: 1).padding())
case .idle(let lastPrompt): case .idle(let lastPrompt, let interval):
guard let theImage = image.wrappedValue else { guard let theImage = image.wrappedValue else {
return AnyView(Image(systemName: "exclamationmark.triangle").resizable()) return AnyView(Image(systemName: "exclamationmark.triangle").resizable())
} }
@ -91,7 +91,14 @@ struct ImageWithPlaceholder: View {
return AnyView( return AnyView(
VStack { VStack {
imageView.resizable().clipShape(RoundedRectangle(cornerRadius: 20)) imageView.resizable().clipShape(RoundedRectangle(cornerRadius: 20))
HStack {
if let interval = interval {
Text(String(format:"Time: %.1fs", interval))
}
Spacer()
ShareButtons(image: theImage, name: lastPrompt) ShareButtons(image: theImage, name: lastPrompt)
Spacer()
}
}) })
} }
} }
@ -110,8 +117,9 @@ struct TextToImage: View {
if case .running = state { return } if case .running = state { return }
Task { Task {
state = .running(nil) state = .running(nil)
image = await generate(pipeline: context.pipeline, prompt: prompt) let interval: TimeInterval?
state = .idle(prompt) (image, interval) = await generate(pipeline: context.pipeline, prompt: prompt) ?? (nil, nil)
state = .idle(prompt, interval)
} }
} }

Loading…
Cancel
Save