Browse MLX and Ollama catalogs in a unified model library
This commit is contained in:
+138
-4
@@ -33,8 +33,11 @@ struct CatalogModel: Identifiable, Hashable, Sendable, Codable {
|
||||
url.path = "/api/models"
|
||||
url.queryItems = [
|
||||
.init(name: "author", value: "mlx-community"),
|
||||
.init(name: "pipeline_tag", value: "text-generation"),
|
||||
.init(name: "search", value: query),
|
||||
.init(name: "limit", value: "25"),
|
||||
.init(name: "sort", value: "downloads"),
|
||||
.init(name: "direction", value: "-1"),
|
||||
.init(name: "full", value: "true")
|
||||
]
|
||||
guard let endpoint = url.url else { throw StudioError("Could not prepare the model search.") }
|
||||
@@ -142,6 +145,15 @@ struct ModelCatalogView: View {
|
||||
@State private var searchStatus = ""
|
||||
@State private var progress: Double?
|
||||
@State private var loadingPath: String?
|
||||
@State private var backend = "MLX"
|
||||
@AppStorage("endpoint") private var ollamaEndpoint = "http://127.0.0.1:11434"
|
||||
@AppStorage("ollamaModel") private var ollamaModel = ""
|
||||
@AppStorage("compatibleEnabled") private var compatibleEnabled = false
|
||||
@State private var ollamaQuery = ""
|
||||
@State private var ollamaCatalog: [String] = []
|
||||
@State private var ollamaSearching = false
|
||||
@State private var ollamaCatalogStatus = ""
|
||||
|
||||
|
||||
private var installedModels: [CatalogModel] {
|
||||
(try? JSONDecoder().decode([CatalogModel].self, from: managed)) ?? []
|
||||
@@ -149,8 +161,25 @@ struct ModelCatalogView: View {
|
||||
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
Picker("Model runtime", selection: $backend) {
|
||||
Text("MLX").tag("MLX")
|
||||
Text("Ollama").tag("Ollama")
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
.frame(width: 260)
|
||||
.padding()
|
||||
if backend == "Ollama" {
|
||||
ollamaLibrary
|
||||
} else {
|
||||
mlxLibrary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var mlxLibrary: some View {
|
||||
Form {
|
||||
Section("Model Library") {
|
||||
Section("Local MLX Models") {
|
||||
HStack {
|
||||
Button("Register Model Folder…") { scanRequested = false; pickingLibrary = true }
|
||||
Button("Scan Folder for Models…") { scanRequested = true; pickingLibrary = true }
|
||||
@@ -185,8 +214,9 @@ struct ModelCatalogView: View {
|
||||
}
|
||||
Section("Hugging Face") {
|
||||
HStack {
|
||||
TextField("Search MLX models", text: $query).onSubmit(search)
|
||||
Button("Search", action: search).disabled(searching || query.trimmingCharacters(in: .whitespaces).isEmpty)
|
||||
TextField("Search MLX models", text: $query, prompt: Text("e.g. Qwen, Gemma"))
|
||||
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(search)
|
||||
Button(query.isEmpty ? "Browse" : "Search", action: search).disabled(searching)
|
||||
if searching { ProgressView().controlSize(.small) }
|
||||
}
|
||||
if !searchStatus.isEmpty { Text(searchStatus).foregroundStyle(.secondary) }
|
||||
@@ -207,6 +237,7 @@ struct ModelCatalogView: View {
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
.onAppear { if matches.isEmpty && searchStatus.isEmpty { search() } }
|
||||
.fileImporter(isPresented: $pickingLibrary, allowedContentTypes: [.folder]) { result in
|
||||
do {
|
||||
let url = try result.get()
|
||||
@@ -236,6 +267,85 @@ struct ModelCatalogView: View {
|
||||
} message: { model in Text(model.title) }
|
||||
}
|
||||
|
||||
private var ollamaLibrary: some View {
|
||||
Form {
|
||||
Section("Installed on Ollama") {
|
||||
LabeledContent("Server", value: ollamaEndpoint)
|
||||
Button("Refresh Installed Models") { studio.refresh(endpoint: ollamaEndpoint) }
|
||||
.disabled(studio.managing)
|
||||
if studio.modelListEndpoint == ollamaEndpoint {
|
||||
if studio.models.isEmpty { Text("No models installed on this server.").foregroundStyle(.secondary) }
|
||||
ForEach(studio.models) { model in
|
||||
HStack {
|
||||
Label(model.name, systemImage: "cube")
|
||||
Spacer()
|
||||
if let size = model.size {
|
||||
Text(ByteCountFormatter.string(fromByteCount: size, countStyle: .file))
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
}
|
||||
let active = ollamaModel == model.name && !mlxOnly && !compatibleEnabled
|
||||
Button(active ? "Active" : "Use Model") {
|
||||
ollamaModel = model.name
|
||||
compatibleEnabled = false
|
||||
mlxOnly = false
|
||||
}
|
||||
.disabled(active || studio.running)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !studio.modelStatus.isEmpty { Text(studio.modelStatus).foregroundStyle(.secondary) }
|
||||
if studio.managing {
|
||||
ProgressView().controlSize(.small)
|
||||
Button("Cancel") { studio.cancelManagement() }
|
||||
}
|
||||
}
|
||||
Section("Ollama Catalog") {
|
||||
HStack {
|
||||
TextField("Search Ollama models", text: $ollamaQuery, prompt: Text("e.g. Qwen, Llama"))
|
||||
.labelsHidden().textFieldStyle(.roundedBorder).onSubmit(searchOllama)
|
||||
Button(ollamaQuery.isEmpty ? "Browse" : "Search", action: searchOllama).disabled(ollamaSearching)
|
||||
if ollamaSearching { ProgressView().controlSize(.small) }
|
||||
}
|
||||
if !ollamaCatalogStatus.isEmpty { Text(ollamaCatalogStatus).foregroundStyle(.secondary) }
|
||||
ForEach(ollamaCatalog, id: \.self) { name in
|
||||
HStack {
|
||||
Text(name).font(.headline)
|
||||
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") { studio.pull(name, endpoint: ollamaEndpoint) }
|
||||
.disabled(studio.managing)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
.onAppear {
|
||||
studio.refresh(endpoint: ollamaEndpoint)
|
||||
if ollamaCatalog.isEmpty && ollamaCatalogStatus.isEmpty { searchOllama() }
|
||||
}
|
||||
}
|
||||
|
||||
private func searchOllama() {
|
||||
guard !ollamaSearching else { return }
|
||||
let term = ollamaQuery.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
ollamaSearching = true
|
||||
ollamaCatalogStatus = ""
|
||||
Task {
|
||||
defer { ollamaSearching = false }
|
||||
do {
|
||||
ollamaCatalog = try await OllamaCatalog.search(term)
|
||||
if ollamaCatalog.isEmpty { ollamaCatalogStatus = "No models found." }
|
||||
} catch { ollamaCatalogStatus = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
|
||||
private func modelRow(_ model: CatalogModel) -> some View {
|
||||
let saved = installedModels.first { $0.id == model.id } ?? model
|
||||
let installed = saved.installed(at: saved.localFolder)
|
||||
@@ -274,7 +384,7 @@ struct ModelCatalogView: View {
|
||||
|
||||
private func search() {
|
||||
let term = query.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !term.isEmpty, !searching else { return }
|
||||
guard !searching else { return }
|
||||
searching = true
|
||||
searchStatus = ""
|
||||
Task {
|
||||
@@ -368,3 +478,27 @@ enum MLXLibrary {
|
||||
((try? MLXRuntimeLimits.weightBytes(in: url)) ?? 0) > 0
|
||||
}
|
||||
}
|
||||
|
||||
enum OllamaCatalog {
|
||||
static func search(_ term: String) async throws -> [String] {
|
||||
var parts = URLComponents()
|
||||
parts.scheme = "https"
|
||||
parts.host = "ollama.com"
|
||||
parts.path = "/search"
|
||||
parts.queryItems = [.init(name: "q", value: term)]
|
||||
guard let url = parts.url else { throw StudioError("Could not prepare the Ollama catalog request.") }
|
||||
let result = try await WebFetch.load(url, types: ["text/html"])
|
||||
return modelNames(in: String(decoding: result.data, as: UTF8.self))
|
||||
}
|
||||
|
||||
static func modelNames(in html: String) -> [String] {
|
||||
let pattern = #"href\s*=\s*["']/library/([A-Za-z0-9._-]+)["']"#
|
||||
guard let regex = try? NSRegularExpression(pattern: pattern) else { return [] }
|
||||
var seen = Set<String>()
|
||||
return regex.matches(in: html, range: NSRange(html.startIndex..., in: html)).compactMap { match in
|
||||
guard let range = Range(match.range(at: 1), in: html) else { return nil }
|
||||
let name = String(html[range])
|
||||
return name != "." && name != ".." && seen.insert(name).inserted ? name : nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user