rework wildcard random handling

to have trackable seeds - for https://github.com/mcmonkeyprojects/SwarmUI/discussions/6
This commit is contained in:
Alex "mcmonkey" Goodwin
2024-06-24 15:57:32 -07:00
parent e98b79bee1
commit c7f8b2b2ed
2 changed files with 34 additions and 17 deletions

View File

@@ -44,8 +44,6 @@ public class T2IParamInput
public class PromptTagContext public class PromptTagContext
{ {
public Random Random;
public T2IParamInput Input; public T2IParamInput Input;
public string Param; public string Param;
@@ -158,7 +156,7 @@ public class T2IParamInput
List<string> vals = [.. rawVals]; List<string> vals = [.. rawVals];
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
int index = context.Random.Next(vals.Count); int index = context.Input.GetWildcardRandom().Next(vals.Count);
string choice = vals[index]; string choice = vals[index];
if (TryInterpretNumberRange(choice, out string number)) if (TryInterpretNumberRange(choice, out string number))
{ {
@@ -243,7 +241,7 @@ public class T2IParamInput
List<string> vals = [.. wildcard.Options]; List<string> vals = [.. wildcard.Options];
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
int index = context.Random.Next(vals.Count); int index = context.Input.GetWildcardRandom().Next(vals.Count);
string choice = vals[index]; string choice = vals[index];
result += context.Parse(choice).Trim() + partSeparator; result += context.Parse(choice).Trim() + partSeparator;
if (vals.Count == 1) if (vals.Count == 1)
@@ -598,6 +596,34 @@ public class T2IParamInput
proc(T2IParamTypes.NegativePrompt); proc(T2IParamTypes.NegativePrompt);
} }
/// <summary>Random instance for <see cref="T2IParamTypes.WildcardSeed"/>.</summary>
public Random WildcardRandom = null;
/// <summary>Gets the random instance for <see cref="T2IParamTypes.WildcardSeed"/>, initializing it if needed.</summary>
public Random GetWildcardRandom()
{
if (WildcardRandom is not null)
{
return WildcardRandom;
}
long backupSeed = Get(T2IParamTypes.Seed) + Get(T2IParamTypes.VariationSeed, 0) + 17;
if (!TryGet(T2IParamTypes.WildcardSeed, out long wildcardSeed))
{
wildcardSeed = backupSeed;
}
if (wildcardSeed > int.MaxValue)
{
wildcardSeed %= int.MaxValue;
}
if (wildcardSeed == -1)
{
wildcardSeed = Random.Shared.Next(int.MaxValue);
}
Set(T2IParamTypes.WildcardSeed, wildcardSeed);
WildcardRandom = new((int)wildcardSeed);
return WildcardRandom;
}
/// <summary>Special utility to process prompt inputs before the request is executed (to parse wildcards, embeddings, etc).</summary> /// <summary>Special utility to process prompt inputs before the request is executed (to parse wildcards, embeddings, etc).</summary>
public string ProcessPromptLike(T2IRegisteredParam<string> param) public string ProcessPromptLike(T2IRegisteredParam<string> param)
{ {
@@ -607,18 +633,7 @@ public class T2IParamInput
return ""; return "";
} }
string fixedVal = val.Replace('\0', '\a').Replace("\a", ""); string fixedVal = val.Replace('\0', '\a').Replace("\a", "");
long backupSeed = Get(T2IParamTypes.Seed) + Get(T2IParamTypes.VariationSeed, 0) + param.Type.Name.Length; PromptTagContext context = new() { Input = this, Param = param.Type.ID };
long wildcardSeed = Get(T2IParamTypes.WildcardSeed, backupSeed);
if (wildcardSeed > int.MaxValue)
{
wildcardSeed %= int.MaxValue;
}
if (wildcardSeed == -1)
{
wildcardSeed = Random.Shared.Next(int.MaxValue);
}
Random rand = new((int)wildcardSeed);
PromptTagContext context = new() { Input = this, Random = rand, Param = param.Type.ID };
fixedVal = ProcessPromptLike(fixedVal, context); fixedVal = ProcessPromptLike(fixedVal, context);
if (fixedVal != val) if (fixedVal != val)
{ {

View File

@@ -775,7 +775,9 @@ function reuseLastParamVal(paramId) {
} }
let params = JSON.parse(currentMetadataVal).sui_image_params; let params = JSON.parse(currentMetadataVal).sui_image_params;
if (pid in params) { if (pid in params) {
getRequiredElementById(paramId).value = params[pid]; let elem = getRequiredElementById(paramId);
elem.value = params[pid];
triggerChangeFor(elem);
} }
} }