Unify installed MLX models and show download progress in rows
This commit is contained in:
+91
-34
@@ -145,6 +145,9 @@ struct ModelCatalogView: View {
|
|||||||
@State private var searchStatus = ""
|
@State private var searchStatus = ""
|
||||||
@State private var progress: Double?
|
@State private var progress: Double?
|
||||||
@State private var loadingPath: String?
|
@State private var loadingPath: String?
|
||||||
|
@AppStorage("favoriteModelKeys") private var favoriteData = Data()
|
||||||
|
@State private var pullingModel: String?
|
||||||
|
|
||||||
@State private var backend = "MLX"
|
@State private var backend = "MLX"
|
||||||
@AppStorage("endpoint") private var ollamaEndpoint = "http://127.0.0.1:11434"
|
@AppStorage("endpoint") private var ollamaEndpoint = "http://127.0.0.1:11434"
|
||||||
@AppStorage("ollamaModel") private var ollamaModel = ""
|
@AppStorage("ollamaModel") private var ollamaModel = ""
|
||||||
@@ -161,6 +164,32 @@ struct ModelCatalogView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private var readyModels: [CatalogModel] {
|
||||||
|
installedModels.filter { $0.installed(at: $0.localFolder) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private var favorites: Set<String> {
|
||||||
|
(try? JSONDecoder().decode(Set<String>.self, from: favoriteData)) ?? []
|
||||||
|
}
|
||||||
|
|
||||||
|
private func favoriteFirst<T>(_ rows: [T], key: (T) -> String) -> [T] {
|
||||||
|
let saved = favorites
|
||||||
|
return rows.filter { saved.contains(key($0)) } + rows.filter { !saved.contains(key($0)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private func favoriteButton(_ key: String) -> some View {
|
||||||
|
let saved = favorites.contains(key)
|
||||||
|
let label = saved ? "Remove from Favorites" : "Add to Favorites"
|
||||||
|
return Button(label, systemImage: saved ? "star.fill" : "star") {
|
||||||
|
var updated = favorites
|
||||||
|
if saved { updated.remove(key) } else { updated.insert(key) }
|
||||||
|
if let data = try? JSONEncoder().encode(updated) { favoriteData = data }
|
||||||
|
}
|
||||||
|
.labelStyle(.iconOnly)
|
||||||
|
.buttonStyle(.borderless)
|
||||||
|
.help(label)
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack(spacing: 0) {
|
VStack(spacing: 0) {
|
||||||
Picker("Model runtime", selection: $backend) {
|
Picker("Model runtime", selection: $backend) {
|
||||||
@@ -180,25 +209,23 @@ struct ModelCatalogView: View {
|
|||||||
|
|
||||||
private var mlxLibrary: some View {
|
private var mlxLibrary: some View {
|
||||||
Form {
|
Form {
|
||||||
if !installedModels.isEmpty {
|
Section("Installed MLX Models") {
|
||||||
Section("Installed MLX Models") {
|
|
||||||
ForEach(installedModels.filter { $0.installed(at: $0.localFolder) }) { model in modelRow(model) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Section("Local MLX Models") {
|
|
||||||
HStack {
|
HStack {
|
||||||
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
|
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
|
||||||
Button("Scan Folder for Models…") { scanRequested = true; pickingLibrary = true }
|
Button("Scan Folder for Models…") { scanRequested = true; pickingLibrary = true }
|
||||||
if scanningLibrary { ProgressView().controlSize(.small) }
|
if scanningLibrary { ProgressView().controlSize(.small) }
|
||||||
}
|
}
|
||||||
.disabled(studio.running || scanningLibrary)
|
.disabled(studio.running || scanningLibrary)
|
||||||
if library.isEmpty {
|
if library.isEmpty && readyModels.isEmpty {
|
||||||
Text("Register an existing MLX folder, scan a model library, or download a catalog model below.")
|
Text("Register an existing MLX folder, scan a model library, or download a catalog model below.")
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
}
|
}
|
||||||
ForEach(library, id: \.self) { path in
|
ForEach(favoriteFirst(readyModels) { "mlx:" + $0.id }) { model in modelRow(model) }
|
||||||
|
let paths = library.filter { path in !readyModels.contains { $0.localFolder.path == path } }
|
||||||
|
ForEach(favoriteFirst(paths) { "folder:" + $0 }, id: \.self) { path in
|
||||||
HStack {
|
HStack {
|
||||||
Text(URL(fileURLWithPath: path).lastPathComponent).help(path)
|
Text(URL(fileURLWithPath: path).lastPathComponent).help(path)
|
||||||
|
favoriteButton("folder:" + path)
|
||||||
Spacer()
|
Spacer()
|
||||||
Button(mlxPath == path && mlxOnly ? "Active" : "Load Model") { load(path) }
|
Button(mlxPath == path && mlxOnly ? "Active" : "Load Model") { load(path) }
|
||||||
.disabled(studio.running || loadingPath != nil || !MLXLibrary.isModel(at: URL(fileURLWithPath: path)) ||
|
.disabled(studio.running || loadingPath != nil || !MLXLibrary.isModel(at: URL(fileURLWithPath: path)) ||
|
||||||
@@ -217,24 +244,21 @@ struct ModelCatalogView: View {
|
|||||||
HStack {
|
HStack {
|
||||||
TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma"))
|
TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma"))
|
||||||
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(search)
|
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(search)
|
||||||
Button(query.isEmpty ? "Browse" : "Search", action: search).disabled(searching)
|
Button(query.isEmpty ? "Browse" : "Search", action: search).disabled(searching || active != nil)
|
||||||
if searching { ProgressView().controlSize(.small) }
|
if searching { ProgressView().controlSize(.small) }
|
||||||
}
|
}
|
||||||
if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) }
|
if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) }
|
||||||
ForEach(matches.filter { model in !installedModels.contains { $0.id == model.id && $0.installed(at: $0.localFolder) } }) { model in
|
let available = matches.filter { model in !readyModels.contains { $0.id == model.id } }
|
||||||
|
ForEach(favoriteFirst(available) { "mlx:" + $0.id }) { model in
|
||||||
modelRow(model)
|
modelRow(model)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Section("Recommended Models") {
|
Section("Recommended Models") {
|
||||||
ForEach(CatalogModel.all.filter { !$0.installed(at: $0.localFolder) }) { model in modelRow(model) }
|
ForEach(favoriteFirst(CatalogModel.all.filter { !$0.installed(at: $0.localFolder) }) { "mlx:" + $0.id }) { model in modelRow(model) }
|
||||||
}
|
}
|
||||||
if !status.isEmpty {
|
if !status.isEmpty && active == nil {
|
||||||
Section {
|
Section {
|
||||||
Text(status).textSelection(.enabled).foregroundStyle(.secondary)
|
Text(status).textSelection(.enabled).foregroundStyle(.secondary)
|
||||||
if let progress, active != nil {
|
|
||||||
ProgressView(value: progress) { Text("Files downloaded") }
|
|
||||||
Button("Cancel Download") { operation?.cancel() }
|
|
||||||
}
|
|
||||||
if loadingPath != nil { ProgressView().controlSize(.small) }
|
if loadingPath != nil { ProgressView().controlSize(.small) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -278,9 +302,10 @@ struct ModelCatalogView: View {
|
|||||||
.disabled(studio.managing)
|
.disabled(studio.managing)
|
||||||
if studio.modelListEndpoint == ollamaEndpoint {
|
if studio.modelListEndpoint == ollamaEndpoint {
|
||||||
if studio.models.isEmpty { Text("No models installed on this server.").foregroundStyle(.secondary) }
|
if studio.models.isEmpty { Text("No models installed on this server.").foregroundStyle(.secondary) }
|
||||||
ForEach(studio.models) { model in
|
ForEach(favoriteFirst(studio.models) { "ollama:" + $0.name }) { model in
|
||||||
HStack {
|
HStack {
|
||||||
Label(model.name, systemImage: "cube")
|
Label(model.name, systemImage: "cube")
|
||||||
|
favoriteButton("ollama:" + model.name)
|
||||||
Spacer()
|
Spacer()
|
||||||
if let size = model.size {
|
if let size = model.size {
|
||||||
Text(ByteCountFormatter.string(fromByteCount: size, countStyle: .file))
|
Text(ByteCountFormatter.string(fromByteCount: size, countStyle: .file))
|
||||||
@@ -304,37 +329,56 @@ struct ModelCatalogView: View {
|
|||||||
HStack {
|
HStack {
|
||||||
TextField("Search Ollama models", text: $ollamaQuery, prompt: Text("e.g. Qwen, Llama"))
|
TextField("Search Ollama models", text: $ollamaQuery, prompt: Text("e.g. Qwen, Llama"))
|
||||||
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(searchOllama)
|
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(searchOllama)
|
||||||
Button(ollamaQuery.isEmpty ? "Browse" : "Search", action: searchOllama).disabled(ollamaSearching)
|
Button(ollamaQuery.isEmpty ? "Browse" : "Search", action: searchOllama).disabled(ollamaSearching || pullingModel != nil)
|
||||||
if ollamaSearching { ProgressView().controlSize(.small) }
|
if ollamaSearching { ProgressView().controlSize(.small) }
|
||||||
}
|
}
|
||||||
if !ollamaCatalogStatus.isEmpty { Text(ollamaCatalogStatus).foregroundStyle(.secondary) }
|
if !ollamaCatalogStatus.isEmpty { Text(ollamaCatalogStatus).foregroundStyle(.secondary) }
|
||||||
ForEach(ollamaCatalog, id: \.self) { name in
|
ForEach(favoriteFirst(ollamaCatalog) { "ollama:" + $0 + ":latest" }, id: \.self) { name in
|
||||||
HStack {
|
VStack(alignment: .leading, spacing: 6) {
|
||||||
Text(name).font(.headline)
|
HStack {
|
||||||
Spacer()
|
Text(name).font(.headline)
|
||||||
if let card = URL(string: "https://ollama.com/library/" + name) {
|
favoriteButton("ollama:" + name + ":latest")
|
||||||
Link("Tags", destination: card)
|
Spacer()
|
||||||
|
if let card = URL(string: "https://ollama.com/library/" + name) {
|
||||||
|
Link("Tags", destination: card)
|
||||||
|
}
|
||||||
|
if studio.modelListEndpoint == ollamaEndpoint,
|
||||||
|
studio.models.contains(where: { $0.name == name || $0.name == name + ":latest" }) {
|
||||||
|
Text("Installed").foregroundStyle(.secondary)
|
||||||
|
} else {
|
||||||
|
Button("Pull Latest") {
|
||||||
|
pullingModel = name
|
||||||
|
studio.pull(name, endpoint: ollamaEndpoint)
|
||||||
|
}
|
||||||
|
.disabled(studio.managing)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if studio.modelListEndpoint == ollamaEndpoint,
|
if studio.managing && pullingModel == name {
|
||||||
studio.models.contains(where: { $0.name == name || $0.name == name + ":latest" }) {
|
ProgressView(value: ollamaPullFraction).progressViewStyle(.linear)
|
||||||
Text("Installed").foregroundStyle(.secondary)
|
.accessibilityLabel("Download progress")
|
||||||
} else {
|
Text(studio.modelStatus).font(.caption).foregroundStyle(.secondary)
|
||||||
Button("Pull Latest") { studio.pull(name, endpoint: ollamaEndpoint) }
|
|
||||||
.disabled(studio.managing)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.formStyle(.grouped)
|
.formStyle(.grouped)
|
||||||
|
.onChange(of: studio.managing) { _, busy in if !busy { pullingModel = nil } }
|
||||||
.onAppear {
|
.onAppear {
|
||||||
studio.refresh(endpoint: ollamaEndpoint)
|
studio.refresh(endpoint: ollamaEndpoint)
|
||||||
if ollamaCatalog.isEmpty && ollamaCatalogStatus.isEmpty { searchOllama() }
|
if ollamaCatalog.isEmpty && ollamaCatalogStatus.isEmpty { searchOllama() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var ollamaPullFraction: Double? {
|
||||||
|
guard studio.modelStatus.hasSuffix("%"),
|
||||||
|
let value = studio.modelStatus.split(separator: " ").last,
|
||||||
|
let percent = Double(value.dropLast()) else { return nil }
|
||||||
|
return percent / 100
|
||||||
|
}
|
||||||
|
|
||||||
private func searchOllama() {
|
private func searchOllama() {
|
||||||
guard !ollamaSearching else { return }
|
guard !ollamaSearching, pullingModel == nil else { return }
|
||||||
let term = ollamaQuery.trimmingCharacters(in: .whitespacesAndNewlines)
|
let term = ollamaQuery.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
ollamaSearching = true
|
ollamaSearching = true
|
||||||
ollamaCatalogStatus = ""
|
ollamaCatalogStatus = ""
|
||||||
@@ -353,7 +397,10 @@ struct ModelCatalogView: View {
|
|||||||
let isActive = mlxPath == saved.localFolder.path && mlxOnly
|
let isActive = mlxPath == saved.localFolder.path && mlxOnly
|
||||||
let diskBytes = installed ? try? MLXRuntimeLimits.weightBytes(in: saved.localFolder) : nil
|
let diskBytes = installed ? try? MLXRuntimeLimits.weightBytes(in: saved.localFolder) : nil
|
||||||
return VStack(alignment: .leading, spacing: 6) {
|
return VStack(alignment: .leading, spacing: 6) {
|
||||||
Text(model.title).font(.headline)
|
HStack {
|
||||||
|
Text(model.title).font(.headline)
|
||||||
|
favoriteButton("mlx:" + model.id)
|
||||||
|
}
|
||||||
HStack {
|
HStack {
|
||||||
if let diskBytes {
|
if let diskBytes {
|
||||||
Text(ByteCountFormatter.string(fromByteCount: Int64(clamping: diskBytes), countStyle: .file) + " weights")
|
Text(ByteCountFormatter.string(fromByteCount: Int64(clamping: diskBytes), countStyle: .file) + " weights")
|
||||||
@@ -372,20 +419,30 @@ struct ModelCatalogView: View {
|
|||||||
.disabled(studio.running || loadingPath != nil)
|
.disabled(studio.running || loadingPath != nil)
|
||||||
Button("Reveal") { NSWorkspace.shared.activateFileViewerSelecting([saved.localFolder]) }
|
Button("Reveal") { NSWorkspace.shared.activateFileViewerSelecting([saved.localFolder]) }
|
||||||
} else {
|
} else {
|
||||||
Button("Download") { download(model) }.disabled(active != nil)
|
if active?.id == model.id {
|
||||||
|
Button("Cancel Download") { operation?.cancel() }
|
||||||
|
} else {
|
||||||
|
Button("Download") { download(model) }.disabled(active != nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if FileManager.default.fileExists(atPath: saved.localFolder.path) {
|
if FileManager.default.fileExists(atPath: saved.localFolder.path) {
|
||||||
Button("Remove…", role: .destructive) { removing = saved }
|
Button("Remove…", role: .destructive) { removing = saved }
|
||||||
.disabled(studio.running || active != nil || loadingPath != nil)
|
.disabled(studio.running || active != nil || loadingPath != nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if active?.id == model.id, let progress {
|
||||||
|
ProgressView(value: progress)
|
||||||
|
.progressViewStyle(.linear)
|
||||||
|
.accessibilityLabel("Download progress")
|
||||||
|
Text(status).font(.caption).foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.padding(.vertical, 4)
|
.padding(.vertical, 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func search() {
|
private func search() {
|
||||||
let term = query.trimmingCharacters(in: .whitespacesAndNewlines)
|
let term = query.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
guard !searching else { return }
|
guard !searching, active == nil else { return }
|
||||||
searching = true
|
searching = true
|
||||||
searchStatus = ""
|
searchStatus = ""
|
||||||
Task {
|
Task {
|
||||||
|
|||||||
Reference in New Issue
Block a user