From d211db88ab9bbd521953af99b81b8d75913ae6db Mon Sep 17 00:00:00 2001 From: JT Date: Mon, 8 Apr 2024 18:02:13 -0700 Subject: [PATCH] Chunk installed model requests with more than 100 installed models cuz Civit doesn't paginate that --- CHANGELOG.md | 1 + .../CivitAiBrowserViewModel.cs | 42 ++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4dcaaf..8cb19019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2 ### Fixed - Fixed [#559](https://github.com/LykosAI/StabilityMatrix/issues/559) - "Unable to load bitmap from provided data" error in Checkpoints page - Fixed [#522](https://github.com/LykosAI/StabilityMatrix/issues/522) - Incorrect output directory path for latest Auto1111 +- Fixed Civitai model browser error when sorting by Installed with more than 100 installed models ## v2.10.0-pre.2 ### Added diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivitAiBrowserViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivitAiBrowserViewModel.cs index 4754397d..ebf5401a 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivitAiBrowserViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CivitAiBrowserViewModel.cs @@ -196,10 +196,40 @@ public partial class CivitAiBrowserViewModel : TabViewModelBase, IInfinitelyScro { var timer = Stopwatch.StartNew(); var queryText = request.Query; + var models = new List(); + CivitModelsResponse? modelsResponse = null; try { - var modelsResponse = await civitApi.GetModels(request); - var models = modelsResponse.Items; + if (!string.IsNullOrWhiteSpace(request.CommaSeparatedModelIds)) + { + // count IDs + var ids = request.CommaSeparatedModelIds.Split(','); + if (ids.Length > 100) + { + var idChunks = ids.Chunk(100); + foreach (var chunk in idChunks) + { + request.CommaSeparatedModelIds = string.Join(",", chunk); + var chunkModelsResponse = await civitApi.GetModels(request); + + if (chunkModelsResponse.Items != null) + { + models.AddRange(chunkModelsResponse.Items); + } + } + } + else + { + modelsResponse = await civitApi.GetModels(request); + models = modelsResponse.Items; + } + } + else + { + modelsResponse = await civitApi.GetModels(request); + models = modelsResponse.Items; + } + if (models is null) { Logger.Debug( @@ -241,13 +271,13 @@ public partial class CivitAiBrowserViewModel : TabViewModelBase, IInfinitelyScro InsertedAt = DateTimeOffset.UtcNow, Request = request, Items = models, - Metadata = modelsResponse.Metadata + Metadata = modelsResponse?.Metadata } ); UpdateModelCards(models, isInfiniteScroll); - NextPageCursor = modelsResponse.Metadata?.NextCursor; + NextPageCursor = modelsResponse?.Metadata?.NextCursor; } catch (OperationCanceledException) { @@ -428,12 +458,14 @@ public partial class CivitAiBrowserViewModel : TabViewModelBase, IInfinitelyScro { var connectedModels = CheckpointFile .GetAllCheckpointFiles(settingsManager.ModelsDirectory) - .Where(c => c.IsConnectedModel); + .Where(c => c.IsConnectedModel) + .ToList(); modelRequest.CommaSeparatedModelIds = string.Join( ",", connectedModels.Select(c => c.ConnectedModel!.ModelId).GroupBy(m => m).Select(g => g.First()) ); + modelRequest.Sort = null; modelRequest.Period = null; }