Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Models/DeviceCatalogView.swift3// Description: MLX models proposed per Apple device class, from the live mlx-community listing on Hugging Face: best picks and the full list of models that fit.4import SwiftUI5import LocalChatCore6import LocalChatModelHub78@MainActor @Observable9final class DeviceCatalogModel {10 var models: [CatalogModel] = []11 var fetchedAt: Date?12 var isLoading = false13 var error: String?14 var device: AppleDeviceClass15 var task: CatalogModel.Task = .chat16 var includeTight = true17 var query = ""1819 init(device: AppleDeviceClass) { self.device = device }2021 func load(_ catalog: DeviceModelCatalog, force: Bool = false) async {22 isLoading = true; error = nil23 defer { isLoading = false }24 do { let snapshot = try await catalog.models(forceRefresh: force); models = snapshot.models; fetchedAt = snapshot.fetchedAt }25 catch { self.error = "The model list could not be loaded from Hugging Face: \(error.localizedDescription)" }26 }2728 var picks: [DeviceCatalogEntry] { DeviceModelCatalog.bestPicks(models, for: device, task: task, count: 6) }29 var entries: [DeviceCatalogEntry] {30 let q = query.trimmingCharacters(in: .whitespaces).lowercased()31 return DeviceModelCatalog.entries(models, for: device, task: task, includeTight: includeTight).filter { q.isEmpty || $0.model.repoID.lowercased().contains(q) }32 }33 var notLoadable: Int { models.filter { $0.task == task && !$0.isSupported }.count }34 var unknownSize: Int { models.filter { $0.task == task && $0.isSupported && $0.estimatedSizeBytes == nil }.count }35}3637struct DeviceCatalogView: View {38 @Environment(AppModel.self) private var app39 let showDownloads: () -> Void40 @State private var model: DeviceCatalogModel?41 @State private var selectedRepo: RepoRef?4243 struct RepoRef: Identifiable { let id: String }4445 var body: some View {46 Group {47 if let model { content(model) } else { ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity) }48 }49 .task {50 guard model == nil else { return }51 let m = DeviceCatalogModel(device: AppleDeviceClass.matching(app.device))52 model = m53 await m.load(app.deviceCatalog)54 }55 .sheet(item: $selectedRepo) { ref in56 ModelDetailView(repoID: ref.id, showDownloads: { selectedRepo = nil; showDownloads() })57 #if os(macOS)58 .frame(minWidth: 620, idealWidth: 760, minHeight: 520, idealHeight: 680)59 #endif60 }61 }6263 @ViewBuilder private func content(_ model: DeviceCatalogModel) -> some View {64 @Bindable var model = model65 let mine = AppleDeviceClass.matching(app.device)66 ScrollView {67 VStack(alignment: .leading, spacing: DS.S.lg) {68 // device picker69 VStack(alignment: .leading, spacing: 10) {70 SectionHeader("Choose a device", subtitle: "Models are proposed for the memory of each Apple device, not only this one.", symbol: "macbook.and.iphone")71 ForEach(AppleDeviceClass.Family.allCases, id: \.self) { family in72 ScrollView(.horizontal, showsIndicators: false) {73 HStack(spacing: 6) {74 Label(family.title, systemImage: family.symbol).font(DS.F.caption).foregroundStyle(DS.C.muted).frame(width: 72, alignment: .leading)75 ForEach(AppleDeviceClass.all.filter { $0.family == family }) { device in76 Button { withAnimation(DS.M.snappy) { model.device = device } } label: {77 HStack(spacing: 4) {78 Text("\(device.memoryGB) GB").font(.system(size: 12, weight: .semibold))79 if device == mine { Image(systemName: "location.fill").font(.system(size: 8)) }80 }81 .foregroundStyle(model.device == device ? .white : DS.C.text2)82 .padding(.horizontal, 11).padding(.vertical, 6)83 .background(model.device == device ? AnyShapeStyle(DS.G.accent) : AnyShapeStyle(DS.C.surface2), in: Capsule())84 }85 .buttonStyle(.plain).accessibilityLabel("\(device.title)\(device == mine ? ", this device" : "")")86 .accessibilityIdentifier("device-\(device.id)")87 }88 }89 }90 }91 HStack(spacing: 8) {92 Pill(model.device.title, symbol: model.device.family.symbol, tone: .accent)93 Pill("\(ByteFormat.memory(model.device.usableModelMemoryBytes)) for a model", symbol: "memorychip")94 if model.device == mine { Pill("This device", symbol: "location.fill", tone: .ok) }95 }96 Text("Typical devices: \(model.device.examples). On iPhone and iPad the budget assumes the increased memory limit; iOS can still end an app earlier.")97 .font(DS.F.micro).foregroundStyle(DS.C.muted)98 }99 .card()100101 HStack(spacing: 8) {102 Picker("Task", selection: $model.task) { ForEach(CatalogModel.Task.allCases) { Text($0.title).tag($0) } }.pickerStyle(.segmented).fixedSize().labelsHidden()103 Toggle("Include tight fits", isOn: $model.includeTight).toggleStyle(.switch).controlSize(.small).font(DS.F.caption)104 Spacer(minLength: 4)105 if model.isLoading { ProgressView().controlSize(.small) }106 Button { Task { await model.load(app.deviceCatalog, force: true) } } label: { Label("Refresh", systemImage: "arrow.clockwise") }107 .buttonStyle(.lcSecondaryCompact).disabled(model.isLoading)108 }109 if let error = model.error { InlineNotice(.error, error) }110111 if !model.picks.isEmpty {112 SectionHeader("Best picks for \(model.device.title)", subtitle: "The largest well-downloaded models that fit comfortably, one quantization per model.", symbol: "sparkles")113 TileGrid(minimum: 250) { ForEach(model.picks) { entry in pickCard(entry) } }114 }115116 VStack(alignment: .leading, spacing: 8) {117 HStack {118 SectionHeader("Everything that fits · \(model.entries.count)", subtitle: "From mlx-community on Hugging Face, most downloaded first.", symbol: "list.bullet")119 TextField("Filter", text: $model.query).textFieldStyle(.roundedBorder).frame(maxWidth: 180)120 }121 if model.entries.isEmpty && !model.isLoading {122 Text("No model of this kind fits this device class.").font(DS.F.callout).foregroundStyle(DS.C.muted).padding(.vertical, 8)123 }124 LazyVStack(spacing: 6) { ForEach(model.entries) { entry in row(entry) } }125 }126 footer(model)127 }128 .padding(DS.S.lg)129 }130 }131132 private func pickCard(_ entry: DeviceCatalogEntry) -> some View {133 Button { selectedRepo = RepoRef(id: entry.model.repoID) } label: {134 VStack(alignment: .leading, spacing: 8) {135 Text(entry.model.name).font(.system(size: 13.5, weight: .semibold)).foregroundStyle(DS.C.text).lineLimit(2).multilineTextAlignment(.leading)136 HStack(spacing: 5) {137 if let p = entry.model.descriptor.parameterLabel { Pill(p) }138 if let q = entry.model.quantization { Pill(q) }139 Pill(entry.fit.label, tone: entry.fit == .comfortable ? .ok : .warn)140 }141 HStack(spacing: 10) {142 Label("≈ \(MetricFormat.memory(entry.estimatedMemoryBytes))", systemImage: "memorychip")143 Label(entry.model.downloads.formatted(.number.notation(.compactName)), systemImage: "arrow.down.circle")144 if app.installedModels.contains(where: { $0.repoID == entry.model.repoID }) { Label("Installed", systemImage: "checkmark.circle.fill").foregroundStyle(DS.C.ok) }145 }146 .font(DS.F.micro).foregroundStyle(DS.C.muted)147 }148 .frame(maxWidth: .infinity, alignment: .leading).card(padding: DS.S.md)149 }150 .buttonStyle(.plain).accessibilityIdentifier("pick-\(entry.model.repoID)")151 }152153 private func row(_ entry: DeviceCatalogEntry) -> some View {154 Button { selectedRepo = RepoRef(id: entry.model.repoID) } label: {155 HStack(spacing: 10) {156 VStack(alignment: .leading, spacing: 3) {157 Text(entry.model.name).font(.system(size: 13, weight: .medium)).foregroundStyle(DS.C.text).lineLimit(1)158 Text([entry.model.architecture, entry.model.descriptor.parameterLabel.map { "\($0) params" }, entry.model.quantization,159 entry.model.estimatedSizeBytes.map { "≈ \(ByteFormat.string($0)) download" }, entry.model.license].compactMap { $0 }.joined(separator: " · "))160 .font(DS.F.micro).foregroundStyle(DS.C.muted).lineLimit(1)161 }162 Spacer(minLength: 6)163 if entry.model.isGated { Pill("Gated", symbol: "lock", tone: .warn) }164 Text("≈ \(MetricFormat.memory(entry.estimatedMemoryBytes))").font(DS.F.mono(11)).foregroundStyle(DS.C.text2)165 Pill(entry.fit.label, tone: entry.fit == .comfortable ? .ok : .warn)166 Image(systemName: "chevron.right").font(.system(size: 10, weight: .semibold)).foregroundStyle(DS.C.muted)167 }168 .padding(.horizontal, 12).padding(.vertical, 9)169 .background(DS.C.surface, in: RoundedRectangle(cornerRadius: DS.R.control, style: .continuous))170 .overlay(RoundedRectangle(cornerRadius: DS.R.control, style: .continuous).strokeBorder(DS.C.border, lineWidth: 1))171 }172 .buttonStyle(.plain)173 }174175 private func footer(_ model: DeviceCatalogModel) -> some View {176 VStack(alignment: .leading, spacing: 4) {177 Text("Sizes and memory are estimates (parameters × nominal bits per weight, plus the KV cache for a 4,096-token context and 350 MB of runtime). The exact download size is shown when you open a model, and the measured memory once it is loaded.")178 Text("\(model.notLoadable) listed models are hidden because MLX Swift \(MLXArchitectures.version) does not implement their architecture; \(model.unknownSize) because their size cannot be estimated from the listing.")179 if let date = model.fetchedAt { Text("List fetched from huggingface.co/mlx-community \(date.formatted(.relative(presentation: .named))).") }180 }181 .font(DS.F.micro).foregroundStyle(DS.C.muted)182 }183}184