Browse Source

Merge pull request #116 from ionite34/fixes

pull/5/head
Ionite 1 year ago committed by GitHub
parent
commit
9197712f6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      StabilityMatrix/Converters/Json/DefaultUnknownEnumConverter.cs
  2. 3
      StabilityMatrix/Helper/IPrerequisiteHelper.cs
  3. 2
      StabilityMatrix/Helper/PrerequisiteHelper.cs
  4. 4
      StabilityMatrix/Models/Api/CivitModelType.cs
  5. 23
      StabilityMatrix/SettingsPage.xaml
  6. 13
      StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs
  7. 17
      StabilityMatrix/ViewModels/SettingsViewModel.cs

41
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<T> : JsonConverter<T> 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());
}
}

3
StabilityMatrix/Helper/IPrerequisiteHelper.cs

@ -6,10 +6,13 @@ namespace StabilityMatrix.Helper;
public interface IPrerequisiteHelper public interface IPrerequisiteHelper
{ {
bool IsPythonInstalled { get; }
Task InstallAllIfNecessary(IProgress<ProgressReport>? progress = null); Task InstallAllIfNecessary(IProgress<ProgressReport>? progress = null);
Task UnpackResourcesIfNecessary(IProgress<ProgressReport>? progress = null); Task UnpackResourcesIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallGitIfNecessary(IProgress<ProgressReport>? progress = null); Task InstallGitIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallVcRedistIfNecessary(IProgress<ProgressReport>? progress = null); Task InstallVcRedistIfNecessary(IProgress<ProgressReport>? progress = null);
Task InstallPythonIfNecessary(IProgress<ProgressReport>? progress = null);
Task SetupPythonDependencies(string installLocation, string requirementsFileName, Task SetupPythonDependencies(string installLocation, string requirementsFileName,
IProgress<ProgressReport>? progress = null, Action<string?>? onConsoleOutput = null); IProgress<ProgressReport>? progress = null, Action<string?>? onConsoleOutput = null);

2
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 PortableGitDownloadPath = Path.Combine(HomeDir, "PortableGit.7z.exe");
private static readonly string GitExePath = Path.Combine(PortableGitInstallDir, "bin", "git.exe"); private static readonly string GitExePath = Path.Combine(PortableGitInstallDir, "bin", "git.exe");
public static readonly string GitBinPath = Path.Combine(PortableGitInstallDir, "bin"); public static readonly string GitBinPath = Path.Combine(PortableGitInstallDir, "bin");
public bool IsPythonInstalled => File.Exists(PythonDllPath);
public PrerequisiteHelper(ILogger<PrerequisiteHelper> logger, IGitHubClient gitHubClient, public PrerequisiteHelper(ILogger<PrerequisiteHelper> logger, IGitHubClient gitHubClient,
IDownloadService downloadService, ISettingsManager settingsManager) IDownloadService downloadService, ISettingsManager settingsManager)

4
StabilityMatrix/Models/Api/CivitModelType.cs

@ -1,13 +1,15 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using StabilityMatrix.Converters.Json;
using StabilityMatrix.Extensions; using StabilityMatrix.Extensions;
namespace StabilityMatrix.Models.Api; namespace StabilityMatrix.Models.Api;
[JsonConverter(typeof(JsonStringEnumConverter))] [JsonConverter(typeof(DefaultUnknownEnumConverter<CivitModelType>))]
[SuppressMessage("ReSharper", "InconsistentNaming")] [SuppressMessage("ReSharper", "InconsistentNaming")]
public enum CivitModelType public enum CivitModelType
{ {
Unknown,
[ConvertTo<SharedFolderType>(SharedFolderType.StableDiffusion)] [ConvertTo<SharedFolderType>(SharedFolderType.StableDiffusion)]
Checkpoint, Checkpoint,
[ConvertTo<SharedFolderType>(SharedFolderType.TextualInversion)] [ConvertTo<SharedFolderType>(SharedFolderType.TextualInversion)]

23
StabilityMatrix/SettingsPage.xaml

@ -133,10 +133,25 @@
FontWeight="Bold" FontWeight="Bold"
Margin="0,8" Margin="0,8"
Text="Embedded Python" /> Text="Embedded Python" />
<Button <StackPanel Orientation="Horizontal">
Command="{Binding PythonVersionCommand}" <ui:Button
Content="Check Version Info" Command="{Binding PythonVersionCommand}"
Margin="8" /> Content="Check Version Info"
Margin="8" />
<!-- Progress for python install if needed -->
<StackPanel Orientation="Horizontal" Visibility="{Binding IsPythonInstalling, Converter={StaticResource BoolToVisibilityConverter}, FallbackValue=Collapsed}">
<ui:ProgressRing
Height="24"
IsEnabled="{Binding IsPythonInstalling}"
IsIndeterminate="True"
Margin="8"
Width="24" />
<TextBlock
Margin="4"
Text="Preparing Environment"
VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</StackPanel> </StackPanel>
</ui:Card> </ui:Card>

13
StabilityMatrix/ViewModels/CheckpointBrowserViewModel.cs

@ -52,7 +52,7 @@ public partial class CheckpointBrowserViewModel : ObservableObject
public IEnumerable<CivitModelType> AllModelTypes => Enum.GetValues(typeof(CivitModelType)) public IEnumerable<CivitModelType> AllModelTypes => Enum.GetValues(typeof(CivitModelType))
.Cast<CivitModelType>() .Cast<CivitModelType>()
.Where(t => t != CivitModelType.AestheticGradient && t != CivitModelType.Poses) .Where(t => t == CivitModelType.All || t.ConvertTo<SharedFolderType>() > 0)
.OrderBy(t => t.ToString()); .OrderBy(t => t.ToString());
public CheckpointBrowserViewModel( public CheckpointBrowserViewModel(
@ -104,6 +104,17 @@ public partial class CheckpointBrowserViewModel : ObservableObject
return; return;
} }
Logger.Debug("CivitAI Query {Text} returned {Results} results (in {Elapsed:F1} s)", queryText, models.Count, timer.Elapsed.TotalSeconds); 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<SharedFolderType>() > 0).ToList();
// Database update calls will invoke `OnModelsUpdated` // Database update calls will invoke `OnModelsUpdated`
// Add to database // Add to database
await liteDbContext.UpsertCivitModelAsync(models); await liteDbContext.UpsertCivitModelAsync(models);

17
StabilityMatrix/ViewModels/SettingsViewModel.cs

@ -41,6 +41,7 @@ public partial class SettingsViewModel : ObservableObject
private readonly IPyRunner pyRunner; private readonly IPyRunner pyRunner;
private readonly ISnackbarService snackbarService; private readonly ISnackbarService snackbarService;
private readonly ILiteDbContext liteDbContext; private readonly ILiteDbContext liteDbContext;
private readonly IPrerequisiteHelper prerequisiteHelper;
private static string LicensesPath => "pack://application:,,,/Assets/licenses.json"; private static string LicensesPath => "pack://application:,,,/Assets/licenses.json";
public TextToFlowDocumentConverter? TextToFlowDocumentConverter { get; set; } public TextToFlowDocumentConverter? TextToFlowDocumentConverter { get; set; }
@ -62,6 +63,8 @@ public partial class SettingsViewModel : ObservableObject
[ObservableProperty] private bool isFileSearchFlyoutOpen; [ObservableProperty] private bool isFileSearchFlyoutOpen;
[ObservableProperty] private double fileSearchProgress; [ObservableProperty] private double fileSearchProgress;
[ObservableProperty] private bool isPythonInstalling;
[ObservableProperty] private string? webApiHost; [ObservableProperty] private string? webApiHost;
[ObservableProperty] private string? webApiPort; [ObservableProperty] private string? webApiPort;
[ObservableProperty] private string? webApiActivePackageHost; [ObservableProperty] private string? webApiActivePackageHost;
@ -94,7 +97,8 @@ public partial class SettingsViewModel : ObservableObject
ISnackbarService snackbarService, ISnackbarService snackbarService,
ILogger<SettingsViewModel> logger, ILogger<SettingsViewModel> logger,
IPackageFactory packageFactory, IPackageFactory packageFactory,
ILiteDbContext liteDbContext) ILiteDbContext liteDbContext,
IPrerequisiteHelper prerequisiteHelper)
{ {
this.logger = logger; this.logger = logger;
this.settingsManager = settingsManager; this.settingsManager = settingsManager;
@ -104,6 +108,7 @@ public partial class SettingsViewModel : ObservableObject
this.a3WebApiManager = a3WebApiManager; this.a3WebApiManager = a3WebApiManager;
this.pyRunner = pyRunner; this.pyRunner = pyRunner;
this.liteDbContext = liteDbContext; this.liteDbContext = liteDbContext;
this.prerequisiteHelper = prerequisiteHelper;
SelectedTheme = settingsManager.Settings.Theme ?? "Dark"; SelectedTheme = settingsManager.Settings.Theme ?? "Dark";
WindowBackdropType = settingsManager.Settings.WindowBackdropType ?? WindowBackdropType.Mica; WindowBackdropType = settingsManager.Settings.WindowBackdropType ?? WindowBackdropType.Mica;
} }
@ -169,6 +174,16 @@ public partial class SettingsViewModel : ObservableObject
public AsyncRelayCommand PythonVersionCommand => new(async () => public AsyncRelayCommand PythonVersionCommand => new(async () =>
{ {
// Ensure python installed
if (!prerequisiteHelper.IsPythonInstalled)
{
IsPythonInstalling = true;
// Need 7z as well for site packages repack
await prerequisiteHelper.UnpackResourcesIfNecessary();
await prerequisiteHelper.InstallPythonIfNecessary();
IsPythonInstalling = false;
}
// Get python version // Get python version
await pyRunner.Initialize(); await pyRunner.Initialize();
var result = await pyRunner.GetVersionInfo(); var result = await pyRunner.GetVersionInfo();

Loading…
Cancel
Save