-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathContentView.swift
524 lines (472 loc) · 19.6 KB
/
ContentView.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright 2024 Apple Inc.
import AVKit
import AsyncAlgorithms
import CoreImage
import MLX
import MLXLMCommon
import MLXRandom
import MLXVLM
import PhotosUI
import SwiftUI
#if os(iOS) || os(visionOS)
typealias PlatformImage = UIImage
#else
typealias PlatformImage = NSImage
#endif
let videoSystemPrompt =
"Focus only on describing the key dramatic action or notable event occurring in this video segment. Skip general context or scene-setting details unless they are crucial to understanding the main action."
let imageSystemPrompt =
"You are an image understanding model capable of describing the salient features of any image."
struct ContentView: View {
@State var llm = VLMEvaluator()
@Environment(DeviceStat.self) private var deviceStat
@State private var selectedImage: PlatformImage? = nil {
didSet {
if selectedImage != nil {
selectedVideoURL = nil
player = nil
}
}
}
@State private var selectedVideoURL: URL? {
didSet {
if let selectedVideoURL {
player = AVPlayer(url: selectedVideoURL)
selectedImage = nil
}
}
}
@State private var showingImagePicker = false
@State private var selectedItem: PhotosPickerItem? = nil
@State private var player: AVPlayer? = nil
private var currentImageURL: URL? {
selectedImage == nil && selectedVideoURL == nil
? URL(
string:
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
) : nil
}
var body: some View {
VStack(alignment: .leading) {
VStack {
HStack {
Text(llm.modelInfo)
.textFieldStyle(.roundedBorder)
Spacer()
Text(llm.stat)
}
VStack {
if let player {
VideoPlayer(player: player)
.frame(height: 300)
.cornerRadius(12)
} else if let selectedImage {
Group {
#if os(iOS) || os(visionOS)
Image(uiImage: selectedImage)
.resizable()
#else
Image(nsImage: selectedImage)
.resizable()
#endif
}
.scaledToFit()
.cornerRadius(12)
.frame(height: 300)
} else if let imageURL = currentImageURL {
AsyncImage(url: imageURL) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.scaledToFit()
.cornerRadius(12)
.frame(height: 200)
case .failure:
Image(systemName: "photo.badge.exclamationmark")
@unknown default:
EmptyView()
}
}
}
HStack {
#if os(iOS) || os(visionOS)
PhotosPicker(
selection: $selectedItem,
matching: PHPickerFilter.any(of: [
PHPickerFilter.images, PHPickerFilter.videos,
])
) {
Label("Select Image/Video", systemImage: "photo.badge.plus")
}
.onChange(of: selectedItem) {
Task {
if let video = try? await selectedItem?.loadTransferable(
type: TransferableVideo.self)
{
selectedVideoURL = video.url
} else if let data = try? await selectedItem?.loadTransferable(
type: Data.self)
{
selectedImage = PlatformImage(data: data)
}
}
}
#else
Button("Select Image/Video") {
showingImagePicker = true
}
.fileImporter(
isPresented: $showingImagePicker,
allowedContentTypes: [.image, .movie]
) { result in
switch result {
case .success(let file):
Task { @MainActor in
do {
let data = try loadData(from: file)
if let image = PlatformImage(data: data) {
selectedImage = image
} else if let fileType = UTType(
filenameExtension: file.pathExtension),
fileType.conforms(to: .movie)
{
if let sandboxURL = try? loadVideoToSandbox(
from: file)
{
selectedVideoURL = sandboxURL
}
} else {
print("Failed to create image from data")
}
} catch {
print(
"Failed to load image: \(error.localizedDescription)"
)
}
}
case .failure(let error):
print(error.localizedDescription)
}
}
#endif
if selectedImage != nil {
Button("Clear", role: .destructive) {
selectedImage = nil
selectedItem = nil
}
}
}
}
.padding()
HStack {
Spacer()
if llm.running {
ProgressView()
.frame(maxHeight: 20)
Spacer()
}
}
}
ScrollView(.vertical) {
ScrollViewReader { sp in
Text(llm.output)
.textSelection(.enabled)
.onChange(of: llm.output) { _, _ in
sp.scrollTo("bottom")
}
Spacer()
.frame(width: 1, height: 1)
.id("bottom")
}
}
.frame(minHeight: 200)
HStack {
TextField("prompt", text: Bindable(llm).prompt)
.onSubmit(generate)
.disabled(llm.running)
#if os(visionOS)
.textFieldStyle(.roundedBorder)
#endif
Button(llm.running ? "stop" : "generate", action: llm.running ? cancel : generate)
}
}
.onAppear {
selectedVideoURL = URL(
string:
"https://videos.pexels.com/video-files/4066325/4066325-uhd_2560_1440_24fps.mp4")!
}
#if os(visionOS)
.padding(40)
#else
.padding()
#endif
.toolbar {
ToolbarItem {
Label(
"Memory Usage: \(deviceStat.gpuUsage.activeMemory.formatted(.byteCount(style: .memory)))",
systemImage: "info.circle.fill"
)
.labelStyle(.titleAndIcon)
.padding(.horizontal)
.help(
Text(
"""
Active Memory: \(deviceStat.gpuUsage.activeMemory.formatted(.byteCount(style: .memory)))/\(GPU.memoryLimit.formatted(.byteCount(style: .memory)))
Cache Memory: \(deviceStat.gpuUsage.cacheMemory.formatted(.byteCount(style: .memory)))/\(GPU.cacheLimit.formatted(.byteCount(style: .memory)))
Peak Memory: \(deviceStat.gpuUsage.peakMemory.formatted(.byteCount(style: .memory)))
"""
)
)
}
ToolbarItem(placement: .primaryAction) {
Button {
Task {
copyToClipboard(llm.output)
}
} label: {
Label("Copy Output", systemImage: "doc.on.doc.fill")
}
.disabled(llm.output == "")
.labelStyle(.titleAndIcon)
}
}
.task {
_ = try? await llm.load()
}
}
private func generate() {
Task {
if let selectedImage = selectedImage {
#if os(iOS) || os(visionOS)
let ciImage = CIImage(image: selectedImage)
llm.generate(image: ciImage ?? CIImage(), videoURL: nil)
#else
if let cgImage = selectedImage.cgImage(
forProposedRect: nil, context: nil, hints: nil)
{
let ciImage = CIImage(cgImage: cgImage)
llm.generate(image: ciImage, videoURL: nil)
}
#endif
} else if let imageURL = currentImageURL {
do {
let (data, _) = try await URLSession.shared.data(from: imageURL)
if let ciImage = CIImage(data: data) {
llm.generate(image: ciImage, videoURL: nil)
}
} catch {
print("Failed to load image: \(error.localizedDescription)")
}
} else {
if let videoURL = selectedVideoURL {
llm.generate(image: nil, videoURL: videoURL)
}
}
}
}
private func cancel() {
llm.cancelGeneration()
}
#if os(macOS)
private func loadData(from url: URL) throws -> Data {
guard url.startAccessingSecurityScopedResource() else {
throw NSError(
domain: "FileAccess", code: -1,
userInfo: [NSLocalizedDescriptionKey: "Failed to access the file."])
}
defer { url.stopAccessingSecurityScopedResource() }
return try Data(contentsOf: url)
}
private func loadVideoToSandbox(from url: URL) throws -> URL {
guard url.startAccessingSecurityScopedResource() else {
throw NSError(
domain: "FileAccess", code: -1,
userInfo: [NSLocalizedDescriptionKey: "Failed to access the file."])
}
defer { url.stopAccessingSecurityScopedResource() }
let sandboxURL = try SandboxFileTransfer.transferFileToTemp(from: url)
return sandboxURL
}
#endif
private func copyToClipboard(_ string: String) {
#if os(macOS)
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(string, forType: .string)
#else
UIPasteboard.general.string = string
#endif
}
}
@Observable
@MainActor
class VLMEvaluator {
var running = false
var prompt = ""
var output = ""
var modelInfo = ""
var stat = ""
/// This controls which model loads. `smolvlm` is very small even unquantized, so it will fit on
/// more devices.
let modelConfiguration = VLMRegistry.smolvlm
/// parameters controlling the output – use values appropriate for the model selected above
let generateParameters = MLXLMCommon.GenerateParameters(
maxTokens: 800, temperature: 0.7, topP: 0.9)
let updateInterval = Duration.seconds(0.25)
/// A task responsible for handling the generation process.
var generationTask: Task<Void, Error>?
enum LoadState {
case idle
case loaded(ModelContainer)
}
var loadState = LoadState.idle
/// load and return the model -- can be called multiple times, subsequent calls will
/// just return the loaded model
func load() async throws -> ModelContainer {
switch loadState {
case .idle:
// limit the buffer cache
MLX.GPU.set(cacheLimit: 20 * 1024 * 1024)
let modelContainer = try await VLMModelFactory.shared.loadContainer(
configuration: modelConfiguration
) { [modelConfiguration] progress in
Task { @MainActor in
self.modelInfo =
"Downloading \(modelConfiguration.name): \(Int(progress.fractionCompleted * 100))%"
}
}
let numParams = await modelContainer.perform { context in
context.model.numParameters()
}
self.prompt = modelConfiguration.defaultPrompt
self.modelInfo = "Loaded \(modelConfiguration.id). Weights: \(numParams / (1024*1024))M"
loadState = .loaded(modelContainer)
return modelContainer
case .loaded(let modelContainer):
return modelContainer
}
}
private func generate(prompt: String, image: CIImage?, videoURL: URL?) async {
self.output = ""
do {
let modelContainer = try await load()
// each time you generate you will get something new
MLXRandom.seed(UInt64(Date.timeIntervalSinceReferenceDate * 1000))
try await modelContainer.perform { (context: ModelContext) -> Void in
let images: [UserInput.Image] =
if let image {
[UserInput.Image.ciImage(image)]
} else {
[]
}
let videos: [UserInput.Video] =
if let videoURL {
[.url(videoURL)]
} else {
[]
}
let messages: [[String: Any]] =
if !images.isEmpty || !videos.isEmpty {
[
[
"role": "system",
"content": [
[
"type": "text",
"text": videoURL != nil
? videoSystemPrompt : imageSystemPrompt,
]
],
],
[
"role": "user",
"content": [
[
"type": "text",
"text": prompt,
]
]
// Messages format for Qwen 2 VL, Qwen 2.5 VL. May need to be adapted for other models.
+ images.map { _ in
["type": "image"]
}
+ videos.map { _ in
["type": "video"]
},
],
]
} else {
[
[
"role": "user",
"content": prompt,
]
]
}
var userInput = UserInput(messages: messages, images: images, videos: videos)
userInput.processing.resize = .init(width: 448, height: 448)
let lmInput = try await context.processor.prepare(input: userInput)
let stream = try MLXLMCommon.generate(
input: lmInput, parameters: generateParameters, context: context)
// generate and output in batches
for await batch in stream._throttle(
for: updateInterval, reducing: Generation.collect)
{
let output = batch.compactMap { $0.chunk }.joined(separator: "")
if !output.isEmpty {
Task { @MainActor [output] in
self.output += output
}
}
if let completion = batch.compactMap({ $0.info }).first {
Task { @MainActor in
self.stat = "\(completion.tokensPerSecond) tokens/s"
}
}
}
}
} catch {
output = "Failed: \(error)"
}
}
func generate(image: CIImage?, videoURL: URL?) {
guard !running else { return }
let currentPrompt = prompt
prompt = ""
generationTask = Task {
running = true
await generate(prompt: currentPrompt, image: image, videoURL: videoURL)
running = false
}
}
func cancelGeneration() {
generationTask?.cancel()
running = false
}
}
#if os(iOS) || os(visionOS)
struct TransferableVideo: Transferable {
let url: URL
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .movie) { movie in
SentTransferredFile(movie.url)
} importing: { received in
let sandboxURL = try SandboxFileTransfer.transferFileToTemp(from: received.file)
return .init(url: sandboxURL)
}
}
}
#endif
struct SandboxFileTransfer {
static func transferFileToTemp(from sourceURL: URL) throws -> URL {
let tempDir = FileManager.default.temporaryDirectory
let sandboxURL = tempDir.appendingPathComponent(sourceURL.lastPathComponent)
if FileManager.default.fileExists(atPath: sandboxURL.path()) {
try FileManager.default.removeItem(at: sandboxURL)
}
try FileManager.default.copyItem(at: sourceURL, to: sandboxURL)
return sandboxURL
}
}