diff --git a/StabilityMatrix.Avalonia/Styles/ControlThemes/BetterComboBoxStyles.axaml b/StabilityMatrix.Avalonia/Styles/ControlThemes/BetterComboBoxStyles.axaml index 5a86e5ca2..5e4be4c11 100644 --- a/StabilityMatrix.Avalonia/Styles/ControlThemes/BetterComboBoxStyles.axaml +++ b/StabilityMatrix.Avalonia/Styles/ControlThemes/BetterComboBoxStyles.axaml @@ -412,7 +412,7 @@ Margin="0,0,0,0" Padding="6,0,6,0" Classes="Circle LightBlue" - Content="{Binding Local.ConnectedModelInfo.BaseModel}" + Content="{Binding Local.DisplayBaseModel}" FontSize="10" FontWeight="SemiBold" Theme="{StaticResource TagLabel}" /> @@ -517,7 +517,7 @@ Margin="0,0,0,0" Padding="6,0,6,0" Classes="Circle LightBlue" - Content="{Binding Local.ConnectedModelInfo.BaseModel}" + Content="{Binding Local.DisplayBaseModel}" FontSize="10" FontWeight="SemiBold" Theme="{StaticResource TagLabel}" /> diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFileViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFileViewModel.cs index 2b230df9c..4729d5bc1 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFileViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFileViewModel.cs @@ -63,8 +63,8 @@ public partial class CheckpointFileViewModel : SelectableViewModelBase private readonly IServiceManager vmFactory; private readonly ILogger logger; - public bool CanShowTriggerWords => CheckpointFile.ConnectedModelInfo?.TrainedWords?.Length > 0; - public string BaseModelName => CheckpointFile.ConnectedModelInfo?.BaseModel ?? string.Empty; + public bool CanShowTriggerWords => CheckpointFile.DisplayTrainedWords?.Length > 0; + public string BaseModelName => CheckpointFile.DisplayBaseModel ?? string.Empty; public CivitModelType ModelType => CheckpointFile.ConnectedModelInfo?.ModelType ?? CivitModelType.Unknown; public bool HasEarlyAccessUpdateOnly => CheckpointFile.HasEarlyAccessUpdateOnly; public bool HasStandardUpdate => CheckpointFile.HasUpdate && !CheckpointFile.HasEarlyAccessUpdateOnly; @@ -115,10 +115,7 @@ LocalModelFile checkpointFile [RelayCommand] private Task CopyTriggerWords() { - if (!CheckpointFile.HasConnectedModel) - return Task.CompletedTask; - - var words = CheckpointFile.ConnectedModelInfo.TrainedWordsString; + var words = CheckpointFile.DisplayTrainedWordsString; return string.IsNullOrWhiteSpace(words) ? Task.CompletedTask : App.Clipboard.SetTextAsync(words); } @@ -203,7 +200,7 @@ is not IMetadataImportService importService CheckpointFile.ConnectedModelInfo = cmInfo; var uri = - CheckpointFile.GetPreviewImageFullPath(settingsManager.ModelsDirectory) + CheckpointFile.GetDisplayPreviewImageFullPath(settingsManager.ModelsDirectory) ?? cmInfo.ThumbnailImageUrl; if (string.IsNullOrWhiteSpace(uri)) { @@ -638,7 +635,7 @@ private void UpdateImage() else { var previewPath = settingsManager.IsLibraryDirSet - ? CheckpointFile.GetPreviewImageFullPath(settingsManager.ModelsDirectory) + ? CheckpointFile.GetDisplayPreviewImageFullPath(settingsManager.ModelsDirectory) ?? CheckpointFile.ConnectedModelInfo?.ThumbnailImageUrl : null; diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs index ecaddeb87..723932ea8 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs @@ -227,10 +227,10 @@ protected override async Task OnInitialLoadedAsync() || file.DisplayModelName.Contains(SearchQuery, StringComparison.OrdinalIgnoreCase) || file.DisplayModelVersion.Contains(SearchQuery, StringComparison.OrdinalIgnoreCase) || ( - file.ConnectedModelInfo?.TrainedWordsString.Contains( + file.DisplayTrainedWordsString.Contains( SearchQuery, StringComparison.OrdinalIgnoreCase - ) ?? false + ) ) ) ) @@ -281,12 +281,8 @@ or nameof(SortConnectedModelsFirst) case CheckpointSortMode.BaseModel: comparer = SelectedSortDirection == ListSortDirection.Ascending - ? comparer.ThenByAscending(vm => - vm.CheckpointFile.ConnectedModelInfo?.BaseModel - ) - : comparer.ThenByDescending(vm => - vm.CheckpointFile.ConnectedModelInfo?.BaseModel - ); + ? comparer.ThenByAscending(vm => vm.CheckpointFile.DisplayBaseModel) + : comparer.ThenByDescending(vm => vm.CheckpointFile.DisplayBaseModel); comparer = comparer.ThenByAscending(vm => vm.CheckpointFile.DisplayModelName); break; @@ -1299,10 +1295,8 @@ private bool FilterModels(LocalModelFile file) return false; if (SelectedCategory?.Path is null || SelectedCategory?.Path == settingsManager.ModelsDirectory) - return file.HasConnectedModel - ? SelectedBaseModels.Count == 0 - || SelectedBaseModels.Contains(file.ConnectedModelInfo.BaseModel ?? "Other") - : SelectedBaseModels.Count == 0 || SelectedBaseModels.Contains("Other"); + return SelectedBaseModels.Count == 0 + || SelectedBaseModels.Contains(file.DisplayBaseModel ?? "Other"); var categoryRelativePath = SelectedCategory ?.Path.Replace(settingsManager.ModelsDirectory, string.Empty) @@ -1311,15 +1305,7 @@ private bool FilterModels(LocalModelFile file) if (categoryRelativePath == null || folderPath == null) return false; - if ( - ( - file.HasConnectedModel - ? SelectedBaseModels.Count == 0 - || SelectedBaseModels.Contains(file.ConnectedModelInfo?.BaseModel ?? "Other") - : SelectedBaseModels.Count == 0 || SelectedBaseModels.Contains("Other") - ) - is false - ) + if (SelectedBaseModels.Count != 0 && !SelectedBaseModels.Contains(file.DisplayBaseModel ?? "Other")) return false; // If not showing nested models, just check if the file is directly in this folder diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/ExtraNetworkCardViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/ExtraNetworkCardViewModel.cs index 864c49eb6..4d916cfc6 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/ExtraNetworkCardViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/ExtraNetworkCardViewModel.cs @@ -121,8 +121,7 @@ IServiceManager vmFactory public IObservableCollection LoraModels { get; } = new ObservableCollectionExtended(); - public string TriggerWords => - SelectedModel?.Local?.ConnectedModelInfo?.TrainedWordsString ?? string.Empty; + public string TriggerWords => SelectedModel?.Local?.DisplayTrainedWordsString ?? string.Empty; public bool ShowTriggerWords => !string.IsNullOrWhiteSpace(TriggerWords); public IInferenceClientManager ClientManager { get; } diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/ModelCardViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/ModelCardViewModel.cs index 1409512f2..800af5760 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/ModelCardViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/ModelCardViewModel.cs @@ -404,7 +404,7 @@ SelectedWorkflowProfile is InferenceWorkflowProfile.Auto { // Respect explicit metadata: an SDXL-tagged checkpoint that merely contains // "anima" or "flux2" in its filename is not a misplaced standalone model. - var taggedBase = local.ConnectedModelInfo?.BaseModel; + var taggedBase = local.DisplayBaseModel; if (!string.IsNullOrWhiteSpace(taggedBase) && IsKnownCheckpointBaseModelTag(taggedBase)) return null; @@ -901,7 +901,7 @@ private static InferenceWorkflowProfile InferWorkflowProfile(HybridModelFile? mo if (!isUnetModel) return InferenceWorkflowProfile.DefaultCheckpoint; - var baseModel = model?.Local?.ConnectedModelInfo?.BaseModel; + var baseModel = model?.Local?.DisplayBaseModel; if (!string.IsNullOrWhiteSpace(baseModel)) { diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs index 12c106926..ca8472221 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs @@ -616,7 +616,7 @@ private async Task AmplifyPrompt() var negativePrompt = GetNegativePrompt(); var selectedModel = tabContext.SelectedModel; - var modelTags = selectedModel?.Local?.ConnectedModelInfo?.BaseModel?.ToLower() switch + var modelTags = selectedModel?.Local?.DisplayBaseModel?.ToLower() switch { { } baseModel when baseModel.Contains("flux") => new List { ModelTags.Flux }, { } baseModel when baseModel.Contains("sdxl") => [ModelTags.Sdxl], diff --git a/StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml b/StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml index 05fbb6138..0adf9403f 100644 --- a/StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml +++ b/StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml @@ -602,14 +602,21 @@ - + + + + + + + + Classes="info" + IsVisible="{Binding CheckpointFile.HasConnectedModel}"> + ToolTip.Tip="{Binding CheckpointFile.DisplayTrainedWordsString}"> - + diff --git a/StabilityMatrix.Avalonia/Views/Dialogs/ModelPickerDialog.axaml b/StabilityMatrix.Avalonia/Views/Dialogs/ModelPickerDialog.axaml index 7b23c8859..1dcbf7af3 100644 --- a/StabilityMatrix.Avalonia/Views/Dialogs/ModelPickerDialog.axaml +++ b/StabilityMatrix.Avalonia/Views/Dialogs/ModelPickerDialog.axaml @@ -354,10 +354,10 @@ MinHeight="0" Padding="8,2" Classes="Circle LightBlue" - Content="{Binding Local.ConnectedModelInfo.BaseModel, FallbackValue=''}" + Content="{Binding Local.DisplayBaseModel, FallbackValue=''}" FontSize="11" FontWeight="SemiBold" - IsVisible="{Binding Local.ConnectedModelInfo.BaseModel, Converter={x:Static StringConverters.IsNotNullOrEmpty}, FallbackValue=False}" + IsVisible="{Binding Local.DisplayBaseModel, Converter={x:Static StringConverters.IsNotNullOrEmpty}, FallbackValue=False}" Theme="{StaticResource TagLabel}" /> @@ -486,10 +486,10 @@ MinHeight="0" Padding="8,2" Classes="Circle LightBlue" - Content="{Binding Local.ConnectedModelInfo.BaseModel, FallbackValue=''}" + Content="{Binding Local.DisplayBaseModel, FallbackValue=''}" FontSize="11" FontWeight="SemiBold" - IsVisible="{Binding Local.ConnectedModelInfo.BaseModel, Converter={x:Static StringConverters.IsNotNullOrEmpty}, FallbackValue=False}" + IsVisible="{Binding Local.DisplayBaseModel, Converter={x:Static StringConverters.IsNotNullOrEmpty}, FallbackValue=False}" Theme="{StaticResource TagLabel}" /> diff --git a/StabilityMatrix.Core/Helper/ModelCompatChecker.cs b/StabilityMatrix.Core/Helper/ModelCompatChecker.cs index abac26f06..eba0a5d24 100644 --- a/StabilityMatrix.Core/Helper/ModelCompatChecker.cs +++ b/StabilityMatrix.Core/Helper/ModelCompatChecker.cs @@ -11,22 +11,16 @@ public class ModelCompatChecker public bool? IsLoraCompatibleWithBaseModel(HybridModelFile? lora, HybridModelFile? baseModel) { - // Require connected info for both + // Require a known base model for both if ( - lora?.Local?.ConnectedModelInfo is not { } loraInfo - || baseModel?.Local?.ConnectedModelInfo is not { } baseModelInfo + lora?.Local?.DisplayBaseModel is not { } loraBaseModel + || !baseModelNamesToTypes.TryGetValue(loraBaseModel, out var loraBaseModelType) ) return null; if ( - loraInfo.BaseModel is null - || !baseModelNamesToTypes.TryGetValue(loraInfo.BaseModel, out var loraBaseModelType) - ) - return null; - - if ( - baseModelInfo.BaseModel is null - || !baseModelNamesToTypes.TryGetValue(baseModelInfo.BaseModel, out var baseModelType) + baseModel?.Local?.DisplayBaseModel is not { } baseModelName + || !baseModelNamesToTypes.TryGetValue(baseModelName, out var baseModelType) ) return null; diff --git a/StabilityMatrix.Core/Helper/ModelFinder.cs b/StabilityMatrix.Core/Helper/ModelFinder.cs index bb3c1b734..857ea322f 100644 --- a/StabilityMatrix.Core/Helper/ModelFinder.cs +++ b/StabilityMatrix.Core/Helper/ModelFinder.cs @@ -38,7 +38,7 @@ public ModelFinder(ILiteDbContext liteDbContext, ICivitApi civitApi) return new ModelSearchResult(model, version, file); } - // Finds a model using Civit API using file hash + // Finds a model using Civit API using file hash (BLAKE3 or SHA256) public async Task RemoteFindModel(string hashBlake3) { Logger.Info("Searching Civit API for model version using hash {Hash}", hashBlake3); @@ -58,6 +58,7 @@ public ModelFinder(ILiteDbContext liteDbContext, ICivitApi civitApi) var file = versionResponse.Files.FirstOrDefault(file => hashBlake3.Equals(file.Hashes.BLAKE3, StringComparison.OrdinalIgnoreCase) + || hashBlake3.Equals(file.Hashes.SHA256, StringComparison.OrdinalIgnoreCase) ); // Archived models do not have files diff --git a/StabilityMatrix.Core/Models/Database/LocalModelFile.cs b/StabilityMatrix.Core/Models/Database/LocalModelFile.cs index b6d4ed58f..eb0a0ff00 100644 --- a/StabilityMatrix.Core/Models/Database/LocalModelFile.cs +++ b/StabilityMatrix.Core/Models/Database/LocalModelFile.cs @@ -85,6 +85,24 @@ public override int GetHashCode() /// public string? PreviewImageFullPath { get; set; } + /// + /// Optional full path to a cached copy of the cover image embedded in the model file. + /// Only used when there is no preview image file. + /// + public string? EmbeddedPreviewImageFullPath { get; set; } + + /// + /// Optional base model declared in the model file's metadata. + /// Only used when has no base model. + /// + public string? EmbeddedBaseModel { get; set; } + + /// + /// Optional trigger words declared in the model file's metadata. + /// Only used when has no trigger words. + /// + public string[]? EmbeddedTrainedWords { get; set; } + /// /// Optional full path to the model's configuration (.yaml) file. /// @@ -142,12 +160,28 @@ public override int GetHashCode() [BsonIgnore] public string? PreviewImageFullPathGlobal => - PreviewImageFullPath ?? GetPreviewImageFullPath(GlobalConfig.ModelsDir); + PreviewImageFullPath ?? GetDisplayPreviewImageFullPath(GlobalConfig.ModelsDir); [BsonIgnore] public Uri? PreviewImageUriGlobal => PreviewImageFullPathGlobal == null ? null : new Uri(PreviewImageFullPathGlobal); + [BsonIgnore] + public string? DisplayBaseModel => + string.IsNullOrWhiteSpace(ConnectedModelInfo?.BaseModel) + ? EmbeddedBaseModel + : ConnectedModelInfo.BaseModel; + + [BsonIgnore] + public string[]? DisplayTrainedWords => + ConnectedModelInfo?.TrainedWords is { Length: > 0 } trainedWords + ? trainedWords + : EmbeddedTrainedWords; + + [BsonIgnore] + public string DisplayTrainedWordsString => + DisplayTrainedWords is { } trainedWords ? string.Join(", ", trainedWords) : string.Empty; + [BsonIgnore] public string DisplayModelName => ConnectedModelInfo?.ModelName ?? FileNameWithoutExtension; @@ -196,6 +230,15 @@ public string GetFullPath(string rootModelDirectory) : Path.Combine(rootModelDirectory, PreviewImageRelativePath); } + /// + /// Gets the image to show for this model: the preview image file if there is one, + /// otherwise the cover image embedded in the model file. + /// + public string? GetDisplayPreviewImageFullPath(string rootModelDirectory) + { + return GetPreviewImageFullPath(rootModelDirectory) ?? EmbeddedPreviewImageFullPath; + } + public string GetConnectedModelInfoFullPath(string rootModelDirectory) { var modelNameNoExt = Path.GetFileNameWithoutExtension(RelativePath); diff --git a/StabilityMatrix.Core/Models/SafetensorEmbeddedInfo.cs b/StabilityMatrix.Core/Models/SafetensorEmbeddedInfo.cs new file mode 100644 index 000000000..e7df13f29 --- /dev/null +++ b/StabilityMatrix.Core/Models/SafetensorEmbeddedInfo.cs @@ -0,0 +1,269 @@ +using System.Globalization; +using System.Text.Json; +using StabilityMatrix.Core.Extensions; +using StabilityMatrix.Core.Models.Api; + +namespace StabilityMatrix.Core.Models; + +/// +/// Model information embedded in the __metadata__ of a SafeTensor file: the +/// LoRA-Container cover and connected model info, the ModelSpec thumbnail and trigger phrase, +/// and the base model declared by ModelSpec or kohya-ss training metadata. +/// +public record SafetensorEmbeddedInfo +{ + /// + /// Cover image bytes (JPEG or PNG), from cover_1 or modelspec.thumbnail. + /// + public byte[]? Cover { get; init; } + + /// + /// Copy of the model's connected model info, from cm_info. + /// + public ConnectedModelInfo? ConnectedModelInfo { get; init; } + + /// + /// Base model, as a CivitAI base model name (e.g. "SDXL 1.0"). + /// + public string? BaseModel { get; init; } + + /// + /// Trigger words, from modelspec.trigger_phrase. + /// + public string[]? TrainedWords { get; init; } + + /// + /// Lower-case hex SHA-256 of the file before anything was embedded, from model_sha256_original. + /// + public string? OriginalSha256 { get; init; } + + public bool IsEmpty => + Cover is null + && ConnectedModelInfo is null + && BaseModel is null + && TrainedWords is null + && OriginalSha256 is null; + + /// + /// Reads the embedded model information of a SafeTensor file. + /// + /// The embedded information, or if the file does not contain metadata. + public static async Task ReadAsync( + string safetensorPath, + CancellationToken cancellationToken = default + ) + { + await using var stream = new FileStream( + safetensorPath, + FileMode.Open, + FileAccess.Read, + FileShare.Read + ); + var metadataDict = await SafetensorMetadata + .ReadMetadataDictionaryAsync(stream, cancellationToken) + .ConfigureAwait(false); + + return metadataDict is null ? null : FromMetadata(metadataDict); + } + + internal static SafetensorEmbeddedInfo FromMetadata(IReadOnlyDictionary metadataDict) + { + return new SafetensorEmbeddedInfo + { + Cover = GetCover(metadataDict) ?? GetModelSpecThumbnail(metadataDict), + ConnectedModelInfo = GetConnectedModelInfo(metadataDict), + BaseModel = GetBaseModel(metadataDict), + TrainedWords = GetTrainedWords(metadataDict), + OriginalSha256 = GetOriginalSha256(metadataDict), + }; + } + + /// + /// Gets a LoRA-Container cover image. + /// cover_1 is the preview image, cover_2 onwards are gallery images. + /// + /// The __metadata__ entries of a SafeTensor file. + /// 1-based number of the cover to read. + /// The image bytes, or if the cover is missing or invalid. + internal static byte[]? GetCover(IReadOnlyDictionary metadataDict, int coverNumber = 1) + { + if ( + coverNumber < 1 + || !metadataDict.TryGetValue("cover_count", out var coverCountText) + || !int.TryParse( + coverCountText, + NumberStyles.None, + CultureInfo.InvariantCulture, + out var coverCount + ) + || coverNumber > coverCount + || !metadataDict.TryGetValue($"cover_{coverNumber}", out var coverValue) + ) + { + return null; + } + + return DecodeImage(coverValue); + } + + internal static byte[]? GetModelSpecThumbnail(IReadOnlyDictionary metadataDict) + { + return metadataDict.TryGetValue("modelspec.thumbnail", out var thumbnail) + ? DecodeImage(thumbnail) + : null; + } + + /// + /// Whether a metadata key holds an embedded image. + /// + internal static bool IsImageKey(string key) + { + if (key == "modelspec.thumbnail") + { + return true; + } + + return key.StartsWith("cover_", StringComparison.Ordinal) + && key.Length > "cover_".Length + && key.AsSpan("cover_".Length).IndexOfAnyExceptInRange('0', '9') < 0; + } + + /// + /// Decodes a base64 image, with or without a data:image/...;base64, prefix. + /// + internal static byte[]? DecodeImage(string value) + { + var base64 = value.AsSpan().Trim(); + if (base64.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) + { + var separator = base64.IndexOf(','); + if (separator < 0 || !base64[..separator].EndsWith(";base64", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + base64 = base64[(separator + 1)..]; + } + + if (base64.IsEmpty) + { + return null; + } + + var buffer = new byte[base64.Length * 3 / 4]; + if (!Convert.TryFromBase64Chars(base64, buffer, out var bytesWritten) || bytesWritten == 0) + { + return null; + } + + return bytesWritten == buffer.Length ? buffer : buffer[..bytesWritten]; + } + + internal static ConnectedModelInfo? GetConnectedModelInfo( + IReadOnlyDictionary metadataDict + ) + { + if (!metadataDict.TryGetValue("cm_info", out var cmInfoJson) || string.IsNullOrWhiteSpace(cmInfoJson)) + { + return null; + } + + ConnectedModelInfo? cmInfo; + try + { + cmInfo = JsonSerializer.Deserialize( + cmInfoJson, + ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo + ); + } + catch (JsonException) + { + return null; + } + + if (cmInfo is null) + { + return null; + } + + cmInfo.ModelName ??= string.Empty; + cmInfo.ModelDescription ??= string.Empty; + cmInfo.VersionName ??= string.Empty; + cmInfo.Tags ??= []; + cmInfo.Hashes ??= new CivitFileHashes(); + + return cmInfo; + } + + /// + /// Gets the base model from modelspec.architecture, falling back to kohya-ss + /// ss_base_model_version. Only architectures that map to one CivitAI base model are used. + /// + internal static string? GetBaseModel(IReadOnlyDictionary metadataDict) + { + if (metadataDict.TryGetValue("modelspec.architecture", out var architecture)) + { + var slash = architecture.IndexOf('/'); + var baseArchitecture = (slash < 0 ? architecture : architecture[..slash]).Trim(); + + var baseModel = baseArchitecture.ToLowerInvariant() switch + { + "stable-diffusion-v1" => CivitBaseModelType.Sd15, + "stable-diffusion-xl-v1-base" => CivitBaseModelType.Sdxl10, + "stable-diffusion-v3-medium" => CivitBaseModelType.Sd3, + "stable-diffusion-v3.5-medium" => CivitBaseModelType.Sd35Medium, + "stable-diffusion-v3.5-large" => CivitBaseModelType.Sd35Large, + "flux-1-dev" => CivitBaseModelType.Flux1D, + "flux-1-schnell" => CivitBaseModelType.Flux1S, + _ => (CivitBaseModelType?)null, + }; + + if (baseModel is not null) + { + return baseModel.Value.GetStringValue(); + } + } + + if (metadataDict.TryGetValue("ss_base_model_version", out var kohyaBaseModel)) + { + var normalized = kohyaBaseModel.Trim().ToLowerInvariant(); + if (normalized.StartsWith("sd_v1", StringComparison.Ordinal)) + { + return CivitBaseModelType.Sd15.GetStringValue(); + } + + if (normalized.StartsWith("sdxl", StringComparison.Ordinal)) + { + return CivitBaseModelType.Sdxl10.GetStringValue(); + } + } + + return null; + } + + internal static string[]? GetTrainedWords(IReadOnlyDictionary metadataDict) + { + if (!metadataDict.TryGetValue("modelspec.trigger_phrase", out var triggerPhrase)) + { + return null; + } + + var words = triggerPhrase + .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Distinct(StringComparer.Ordinal) + .ToArray(); + + return words.Length > 0 ? words : null; + } + + internal static string? GetOriginalSha256(IReadOnlyDictionary metadataDict) + { + if (!metadataDict.TryGetValue("model_sha256_original", out var sha256)) + { + return null; + } + + sha256 = sha256.Trim(); + return sha256.Length == 64 && sha256.All(char.IsAsciiHexDigit) ? sha256.ToLowerInvariant() : null; + } +} diff --git a/StabilityMatrix.Core/Models/SafetensorMetadata.cs b/StabilityMatrix.Core/Models/SafetensorMetadata.cs index 870f56879..ddeb11098 100644 --- a/StabilityMatrix.Core/Models/SafetensorMetadata.cs +++ b/StabilityMatrix.Core/Models/SafetensorMetadata.cs @@ -30,11 +30,24 @@ public record SafetensorMetadata /// Stream to the SafeTensor file. /// The parsed metadata. Can be if the file does not contain metadata. public static async Task ParseAsync(Stream safetensorStream) + { + var metadataDict = await ReadMetadataDictionaryAsync(safetensorStream).ConfigureAwait(false); + return metadataDict is null ? null : FromDictionary(metadataDict); + } + + /// + /// Reads the raw __metadata__ entries from a SafeTensor file. + /// + /// The metadata entries, or if the file does not contain metadata. + internal static async Task?> ReadMetadataDictionaryAsync( + Stream safetensorStream, + CancellationToken cancellationToken = default + ) { // 8 bytes unsigned little-endian 64-bit integer // 1 byte start of JSON object '{' Memory buffer = new byte[9]; - await safetensorStream.ReadExactlyAsync(buffer).ConfigureAwait(false); + await safetensorStream.ReadExactlyAsync(buffer, cancellationToken).ConfigureAwait(false); var span = buffer.Span; const ulong MAX_ALLOWED_JSON_LENGTH = 100 * 1024 * 1024; // 100 MB @@ -60,7 +73,7 @@ public record SafetensorMetadata // we already read {, so start from index 1 jsonBytes[0] = (byte)'{'; await safetensorStream - .ReadExactlyAsync(jsonBytes, 1, (int)(jsonLength - 1)) + .ReadExactlyAsync(jsonBytes.AsMemory(1, (int)(jsonLength - 1)), cancellationToken) .ConfigureAwait(false); // read the JSON with Utf8JsonReader, then only deserialize what we need @@ -97,7 +110,7 @@ await safetensorStream { if (JsonSerializer.Deserialize>(ref reader) is { } dict) { - return FromDictionary(dict); + return dict; } // got null from Deserialize @@ -141,6 +154,14 @@ internal static SafetensorMetadata FromDictionary(Dictionary met continue; } + if (SafetensorEmbeddedInfo.IsImageKey(kv.Key)) + { + otherMetadata.Add( + new Metadata(kv.Key, $"(embedded image, {kv.Value.Length * 3 / 4 / 1024:N0} KB)") + ); + continue; + } + otherMetadata.Add(new Metadata(kv.Key, kv.Value)); } otherMetadata.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal)); diff --git a/StabilityMatrix.Core/Services/MetadataImportService.cs b/StabilityMatrix.Core/Services/MetadataImportService.cs index cae475554..fd5354309 100644 --- a/StabilityMatrix.Core/Services/MetadataImportService.cs +++ b/StabilityMatrix.Core/Services/MetadataImportService.cs @@ -87,7 +87,7 @@ public async Task ScanDirectoryForMissingInfo( continue; } - var modelInfo = await modelFinder.RemoteFindModel(blake3).ConfigureAwait(false); + var modelInfo = await RemoteFindModel(checkpointFilePath, blake3).ConfigureAwait(false); if (modelInfo == null) { logger.LogWarning($"Could not find model for {blake3}"); @@ -244,7 +244,7 @@ await updatedCmInfo return null; } - var modelInfo = await modelFinder.RemoteFindModel(blake3).ConfigureAwait(false); + var modelInfo = await RemoteFindModel(filePath, blake3).ConfigureAwait(false); if (modelInfo == null) { logger.LogWarning($"Could not find model for {blake3}"); @@ -265,6 +265,46 @@ await updatedCmInfo return updatedCmInfo; } + /// + /// Finds a model on CivitAI by its file hash. For safetensors files whose header was edited + /// after release (e.g. to embed a cover image), falls back to the original file hash + /// recorded in the header. + /// + private async Task RemoteFindModel(FilePath filePath, string blake3) + { + var modelInfo = await modelFinder.RemoteFindModel(blake3).ConfigureAwait(false); + if (modelInfo != null || filePath.Extension != ".safetensors") + { + return modelInfo; + } + + string? originalSha256; + try + { + originalSha256 = ( + await SafetensorEmbeddedInfo.ReadAsync(filePath).ConfigureAwait(false) + )?.OriginalSha256; + } + catch (Exception e) + when (e is IOException or UnauthorizedAccessException or InvalidDataException or JsonException) + { + logger.LogDebug(e, "Failed to read safetensors metadata from {Path}", filePath); + return null; + } + + if (originalSha256 is null) + { + return null; + } + + logger.LogInformation( + "Searching for {Path} using its original hash {Hash} from the file metadata", + filePath, + originalSha256 + ); + return await modelFinder.RemoteFindModel(originalSha256).ConfigureAwait(false); + } + private async Task BuildConnectedModelInfoAsync( FilePath modelFilePath, ConnectedModelInfo? existingCmInfo, diff --git a/StabilityMatrix.Core/Services/ModelIndexService.cs b/StabilityMatrix.Core/Services/ModelIndexService.cs index bd2cda9e0..4a881e78f 100644 --- a/StabilityMatrix.Core/Services/ModelIndexService.cs +++ b/StabilityMatrix.Core/Services/ModelIndexService.cs @@ -2,6 +2,7 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Globalization; +using System.Security.Cryptography; using System.Text; using AsyncAwaitBestPractices; using AutoCtor; @@ -17,6 +18,7 @@ using StabilityMatrix.Core.Models.Api; using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Models.FileInterfaces; +using JsonException = System.Text.Json.JsonException; using JsonSerializer = System.Text.Json.JsonSerializer; namespace StabilityMatrix.Core.Services; @@ -32,6 +34,8 @@ public partial class ModelIndexService : IModelIndexService private DateTimeOffset lastUpdateCheck = DateTimeOffset.MinValue; + private readonly ConcurrentDictionary embeddedInfoCache = new(); + private Dictionary> _modelIndex = new(); private HashSet? _modelIndexBlake3Hashes; @@ -522,20 +526,9 @@ private async Task RefreshIndexParallelCore() ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo ); - // Seems there is a limitation of LiteDB datetime resolution, so drop nanoseconds on load - // Otherwise new loaded models with ns will cause mismatching equality with models loaded from db with no ns - if (connectedModelInfo?.ImportedAt is { } importedAt && importedAt.Nanosecond != 0) + if (connectedModelInfo is not null) { - connectedModelInfo.ImportedAt = new DateTimeOffset( - importedAt.Year, - importedAt.Month, - importedAt.Day, - importedAt.Hour, - importedAt.Minute, - importedAt.Second, - importedAt.Millisecond, - importedAt.Offset - ); + TruncateImportedAt(connectedModelInfo); } localModel.ConnectedModelInfo = connectedModelInfo; @@ -576,6 +569,8 @@ private async Task RefreshIndexParallelCore() var newIndexComplete = newIndexFlat.ToArray(); + await LoadEmbeddedInfoAsync(newIndexComplete, modelsDir).ConfigureAwait(false); + var modelsDict = ModelIndex .Values.SelectMany(x => x) .DistinctBy(f => f.RelativePath) @@ -664,6 +659,182 @@ public void BackgroundRefreshIndex() }); } + private static ReadOnlySpan PngSignature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; + + private static DirectoryPath EmbeddedCoversCacheDirectory => + new(Path.GetTempPath(), "StabilityMatrix", "Cache", "EmbeddedCovers"); + + /// + /// Fills in what safetensors models without a preview image or connected model info file + /// carry in their header: the cover image, connected model info, base model and trigger words. + /// + private async Task LoadEmbeddedInfoAsync(IEnumerable models, DirectoryPath modelsDir) + { + var candidates = models + .Where(model => + model.IsSafetensorFile + && (model.PreviewImageRelativePath is null || model.ConnectedModelInfo is null) + ) + .ToList(); + + if (candidates.Count == 0) + { + return; + } + + await Parallel + .ForEachAsync( + candidates, + async (model, cancellationToken) => + { + var embedded = await GetEmbeddedInfoAsync(model.GetFullPath(modelsDir), cancellationToken) + .ConfigureAwait(false); + + if (embedded is null) + { + return; + } + + if (model.PreviewImageRelativePath is null) + { + model.EmbeddedPreviewImageFullPath = embedded.CoverPath; + } + + if (model.ConnectedModelInfo is null && embedded.ConnectedModelInfo is not null) + { + model.ConnectedModelInfo = JsonSerializer.Deserialize( + embedded.ConnectedModelInfo, + ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo + ); + } + + model.EmbeddedBaseModel = embedded.BaseModel; + model.EmbeddedTrainedWords = embedded.TrainedWords; + } + ) + .ConfigureAwait(false); + } + + private async Task GetEmbeddedInfoAsync( + string modelPath, + CancellationToken cancellationToken + ) + { + var fileInfo = new FileInfo(modelPath); + if (!fileInfo.Exists) + { + return null; + } + + if ( + embeddedInfoCache.TryGetValue(modelPath, out var cached) + && cached.Length == fileInfo.Length + && cached.LastWriteTimeUtc == fileInfo.LastWriteTimeUtc + && (cached.CoverPath is null || File.Exists(cached.CoverPath)) + ) + { + return cached; + } + + var entry = new EmbeddedInfoCacheEntry(fileInfo.Length, fileInfo.LastWriteTimeUtc); + try + { + var embedded = await SafetensorEmbeddedInfo + .ReadAsync(modelPath, cancellationToken) + .ConfigureAwait(false); + + if (embedded is not null) + { + if (embedded.ConnectedModelInfo is { } cmInfo) + { + TruncateImportedAt(cmInfo); + } + + entry = entry with + { + CoverPath = embedded.Cover is { } cover + ? await SaveEmbeddedCoverAsync(cover, cancellationToken).ConfigureAwait(false) + : null, + ConnectedModelInfo = embedded.ConnectedModelInfo is { } info + ? JsonSerializer.Serialize( + info, + ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo + ) + : null, + BaseModel = embedded.BaseModel, + TrainedWords = embedded.TrainedWords, + }; + } + } + catch (Exception e) + when (e is IOException or UnauthorizedAccessException or InvalidDataException or JsonException) + { + logger.LogDebug(e, "Failed to read embedded model info from {Path}", modelPath); + } + + embeddedInfoCache[modelPath] = entry; + + return entry; + } + + private static async Task SaveEmbeddedCoverAsync( + byte[] cover, + CancellationToken cancellationToken + ) + { + var extension = cover.AsSpan().StartsWith(PngSignature) ? ".png" : ".jpeg"; + + var coversDir = EmbeddedCoversCacheDirectory; + var coverFile = coversDir.JoinFile($"{Convert.ToHexStringLower(SHA256.HashData(cover))}{extension}"); + + if (!coverFile.Exists) + { + coversDir.Create(); + + var tempFile = coversDir.JoinFile($"{coverFile.Name}.{Guid.NewGuid():N}.tmp"); + await File.WriteAllBytesAsync(tempFile, cover, cancellationToken).ConfigureAwait(false); + File.Move(tempFile, coverFile, overwrite: true); + } + + return coverFile.FullPath; + } + + /// + /// Drops nanoseconds from . LiteDB stores datetimes + /// at a lower resolution, so models loaded from the database would otherwise not equal newly + /// indexed ones. + /// + private static void TruncateImportedAt(ConnectedModelInfo connectedModelInfo) + { + var importedAt = connectedModelInfo.ImportedAt; + if (importedAt.Nanosecond != 0) + { + connectedModelInfo.ImportedAt = new DateTimeOffset( + importedAt.Year, + importedAt.Month, + importedAt.Day, + importedAt.Hour, + importedAt.Minute, + importedAt.Second, + importedAt.Millisecond, + importedAt.Offset + ); + } + } + + private sealed record EmbeddedInfoCacheEntry(long Length, DateTime LastWriteTimeUtc) + { + public string? CoverPath { get; init; } + + /// + /// Embedded connected model info as JSON, so that every index refresh gets its own copy. + /// + public string? ConnectedModelInfo { get; init; } + + public string? BaseModel { get; init; } + public string[]? TrainedWords { get; init; } + } + /// public async Task RemoveModelAsync(LocalModelFile model) { diff --git a/StabilityMatrix.Tests/Core/MetadataImportServiceTests.cs b/StabilityMatrix.Tests/Core/MetadataImportServiceTests.cs index bd3fe4737..6c908cc8f 100644 --- a/StabilityMatrix.Tests/Core/MetadataImportServiceTests.cs +++ b/StabilityMatrix.Tests/Core/MetadataImportServiceTests.cs @@ -129,6 +129,50 @@ await File.ReadAllTextAsync(Path.Combine(tempRoot, "model.cm-info.json")) } } + [TestMethod] + public async Task GetMetadataForFile_FallsBackToOriginalHashFromSafetensorsHeader() + { + const string originalSha256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + var tempRoot = CreateTempDirectory(); + + try + { + var modelPath = Path.Combine(tempRoot, "model.safetensors"); + var header = System.Text.Encoding.UTF8.GetBytes( + $$$"""{"__metadata__":{"model_sha256_original":"{{{originalSha256}}}"}}""" + ); + await using (var stream = File.Create(modelPath)) + { + var length = new byte[8]; + System.Buffers.Binary.BinaryPrimitives.WriteUInt64LittleEndian(length, (ulong)header.Length); + stream.Write(length); + stream.Write(header); + } + + var blake3 = await FileHash.GetBlake3Async(new FilePath(modelPath)); + var service = CreateMetadataImportService(); + service + .Api.GetModelVersionByHash(blake3) + .Returns(_ => throw new TaskCanceledException()); + ConfigureCivitLookup( + service.Api, + "remote-blake3", + sha256: originalSha256, + lookupHash: originalSha256 + ); + + var result = await service.Instance.GetMetadataForFile(new FilePath(modelPath)); + + Assert.IsNotNull(result); + Assert.AreEqual(123, result.ModelId); + Assert.IsTrue(File.Exists(Path.Combine(tempRoot, "model.cm-info.json"))); + } + finally + { + Directory.Delete(tempRoot, true); + } + } + private static string CreateTempDirectory() { var path = Path.Combine(Path.GetTempPath(), $"sm-metadata-tests-{Guid.NewGuid():N}"); @@ -136,7 +180,13 @@ private static string CreateTempDirectory() return path; } - private static void ConfigureCivitLookup(ICivitApi api, string hash, bool includeImage = false) + private static void ConfigureCivitLookup( + ICivitApi api, + string hash, + bool includeImage = false, + string sha256 = "sha256", + string? lookupHash = null + ) { var file = new CivitFile { @@ -145,7 +195,7 @@ private static void ConfigureCivitLookup(ICivitApi api, string hash, bool includ DownloadUrl = "https://example.invalid/model", Type = CivitFileType.Model, Metadata = new CivitFileMetadata { Size = "pruned" }, - Hashes = new CivitFileHashes { BLAKE3 = hash, SHA256 = "sha256" }, + Hashes = new CivitFileHashes { BLAKE3 = hash, SHA256 = sha256 }, }; var version = new CivitModelVersion { @@ -169,7 +219,7 @@ private static void ConfigureCivitLookup(ICivitApi api, string hash, bool includ Stats = new CivitModelStats(), }; - api.GetModelVersionByHash(hash) + api.GetModelVersionByHash(lookupHash ?? hash) .Returns( Task.FromResult( new CivitModelVersionResponse( diff --git a/StabilityMatrix.Tests/Models/LocalModelFileTests.cs b/StabilityMatrix.Tests/Models/LocalModelFileTests.cs index 4831989b2..ae30fdcee 100644 --- a/StabilityMatrix.Tests/Models/LocalModelFileTests.cs +++ b/StabilityMatrix.Tests/Models/LocalModelFileTests.cs @@ -37,6 +37,64 @@ public void RelativePathConnectedModelInfoComparer_TreatsEarlyAccessFlagAsDistin Assert.AreEqual(2, set.Count); } + [TestMethod] + public void GetDisplayPreviewImageFullPath_PrefersPreviewImageFile() + { + var rootModelDirectory = Path.Combine(Path.GetTempPath(), "Models"); + var model = CreateLocalModelFile(hasEarlyAccessUpdateOnly: false) with + { + PreviewImageRelativePath = Path.Combine("StableDiffusion", "model-a.preview.jpeg"), + EmbeddedPreviewImageFullPath = Path.Combine(Path.GetTempPath(), "cover.jpeg"), + }; + + Assert.AreEqual( + Path.Combine(rootModelDirectory, "StableDiffusion", "model-a.preview.jpeg"), + model.GetDisplayPreviewImageFullPath(rootModelDirectory) + ); + } + + [TestMethod] + public void GetDisplayPreviewImageFullPath_FallsBackToEmbeddedCover() + { + var embeddedCoverPath = Path.Combine(Path.GetTempPath(), "cover.jpeg"); + var model = CreateLocalModelFile(hasEarlyAccessUpdateOnly: false) with + { + EmbeddedPreviewImageFullPath = embeddedCoverPath, + }; + + Assert.IsNull(model.GetPreviewImageFullPath(Path.GetTempPath())); + Assert.AreEqual(embeddedCoverPath, model.GetDisplayPreviewImageFullPath(Path.GetTempPath())); + } + + [TestMethod] + public void DisplayBaseModelAndTrainedWords_PreferConnectedModelInfo() + { + var model = CreateLocalModelFile(hasEarlyAccessUpdateOnly: false) with + { + EmbeddedBaseModel = "SDXL 1.0", + EmbeddedTrainedWords = ["embedded"], + }; + model.ConnectedModelInfo!.BaseModel = "Illustrious"; + model.ConnectedModelInfo.TrainedWords = ["connected"]; + + Assert.AreEqual("Illustrious", model.DisplayBaseModel); + Assert.AreEqual("connected", model.DisplayTrainedWordsString); + } + + [TestMethod] + public void DisplayBaseModelAndTrainedWords_FallBackToEmbeddedMetadata() + { + var model = CreateLocalModelFile(hasEarlyAccessUpdateOnly: false) with + { + ConnectedModelInfo = null, + EmbeddedBaseModel = "SDXL 1.0", + EmbeddedTrainedWords = ["mychar", "red jacket"], + }; + + Assert.AreEqual("SDXL 1.0", model.DisplayBaseModel); + Assert.AreEqual("mychar, red jacket", model.DisplayTrainedWordsString); + } + private static LocalModelFile CreateLocalModelFile(bool hasEarlyAccessUpdateOnly) { return new LocalModelFile diff --git a/StabilityMatrix.Tests/Models/SafetensorEmbeddedInfoTests.cs b/StabilityMatrix.Tests/Models/SafetensorEmbeddedInfoTests.cs new file mode 100644 index 000000000..f5b27e822 --- /dev/null +++ b/StabilityMatrix.Tests/Models/SafetensorEmbeddedInfoTests.cs @@ -0,0 +1,242 @@ +using System.Buffers.Binary; +using System.Text.Json; +using StabilityMatrix.Core.Models; + +namespace StabilityMatrix.Tests.Models; + +[TestClass] +public class SafetensorEmbeddedInfoTests +{ + private static readonly byte[] CoverJpegBytes = [0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0xFF, 0xD9]; + private static readonly byte[] GalleryJpegBytes = [0xFF, 0xD8, 0x01, 0x02, 0x03, 0xFF, 0xD9]; + private const string OriginalSha256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + + [TestMethod] + public async Task ReadAsync_ReadsLoraContainerMetadata() + { + var cmInfo = """ + {"ModelId":123,"ModelName":"My LoRA","BaseModel":"Illustrious","TrainedWords":["mychar"], + "Hashes":{"SHA256":"ABC"},"ThumbnailImageUrl":null} + """; + var path = WriteSafetensorFile( + new Dictionary + { + ["ss_base_model_version"] = "sdxl_base_v1-0", + ["cover_format"] = "1", + ["cover_count"] = "2", + ["cover_1"] = Convert.ToBase64String(CoverJpegBytes), + ["cover_2"] = Convert.ToBase64String(GalleryJpegBytes), + ["model_sha256_original"] = OriginalSha256.ToUpperInvariant(), + ["cm_info"] = cmInfo, + } + ); + + try + { + var info = await SafetensorEmbeddedInfo.ReadAsync(path); + + Assert.IsNotNull(info); + CollectionAssert.AreEqual(CoverJpegBytes, info.Cover); + Assert.AreEqual(OriginalSha256, info.OriginalSha256); + Assert.AreEqual("SDXL 1.0", info.BaseModel); + Assert.IsNotNull(info.ConnectedModelInfo); + Assert.AreEqual(123, info.ConnectedModelInfo.ModelId); + Assert.AreEqual("My LoRA", info.ConnectedModelInfo.ModelName); + Assert.AreEqual("Illustrious", info.ConnectedModelInfo.BaseModel); + CollectionAssert.AreEqual(new[] { "mychar" }, info.ConnectedModelInfo.TrainedWords); + Assert.AreEqual(string.Empty, info.ConnectedModelInfo.VersionName); + Assert.AreEqual(0, info.ConnectedModelInfo.Tags.Length); + } + finally + { + File.Delete(path); + } + } + + [TestMethod] + public async Task ReadAsync_ReturnsNull_WhenFileHasNoMetadata() + { + var path = WriteSafetensorFile(null); + + try + { + Assert.IsNull(await SafetensorEmbeddedInfo.ReadAsync(path)); + } + finally + { + File.Delete(path); + } + } + + [TestMethod] + public void FromMetadata_IsEmpty_WhenNothingUsefulIsEmbedded() + { + var info = SafetensorEmbeddedInfo.FromMetadata( + new Dictionary { ["ss_network_module"] = "networks.lora" } + ); + + Assert.IsTrue(info.IsEmpty); + } + + [TestMethod] + public void GetCover_ReturnsGalleryCover() + { + var metadata = new Dictionary + { + ["cover_count"] = "2", + ["cover_1"] = Convert.ToBase64String(CoverJpegBytes), + ["cover_2"] = Convert.ToBase64String(GalleryJpegBytes), + }; + + CollectionAssert.AreEqual(GalleryJpegBytes, SafetensorEmbeddedInfo.GetCover(metadata, 2)); + Assert.IsNull(SafetensorEmbeddedInfo.GetCover(metadata, 3)); + } + + [TestMethod] + public void GetCover_AcceptsDataUriPrefix() + { + var metadata = new Dictionary + { + ["cover_count"] = "1", + ["cover_1"] = "data:image/jpeg;base64," + Convert.ToBase64String(CoverJpegBytes), + }; + + CollectionAssert.AreEqual(CoverJpegBytes, SafetensorEmbeddedInfo.GetCover(metadata)); + } + + [TestMethod] + public void GetCover_ReturnsNull_WhenCoverCountIsMissing() + { + var metadata = new Dictionary + { + ["cover_1"] = Convert.ToBase64String(CoverJpegBytes), + }; + + Assert.IsNull(SafetensorEmbeddedInfo.GetCover(metadata)); + } + + [TestMethod] + public void GetCover_ReturnsNull_WhenCoverIsNotBase64() + { + var metadata = new Dictionary { ["cover_count"] = "1", ["cover_1"] = "not base64!" }; + + Assert.IsNull(SafetensorEmbeddedInfo.GetCover(metadata)); + } + + [TestMethod] + public void FromMetadata_FallsBackToModelSpecThumbnail() + { + var info = SafetensorEmbeddedInfo.FromMetadata( + new Dictionary + { + ["modelspec.thumbnail"] = "data:image/png;base64," + Convert.ToBase64String(GalleryJpegBytes), + } + ); + + CollectionAssert.AreEqual(GalleryJpegBytes, info.Cover); + } + + [TestMethod] + public void FromMetadata_PrefersLoraContainerCoverOverModelSpecThumbnail() + { + var info = SafetensorEmbeddedInfo.FromMetadata( + new Dictionary + { + ["modelspec.thumbnail"] = "data:image/png;base64," + Convert.ToBase64String(GalleryJpegBytes), + ["cover_count"] = "1", + ["cover_1"] = Convert.ToBase64String(CoverJpegBytes), + } + ); + + CollectionAssert.AreEqual(CoverJpegBytes, info.Cover); + } + + [TestMethod] + [DataRow("stable-diffusion-v1/lora", null, "SD 1.5")] + [DataRow("stable-diffusion-xl-v1-base/lora", null, "SDXL 1.0")] + [DataRow("flux-1-dev/lora", null, "Flux.1 D")] + [DataRow("flux-1-schnell", null, "Flux.1 S")] + [DataRow("stable-diffusion-v3.5-large/lora", null, "SD 3.5 Large")] + [DataRow("flux-1-dev/lora", "sdxl_base_v1-0", "Flux.1 D")] + [DataRow(null, "sd_v1", "SD 1.5")] + [DataRow(null, "sdxl_base_v1-0", "SDXL 1.0")] + [DataRow("some-unknown-architecture/lora", "sdxl_base_v1-0", "SDXL 1.0")] + [DataRow(null, "flux1", null)] + [DataRow(null, "sd_v2", null)] + public void GetBaseModel_MapsArchitectureToCivitBaseModel( + string? architecture, + string? kohyaBaseModel, + string? expected + ) + { + var metadata = new Dictionary(); + if (architecture is not null) + metadata["modelspec.architecture"] = architecture; + if (kohyaBaseModel is not null) + metadata["ss_base_model_version"] = kohyaBaseModel; + + Assert.AreEqual(expected, SafetensorEmbeddedInfo.GetBaseModel(metadata)); + } + + [TestMethod] + public void GetTrainedWords_SplitsTriggerPhrase() + { + var metadata = new Dictionary + { + ["modelspec.trigger_phrase"] = "mychar, red jacket,, mychar ", + }; + + CollectionAssert.AreEqual( + new[] { "mychar", "red jacket" }, + SafetensorEmbeddedInfo.GetTrainedWords(metadata) + ); + } + + [TestMethod] + [DataRow("not-a-hash")] + [DataRow("0123456789abcdef")] + [DataRow("zz23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")] + public void GetOriginalSha256_IgnoresInvalidHashes(string value) + { + var metadata = new Dictionary { ["model_sha256_original"] = value }; + + Assert.IsNull(SafetensorEmbeddedInfo.GetOriginalSha256(metadata)); + } + + [TestMethod] + public void GetConnectedModelInfo_ReturnsNull_WhenJsonIsInvalid() + { + var metadata = new Dictionary { ["cm_info"] = "{not json" }; + + Assert.IsNull(SafetensorEmbeddedInfo.GetConnectedModelInfo(metadata)); + } + + private static string WriteSafetensorFile(Dictionary? metadata) + { + var header = new Dictionary + { + ["weight"] = new + { + dtype = "F32", + shape = new[] { 1 }, + data_offsets = new[] { 0, 4 }, + }, + }; + if (metadata is not null) + { + header["__metadata__"] = metadata; + } + + var headerBytes = JsonSerializer.SerializeToUtf8Bytes(header); + var path = Path.GetTempFileName(); + + using var stream = File.Create(path); + Span lengthBuffer = stackalloc byte[8]; + BinaryPrimitives.WriteUInt64LittleEndian(lengthBuffer, (ulong)headerBytes.Length); + stream.Write(lengthBuffer); + stream.Write(headerBytes); + stream.Write(new byte[4]); + + return path; + } +} diff --git a/StabilityMatrix.Tests/Models/SafetensorMetadataTests.cs b/StabilityMatrix.Tests/Models/SafetensorMetadataTests.cs index 780ff015e..943351997 100644 --- a/StabilityMatrix.Tests/Models/SafetensorMetadataTests.cs +++ b/StabilityMatrix.Tests/Models/SafetensorMetadataTests.cs @@ -38,4 +38,24 @@ public async Task TestParseStreamAsync() metadata.TagFrequency ); } + + [TestMethod] + public void FromDictionary_ShowsEmbeddedImagesAsPlaceholders() + { + var metadata = SafetensorMetadata.FromDictionary( + new Dictionary + { + ["cover_count"] = "1", + ["cover_1"] = new string('A', 4096), + ["modelspec.thumbnail"] = "data:image/jpeg;base64," + new string('A', 4096), + ["cover_format"] = "1", + } + ); + + var values = metadata.OtherMetadata.ToDictionary(x => x.Name, x => x.Value); + Assert.AreEqual("1", values["cover_count"]); + Assert.AreEqual("1", values["cover_format"]); + StringAssert.StartsWith(values["cover_1"], "(embedded image"); + StringAssert.StartsWith(values["modelspec.thumbnail"], "(embedded image"); + } }