Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions StabilityMatrix.Avalonia/Languages/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@
<data name="Label_OnlyAvailableOnWindows" xml:space="preserve">
<value>Only available on Windows</value>
</data>
<data name="Label_OnlySharedComponentsInstalled" xml:space="preserve">
<value>Only shared components (VAE, text encoder, etc.) are installed</value>
</data>
<data name="Action_AddForCurrentUser" xml:space="preserve">
<value>Add for Current User</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,25 @@ public partial class ModelVersionViewModel : DisposableViewModelBase
[ObservableProperty]
public partial bool IsInstalled { get; set; }

[ObservableProperty]
public partial bool IsComponentsOnlyInstalled { get; set; }

public ModelVersionViewModel(IModelIndexService modelIndexService, CivitModelVersion modelVersion)
{
this.modelIndexService = modelIndexService;

ModelVersion = modelVersion;

// Install detection is purely hash-based: a file whose hash is in the local model index is
// on disk, regardless of the type Civitai reports (which may be Unknown for new types)
IsInstalled =
ModelVersion.Files?.Any(file =>
file is { Hashes.BLAKE3: not null }
&& modelIndexService.ModelIndexBlake3Hashes.Contains(file.Hashes.BLAKE3)
) ?? false;
RefreshInstallStatus();

EventManager.Instance.ModelIndexChanged += ModelIndexChanged;
}

public void RefreshInstallStatus()
{
IsInstalled =
ModelVersion.Files?.Any(file =>
file is { Hashes.BLAKE3: not null }
&& modelIndexService.ModelIndexBlake3Hashes.Contains(file.Hashes.BLAKE3)
) ?? false;
var installState = ModelVersion.GetInstallState(modelIndexService.ModelIndexBlake3Hashes);
IsInstalled = installState is CivitModelVersionInstallState.Installed;
IsComponentsOnlyInstalled = installState is CivitModelVersionInstallState.ComponentsOnly;
}

private void ModelIndexChanged(object? sender, EventArgs e)
Expand Down
9 changes: 9 additions & 0 deletions StabilityMatrix.Avalonia/Views/CivitDetailsPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@
VerticalAlignment="Center"
IsVisible="{Binding IsInstalled}"
Value="fa-solid fa-check" />
<avalonia:Icon
Grid.Column="2"
Margin="8,2,0,0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
IsVisible="{Binding IsComponentsOnlyInstalled}"
Opacity="0.6"
ToolTip.Tip="{x:Static lang:Resources.Label_OnlySharedComponentsInstalled}"
Value="fa-solid fa-puzzle-piece" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
Expand Down
19 changes: 19 additions & 0 deletions StabilityMatrix.Core/Models/Api/CivitFileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ or CivitFileType.PrunedModel
or CivitFileType.DiffusionModel
or CivitFileType.UNet;

/// <summary>
/// True for companion files (VAEs, encoders, configs, etc) that ship alongside a version's main
/// weights. These are often shared between versions and models, so having one installed does
/// not mean the version itself is installed.
/// </summary>
public static bool IsSupportingComponent(this CivitFileType type) =>
type
is CivitFileType.VAE
or CivitFileType.TextEncoder
or CivitFileType.VisionEncoder
or CivitFileType.CLIPVision
or CivitFileType.Config
or CivitFileType.TrainingData
or CivitFileType.Negative
or CivitFileType.ControlNet
or CivitFileType.Upscaler
or CivitFileType.EnhancementLora
or CivitFileType.Workflow;

/// <summary>
/// True for file types worth listing as downloadable files in the model browser —
/// model weights plus companion components (VAE, text/vision encoders, etc).
Expand Down
33 changes: 33 additions & 0 deletions StabilityMatrix.Core/Models/Api/CivitModelVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,37 @@ public class CivitModelVersion
public bool IsEarlyAccess =>
(Availability?.Equals("EarlyAccess", StringComparison.OrdinalIgnoreCase) ?? false)
|| EarlyAccessDeadline > DateTimeOffset.UtcNow;

