Merge branch 'phase/60-portrait-models'
ci / server (push) Failing after 3m46s
ci / client (push) Successful in 21s

# Conflicts:
#	docs/phases/off-queue/README.md
This commit is contained in:
Leonid Pershin
2026-08-20 14:46:20 +03:00
20 changed files with 1142 additions and 180 deletions
+1 -1
View File
@@ -721,7 +721,7 @@ internal sealed class GameLoopService(
save.DressRules,
save.SpeechRules,
save.Owner,
save.PortraitSettings);
save.PortraitSettings is null ? null : SwarmUiConfigFile.Clone(save.PortraitSettings));
worker.Start();
try
@@ -11,32 +11,23 @@ internal static class PortraitPromptBuilder
PortraitKind kind,
string? promptExtra = null)
{
var kindPreset = profile.KindPreset;
var parts = new List<string>();
if (!string.IsNullOrWhiteSpace(profile.Positive))
{
parts.Add(profile.Positive.Trim());
}
Add(parts, profile.ModelPositive);
Add(parts, profile.Style);
Add(parts, profile.ShotType);
if (kind == PortraitKind.Custom)
{
if (!string.IsNullOrWhiteSpace(promptExtra))
{
parts.Add(promptExtra.Trim());
}
}
else if (!string.IsNullOrWhiteSpace(kindPreset.Positive))
{
parts.Add(kindPreset.Positive.Trim());
Add(parts, promptExtra);
}
parts.Add(DescribeSubject(card));
Add(parts, profile.Pose);
Add(parts, DescribeSubject(card));
parts.Add($"age {card.Age}");
foreach (var row in card.Body)
{
parts.Add($"{row.Label.ToLowerInvariant()} {row.Value.ToLowerInvariant()}");
Add(parts, $"{row.Label.ToLowerInvariant()} {row.Value.ToLowerInvariant()}");
}
foreach (var item in PortraitVisibleWorn.Filter(card.Worn))
@@ -44,15 +35,15 @@ internal static class PortraitPromptBuilder
var color = item.ColorLabel ?? item.Color;
if (!string.IsNullOrWhiteSpace(color))
{
parts.Add($"wearing {item.Label.ToLowerInvariant()} in {color.ToLowerInvariant()}");
Add(parts, $"wearing {item.Label.ToLowerInvariant()} in {color.ToLowerInvariant()}");
}
else
{
parts.Add($"wearing {item.Label.ToLowerInvariant()}");
Add(parts, $"wearing {item.Label.ToLowerInvariant()}");
}
}
var positive = string.Join(", ", parts.Where(part => part.Length > 0));
var positive = string.Join(", ", parts);
var negative = SwarmUiLoraFormatter.AppendLoraTags(
profile.Negative.Trim(),
profile.NegativeLoras);
@@ -60,6 +51,16 @@ internal static class PortraitPromptBuilder
return (positive, negative);
}
private static void Add(List<string> parts, string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}
parts.Add(value.Trim());
}
private static string DescribeSubject(PersonCardResponse card)
{
if (card.Age <= 11)
+387 -60
View File
@@ -90,6 +90,8 @@ internal sealed class SwarmUiConfigFile
{
public string ActivePresetId { get; set; } = "default";
public List<SwarmUiModelDefinition> Models { get; set; } = [];
public List<SwarmUiPresetDefinition> Presets { get; set; } = [];
public List<SwarmUiAgeRule> AgeRules { get; set; } = [];
@@ -135,19 +137,44 @@ internal sealed class SwarmUiConfigFile
ActivePresetId = "default";
}
if (Presets.Count == 0)
{
Presets = [SwarmUiPresetDefinition.CreateDefault()];
ActivePresetId = "default";
}
LiftModelsFromPresets();
foreach (var preset in Presets)
{
preset.Avatar ??= new SwarmUiKindPreset();
preset.Custom ??= new SwarmUiKindPreset();
preset.FullBody ??= new SwarmUiKindPreset();
preset.PositiveLoras ??= [];
preset.NegativeLoras ??= [];
preset.LiftLegacyPromptFields();
}
foreach (var model in Models)
{
model.PositiveLoras ??= [];
model.NegativeLoras ??= [];
if (string.IsNullOrWhiteSpace(model.Label))
{
model.Label = LabelFromId(model.Id);
}
}
if (Models.Count == 0)
{
Models = SwarmUiModelDefinition.Catalog();
}
foreach (var preset in Presets)
{
if (string.IsNullOrWhiteSpace(preset.Model))
{
preset.Model = Models[0].Id;
}
ClearMatchingOverrides(preset);
}
if (Presets.Count == 0)
{
return;
}
if (string.IsNullOrWhiteSpace(ActivePresetId) || FindPreset(ActivePresetId) is null)
@@ -158,11 +185,32 @@ internal sealed class SwarmUiConfigFile
public void Validate()
{
if (Models.Count == 0)
{
throw new InvalidOperationException("At least one model is required.");
}
if (Presets.Count == 0)
{
throw new InvalidOperationException("At least one preset is required.");
}
var modelIds = new HashSet<string>(StringComparer.Ordinal);
foreach (var model in Models)
{
if (string.IsNullOrWhiteSpace(model.Id))
{
throw new InvalidOperationException("Every model needs a non-empty id.");
}
if (!modelIds.Add(model.Id))
{
throw new InvalidOperationException($"Duplicate model id '{model.Id}'.");
}
model.Validate();
}
var ids = new HashSet<string>(StringComparer.Ordinal);
foreach (var preset in Presets)
{
@@ -181,6 +229,11 @@ internal sealed class SwarmUiConfigFile
preset.Label = preset.Id;
}
if (FindModel(preset.Model) is null)
{
throw new InvalidOperationException($"Preset '{preset.Id}' references unknown model '{preset.Model}'.");
}
preset.Validate();
}
@@ -207,9 +260,110 @@ internal sealed class SwarmUiConfigFile
{
var presetId = ResolvePresetId(age);
var preset = FindPreset(presetId) ?? FindPreset(ActivePresetId) ?? Presets[0];
return preset.ToProfile(kind);
var model = FindModel(preset.Model) ?? Models[0];
return preset.ToProfile(kind, model);
}
public IReadOnlyList<string> AllowedModelIds(SwarmUiDiscovery discovery)
{
var catalog = Models
.Select(model => model.Id)
.Where(id => !string.IsNullOrWhiteSpace(id))
.ToList();
if (!discovery.Connected || discovery.Models.Count == 0)
{
return catalog;
}
return discovery.Models
.Where(name => catalog.Contains(name, StringComparer.Ordinal))
.ToList();
}
private void LiftModelsFromPresets()
{
if (Models.Count > 0)
{
return;
}
foreach (var preset in Presets)
{
if (string.IsNullOrWhiteSpace(preset.Model) || FindModel(preset.Model) is not null)
{
continue;
}
Models.Add(new SwarmUiModelDefinition
{
Id = preset.Model,
Label = LabelFromId(preset.Model),
Steps = preset.Steps ?? 8,
CfgScale = preset.CfgScale ?? 1,
ClipSkip = preset.ClipSkip ?? 0,
Sampler = preset.Sampler ?? "",
Scheduler = preset.Scheduler ?? "",
Seed = preset.Seed ?? -1,
Positive = "",
Negative = "",
PositiveLoras = CopyLoras(preset.PositiveLoras),
NegativeLoras = CopyLoras(preset.NegativeLoras),
});
}
}
private void ClearMatchingOverrides(SwarmUiPresetDefinition preset)
{
var model = FindModel(preset.Model);
if (model is null)
{
return;
}
if (preset.Steps == model.Steps)
{
preset.Steps = null;
}
if (preset.CfgScale == model.CfgScale)
{
preset.CfgScale = null;
}
if (preset.ClipSkip == model.ClipSkip)
{
preset.ClipSkip = null;
}
if (string.Equals(preset.Sampler, model.Sampler, StringComparison.Ordinal))
{
preset.Sampler = null;
}
if (string.Equals(preset.Scheduler, model.Scheduler, StringComparison.Ordinal))
{
preset.Scheduler = null;
}
if (preset.Seed == model.Seed)
{
preset.Seed = null;
}
if (SameLoras(preset.PositiveLoras, model.PositiveLoras))
{
preset.PositiveLoras = null;
}
if (SameLoras(preset.NegativeLoras, model.NegativeLoras))
{
preset.NegativeLoras = null;
}
}
public SwarmUiModelDefinition? FindModel(string id) =>
Models.FirstOrDefault(model => string.Equals(model.Id, id, StringComparison.Ordinal));
private string ResolvePresetId(int age)
{
foreach (var rule in AgeRules.OrderBy(rule => rule.MinAge))
@@ -230,6 +384,7 @@ internal sealed class SwarmUiConfigFile
new()
{
ActivePresetId = "default",
Models = SwarmUiModelDefinition.Catalog(),
Presets = [SwarmUiPresetDefinition.CreateDefault(), SwarmUiPresetDefinition.CreateChild()],
AgeRules = [new SwarmUiAgeRule { MinAge = 6, MaxAge = 11, PresetId = "child" }],
};
@@ -254,6 +409,42 @@ internal sealed class SwarmUiConfigFile
return copy;
}
internal static string LabelFromId(string id)
{
var name = Path.GetFileNameWithoutExtension(id);
return string.IsNullOrWhiteSpace(name) ? id : name;
}
internal static List<SwarmUiLoraEntry> CopyLoras(IReadOnlyList<SwarmUiLoraEntry>? source)
{
if (source is null || source.Count == 0)
{
return [];
}
return source.Select(lora => new SwarmUiLoraEntry { Name = lora.Name, Weight = lora.Weight }).ToList();
}
internal static bool SameLoras(IReadOnlyList<SwarmUiLoraEntry>? left, IReadOnlyList<SwarmUiLoraEntry>? right)
{
var a = left ?? [];
var b = right ?? [];
if (a.Count != b.Count)
{
return false;
}
for (var i = 0; i < a.Count; i++)
{
if (!string.Equals(a[i].Name, b[i].Name, StringComparison.Ordinal) || a[i].Weight != b[i].Weight)
{
return false;
}
}
return true;
}
private static readonly JsonSerializerOptions CloneJson = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
@@ -262,19 +453,22 @@ internal sealed class SwarmUiConfigFile
};
}
internal sealed class SwarmUiPresetDefinition
internal sealed class SwarmUiModelDefinition
{
public const string BabesId = "babesByStableYogi_v4XLLightning.safetensors";
public const string DreamShaperId = "DreamShaper_XL_-_Lightning_DPM++_SDE.safetensors";
public const string EpicRealismId = "epicrealismXL_VXIAbeast4SLightning.safetensors";
public const string LustifyId = "lustifyNSFWCheckpoint_v40DMD2.safetensors";
public string Id { get; set; } = "";
public string Label { get; set; } = "";
public string Model { get; set; } = "";
public int Steps { get; set; } = 8;
public double CfgScale { get; set; } = 1;
public int ClipSkip { get; set; } = 1;
public int ClipSkip { get; set; } = 0;
public string Sampler { get; set; } = "";
@@ -290,12 +484,107 @@ internal sealed class SwarmUiPresetDefinition
public List<SwarmUiLoraEntry>? NegativeLoras { get; set; }
public void Validate()
{
if (Steps is < 1 or > 200)
{
throw new InvalidOperationException($"Model '{Id}' steps must be between 1 and 200.");
}
if (CfgScale is < 0 or > 30)
{
throw new InvalidOperationException($"Model '{Id}' cfgScale must be between 0 and 30.");
}
if (ClipSkip is < 0 or > 12)
{
throw new InvalidOperationException($"Model '{Id}' clipSkip must be between 0 and 12.");
}
SwarmUiPresetDefinition.ValidateLoras(Id, PositiveLoras, "positive");
SwarmUiPresetDefinition.ValidateLoras(Id, NegativeLoras, "negative");
}
public static List<SwarmUiModelDefinition> Catalog() =>
[
Lightning(BabesId, "babesByStableYogi v4 XL Lightning", 7, 1.5, 0, "euler", "normal", 0),
Lightning(DreamShaperId, "DreamShaper XL Lightning", 4, 2, 2, "dpmpp_sde", "karras", 3346112079),
Lightning(EpicRealismId, "epicrealism XL Lightning", 7, 1.5, 0, "euler", "normal", 0),
Lightning(LustifyId, "lustify NSFW v40 DMD2", 7, 1.5, 0, "euler", "normal", 0),
];
private static SwarmUiModelDefinition Lightning(
string id,
string label,
int steps,
double cfg,
int clipSkip,
string sampler,
string scheduler,
long seed) =>
new()
{
Id = id,
Label = label,
Steps = steps,
CfgScale = cfg,
ClipSkip = clipSkip,
Sampler = sampler,
Scheduler = scheduler,
Seed = seed,
};
}
internal sealed class SwarmUiPresetDefinition
{
public string Id { get; set; } = "";
public string Label { get; set; } = "";
public string Model { get; set; } = "";
public string Style { get; set; } = "";
public string Negative { get; set; } = "";
public int? Steps { get; set; }
public double? CfgScale { get; set; }
public int? ClipSkip { get; set; }
public string? Sampler { get; set; }
public string? Scheduler { get; set; }
public long? Seed { get; set; }
/// <summary>Legacy preset-wide positive; copied into <see cref="Style"/> on load.</summary>
public string? Positive { get; set; }
public List<SwarmUiLoraEntry>? PositiveLoras { get; set; }
public List<SwarmUiLoraEntry>? NegativeLoras { get; set; }
public SwarmUiKindPreset? Avatar { get; set; }
public SwarmUiKindPreset? Custom { get; set; }
public SwarmUiKindPreset? FullBody { get; set; }
public void LiftLegacyPromptFields()
{
if (string.IsNullOrWhiteSpace(Style) && !string.IsNullOrWhiteSpace(Positive))
{
Style = Positive;
}
Positive = null;
Avatar?.LiftShotType();
Custom?.LiftShotType();
FullBody?.LiftShotType();
}
public void Validate()
{
if (Steps is < 1 or > 200)
@@ -313,11 +602,11 @@ internal sealed class SwarmUiPresetDefinition
throw new InvalidOperationException($"Preset '{Id}' clipSkip must be between 0 and 12.");
}
ValidateLoras(PositiveLoras, "positive");
ValidateLoras(NegativeLoras, "negative");
ValidateLoras(Id, PositiveLoras, "positive");
ValidateLoras(Id, NegativeLoras, "negative");
}
private void ValidateLoras(IReadOnlyList<SwarmUiLoraEntry>? loras, string side)
internal static void ValidateLoras(string ownerId, IReadOnlyList<SwarmUiLoraEntry>? loras, string side)
{
if (loras is null)
{
@@ -328,32 +617,50 @@ internal sealed class SwarmUiPresetDefinition
{
if (string.IsNullOrWhiteSpace(lora.Name))
{
throw new InvalidOperationException($"Preset '{Id}' has an empty {side} LoRA name.");
throw new InvalidOperationException($"'{ownerId}' has an empty {side} LoRA name.");
}
if (lora.Weight is < -4 or > 4)
{
throw new InvalidOperationException($"Preset '{Id}' LoRA '{lora.Name}' weight is out of range.");
throw new InvalidOperationException($"'{ownerId}' LoRA '{lora.Name}' weight is out of range.");
}
}
}
public SwarmUiResolvedProfile ToProfile(PortraitKind kind) =>
new(
public SwarmUiResolvedProfile ToProfile(PortraitKind kind, SwarmUiModelDefinition? model = null)
{
model ??= new SwarmUiModelDefinition
{
Id = Model,
Steps = 8,
CfgScale = 1,
ClipSkip = 0,
Sampler = "",
Scheduler = "",
Seed = -1,
};
var kindPreset = KindPresetFor(kind);
var negative = JoinPrompts(model.Negative, Negative);
return new SwarmUiResolvedProfile(
Id,
Label,
Model,
Steps,
CfgScale,
ClipSkip,
Sampler,
Scheduler,
Seed,
Positive,
Negative,
PositiveLoras ?? [],
NegativeLoras ?? [],
KindPresetFor(kind));
string.IsNullOrWhiteSpace(Model) ? model.Id : Model,
Steps ?? model.Steps,
CfgScale ?? model.CfgScale,
ClipSkip ?? model.ClipSkip,
Sampler ?? model.Sampler,
Scheduler ?? model.Scheduler,
Seed ?? model.Seed,
model.Positive,
Style,
kindPreset.ResolvedShotType(),
"",
negative,
PositiveLoras ?? model.PositiveLoras ?? [],
NegativeLoras ?? model.NegativeLoras ?? [],
kindPreset);
}
private SwarmUiKindPreset KindPresetFor(PortraitKind kind) => kind switch
{
@@ -363,19 +670,28 @@ internal sealed class SwarmUiPresetDefinition
_ => throw new ArgumentOutOfRangeException(nameof(kind)),
};
private static string JoinPrompts(string left, string right)
{
if (string.IsNullOrWhiteSpace(left))
{
return right.Trim();
}
if (string.IsNullOrWhiteSpace(right))
{
return left.Trim();
}
return $"{left.Trim()}, {right.Trim()}";
}
public static SwarmUiPresetDefinition CreateDefault() =>
new()
{
Id = "default",
Label = "Default",
Model = "DreamShaper_XL_-_Lightning_DPM++_SDE.safetensors",
Steps = 4,
CfgScale = 2,
ClipSkip = 2,
Sampler = "dpmpp_sde",
Scheduler = "karras",
Seed = 3346112079,
Positive =
Model = SwarmUiModelDefinition.BabesId,
Style =
"cinematic photo, realist detail, detailed character expressions, amazing quality, analog film grain, school portrait photograph, neutral background, natural lighting, realistic, sharp focus",
Negative =
"(low quality, worst quality:1.4), cgi, text, signature, watermark, extra limbs, nsfw, nude, naked, explicit, blurry, deformed, bad anatomy, logo",
@@ -383,49 +699,41 @@ internal sealed class SwarmUiPresetDefinition
{
Width = 1024,
Height = 1024,
Positive = "close up, head and shoulders portrait, facing the camera, upper body visible.",
ShotType = "close up, head and shoulders portrait, facing the camera, upper body visible.",
},
Custom = new SwarmUiKindPreset { Width = 896, Height = 1152 },
FullBody = new SwarmUiKindPreset
{
Width = 896,
Height = 1152,
Positive = "full body standing portrait, head to toe visible, neutral pose, current outfit clearly visible.",
ShotType = "full body standing portrait, head to toe visible, neutral pose, current outfit clearly visible.",
},
};
public static SwarmUiPresetDefinition CreateChild()
{
var adult = CreateDefault();
return new SwarmUiPresetDefinition
public static SwarmUiPresetDefinition CreateChild() =>
new()
{
Id = "child",
Label = "Children",
Model = adult.Model,
Steps = adult.Steps,
CfgScale = adult.CfgScale,
ClipSkip = adult.ClipSkip,
Sampler = adult.Sampler,
Scheduler = adult.Scheduler,
Seed = adult.Seed,
Positive =
Model = SwarmUiModelDefinition.DreamShaperId,
Style =
"cinematic photo, child-friendly school portrait, soft natural features, gentle expression, neutral background, natural lighting, realistic, sharp focus",
Negative = adult.Negative,
Negative =
"(low quality, worst quality:1.4), cgi, text, signature, watermark, extra limbs, nsfw, nude, naked, explicit, blurry, deformed, bad anatomy, logo",
Avatar = new SwarmUiKindPreset
{
Width = 1024,
Height = 1024,
Positive = "close up, head and shoulders portrait of a child, facing the camera, upper body visible, soft features.",
ShotType = "close up, head and shoulders portrait of a child, facing the camera, upper body visible, soft features.",
},
Custom = adult.Custom,
Custom = new SwarmUiKindPreset { Width = 896, Height = 1152 },
FullBody = new SwarmUiKindPreset
{
Width = 896,
Height = 1152,
Positive = "full body standing portrait of a child, head to toe visible, neutral pose, current outfit clearly visible.",
ShotType = "full body standing portrait of a child, head to toe visible, neutral pose, current outfit clearly visible.",
},
};
}
}
internal sealed class SwarmUiAgeRule
@@ -443,7 +751,23 @@ internal sealed class SwarmUiKindPreset
public int Height { get; set; } = 512;
public string Positive { get; set; } = "";
public string ShotType { get; set; } = "";
/// <summary>Legacy kind positive; copied into <see cref="ShotType"/> on load.</summary>
public string? Positive { get; set; }
public void LiftShotType()
{
if (string.IsNullOrWhiteSpace(ShotType) && !string.IsNullOrWhiteSpace(Positive))
{
ShotType = Positive;
}
Positive = null;
}
public string ResolvedShotType() =>
string.IsNullOrWhiteSpace(ShotType) ? (Positive ?? "") : ShotType;
}
internal sealed class SwarmUiLoraEntry
@@ -463,7 +787,10 @@ internal sealed record SwarmUiResolvedProfile(
string Sampler,
string Scheduler,
long Seed,
string Positive,
string ModelPositive,
string Style,
string ShotType,
string Pose,
string Negative,
IReadOnlyList<SwarmUiLoraEntry> PositiveLoras,
IReadOnlyList<SwarmUiLoraEntry> NegativeLoras,