Skip to content

Commit c75a448

Browse files
committed
Fix for invalid NaN values in ComfyUI
Add EfficientSDXL Fix for empty sort value
1 parent a6f505f commit c75a448

4 files changed

Lines changed: 151 additions & 101 deletions

File tree

Diffusion.Scanner/Metadata.cs

Lines changed: 130 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -382,140 +382,181 @@ int GetIntTag(string key)
382382
private static FileParameters ReadComfyUIParameters(string file, string description)
383383
{
384384
var fp = new FileParameters();
385-
var json = description.Substring("prompt: ".Length);
386385

387-
var root = JsonDocument.Parse(json);
388-
var nodes = root.RootElement.EnumerateObject().ToDictionary(o => o.Name, o => o.Value);
386+
try
387+
{
388+
var json = description.Substring("prompt: ".Length);
389389

390-
var isSDXL = false;
391-
var isEfficient = false;
390+
// fix for errant nodes
391+
json = json.Replace("NaN", "null");
392392

393-
var ksampler = nodes.Values.SingleOrDefault(o =>
394-
{
395-
if (o.TryGetProperty("class_type", out var element))
396-
{
397-
return element.GetString() == "KSampler";
398-
}
393+
var root = JsonDocument.Parse(json);
394+
var nodes = root.RootElement.EnumerateObject().ToDictionary(o => o.Name, o => o.Value);
399395

400-
return false;
401-
});
396+
var isSDXL = false;
397+
var isEfficient = false;
398+
var isEfficientSDXL = false;
402399

403-
if (ksampler.ValueKind == JsonValueKind.Undefined)
404-
{
405-
ksampler = nodes.Values.FirstOrDefault(o =>
400+
var ksampler = nodes.Values.SingleOrDefault(o =>
406401
{
407402
if (o.TryGetProperty("class_type", out var element))
408403
{
409-
return element.GetString() == "KSampler (Efficient)";
404+
return element.GetString() == "KSampler";
410405
}
411406

412407
return false;
413408
});
414409

415-
if (ksampler.ValueKind != JsonValueKind.Undefined)
416-
{
417-
isEfficient = true;
418-
}
419-
}
420-
421-
if (ksampler.ValueKind == JsonValueKind.Undefined)
422-
{
423-
ksampler = nodes.Values.FirstOrDefault(o =>
410+
if (ksampler.ValueKind == JsonValueKind.Undefined)
424411
{
425-
if (o.TryGetProperty("class_type", out var element))
412+
ksampler = nodes.Values.FirstOrDefault(o =>
426413
{
427-
return element.GetString() == "KSamplerAdvanced";
428-
}
414+
if (o.TryGetProperty("class_type", out var element))
415+
{
416+
return element.GetString() == "KSampler (Efficient)";
417+
}
429418

430-
return false;
431-
});
419+
return false;
420+
});
432421

433-
if (ksampler.ValueKind != JsonValueKind.Undefined)
434-
{
435-
isSDXL = true;
422+
if (ksampler.ValueKind != JsonValueKind.Undefined)
423+
{
424+
isEfficient = true;
425+
}
436426
}
437-
}
438427

428+
if (ksampler.ValueKind == JsonValueKind.Undefined)
429+
{
430+
ksampler = nodes.Values.FirstOrDefault(o =>
431+
{
432+
if (o.TryGetProperty("class_type", out var element))
433+
{
434+
return element.GetString() == "KSamplerAdvanced";
435+
}
439436

440-
if (ksampler.ValueKind != JsonValueKind.Undefined)
441-
{
442-
443-
var image = ksampler.GetProperty("inputs");
437+
return false;
438+
});
444439

445-
if (image.TryGetProperty("positive", out var positive))
446-
{
447-
var promptIndex = positive.EnumerateArray().First().GetString();
448-
var promptObject = nodes[promptIndex].GetProperty("inputs");
449-
if (isSDXL)
440+
if (ksampler.ValueKind != JsonValueKind.Undefined)
450441
{
451-
fp.Prompt = promptObject.GetProperty("text_g").GetString();
442+
isSDXL = true;
452443
}
453-
else if (isEfficient)
444+
}
445+
446+
if (ksampler.ValueKind == JsonValueKind.Undefined)
447+
{
448+
ksampler = nodes.Values.FirstOrDefault(o =>
454449
{
455-
fp.Prompt = promptObject.GetProperty("positive").GetString();
456-
}
457-
else
450+
if (o.TryGetProperty("class_type", out var element))
451+
{
452+
return element.GetString() == "Eff. Loader SDXL";
453+
}
454+
455+
return false;
456+
});
457+
458+
if (ksampler.ValueKind != JsonValueKind.Undefined)
458459
{
459-
fp.Prompt = promptObject.GetProperty("text").GetString();
460+
isEfficientSDXL = true;
460461
}
461462
}
462463

463-
if (image.TryGetProperty("negative", out var negative))
464+
465+
if (ksampler.ValueKind != JsonValueKind.Undefined)
464466
{
465-
var promptIndex = negative.EnumerateArray().First().GetString();
466-
var promptObject = nodes[promptIndex].GetProperty("inputs");
467-
if (isSDXL)
467+
468+
var image = ksampler.GetProperty("inputs");
469+
470+
if (image.TryGetProperty("positive", out var positive))
468471
{
469-
fp.NegativePrompt = promptObject.GetProperty("text_g").GetString();
472+
var promptIndex = positive.EnumerateArray().First().GetString();
473+
var promptObject = nodes[promptIndex].GetProperty("inputs");
474+
if (isSDXL)
475+
{
476+
fp.Prompt = promptObject.GetProperty("text_g").GetString();
477+
}
478+
else if (isEfficient)
479+
{
480+
fp.Prompt = promptObject.GetProperty("positive").GetString();
481+
}
482+
else if (isEfficientSDXL)
483+
{
484+
fp.Prompt = promptObject.GetProperty("text").GetString();
485+
}
486+
else
487+
{
488+
fp.Prompt = promptObject.GetProperty("text").GetString();
489+
}
470490
}
471-
else if (isEfficient)
491+
492+
if (image.TryGetProperty("negative", out var negative))
472493
{
473-
fp.NegativePrompt = promptObject.GetProperty("negative").GetString();
494+
if (negative.ValueKind == JsonValueKind.Array)
495+
{
496+
var promptIndex = negative.EnumerateArray().First().GetString();
497+
var promptObject = nodes[promptIndex].GetProperty("inputs");
498+
if (isSDXL)
499+
{
500+
fp.NegativePrompt = promptObject.GetProperty("text_g").GetString();
501+
}
502+
else if (isEfficient)
503+
{
504+
fp.NegativePrompt = promptObject.GetProperty("negative").GetString();
505+
}
506+
else
507+
{
508+
fp.NegativePrompt = promptObject.GetProperty("text").GetString();
509+
}
510+
}
511+
else if (negative.ValueKind == JsonValueKind.String)
512+
{
513+
fp.NegativePrompt = negative.GetString();
514+
}
515+
474516
}
475-
else
517+
518+
if (image.TryGetProperty("latent_image", out var latent_image))
476519
{
477-
fp.NegativePrompt = promptObject.GetProperty("text").GetString();
520+
var index = latent_image.EnumerateArray().First().GetString();
521+
var promptObject = nodes[index].GetProperty("inputs");
522+
var hasWidth = promptObject.TryGetProperty("width", out var widthObject);
523+
var hasHeight = promptObject.TryGetProperty("height", out var heightObject);
524+
525+
if (hasWidth && hasHeight)
526+
{
527+
fp.Width = widthObject.GetInt32();
528+
fp.Height = heightObject.GetInt32();
529+
}
478530
}
479-
}
480531

481-
if (image.TryGetProperty("latent_image", out var latent_image))
482-
{
483-
var index = latent_image.EnumerateArray().First().GetString();
484-
var promptObject = nodes[index].GetProperty("inputs");
485-
var hasWidth = promptObject.TryGetProperty("width", out var widthObject);
486-
var hasHeight = promptObject.TryGetProperty("height", out var heightObject);
532+
fp.Steps = image.GetProperty("steps").GetInt32();
533+
fp.CFGScale = image.GetProperty("cfg").GetDecimal();
487534

488-
if (hasWidth && hasHeight)
535+
if (isSDXL)
489536
{
490-
fp.Width = widthObject.GetInt32();
491-
fp.Height = heightObject.GetInt32();
537+
fp.Seed = image.GetProperty("noise_seed").GetInt64();
492538
}
493-
}
539+
else
540+
{
541+
var seed = image.GetProperty("seed");
494542

495-
fp.Steps = image.GetProperty("steps").GetInt32();
496-
fp.CFGScale = image.GetProperty("cfg").GetDecimal();
543+
if (seed.ValueKind == JsonValueKind.Number)
544+
{
545+
fp.Seed = seed.GetInt64();
546+
}
547+
}
497548

498-
if (isSDXL)
499-
{
500-
fp.Seed = image.GetProperty("noise_seed").GetInt64();
501-
}
502-
else
503-
{
504-
var seed = image.GetProperty("seed");
549+
fp.Sampler = image.GetProperty("sampler_name").GetString();
505550

506-
if (seed.ValueKind == JsonValueKind.Number)
507-
{
508-
fp.Seed = seed.GetInt64();
509-
}
551+
fp.OtherParameters = $"Steps: {fp.Steps} Sampler: {fp.Sampler} CFG Scale: {fp.CFGScale} Seed: {fp.Seed} Size: {fp.Width}x{fp.Height}";
510552
}
511553

512-
fp.Sampler = image.GetProperty("sampler_name").GetString();
513-
514-
fp.OtherParameters = $"Steps: {fp.Steps} Sampler: {fp.Sampler} CFG Scale: {fp.CFGScale} Seed: {fp.Seed} Size: {fp.Width}x{fp.Height}";
554+
return fp;
555+
}
556+
catch
557+
{
558+
return fp;
515559
}
516-
517-
518-
return fp;
519560
}
520561

521562
private static FileParameters ReadInvokeAIParametersNew(string file, string description)

Diffusion.Toolkit/MainWindow.xaml.Scanning.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private async Task RescanTask()
2424
else
2525
{
2626
await _messagePopupManager.Show("No image paths configured!", "Rescan Folders");
27+
ShowSettings(null);
2728
}
2829
}
2930

