|
| 1 | +// |
| 2 | +// PromptView.swift |
| 3 | +// Diffusion-macOS |
| 4 | +// |
| 5 | +// Created by Cyril Zakka on 1/12/23. |
| 6 | +// See LICENSE at https://github.com/huggingface/swift-coreml-diffusers/LICENSE |
| 7 | +// |
| 8 | + |
| 9 | +import Combine |
| 10 | +import SwiftUI |
| 11 | +import CompactSlider |
| 12 | + |
| 13 | +enum PipelineState { |
| 14 | + case downloading(Double) |
| 15 | + case uncompressing |
| 16 | + case loading |
| 17 | + case ready |
| 18 | + case failed(Error) |
| 19 | +} |
| 20 | + |
| 21 | +struct ControlsView: View { |
| 22 | + @EnvironmentObject var generation: GenerationContext |
| 23 | + |
| 24 | + static let models = ModelInfo.MODELS |
| 25 | + static let modelNames = models.map { $0.modelVersion } |
| 26 | + |
| 27 | + @State private var model = Settings.shared.currentModel.modelVersion |
| 28 | + @State private var disclosedPrompt = true |
| 29 | + |
| 30 | + // TODO: refactor download with similar code in Loading.swift (iOS) |
| 31 | + @State private var stateSubscriber: Cancellable? |
| 32 | + @State private var pipelineState: PipelineState = .downloading(0) |
| 33 | + |
| 34 | + func modelDidChange(model: ModelInfo) { |
| 35 | + print("Loading model \(model)") |
| 36 | + Settings.shared.currentModel = model |
| 37 | + |
| 38 | + pipelineState = .downloading(0) |
| 39 | + Task.init { |
| 40 | + let loader = PipelineLoader(model: model) |
| 41 | + stateSubscriber = loader.statePublisher.sink { state in |
| 42 | + DispatchQueue.main.async { |
| 43 | + switch state { |
| 44 | + case .downloading(let progress): |
| 45 | + pipelineState = .downloading(progress) |
| 46 | + case .uncompressing: |
| 47 | + pipelineState = .uncompressing |
| 48 | + case .readyOnDisk: |
| 49 | + pipelineState = .loading |
| 50 | + default: |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + do { |
| 56 | + generation.pipeline = try await loader.prepare() |
| 57 | + pipelineState = .ready |
| 58 | + } catch { |
| 59 | + print("Could not load model, error: \(error)") |
| 60 | + pipelineState = .failed(error) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + var body: some View { |
| 66 | + VStack(alignment: .leading) { |
| 67 | + |
| 68 | + Label("Adjustments", systemImage: "gearshape.2") |
| 69 | + .font(.headline) |
| 70 | + .fontWeight(.bold) |
| 71 | + Divider() |
| 72 | + |
| 73 | + ScrollView { |
| 74 | + Group { |
| 75 | + DisclosureGroup { |
| 76 | + Picker("", selection: $model) { |
| 77 | + ForEach(Self.modelNames, id: \.self) { |
| 78 | + Text($0) |
| 79 | + } |
| 80 | + } |
| 81 | + .onChange(of: model) { theModel in |
| 82 | + guard let model = ModelInfo.from(modelVersion: theModel) else { return } |
| 83 | + modelDidChange(model: model) |
| 84 | + } |
| 85 | + } label: { |
| 86 | + Label("Model", systemImage: "cpu").foregroundColor(.secondary) |
| 87 | + } |
| 88 | + |
| 89 | + Divider() |
| 90 | + |
| 91 | + DisclosureGroup(isExpanded: $disclosedPrompt) { |
| 92 | + Group { |
| 93 | + TextField("Positive prompt", text: $generation.positivePrompt, |
| 94 | + axis: .vertical).lineLimit(5) |
| 95 | + .textFieldStyle(.squareBorder) |
| 96 | + .listRowInsets(EdgeInsets(top: 0, leading: -20, bottom: 0, trailing: 20)) |
| 97 | + TextField("Negative prompt", text: $generation.negativePrompt, |
| 98 | + axis: .vertical).lineLimit(5) |
| 99 | + .textFieldStyle(.squareBorder) |
| 100 | + }.padding(.leading, 10) |
| 101 | + } label: { |
| 102 | + Label("Prompts", systemImage: "text.quote").foregroundColor(.secondary) |
| 103 | + } |
| 104 | + |
| 105 | + Divider() |
| 106 | + |
| 107 | + DisclosureGroup { |
| 108 | + CompactSlider(value: $generation.steps, in: 0...150, step: 5) { |
| 109 | + Text("Steps") |
| 110 | + Spacer() |
| 111 | + Text("\(Int(generation.steps))") |
| 112 | + }.padding(.leading, 10) |
| 113 | + } label: { |
| 114 | + Label("Step count", systemImage: "square.3.layers.3d.down.left").foregroundColor(.secondary) |
| 115 | + } |
| 116 | + Divider() |
| 117 | + |
| 118 | +// DisclosureGroup() { |
| 119 | +// CompactSlider(value: $generation.numImages, in: 0...10, step: 1) { |
| 120 | +// Text("Number of Images") |
| 121 | +// Spacer() |
| 122 | +// Text("\(Int(generation.numImages))") |
| 123 | +// }.padding(.leading, 10) |
| 124 | +// } label: { |
| 125 | +// Label("Number of images", systemImage: "photo.stack").foregroundColor(.secondary) |
| 126 | +// } |
| 127 | +// Divider() |
| 128 | + |
| 129 | + DisclosureGroup() { |
| 130 | + let sliderLabel = generation.seed < 0 ? "Random Seed" : "Seed" |
| 131 | + CompactSlider(value: $generation.seed, in: -1...1000, step: 1) { |
| 132 | + Text(sliderLabel) |
| 133 | + Spacer() |
| 134 | + Text("\(Int(generation.seed))") |
| 135 | + }.padding(.leading, 10) |
| 136 | + } label: { |
| 137 | + Label("Seed", systemImage: "leaf").foregroundColor(.secondary) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + StatusView(pipelineState: $pipelineState) |
| 143 | + } |
| 144 | + .padding() |
| 145 | + .onAppear { |
| 146 | + print(PipelineLoader.models) |
| 147 | + modelDidChange(model: ModelInfo.from(modelVersion: model) ?? ModelInfo.v2Base) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
0 commit comments