new features
This commit is contained in:
+132
-61
@@ -83,18 +83,15 @@ struct CoreMLDownloads {
|
||||
"weight.bin": "1fa5158e6a1c5d3d131a1b08cb8dd3a4030c682a5fefa625e37a289f34d7a7ff"
|
||||
]
|
||||
|
||||
static func install(in destination: URL, progress: @escaping HubGGUFAsset.ProgressUpdate) async throws -> CoreMLLibrary {
|
||||
let checkpoint = destination.appending(path: package)
|
||||
if let installed = try? CoreMLLibrary.inspect(checkpoint) { return installed }
|
||||
let fileManager = FileManager.default
|
||||
let parent = destination.deletingLastPathComponent()
|
||||
try fileManager.createDirectory(at: parent, withIntermediateDirectories: true)
|
||||
let stage = parent.appending(path: ".download-" + UUID().uuidString)
|
||||
defer { try? fileManager.removeItem(at: stage) }
|
||||
static var destination: URL {
|
||||
URL.applicationSupportDirectory.appending(path: "AIStudio/Models/CoreML/Apple-Mistral-7B")
|
||||
}
|
||||
|
||||
private static var files: [(URL, String, Int64)] {
|
||||
let hf = URL(string: "https://huggingface.co")!
|
||||
let weights = hf.appending(path: "apple/mistral-coreml/resolve/ef561211ae2c829934011eec8b396776bf24df9b")
|
||||
let tokenizer = hf.appending(path: "mistralai/Mistral-7B-Instruct-v0.3/resolve/c170c708c41dac9275d15a8fff4eca08d52bab71")
|
||||
let files: [(URL, String, Int64)] = [
|
||||
return [
|
||||
(weights, package + "/Manifest.json", 617),
|
||||
(weights, package + "/Data/com.apple.CoreML/model.mlmodel", 984_023),
|
||||
(weights, package + "/Data/com.apple.CoreML/weights/weight.bin", 4_077_534_016),
|
||||
@@ -102,6 +99,27 @@ struct CoreMLDownloads {
|
||||
(tokenizer, "tokenizer_config.json", 140_874),
|
||||
(tokenizer, "config.json", 601)
|
||||
]
|
||||
}
|
||||
|
||||
static func installed(in destination: URL) -> CoreMLLibrary? {
|
||||
guard files.allSatisfy({ _, path, expected in
|
||||
let size = try? destination.appending(path: path).resourceValues(forKeys: [.fileSizeKey]).fileSize
|
||||
return size.map(Int64.init) == expected
|
||||
}) else { return nil }
|
||||
return try? CoreMLLibrary.inspect(destination.appending(path: package))
|
||||
}
|
||||
|
||||
static func install(in destination: URL, progress: @escaping HubGGUFAsset.ProgressUpdate) async throws -> CoreMLLibrary {
|
||||
if let model = installed(in: destination) { return model }
|
||||
let checkpoint = destination.appending(path: package)
|
||||
let fileManager = FileManager.default
|
||||
guard !fileManager.fileExists(atPath: destination.path) else {
|
||||
throw StudioError("The existing Core ML download is incomplete. Move the folder in Finder and try again. \(destination.path)")
|
||||
}
|
||||
let parent = destination.deletingLastPathComponent()
|
||||
try fileManager.createDirectory(at: parent, withIntermediateDirectories: true)
|
||||
let stage = parent.appending(path: ".download-" + UUID().uuidString)
|
||||
defer { try? fileManager.removeItem(at: stage) }
|
||||
let total = files.reduce(0) { $0 + $1.2 }
|
||||
var written: Int64 = 0
|
||||
for (base, path, bytes) in files {
|
||||
@@ -109,7 +127,14 @@ struct CoreMLDownloads {
|
||||
let file = stage.appending(path: path)
|
||||
try fileManager.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||||
let offset = written
|
||||
try await HubGGUFTransfer.download(base.appending(path: path), file) { count, _ in progress(offset + count, total) }
|
||||
do {
|
||||
try await HubGGUFTransfer.download(base.appending(path: path), file) { count, _ in
|
||||
progress(offset + count, total)
|
||||
}
|
||||
} catch {
|
||||
try Task.checkCancellation()
|
||||
throw StudioError("Could not download \(file.lastPathComponent). \(error.localizedDescription)")
|
||||
}
|
||||
guard try file.resourceValues(forKeys: [.fileSizeKey]).fileSize.map(Int64.init) == bytes else {
|
||||
throw StudioError("The Core ML download returned an unexpected file size.")
|
||||
}
|
||||
@@ -147,58 +172,62 @@ struct CoreMLModels: View {
|
||||
@State private var download: Task<Void, Never>?
|
||||
@State private var received: Int64 = 0
|
||||
@State private var total: Int64?
|
||||
@State private var showingAppleFiles = true
|
||||
@State private var catalogStatus = ""
|
||||
@State private var preview: ModelPreview?
|
||||
|
||||
var body: some View {
|
||||
Section("Core ML") {
|
||||
Group {
|
||||
ModelLibrarySection("Core ML", key: "libraryCoreMLExpanded") {
|
||||
if #available(macOS 15, *) {
|
||||
Button("Import Core ML Model…", systemImage: "plus") { importing = true }.disabled(disabled)
|
||||
ForEach(CoreMLLibrary.read(stored)) { model in
|
||||
HStack {
|
||||
VStack(alignment: .leading) {
|
||||
Text(URL(fileURLWithPath: model.path).deletingPathExtension().lastPathComponent)
|
||||
Text("Core ML · " + ModelLibrarySize.label(model.bytes)).font(.caption).foregroundStyle(.secondary)
|
||||
ModelMemoryWarning(bytes: model.bytes, coreML: true)
|
||||
}
|
||||
Spacer()
|
||||
Button(selection == model.path ? "Active" : "Use") { select(model.path) }
|
||||
.disabled(disabled || selection == model.path || !FileManager.default.fileExists(atPath: model.path))
|
||||
if selection != model.path {
|
||||
Button("Remove", systemImage: "minus.circle") {
|
||||
do { stored = try JSONEncoder().encode(CoreMLLibrary.read(stored).filter { $0.path != model.path }) }
|
||||
catch { status = error.localizedDescription }
|
||||
}.labelStyle(.iconOnly)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !status.isEmpty { Text(status).font(.caption).foregroundStyle(.secondary) }
|
||||
} else {
|
||||
Text("Core ML chat requires macOS 15 or later.").foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
if #available(macOS 15, *) {
|
||||
Button("Import Core ML Model…", systemImage: "plus") { importing = true }.disabled(disabled)
|
||||
ForEach(CoreMLLibrary.read(stored)) { model in
|
||||
ModelLibrarySection("Hugging Face · Core ML", key: "catalogCoreMLExpanded") {
|
||||
HStack {
|
||||
VStack(alignment: .leading) {
|
||||
Text(URL(fileURLWithPath: model.path).deletingPathExtension().lastPathComponent)
|
||||
Text("Core ML · " + ModelLibrarySize.label(model.bytes)).font(.caption).foregroundStyle(.secondary)
|
||||
ModelMemoryWarning(bytes: model.bytes, coreML: true)
|
||||
}
|
||||
Spacer()
|
||||
Button(selection == model.path ? "Active" : "Use") { select(model.path) }
|
||||
.disabled(disabled || selection == model.path || !FileManager.default.fileExists(atPath: model.path))
|
||||
if selection != model.path {
|
||||
Button("Remove", systemImage: "minus.circle") {
|
||||
do { stored = try JSONEncoder().encode(CoreMLLibrary.read(stored).filter { $0.path != model.path }) }
|
||||
catch { status = error.localizedDescription }
|
||||
}.labelStyle(.iconOnly)
|
||||
TextField("Search Hugging Face", text: $search, prompt: Text("Model name or owner/model"))
|
||||
.textFieldStyle(.roundedBorder).onSubmit(find)
|
||||
Button(search.isEmpty ? "Browse" : "Search", action: find)
|
||||
.disabled(searching || download != nil)
|
||||
if searching { ProgressView().controlSize(.small) }
|
||||
}
|
||||
ForEach(results) { result in
|
||||
HubRepositoryRow(result: result, format: "Core ML") {
|
||||
if result.id == CoreMLDownloads.repository {
|
||||
Button("View Files") { showingAppleFiles = true }
|
||||
} else {
|
||||
Link("View Files", destination: URL(string: "https://huggingface.co")!.appending(path: result.id))
|
||||
}
|
||||
}
|
||||
}
|
||||
if showingAppleFiles { appleDownloadRow }
|
||||
if !catalogStatus.isEmpty { Text(catalogStatus).font(.caption).foregroundStyle(.secondary) }
|
||||
}
|
||||
HStack {
|
||||
VStack(alignment: .leading) {
|
||||
Text("Apple Mistral 7B Instruct")
|
||||
Text("Core ML · Int4 · " + ModelLibrarySize.label(CoreMLDownloads.modelBytes))
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
ModelMemoryWarning(bytes: CoreMLDownloads.modelBytes, coreML: true)
|
||||
}
|
||||
Spacer()
|
||||
if let download { Button("Cancel") { download.cancel() } }
|
||||
else { Button("Download from Hugging Face", action: startDownload) }
|
||||
}
|
||||
if download != nil {
|
||||
if let total { ProgressView(value: Double(min(total, received)), total: Double(total)) }
|
||||
else { ProgressView("Starting download…") }
|
||||
}
|
||||
HStack {
|
||||
TextField("Search Hugging Face", text: $search, prompt: Text("Core ML text-generation models"))
|
||||
.onSubmit(find)
|
||||
Button(search.isEmpty ? "Browse" : "Search", action: find).disabled(searching)
|
||||
if searching { ProgressView().controlSize(.small) }
|
||||
}
|
||||
ForEach(results) { result in
|
||||
Link(result.id, destination: URL(string: "https://huggingface.co")!.appending(path: result.id))
|
||||
}
|
||||
if !status.isEmpty { Text(status).font(.caption).foregroundStyle(.secondary) }
|
||||
} else {
|
||||
Text("Core ML chat requires macOS 15 or later.").foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.sheet(item: $preview) { ModelPreviewSheet(preview: $0) }
|
||||
.fileImporter(isPresented: $importing, allowedContentTypes: [.folder] + ["mlpackage", "mlmodel", "mlmodelc"].compactMap { UTType(filenameExtension: $0) }) { result in
|
||||
do {
|
||||
let url = try result.get()
|
||||
@@ -209,6 +238,46 @@ struct CoreMLModels: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var appleDownloadRow: some View {
|
||||
let source = URL(string: "https://huggingface.co/apple/mistral-coreml")!
|
||||
return HubCatalogRow {
|
||||
Text("Apple Mistral 7B Instruct")
|
||||
HubCatalogFavorite(key: "Core ML:" + CoreMLDownloads.repository)
|
||||
} metadata: {
|
||||
Text("Core ML · Int4")
|
||||
Text(ModelLibrarySize.label(CoreMLDownloads.modelBytes))
|
||||
ModelMemoryWarning(bytes: CoreMLDownloads.modelBytes, coreML: true)
|
||||
} actions: {
|
||||
Button("Preview") {
|
||||
preview = ModelPreview(title: "Apple Mistral 7B Instruct", fields: [
|
||||
("Format", "Core ML · Int4"), ("Size", ModelLibrarySize.label(CoreMLDownloads.modelBytes)),
|
||||
("Package", CoreMLDownloads.package)
|
||||
], source: source)
|
||||
}
|
||||
Link("Model Card", destination: source)
|
||||
Spacer()
|
||||
if let download {
|
||||
Button("Cancel") { download.cancel() }
|
||||
} else if let existing = CoreMLDownloads.installed(in: CoreMLDownloads.destination) {
|
||||
Label("Installed", systemImage: "checkmark.circle.fill").foregroundStyle(.secondary)
|
||||
Button(selection == existing.path ? "Selected" : "Use") {
|
||||
do { try register(existing); select(existing.path) }
|
||||
catch { catalogStatus = error.localizedDescription }
|
||||
}.disabled(disabled || selection == existing.path)
|
||||
} else {
|
||||
Button("Download", action: startDownload)
|
||||
}
|
||||
} footer: {
|
||||
if download != nil {
|
||||
if let total {
|
||||
ProgressView(ModelLibrarySize.label(received), value: Double(min(total, received)), total: Double(total))
|
||||
} else {
|
||||
ProgressView("Starting download…")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func register(_ model: CoreMLLibrary) throws {
|
||||
stored = try JSONEncoder().encode(CoreMLLibrary.read(stored).filter { $0.path != model.path } + [model])
|
||||
status = "Core ML model added."
|
||||
@@ -217,25 +286,26 @@ struct CoreMLModels: View {
|
||||
private func find() {
|
||||
guard !searching else { return }
|
||||
searching = true
|
||||
status = ""
|
||||
catalogStatus = ""
|
||||
Task {
|
||||
defer { searching = false }
|
||||
do {
|
||||
results = try await HubGGUFAsset.search(search.trimmingCharacters(in: .whitespaces), format: "coreml")
|
||||
if results.isEmpty { status = "No public Core ML text-generation models found." }
|
||||
} catch { status = error.localizedDescription }
|
||||
let query = search.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
showingAppleFiles = query.caseInsensitiveCompare(CoreMLDownloads.repository) == .orderedSame
|
||||
results = try await HubGGUFAsset.search(query, format: "coreml")
|
||||
if results.isEmpty { catalogStatus = "No public Core ML text-generation models found." }
|
||||
} catch { catalogStatus = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
|
||||
private func startDownload() {
|
||||
received = 0
|
||||
total = nil
|
||||
status = ""
|
||||
catalogStatus = ""
|
||||
download = Task {
|
||||
defer { download = nil }
|
||||
do {
|
||||
let path = URL.applicationSupportDirectory.appending(path: "AIStudio/Models/CoreML/Apple-Mistral-7B")
|
||||
let model = try await CoreMLDownloads.install(in: path) { written, expected in
|
||||
let model = try await CoreMLDownloads.install(in: CoreMLDownloads.destination) { written, expected in
|
||||
Task { @MainActor in
|
||||
guard download != nil, written >= received else { return }
|
||||
received = written
|
||||
@@ -243,8 +313,9 @@ struct CoreMLModels: View {
|
||||
}
|
||||
}
|
||||
try register(model)
|
||||
catalogStatus = "Apple Mistral installed."
|
||||
} catch {
|
||||
status = Task.isCancelled ? "Download cancelled." : error.localizedDescription
|
||||
catalogStatus = Task.isCancelled ? "Download cancelled." : error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user