@@ -45,6 +46,7 @@ private async Task RebuildTask()
4546
else
4647
{
4748
await _messagePopupManager.Show("No image paths configured!", "Rebuild Metadata");
49+
ShowSettings(null);
4850
}
4951
}
5052

Diffusion.Toolkit/MainWindow.xaml.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ private async void OnLoaded(object sender, RoutedEventArgs e)
340340
_settings.ShowAlbumPanel ??= true;
341341
_settings.RecurseFolders ??= true;
342342
_settings.UseBuiltInViewer ??= true;
343+
_settings.SortAlbumsBy ??= "Name";
343344

344345
UpdateTheme();
345346

@@ -795,22 +796,27 @@ private void LoadModels()
795796

796797
foreach (var hash in hashes.hashes)
797798
{
798-
var path = hash.Key.Substring(index);
799-
if (modelLookup.TryGetValue(path, out var model))
799+
if (index < hash.Key.Length)
800800
{
801-
model.SHA256 = hash.Value.sha256;
802-
}
803-
else
804-
{
805-
_modelsCollection.Add(new Model()
801+
var path = hash.Key.Substring(index);
802+
803+
if (modelLookup.TryGetValue(path, out var model))
804+
{
805+
model.SHA256 = hash.Value.sha256;
806+
}
807+
else
806808
{
807-
Filename = Path.GetFileNameWithoutExtension(path),
808-
Path = path,
809-
SHA256 = hash.Value.sha256,
810-
IsLocal = true
811-
});
809+
_modelsCollection.Add(new Model()
810+
{
811+
Filename = Path.GetFileNameWithoutExtension(path),
812+
Path = path,
813+
SHA256 = hash.Value.sha256,
814+
IsLocal = true
815+
});
812816

817+
}
813818
}
819+
814820
}
815821

816822
//foreach (var model in _modelsCollection.ToList())

Diffusion.Toolkit/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public Settings(bool initialize)
7777
UseBuiltInViewer = true;
7878
OpenInFullScreen = true;
7979
CustomCommandLineArgs = "%1";
80+
SortAlbumsBy = "Name";
8081

8182
if (initialize)
8283
{

0 commit comments

Comments
 (0)