forked from huggingface/swift-coreml-diffusers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatusView.swift
139 lines (128 loc) · 4.58 KB
/
StatusView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// StatusView.swift
// Diffusion-macOS
//
// Created by Cyril Zakka on 1/12/23.
// See LICENSE at https://github.com/huggingface/swift-coreml-diffusers/LICENSE
//
import SwiftUI
struct StatusView: View {
@EnvironmentObject var generation: GenerationContext
var pipelineState: Binding<PipelineState>
@State private var showErrorPopover = false
func submit() {
if case .running = generation.state { return }
Task {
generation.state = .running(nil)
do {
let result = try await generation.generate()
if result.userCanceled {
generation.state = .userCanceled
} else {
generation.state = .complete(generation.positivePrompt, result.image, result.lastSeed, result.interval)
}
} catch {
generation.state = .failed(error)
}
}
}
func errorWithDetails(_ message: String, error: Error) -> any View {
HStack {
Text(message)
Spacer()
Button {
showErrorPopover.toggle()
} label: {
Image(systemName: "info.circle")
}.buttonStyle(.plain)
.popover(isPresented: $showErrorPopover) {
VStack {
Text(verbatim: "\(error)")
.lineLimit(nil)
.padding(.all, 5)
Button {
showErrorPopover.toggle()
} label: {
Text("Dismiss").frame(maxWidth: 200)
}
.padding(.bottom)
}
.frame(minWidth: 400, idealWidth: 400, maxWidth: 400)
.fixedSize()
}
}
}
func generationStatusView() -> any View {
switch generation.state {
case .startup: return EmptyView()
case .running(let progress):
guard let progress = progress, progress.stepCount > 0 else {
// The first time it takes a little bit before generation starts
return HStack {
Text("Preparing model…")
Spacer()
}
}
let step = Int(progress.step) + 1
let fraction = Double(step) / Double(progress.stepCount)
return HStack {
Text("Generating \(Int(round(100*fraction)))%")
Spacer()
}
case .complete(_, let image, let lastSeed, let interval):
guard let _ = image else {
return HStack {
Text("Safety checker triggered, please try a different prompt or seed.")
Spacer()
}
}
return HStack {
let intervalString = String(format: "Time: %.1fs", interval ?? 0)
Text(intervalString)
Spacer()
if generation.seed != lastSeed {
Text(String("Seed: \(formatLargeNumber(lastSeed))"))
Button("Set") {
generation.seed = lastSeed
}
}
}.frame(maxHeight: 25)
case .failed(let error):
return errorWithDetails("Generation error", error: error)
case .userCanceled:
return HStack {
Text("Generation canceled.")
Spacer()
}
}
}
var body: some View {
switch pipelineState.wrappedValue {
case .downloading(let progress):
ProgressView("Downloading…", value: progress*100, total: 110).padding()
case .uncompressing:
ProgressView("Uncompressing…", value: 100, total: 110).padding()
case .loading:
ProgressView("Loading…", value: 105, total: 110).padding()
case .ready:
VStack {
Button {
submit()
} label: {
Text("Generate")
.frame(maxWidth: .infinity)
.frame(height: 50)
}
.buttonStyle(.borderedProminent)
AnyView(generationStatusView())
}
case .failed(let error):
AnyView(errorWithDetails("Pipeline loading error", error: error))
}
}
}
struct StatusView_Previews: PreviewProvider {
static var previews: some View {
StatusView(pipelineState: .constant(.downloading(0.2)))
}
}