From 747ed8c901d65dcafcaaa246c77e7ed193545542 Mon Sep 17 00:00:00 2001 From: Ionite Date: Mon, 26 Jun 2023 18:24:45 -0400 Subject: [PATCH 1/8] Add unknown model type handling --- .../Json/DefaultUnknownEnumConverter.cs | 41 +++++++++++++++++++ StabilityMatrix/Models/Api/CivitModelType.cs | 4 +- .../ViewModels/CheckpointBrowserViewModel.cs | 13 +++++- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 StabilityMatrix/Converters/Json/DefaultUnknownEnumConverter.cs diff --git a/StabilityMatrix/Converters/Json/DefaultUnknownEnumConverter.cs b/StabilityMatrix/Converters/Json/DefaultUnknownEnumConverter.cs new file mode 100644 index 00000000..da44e3ab --- /dev/null +++ b/StabilityMatrix/Converters/Json/DefaultUnknownEnumConverter.cs @@ -0,0 +1,41 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using StabilityMatrix.Extensions; + +namespace StabilityMatrix.Converters.Json; + +public class DefaultUnknownEnumConverter : JsonConverter where T : Enum +{ + public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType != JsonTokenType.String) + { + throw new JsonException(); + } + + var enumText = reader.GetString(); + if (Enum.TryParse(typeof(T), enumText, true, out var result)) + { + return (T) result!; + } + + // Unknown value handling + if (Enum.TryParse(typeof(T), "Unknown", true, out var unknownResult)) + { + return (T) unknownResult!; + } + + throw new JsonException($"Unable to parse '{enumText}' to enum '{typeof(T)}'."); + } + + public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + writer.WriteStringValue(value.GetStringValue()); + } +} diff --git a/StabilityMatrix/Models/Api/CivitModelType.cs b/StabilityMatrix/Models/Api/CivitModelType.cs index e397cf23..29c9a169 100644 --- a/StabilityMatrix/Models/Api/CivitModelType.cs +++ b/StabilityMatrix/Models/Api/CivitModelType.cs @@ -1,13 +1,15 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; +using StabilityMatrix.Converters.Json; using StabilityMatrix.Extensions; namespace StabilityMatrix.Models.Api; -[JsonConverter(typeof(JsonStringEnumConverter))] +[JsonConverter(typeof(DefaultUnknownEnumConverter))] [SuppressMessage("ReSharper", "InconsistentNaming")] public enum CivitModelType { + Unknown, [ConvertTo(SharedFolderType.StableDiffusion)] Checkpoint, [ConvertTo(SharedFolderType.TextualInversion)] diff --git a/StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs b/StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs index 56b4ef1c..6fa4d7aa 100644 --- a/StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs +++ b/StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs @@ -52,7 +52,7 @@ public partial class CheckpointBrowserViewModel : ObservableObject public IEnumerable AllModelTypes => Enum.GetValues(typeof(CivitModelType)) .Cast() - .Where(t => t != CivitModelType.AestheticGradient && t != CivitModelType.Poses) + .Where(t => t == CivitModelType.All || t.ConvertTo() > 0) .OrderBy(t => t.ToString()); public CheckpointBrowserViewModel( @@ -104,6 +104,17 @@ public partial class CheckpointBrowserViewModel : ObservableObject return; } Logger.Debug("CivitAI Query {Text} returned {Results} results (in {Elapsed:F1} s)", queryText, models.Count, timer.Elapsed.TotalSeconds); + + var unknown = models.Where(m => m.Type == CivitModelType.Unknown).ToList(); + if (unknown.Any()) + { + var names = unknown.Select(m => m.Name).ToList(); + Logger.Warn("Excluded {Unknown} unknown model types: {Models}", unknown.Count, names); + } + + // Filter out unknown model types + models = models.Where(m => m.Type.ConvertTo() > 0).ToList(); + // Database update calls will invoke `OnModelsUpdated` // Add to database await liteDbContext.UpsertCivitModelAsync(models); From 4833d15f0b922e5ba91472e39d2db4f83304469c Mon Sep 17 00:00:00 2001 From: Ionite Date: Mon, 26 Jun 2023 18:55:34 -0400 Subject: [PATCH 2/8] Ensure python installed on check python version --- StabilityMatrix/Helper/IPrerequisiteHelper.cs | 3 +++ StabilityMatrix/Helper/PrerequisiteHelper.cs | 2 ++ StabilityMatrix/SettingsPage.xaml | 23 +++++++++++++++---- .../ViewModels/SettingsViewModel.cs | 17 +++++++++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/StabilityMatrix/Helper/IPrerequisiteHelper.cs b/StabilityMatrix/Helper/IPrerequisiteHelper.cs index efc40c39..c6a0b0d7 100644 --- a/StabilityMatrix/Helper/IPrerequisiteHelper.cs +++ b/StabilityMatrix/Helper/IPrerequisiteHelper.cs @@ -6,10 +6,13 @@ namespace StabilityMatrix.Helper; public interface IPrerequisiteHelper { + bool IsPythonInstalled { get; } + Task InstallAllIfNecessary(IProgress? progress = null); Task UnpackResourcesIfNecessary(IProgress? progress = null); Task InstallGitIfNecessary(IProgress? progress = null); Task InstallVcRedistIfNecessary(IProgress? progress = null); + Task InstallPythonIfNecessary(IProgress? progress = null); Task SetupPythonDependencies(string installLocation, string requirementsFileName, IProgress? progress = null, Action? onConsoleOutput = null); diff --git a/StabilityMatrix/Helper/PrerequisiteHelper.cs b/StabilityMatrix/Helper/PrerequisiteHelper.cs index 1a5ba7a6..c46bd3bd 100644 --- a/StabilityMatrix/Helper/PrerequisiteHelper.cs +++ b/StabilityMatrix/Helper/PrerequisiteHelper.cs @@ -43,6 +43,8 @@ public class PrerequisiteHelper : IPrerequisiteHelper private static readonly string PortableGitDownloadPath = Path.Combine(HomeDir, "PortableGit.7z.exe"); private static readonly string GitExePath = Path.Combine(PortableGitInstallDir, "bin", "git.exe"); public static readonly string GitBinPath = Path.Combine(PortableGitInstallDir, "bin"); + + public bool IsPythonInstalled => File.Exists(PythonDllPath); public PrerequisiteHelper(ILogger logger, IGitHubClient gitHubClient, IDownloadService downloadService, ISettingsManager settingsManager) diff --git a/StabilityMatrix/SettingsPage.xaml b/StabilityMatrix/SettingsPage.xaml index fc75fc2f..a8a1ced5 100644 --- a/StabilityMatrix/SettingsPage.xaml +++ b/StabilityMatrix/SettingsPage.xaml @@ -133,10 +133,25 @@ FontWeight="Bold" Margin="0,8" Text="Embedded Python" /> -