Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 39 additions & 38 deletions ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ struct ContentView: View {
prompt: Text("Enter a prompt to display an image"),
axis: .vertical,
label: {})
.disabled(prediction?.status.terminated == false)
.submitLabel(.go)
.onSubmit(of: .text) {
Task {
try! await generate()
}
.disabled(prediction?.status.terminated == false)
.submitLabel(.go)
.onSubmit(of: .text) {
Task {
try! await generate()
}
}
}

if let prediction {
Expand All @@ -52,36 +52,36 @@ struct ContentView: View {
.aspectRatio(1.0, contentMode: .fit)

switch prediction.status {
case .starting, .processing:
VStack{
ProgressView("Generating...")
.padding(32)

Button("Cancel") {
Task { try await cancel() }
}
}
case .failed:
Text(prediction.error?.localizedDescription ?? "Unknown error")
.foregroundColor(.red)
case .succeeded:
if let url = prediction.output?.first {
VStack {
AsyncImage(url: url, scale: 2.0, content: { phase in
phase.image?
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(32)
})

ShareLink("Export", item: url)
case .starting, .processing:
VStack{
ProgressView("Generating...")
.padding(32)


Button("Cancel") {
Task { try await cancel() }
}
}
case .failed:
Text(prediction.error?.localizedDescription ?? "Unknown error")
.foregroundColor(.red)
case .succeeded:
if let url = prediction.output?.first {
VStack {
AsyncImage(url: url, scale: 2.0, content: { phase in
phase.image?
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(32)
})

ShareLink("Export", item: url)
.padding(32)

}
}
}
case .canceled:
Text("The prediction was canceled")
.foregroundColor(.secondary)
case .canceled:
Text("The prediction was canceled")
.foregroundColor(.secondary)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
Expand All @@ -95,12 +95,13 @@ struct ContentView: View {
}

func generate() async throws {
prediction = try await StableDiffusion.predict(with: client,
input: .init(prompt: prompt))
try await prediction?.wait(with: client)
var result = try await StableDiffusion.predict(with: client, input: .init(prompt: prompt))
try await result.wait(with: client)
prediction = result
}

func cancel() async throws {
try await prediction?.cancel(with: client)
guard var result = prediction else { return }
try await result.cancel(with: client)
}
}