Browse Source

Added kohya_ss package (wip) and tkinter install

pull/240/head
JT 1 year ago
parent
commit
95bb528d06
  1. 4
      StabilityMatrix.Avalonia/Assets.cs
  2. BIN
      StabilityMatrix.Avalonia/Assets/win-x64/tkinter_3_10_7.zip
  3. 7
      StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs
  4. 16
      StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs
  5. 162
      StabilityMatrix.Core/Models/Packages/KohyaSs.cs

4
StabilityMatrix.Avalonia/Assets.cs

@ -136,6 +136,10 @@ internal static class Assets
)
);
[SupportedOSPlatform("windows")]
public static AvaloniaResource TkinterZip =>
new("avares://StabilityMatrix.Avalonia/Assets/win-x64/tkinter_3_10_7.zip");
public static IReadOnlyList<RemoteResource> DefaultCompletionTags { get; } =
new[]
{

BIN
StabilityMatrix.Avalonia/Assets/win-x64/tkinter_3_10_7.zip

Binary file not shown.

7
StabilityMatrix.Avalonia/Helpers/UnixPrerequisiteHelper.cs

@ -233,6 +233,13 @@ public class UnixPrerequisiteHelper : IPrerequisiteHelper
throw new NotImplementedException();
}
[UnsupportedOSPlatform("Linux")]
[UnsupportedOSPlatform("macOS")]
public Task InstallTkinterIfNecessary(IProgress<ProgressReport>? progress = null)
{
throw new PlatformNotSupportedException();
}
[UnsupportedOSPlatform("Linux")]
[UnsupportedOSPlatform("macOS")]
public Task InstallVcRedistIfNecessary(IProgress<ProgressReport>? progress = null)

16
StabilityMatrix.Avalonia/Helpers/WindowsPrerequisiteHelper.cs

@ -43,6 +43,8 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper
private string PortableGitInstallDir => Path.Combine(HomeDir, "PortableGit");
private string PortableGitDownloadPath => Path.Combine(HomeDir, "PortableGit.7z.exe");
private string GitExePath => Path.Combine(PortableGitInstallDir, "bin", "git.exe");
private string TkinterZipPath => Path.Combine(AssetsDir, "tkinter.zip");
private string TkinterExtractPath => PythonDir;
public string GitBinPath => Path.Combine(PortableGitInstallDir, "bin");
public bool IsPythonInstalled => File.Exists(PythonDllPath);
@ -223,6 +225,9 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper
pythonPthContent = pythonPthContent.Replace("#import site", "import site");
await File.WriteAllTextAsync(pythonPthPath, pythonPthContent);
// Install TKinter
await InstallTkinterIfNecessary(progress);
progress?.Report(new ProgressReport(1f, "Python install complete"));
}
finally
@ -235,6 +240,17 @@ public class WindowsPrerequisiteHelper : IPrerequisiteHelper
}
}
[SupportedOSPlatform("windows")]
public async Task InstallTkinterIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (!File.Exists(TkinterZipPath))
{
await Assets.TkinterZip.ExtractTo(TkinterZipPath);
}
await ArchiveHelper.Extract(TkinterZipPath, TkinterExtractPath, progress);
}
public async Task InstallGitIfNecessary(IProgress<ProgressReport>? progress = null)
{
if (File.Exists(GitExePath))

162
StabilityMatrix.Core/Models/Packages/KohyaSs.cs

@ -0,0 +1,162 @@
using System.Text.RegularExpressions;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Helper.Cache;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Models.Progress;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Python;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Core.Models.Packages;
[Singleton(typeof(BasePackage))]
public class KohyaSs : BaseGitPackage
{
public KohyaSs(
IGithubApiCache githubApi,
ISettingsManager settingsManager,
IDownloadService downloadService,
IPrerequisiteHelper prerequisiteHelper
)
: base(githubApi, settingsManager, downloadService, prerequisiteHelper) { }
public override string Name => "kohya_ss";
public override string DisplayName { get; set; } = "Kohya's GUI";
public override string Author => "bmaltais";
public override string Blurb =>
"A Windows-focused Gradio GUI for Kohya's Stable Diffusion trainers";
public override string LicenseType => "Apache-2.0";
public override string LicenseUrl =>
"https://github.com/bmaltais/kohya_ss/blob/master/LICENSE.md";
public override string LaunchCommand => "kohya_gui.py";
public override Uri PreviewImageUri =>
new(
"https://camo.githubusercontent.com/2170d2204816f428eec57ff87218f06344e0b4d91966343a6c5f0a76df91ec75/68747470733a2f2f696d672e796f75747562652e636f6d2f76692f6b35696d713031757655592f302e6a7067"
);
public override string OutputFolderName => string.Empty;
public override bool IsCompatible => HardwareHelper.HasNvidiaGpu();
public override TorchVersion GetRecommendedTorchVersion() => TorchVersion.Cuda;
public override bool OfferInOneClickInstaller => false;
public override async Task InstallPackage(
string installLocation,
TorchVersion torchVersion,
DownloadPackageVersionOptions versionOptions,
IProgress<ProgressReport>? progress = null,
Action<ProcessOutput>? onConsoleOutput = null
)
{
if (Compat.IsWindows)
{
progress?.Report(
new ProgressReport(-1f, "Installing prerequisites...", isIndeterminate: true)
);
await PrerequisiteHelper.InstallTkinterIfNecessary(progress).ConfigureAwait(false);
}
progress?.Report(new ProgressReport(-1f, "Setting up venv", isIndeterminate: true));
// Setup venv
await using var venvRunner = new PyVenvRunner(Path.Combine(installLocation, "venv"));
venvRunner.WorkingDirectory = installLocation;
await venvRunner.Setup(true, onConsoleOutput).ConfigureAwait(false);
var setupSmPath = Path.Combine(installLocation, "setup", "setup_sm.py");
var setupText = """
import setup_windows
import setup_common
setup_common.install_requirements('requirements_windows_torch2.txt', check_no_verify_flag=False)
setup_windows.sync_bits_and_bytes_files()
setup_common.configure_accelerate(run_accelerate=False)
""";
await File.WriteAllTextAsync(setupSmPath, setupText).ConfigureAwait(false);
// Install
venvRunner.RunDetached("setup/setup_sm.py", onConsoleOutput);
await venvRunner.Process.WaitForExitAsync().ConfigureAwait(false);
}
public override async Task RunPackage(
string installedPackagePath,
string command,
string arguments,
Action<ProcessOutput>? onConsoleOutput
)
{
await SetupVenv(installedPackagePath).ConfigureAwait(false);
var process = ProcessRunner.StartProcess(
Path.Combine(installedPackagePath, "venv", "Scripts", "accelerate.exe"),
"env",
installedPackagePath,
s => onConsoleOutput?.Invoke(new ProcessOutput { Text = s })
);
await process.WaitForExitAsync().ConfigureAwait(false);
void HandleConsoleOutput(ProcessOutput s)
{
onConsoleOutput?.Invoke(s);
if (!s.Text.Contains("Running on", StringComparison.OrdinalIgnoreCase))
return;
var regex = new Regex(@"(https?:\/\/)([^:\s]+):(\d+)");
var match = regex.Match(s.Text);
if (!match.Success)
return;
WebUrl = match.Value;
OnStartupComplete(WebUrl);
}
var args = $"\"{Path.Combine(installedPackagePath, command)}\" {arguments}";
VenvRunner.EnvironmentVariables = GetEnvVars();
VenvRunner.RunDetached(args.TrimEnd(), HandleConsoleOutput, OnExit);
}
public override SharedFolderMethod RecommendedSharedFolderMethod => SharedFolderMethod.Symlink;
public override IEnumerable<TorchVersion> AvailableTorchVersions => new[] { TorchVersion.Cuda };
public override List<LaunchOptionDefinition> LaunchOptions =>
new() { LaunchOptionDefinition.Extras };
public override Dictionary<SharedFolderType, IReadOnlyList<string>>? SharedFolders { get; }
public override Dictionary<
SharedOutputType,
IReadOnlyList<string>
>? SharedOutputFolders { get; }
public override async Task<string> GetLatestVersion()
{
var release = await GetLatestRelease().ConfigureAwait(false);
return release.TagName!;
}
private Dictionary<string, string> GetEnvVars()
{
// Set additional required environment variables
var env = new Dictionary<string, string>();
if (SettingsManager.Settings.EnvironmentVariables is not null)
{
env.Update(SettingsManager.Settings.EnvironmentVariables);
}
var tkPath = Path.Combine(
SettingsManager.LibraryDir,
"Assets",
"Python310",
"tcl",
"tcl8.6"
);
env["TCL_LIBRARY"] = tkPath;
env["TK_LIBRARY"] = tkPath;
return env;
}
}
Loading…
Cancel
Save