/// <summary>
/// Determines how much of this version is present locally, by BLAKE3 hash. Any one installed
/// main model file (any format or precision) makes the version installed; installed supporting
/// components alone do not, since they are frequently shared with other versions.
/// Files of unknown type count as main files so newly added Civitai types are still detected.
/// </summary>
public CivitModelVersionInstallState GetInstallState(IReadOnlySet<string> installedBlake3Hashes)
{
if (Files is not { Count: > 0 } || installedBlake3Hashes.Count == 0)
{
return CivitModelVersionInstallState.NotInstalled;
}

var installedFiles = Files
.Where(file =>
file is { Hashes.BLAKE3: not null } && installedBlake3Hashes.Contains(file.Hashes.BLAKE3)
)
.ToList();

if (installedFiles.Count == 0)
{
return CivitModelVersionInstallState.NotInstalled;
}

var hasMainFiles = Files.Any(file => !file.Type.IsSupportingComponent());
if (!hasMainFiles || installedFiles.Any(file => !file.Type.IsSupportingComponent()))
{
return CivitModelVersionInstallState.Installed;
}

return CivitModelVersionInstallState.ComponentsOnly;
}
}
14 changes: 14 additions & 0 deletions StabilityMatrix.Core/Models/Api/CivitModelVersionInstallState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace StabilityMatrix.Core.Models.Api;

public enum CivitModelVersionInstallState
{
NotInstalled,

/// <summary>
/// Only supporting components (VAE, text encoder, etc) of the version are installed,
/// none of its main model files.
/// </summary>
ComponentsOnly,

Installed,
}
73 changes: 73 additions & 0 deletions StabilityMatrix.Tests/Core/CivitModelVersionInstallStateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using StabilityMatrix.Core.Models.Api;

namespace StabilityMatrix.Tests.Core;

[TestClass]
public class CivitModelVersionInstallStateTests
{
private static CivitFile File(CivitFileType type, string hash) =>
new()
{
Type = type,
Hashes = new CivitFileHashes { BLAKE3 = hash },
};

private static CivitModelVersion Version(params CivitFile[] files) => new() { Files = files.ToList() };

private static CivitModelVersion MultiFileVersion() =>
Version(
File(CivitFileType.DiffusionModel, "bf16"),
File(CivitFileType.DiffusionModel, "fp8"),
File(CivitFileType.TextEncoder, "shared-encoder"),
File(CivitFileType.VAE, "shared-vae")
);

[TestMethod]
public void NothingInstalled_IsNotInstalled()
{
Assert.AreEqual(
CivitModelVersionInstallState.NotInstalled,
MultiFileVersion().GetInstallState(new HashSet<string> { "unrelated" })
);
}

[TestMethod]
public void OnlySharedComponentsInstalled_IsComponentsOnly()
{
Assert.AreEqual(
CivitModelVersionInstallState.ComponentsOnly,
MultiFileVersion().GetInstallState(new HashSet<string> { "shared-encoder", "shared-vae" })
);
}

[TestMethod]
public void AnyMainModelVariantInstalled_IsInstalled()
{
Assert.AreEqual(
CivitModelVersionInstallState.Installed,
MultiFileVersion().GetInstallState(new HashSet<string> { "fp8" })
);
}

[TestMethod]
public void UnknownFileTypeInstalled_IsInstalled()
{
var version = Version(File(CivitFileType.Unknown, "new-type"), File(CivitFileType.VAE, "vae"));

Assert.AreEqual(
CivitModelVersionInstallState.Installed,
version.GetInstallState(new HashSet<string> { "new-type" })
);
}

[TestMethod]
public void VersionWithOnlyComponentFiles_IsInstalledWhenOneIsPresent()
{
var version = Version(File(CivitFileType.VAE, "vae"), File(CivitFileType.Config, "config"));

Assert.AreEqual(
CivitModelVersionInstallState.Installed,
version.GetInstallState(new HashSet<string> { "vae" })
);
}
}
Loading