From 0b99879db6371f3a73ec542d587ad625284a74b3 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 2 Feb 2024 16:47:33 -0500 Subject: [PATCH] Preserve unknown keys in fooocus config --- StabilityMatrix.Core/Models/FooocusConfig.cs | 99 ------------------- .../Models/Packages/Fooocus.cs | 72 +++++++------- 2 files changed, 36 insertions(+), 135 deletions(-) delete mode 100644 StabilityMatrix.Core/Models/FooocusConfig.cs diff --git a/StabilityMatrix.Core/Models/FooocusConfig.cs b/StabilityMatrix.Core/Models/FooocusConfig.cs deleted file mode 100644 index abfb4cd4..00000000 --- a/StabilityMatrix.Core/Models/FooocusConfig.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System.Text.Json.Serialization; - -namespace StabilityMatrix.Core.Models; - -public class FooocusConfig -{ - [JsonPropertyName("path_checkpoints")] - public string PathCheckpoints { get; set; } - - [JsonPropertyName("path_loras")] - public string PathLoras { get; set; } - - [JsonPropertyName("path_embeddings")] - public string PathEmbeddings { get; set; } - - [JsonPropertyName("path_vae_approx")] - public string PathVaeApprox { get; set; } - - [JsonPropertyName("path_upscale_models")] - public string PathUpscaleModels { get; set; } - - [JsonPropertyName("path_inpaint")] - public string PathInpaint { get; set; } - - [JsonPropertyName("path_controlnet")] - public string PathControlnet { get; set; } - - [JsonPropertyName("path_clip_vision")] - public string PathClipVision { get; set; } - - [JsonPropertyName("path_fooocus_expansion")] - public string PathFooocusExpansion { get; set; } - - [JsonPropertyName("path_outputs")] - public string PathOutputs { get; set; } - - [JsonPropertyName("default_model")] - public string DefaultModel { get; set; } - - [JsonPropertyName("default_refiner")] - public string DefaultRefiner { get; set; } - - [JsonPropertyName("default_refiner_switch")] - public double DefaultRefinerSwitch { get; set; } - - [JsonPropertyName("default_cfg_scale")] - public long DefaultCfgScale { get; set; } - - [JsonPropertyName("default_sample_sharpness")] - public long DefaultSampleSharpness { get; set; } - - [JsonPropertyName("default_sampler")] - public string DefaultSampler { get; set; } - - [JsonPropertyName("default_scheduler")] - public string DefaultScheduler { get; set; } - - [JsonPropertyName("default_styles")] - public string[] DefaultStyles { get; set; } - - [JsonPropertyName("default_prompt_negative")] - public string DefaultPromptNegative { get; set; } - - [JsonPropertyName("default_prompt")] - public string DefaultPrompt { get; set; } - - [JsonPropertyName("default_performance")] - public string DefaultPerformance { get; set; } - - [JsonPropertyName("default_advanced_checkbox")] - public bool DefaultAdvancedCheckbox { get; set; } - - [JsonPropertyName("default_max_image_number")] - public long DefaultMaxImageNumber { get; set; } - - [JsonPropertyName("default_image_number")] - public long DefaultImageNumber { get; set; } - - [JsonPropertyName("available_aspect_ratios")] - public string[] AvailableAspectRatios { get; set; } - - [JsonPropertyName("default_aspect_ratio")] - public string DefaultAspectRatio { get; set; } - - [JsonPropertyName("default_inpaint_engine_version")] - public string DefaultInpaintEngineVersion { get; set; } - - [JsonPropertyName("default_cfg_tsnr")] - public long DefaultCfgTsnr { get; set; } - - [JsonPropertyName("default_overwrite_step")] - public long DefaultOverwriteStep { get; set; } - - [JsonPropertyName("default_overwrite_switch")] - public long DefaultOverwriteSwitch { get; set; } - - [JsonPropertyName("example_inpaint_prompts")] - public string[] ExampleInpaintPrompts { get; set; } -} diff --git a/StabilityMatrix.Core/Models/Packages/Fooocus.cs b/StabilityMatrix.Core/Models/Packages/Fooocus.cs index f32b89c2..7a8943e1 100644 --- a/StabilityMatrix.Core/Models/Packages/Fooocus.cs +++ b/StabilityMatrix.Core/Models/Packages/Fooocus.cs @@ -1,6 +1,6 @@ -using System.Collections.Immutable; -using System.Diagnostics; +using System.Diagnostics; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using StabilityMatrix.Core.Attributes; @@ -299,26 +299,26 @@ public class Fooocus( private async Task SetupModelFoldersConfig(DirectoryPath installDirectory) { var fooocusConfigPath = installDirectory.JoinFile("config.txt"); - var fooocusConfig = new FooocusConfig(); + + var fooocusConfig = new JsonObject(); + if (fooocusConfigPath.Exists) { - var configText = await fooocusConfigPath.ReadAllTextAsync().ConfigureAwait(false); - fooocusConfig = JsonSerializer.Deserialize(configText) ?? new FooocusConfig(); - } - else - { - fooocusConfigPath.Create(); + fooocusConfig = + await JsonSerializer + .DeserializeAsync(fooocusConfigPath.Info.OpenRead()) + .ConfigureAwait(false) ?? new JsonObject(); } - fooocusConfig.PathCheckpoints = Path.Combine(settingsManager.ModelsDirectory, "StableDiffusion"); - fooocusConfig.PathLoras = Path.Combine(settingsManager.ModelsDirectory, "Lora"); - fooocusConfig.PathEmbeddings = Path.Combine(settingsManager.ModelsDirectory, "TextualInversion"); - fooocusConfig.PathVaeApprox = Path.Combine(settingsManager.ModelsDirectory, "ApproxVAE"); - fooocusConfig.PathUpscaleModels = Path.Combine(settingsManager.ModelsDirectory, "ESRGAN"); - fooocusConfig.PathInpaint = Path.Combine(installDirectory, "models", "inpaint"); - fooocusConfig.PathControlnet = Path.Combine(settingsManager.ModelsDirectory, "ControlNet"); - fooocusConfig.PathClipVision = Path.Combine(settingsManager.ModelsDirectory, "CLIP"); - fooocusConfig.PathFooocusExpansion = Path.Combine( + fooocusConfig["path_checkpoints"] = Path.Combine(settingsManager.ModelsDirectory, "StableDiffusion"); + fooocusConfig["path_loras"] = Path.Combine(settingsManager.ModelsDirectory, "Lora"); + fooocusConfig["path_embeddings"] = Path.Combine(settingsManager.ModelsDirectory, "TextualInversion"); + fooocusConfig["path_vae_approx"] = Path.Combine(settingsManager.ModelsDirectory, "ApproxVAE"); + fooocusConfig["path_upscale_models"] = Path.Combine(settingsManager.ModelsDirectory, "ESRGAN"); + fooocusConfig["path_inpaint"] = Path.Combine(installDirectory, "models", "inpaint"); + fooocusConfig["path_controlnet"] = Path.Combine(settingsManager.ModelsDirectory, "ControlNet"); + fooocusConfig["path_clip_vision"] = Path.Combine(settingsManager.ModelsDirectory, "CLIP"); + fooocusConfig["path_fooocus_expansion"] = Path.Combine( installDirectory, "models", "prompt_expansion", @@ -329,7 +329,7 @@ public class Fooocus( // doesn't always exist on first install Directory.CreateDirectory(outputsPath); - fooocusConfig.PathOutputs = outputsPath; + fooocusConfig["path_outputs"] = outputsPath; await fooocusConfigPath .WriteAllTextAsync(JsonSerializer.Serialize(fooocusConfig, jsonSerializerOptions)) @@ -339,32 +339,32 @@ public class Fooocus( private async Task WriteDefaultConfig(DirectoryPath installDirectory) { var fooocusConfigPath = installDirectory.JoinFile("config.txt"); - var fooocusConfig = new FooocusConfig(); + + var fooocusConfig = new JsonObject(); + if (fooocusConfigPath.Exists) { - var configText = await fooocusConfigPath.ReadAllTextAsync().ConfigureAwait(false); - fooocusConfig = JsonSerializer.Deserialize(configText) ?? new FooocusConfig(); - } - else - { - fooocusConfigPath.Create(); + fooocusConfig = + await JsonSerializer + .DeserializeAsync(fooocusConfigPath.Info.OpenRead()) + .ConfigureAwait(false) ?? new JsonObject(); } - fooocusConfig.PathCheckpoints = Path.Combine(installDirectory, "models", "checkpoints"); - fooocusConfig.PathLoras = Path.Combine(installDirectory, "models", "loras"); - fooocusConfig.PathEmbeddings = Path.Combine(installDirectory, "models", "embeddings"); - fooocusConfig.PathVaeApprox = Path.Combine(installDirectory, "models", "vae_approx"); - fooocusConfig.PathUpscaleModels = Path.Combine(installDirectory, "models", "upscale_models"); - fooocusConfig.PathInpaint = Path.Combine(installDirectory, "models", "inpaint"); - fooocusConfig.PathControlnet = Path.Combine(installDirectory, "models", "controlnet"); - fooocusConfig.PathClipVision = Path.Combine(installDirectory, "models", "clip_vision"); - fooocusConfig.PathFooocusExpansion = Path.Combine( + fooocusConfig["path_checkpoints"] = Path.Combine(installDirectory, "models", "checkpoints"); + fooocusConfig["path_loras"] = Path.Combine(installDirectory, "models", "loras"); + fooocusConfig["path_embeddings"] = Path.Combine(installDirectory, "models", "embeddings"); + fooocusConfig["path_vae_approx"] = Path.Combine(installDirectory, "models", "vae_approx"); + fooocusConfig["path_upscale_models"] = Path.Combine(installDirectory, "models", "upscale_models"); + fooocusConfig["path_inpaint"] = Path.Combine(installDirectory, "models", "inpaint"); + fooocusConfig["path_controlnet"] = Path.Combine(installDirectory, "models", "controlnet"); + fooocusConfig["path_clip_vision"] = Path.Combine(installDirectory, "models", "clip_vision"); + fooocusConfig["path_fooocus_expansion"] = Path.Combine( installDirectory, "models", "prompt_expansion", "fooocus_expansion" ); - fooocusConfig.PathOutputs = Path.Combine(installDirectory, OutputFolderName); + fooocusConfig["path_outputs"] = Path.Combine(installDirectory, OutputFolderName); await fooocusConfigPath .WriteAllTextAsync(JsonSerializer.Serialize(fooocusConfig, jsonSerializerOptions))