|
| 1 | +// |
| 2 | +// PromptTextField.swift |
| 3 | +// Diffusion-macOS |
| 4 | +// |
| 5 | +// Created by Dolmere on 22/06/2023. |
| 6 | +// See LICENSE at https://github.com/huggingface/swift-coreml-diffusers/LICENSE |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | +import Combine |
| 11 | +import StableDiffusion |
| 12 | + |
| 13 | +struct PromptTextField: View { |
| 14 | + @State private var output: String = "" |
| 15 | + @State private var input: String = "" |
| 16 | + @State private var typing = false |
| 17 | + @State private var tokenCount: Int = 0 |
| 18 | + @State var isPositivePrompt: Bool = true |
| 19 | + @State private var tokenizer: BPETokenizer? |
| 20 | + @State private var currentModelVersion: String = "" |
| 21 | + |
| 22 | + @Binding var textBinding: String |
| 23 | + @Binding var model: String // the model version as it's stored in Settings |
| 24 | + |
| 25 | + private let maxTokenCount = 77 |
| 26 | + |
| 27 | + private var modelInfo: ModelInfo? { |
| 28 | + ModelInfo.from(modelVersion: $model.wrappedValue) |
| 29 | + } |
| 30 | + |
| 31 | + private var filename: String? { |
| 32 | + let variant = modelInfo?.bestAttention ?? .original |
| 33 | + return modelInfo?.modelURL(for: variant).lastPathComponent |
| 34 | + } |
| 35 | + |
| 36 | + private var downloadedURL: URL? { |
| 37 | + if let filename = filename { |
| 38 | + return PipelineLoader.models.appendingPathComponent(filename) |
| 39 | + } |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + private var packagesFilename: String? { |
| 44 | + (filename as NSString?)?.deletingPathExtension |
| 45 | + } |
| 46 | + |
| 47 | + private var compiledURL: URL? { |
| 48 | + if let packagesFilename = packagesFilename { |
| 49 | + return downloadedURL?.deletingLastPathComponent().appendingPathComponent(packagesFilename) |
| 50 | + } |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + private var textColor: Color { |
| 55 | + switch tokenCount { |
| 56 | + case 0...65: |
| 57 | + return .green |
| 58 | + case 66...75: |
| 59 | + return .orange |
| 60 | + default: |
| 61 | + return .red |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // macOS initializer |
| 66 | + init(text: Binding<String>, isPositivePrompt: Bool, model: Binding<String>) { |
| 67 | + _textBinding = text |
| 68 | + self.isPositivePrompt = isPositivePrompt |
| 69 | + _model = model |
| 70 | + } |
| 71 | + |
| 72 | + // iOS initializer |
| 73 | + init(text: Binding<String>, isPositivePrompt: Bool, model: String) { |
| 74 | + _textBinding = text |
| 75 | + self.isPositivePrompt = isPositivePrompt |
| 76 | + _model = .constant(model) |
| 77 | + } |
| 78 | + |
| 79 | + var body: some View { |
| 80 | + VStack { |
| 81 | + #if os(macOS) |
| 82 | + TextField(isPositivePrompt ? "Positive prompt" : "Negative Prompt", text: $textBinding, |
| 83 | + axis: .vertical) |
| 84 | + .lineLimit(20) |
| 85 | + .textFieldStyle(.squareBorder) |
| 86 | + .listRowInsets(EdgeInsets(top: 0, leading: -20, bottom: 0, trailing: 20)) |
| 87 | + .foregroundColor(textColor == .green ? .primary : textColor) |
| 88 | + .frame(minHeight: 30) |
| 89 | + if modelInfo != nil && tokenizer != nil { |
| 90 | + HStack { |
| 91 | + Spacer() |
| 92 | + if !textBinding.isEmpty { |
| 93 | + Text("\(tokenCount)") |
| 94 | + .foregroundColor(textColor) |
| 95 | + Text(" / \(maxTokenCount)") |
| 96 | + } |
| 97 | + } |
| 98 | + .onReceive(Just(textBinding)) { text in |
| 99 | + updateTokenCount(newText: text) |
| 100 | + } |
| 101 | + .font(.caption) |
| 102 | + } |
| 103 | + #else |
| 104 | + TextField("Prompt", text: $textBinding, axis: .vertical) |
| 105 | + .lineLimit(20) |
| 106 | + .listRowInsets(EdgeInsets(top: 0, leading: -20, bottom: 0, trailing: 20)) |
| 107 | + .foregroundColor(textColor == .green ? .primary : textColor) |
| 108 | + .frame(minHeight: 30) |
| 109 | + HStack { |
| 110 | + if !textBinding.isEmpty { |
| 111 | + Text("\(tokenCount)") |
| 112 | + .foregroundColor(textColor) |
| 113 | + Text(" / \(maxTokenCount)") |
| 114 | + } |
| 115 | + Spacer() |
| 116 | + } |
| 117 | + .onReceive(Just(textBinding)) { text in |
| 118 | + updateTokenCount(newText: text) |
| 119 | + } |
| 120 | + .font(.caption) |
| 121 | + #endif |
| 122 | + } |
| 123 | + .onChange(of: model) { model in |
| 124 | + updateTokenCount(newText: textBinding) |
| 125 | + } |
| 126 | + .onAppear { |
| 127 | + updateTokenCount(newText: textBinding) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private func updateTokenCount(newText: String) { |
| 132 | + // ensure that the compiled URL exists |
| 133 | + guard let compiledURL = compiledURL else { return } |
| 134 | + // Initialize the tokenizer only when it's not created yet or the model changes |
| 135 | + // Check if the model version has changed |
| 136 | + let modelVersion = $model.wrappedValue |
| 137 | + if modelVersion != currentModelVersion { |
| 138 | + do { |
| 139 | + tokenizer = try BPETokenizer( |
| 140 | + mergesAt: compiledURL.appendingPathComponent("merges.txt"), |
| 141 | + vocabularyAt: compiledURL.appendingPathComponent("vocab.json") |
| 142 | + ) |
| 143 | + currentModelVersion = modelVersion |
| 144 | + } catch { |
| 145 | + print("Failed to create tokenizer: \(error)") |
| 146 | + return |
| 147 | + } |
| 148 | + } |
| 149 | + let (tokens, _) = tokenizer?.tokenize(input: newText) ?? ([], []) |
| 150 | + |
| 151 | + DispatchQueue.main.async { |
| 152 | + self.tokenCount = tokens.count |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments