Browse Source

Merge branch 'dev' into kohya

pull/240/head
JT 1 year ago committed by GitHub
parent
commit
b9e383baf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 58
      StabilityMatrix.Core/Models/GenerationParameters.cs

58
StabilityMatrix.Core/Models/GenerationParameters.cs

@ -1,11 +1,14 @@
using System.Diagnostics.CodeAnalysis;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using StabilityMatrix.Core.Models.Api.Comfy;
namespace StabilityMatrix.Core.Models;
[JsonSerializable(typeof(GenerationParameters))]
public record GenerationParameters
public partial record GenerationParameters
{
public string? PositivePrompt { get; set; }
public string? NegativePrompt { get; set; }
@ -29,12 +32,26 @@ public record GenerationParameters
return false;
}
try
{
generationParameters = Parse(text);
}
catch (Exception)
{
generationParameters = null;
return false;
}
return true;
}
public static GenerationParameters Parse(string text)
{
var lines = text.Split('\n');
if (lines.LastOrDefault() is not { } lastLine)
{
generationParameters = null;
return false;
throw new ValidationException("Fields line not found");
}
if (lastLine.StartsWith("Steps:") != true)
@ -44,8 +61,7 @@ public record GenerationParameters
if (lastLine.StartsWith("Steps:") != true)
{
generationParameters = null;
return false;
throw new ValidationException("Unable to locate starting marker of last line");
}
}
@ -60,7 +76,7 @@ public record GenerationParameters
// Parse last line
var lineFields = ParseLine(lastLine);
generationParameters = new GenerationParameters
var generationParameters = new GenerationParameters
{
PositivePrompt = positivePrompt,
NegativePrompt = negativePrompt,
@ -85,7 +101,7 @@ public record GenerationParameters
}
}
return true;
return generationParameters;
}
/// <summary>
@ -97,14 +113,14 @@ public record GenerationParameters
{
var dict = new Dictionary<string, string>();
foreach (var field in fields.Split(','))
// Values main contain commas or colons
foreach (var match in ParametersFieldsRegex().Matches(fields).Cast<Match>())
{
var split = field.Split(':', 2);
if (split.Length < 2)
if (!match.Success)
continue;
var key = split[0].Trim();
var value = split[1].Trim();
var key = match.Groups[1].Value.Trim();
var value = UnquoteValue(match.Groups[2].Value.Trim());
dict.Add(key, value);
}
@ -112,6 +128,19 @@ public record GenerationParameters
return dict;
}
/// <summary>
/// Unquotes a quoted value field if required
/// </summary>
private static string UnquoteValue(string quotedField)
{
if (!(quotedField.StartsWith('"') && quotedField.EndsWith('"')))
{
return quotedField;
}
return JsonNode.Parse(quotedField)?.GetValue<string>() ?? "";
}
/// <summary>
/// Converts current <see cref="Sampler"/> string to <see cref="ComfySampler"/> and <see cref="ComfyScheduler"/>.
/// </summary>
@ -169,4 +198,7 @@ public record GenerationParameters
Sampler = "DPM++ 2M Karras"
};
}
[GeneratedRegex("""\s*([\w ]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)""")]
private static partial Regex ParametersFieldsRegex();
}

Loading…
Cancel
Save