new features

This commit is contained in:
Mark Gregory
2026-11-19 09:18:49 +00:00
parent a07ddf4d6d
commit 0720c8a18b
13 changed files with 918 additions and 211 deletions
+117 -29
View File
@@ -24,11 +24,12 @@ struct HubGGUFAsset: Identifiable, Equatable, Sendable {
struct RepositoryResult: Decodable, Identifiable {
let id: String
let downloads: Int?
let likes: Int?
let gated: CatalogModel.HubGating?
let isPrivate: Bool?
enum CodingKeys: String, CodingKey {
case id, downloads, gated
case id, downloads, likes, gated
case isPrivate = "private"
}
}
@@ -43,7 +44,7 @@ struct HubGGUFAsset: Identifiable, Equatable, Sendable {
.init(name: "sort", value: "downloads"), .init(name: "direction", value: "-1"),
.init(name: "limit", value: "25"), .init(name: "search", value: query),
.init(name: "expand", value: "downloads"), .init(name: "expand", value: "gated"),
.init(name: "expand", value: "private")
.init(name: "expand", value: "private"), .init(name: "expand", value: "likes")
]
guard let url = parts.url else { throw StudioError("Invalid Hugging Face search.") }
let response = try await WebFetch.load(url, types: ["application/json"])
@@ -197,7 +198,7 @@ enum HubGGUFTransfer {
guard let http = downloadTask.response as? HTTPURLResponse,
http.statusCode == 200, http.url?.scheme == "https" else {
let status = (downloadTask.response as? HTTPURLResponse).map { " (HTTP \($0.statusCode))" } ?? ""
result.finish(throwing: StudioError("Could not download the GGUF file\(status)."))
result.finish(throwing: StudioError("Could not download the model file\(status)."))
return
}
do { try FileManager.default.moveItem(at: location, to: destination) }
@@ -220,9 +221,97 @@ enum HubGGUFTransfer {
}
}
struct HubCatalogRow<Heading: View, Metadata: View, Actions: View, Footer: View>: View {
@ViewBuilder let heading: () -> Heading
@ViewBuilder let metadata: () -> Metadata
@ViewBuilder let actions: () -> Actions
@ViewBuilder let footer: () -> Footer
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(content: heading).font(.headline)
HStack(content: metadata).font(.caption).foregroundStyle(.secondary)
HStack(content: actions)
footer()
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.vertical, 4)
}
}
struct HubCatalogFavorite: View {
let key: String
@AppStorage("favoriteModelKeys") private var data = Data()
var body: some View {
let keys = (try? JSONDecoder().decode(Set<String>.self, from: data)) ?? []
let saved = keys.contains(key)
let label = saved ? "Remove from Favorites" : "Add to Favorites"
Button(label, systemImage: saved ? "star.fill" : "star") {
var updated = keys
if saved { updated.remove(key) } else { updated.insert(key) }
if let encoded = try? JSONEncoder().encode(updated) { data = encoded }
}
.labelStyle(.iconOnly)
.buttonStyle(.borderless)
.help(label)
}
}
struct HubRepositoryRow<Action: View>: View {
let result: HubGGUFAsset.RepositoryResult
let format: String
@ViewBuilder let action: () -> Action
@State private var preview: ModelPreview?
@State private var sizes: CatalogVariantSizes?
@State private var sizeError = false
var body: some View {
let source = URL(string: "https://huggingface.co")!.appending(path: result.id)
HubCatalogRow {
Text(result.id).lineLimit(1).help(result.id)
HubCatalogFavorite(key: format + ":" + result.id)
} metadata: {
Text(format)
Text(sizes?.label ?? (sizeError ? "Size unavailable" : "Loading size…"))
if let sizes {
let budget = format == "Core ML" ? ModelMemoryBudget.system : ModelMemoryBudget.gpu
if sizes.someExceed(budget: budget) {
Label(sizes.allExceed(budget: budget) ? "Exceeds memory budget" : "Some variants exceed memory budget",
systemImage: "exclamationmark.triangle.fill")
.foregroundStyle(.red)
}
}
if let count = result.downloads { Label(count.formatted(), systemImage: "arrow.down") }
if let count = result.likes { Label(count.formatted(), systemImage: "heart") }
} actions: {
Button("Preview") {
preview = ModelPreview(title: result.id, fields: [("Format", format),
("Size", sizes?.label), ("Downloads", result.downloads?.formatted()), ("Likes", result.likes?.formatted())],
source: source)
}
Link("Model Card", destination: source)
Spacer()
action()
} footer: {}
.sheet(item: $preview) { ModelPreviewSheet(preview: $0) }
.task(id: result.id + format) {
do {
let metadata = try await HubModelMetadata.load(repository: result.id)
guard !Task.isCancelled else { return }
sizes = metadata.variantSizes(format: format == "Core ML" ? .coreML : .gguf)
sizeError = false
} catch {
if !Task.isCancelled { sizeError = true }
}
}
}
}
struct HubGGUFDownloads: View {
@AppStorage(GGUFModelMetadata.libraryKey) private var stored = Data()
@State private var repository = ""
@State private var preview: ModelPreview?
@State private var repositories: [HubGGUFAsset.RepositoryResult] = []
@State private var assets: [HubGGUFAsset] = []
@State private var lookupInProgress = false
@@ -234,7 +323,7 @@ struct HubGGUFDownloads: View {
@State private var failure: String?
var body: some View {
Section("Hugging Face · GGUF") {
ModelLibrarySection("Hugging Face · GGUF", key: "catalogGGUFExpanded") {
HStack {
TextField("Search Hugging Face", text: $repository, prompt: Text("Model name or owner/model"))
.textFieldStyle(.roundedBorder)
@@ -244,35 +333,34 @@ struct HubGGUFDownloads: View {
if lookupInProgress { ProgressView().controlSize(.small) }
}
ForEach(repositories) { result in
HStack {
VStack(alignment: .leading, spacing: 3) {
Text(result.id).lineLimit(1).help(result.id)
if let downloads = result.downloads {
Text("\(downloads.formatted()) downloads").font(.caption).foregroundStyle(.secondary)
}
}
Spacer()
HubRepositoryRow(result: result, format: "GGUF") {
Button("View Files") { lookup(result.id) }.disabled(lookupInProgress || download != nil)
}
}
ForEach(assets) { asset in
VStack(alignment: .leading, spacing: 6) {
HStack {
VStack(alignment: .leading, spacing: 3) {
Text(asset.filename).fontWeight(.medium).lineLimit(1).help(asset.filename)
Text("GGUF · " + ModelLibrarySize.label(asset.bytes))
.font(.caption).foregroundStyle(.secondary)
ModelMemoryWarning(bytes: asset.bytes)
}
Spacer()
if downloadingID == asset.id {
Button("Cancel") { download?.cancel() }
} else if isRegistered(asset) {
Label("Installed", systemImage: "checkmark.circle.fill").foregroundStyle(.secondary)
} else {
Button("Download") { start(asset) }.disabled(download != nil)
}
let source = URL(string: "https://huggingface.co")!.appending(path: asset.repository)
HubCatalogRow {
Text(asset.filename).lineLimit(1).help(asset.filename)
HubCatalogFavorite(key: "GGUF:" + asset.repository)
} metadata: {
Text("GGUF")
Text(ModelLibrarySize.label(asset.bytes))
ModelMemoryWarning(bytes: asset.bytes)
} actions: {
Button("Preview") {
preview = ModelPreview(title: asset.filename, fields: [("Format", "GGUF"),
("Size", ModelLibrarySize.label(asset.bytes)), ("Revision", asset.revision)], source: source)
}
Link("Model Card", destination: source)
Spacer()
if downloadingID == asset.id {
Button("Cancel") { download?.cancel() }
} else if isRegistered(asset) {
Label("Installed", systemImage: "checkmark.circle.fill").foregroundStyle(.secondary)
} else {
Button("Download") { start(asset) }.disabled(download != nil)
}
} footer: {
if downloadingID == asset.id {
let label = ByteCountFormatter.string(fromByteCount: written, countStyle: .file)
if let expected, expected > 0 {
@@ -283,11 +371,11 @@ struct HubGGUFDownloads: View {
}
}
}
.padding(.vertical, 3)
}
if let notice { Label(notice, systemImage: "checkmark.circle").foregroundStyle(.secondary) }
if let failure { Label(failure, systemImage: "exclamationmark.triangle").foregroundStyle(.orange) }
}
.sheet(item: $preview) { ModelPreviewSheet(preview: $0) }
}
private func isRegistered(_ asset: HubGGUFAsset) -> Bool {