Skip to content

Commit

Permalink
Add back some stuff removed in 8165293
Browse files Browse the repository at this point in the history
  • Loading branch information
ionite34 committed Feb 14, 2024
1 parent 188be7b commit 00e0bfc
Showing 1 changed file with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
Expand All @@ -15,7 +15,6 @@
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using ExifLibrary;
using MetadataExtractor.Formats.Exif;
using NLog;
using Refit;
using SkiaSharp;
Expand All @@ -27,7 +26,6 @@
using StabilityMatrix.Avalonia.ViewModels.Dialogs;
using StabilityMatrix.Avalonia.ViewModels.Inference;
using StabilityMatrix.Avalonia.ViewModels.Inference.Modules;
using StabilityMatrix.Core.Animation;
using StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
Expand Down Expand Up @@ -320,14 +318,18 @@ protected async Task RunGeneration(ImageGenerationEventArgs args, CancellationTo
Task.Run(
async () =>
{
var delayTime = 250 - (int)timer.ElapsedMilliseconds;
if (delayTime > 0)
try
{
await Task.Delay(delayTime, cancellationToken);
var delayTime = 250 - (int)timer.ElapsedMilliseconds;
if (delayTime > 0)
{
await Task.Delay(delayTime, cancellationToken);
}

// ReSharper disable once AccessToDisposedClosure
AttachRunningNodeChangedHandler(promptTask);
}

// ReSharper disable once AccessToDisposedClosure
AttachRunningNodeChangedHandler(promptTask);
catch (TaskCanceledException) { }
},
cancellationToken
)
Expand All @@ -351,10 +353,7 @@ await DialogHelper
// Get output images
var imageOutputs = await client.GetImagesForExecutedPromptAsync(promptTask.Id, cancellationToken);

if (
!imageOutputs.TryGetValue(args.OutputNodeNames[0], out var images)
|| images is not { Count: > 0 }
)
if (imageOutputs.Values.All(images => images is null or { Count: 0 }))
{
// No images match
notificationService.Show(
Expand All @@ -373,7 +372,7 @@ await DialogHelper
ImageGalleryCardViewModel.ImageSources.Clear();
}

var outputImages = await ProcessOutputImages(images, args);
var outputImages = await ProcessAllOutputImages(imageOutputs, args);

var notificationImage = outputImages.FirstOrDefault()?.LocalFile;

Expand Down Expand Up @@ -403,12 +402,34 @@ await notificationService.ShowAsync(
}
}

private async Task<IEnumerable<ImageSource>> ProcessAllOutputImages(
IReadOnlyDictionary<string, List<ComfyImage>?> images,
ImageGenerationEventArgs args
)
{
var results = new List<ImageSource>();

foreach (var (nodeName, imageList) in images)
{
if (imageList is null)
{
Logger.Warn("No images for node {NodeName}", nodeName);
continue;
}

results.AddRange(await ProcessOutputImages(imageList, args, nodeName.Replace('_', ' ')));
}

return results;
}

/// <summary>
/// Handles image output metadata for generation runs
/// </summary>
private async Task<List<ImageSource>> ProcessOutputImages(
IReadOnlyCollection<ComfyImage> images,
ImageGenerationEventArgs args
ImageGenerationEventArgs args,
string? imageLabel = null
)
{
var client = args.Client;
Expand Down Expand Up @@ -464,7 +485,7 @@ ImageGenerationEventArgs args
images.Count
);

outputImages.Add(new ImageSource(filePath));
outputImages.Add(new ImageSource(filePath) { Label = imageLabel });
EventManager.Instance.OnImageFileAdded(filePath);
}
else if (comfyImage.FileName.EndsWith(".webp"))
Expand Down Expand Up @@ -493,7 +514,7 @@ ImageGenerationEventArgs args
fileExtension: Path.GetExtension(comfyImage.FileName).Replace(".", "")
);

outputImages.Add(new ImageSource(filePath));
outputImages.Add(new ImageSource(filePath) { Label = imageLabel });
EventManager.Instance.OnImageFileAdded(filePath);
}
else
Expand All @@ -507,7 +528,7 @@ ImageGenerationEventArgs args
fileExtension: Path.GetExtension(comfyImage.FileName).Replace(".", "")
);

outputImages.Add(new ImageSource(filePath));
outputImages.Add(new ImageSource(filePath) { Label = imageLabel });
EventManager.Instance.OnImageFileAdded(filePath);
}
}
Expand Down Expand Up @@ -577,7 +598,12 @@ private async Task GenerateImage(
}
catch (OperationCanceledException)
{
Logger.Debug($"Image Generation Canceled");
Logger.Debug("Image Generation Canceled");
}
catch (ValidationException e)
{
Logger.Debug("Image Generation Validation Error: {Message}", e.Message);
notificationService.Show("Validation Error", e.Message, NotificationType.Error);
}
}

Expand Down

0 comments on commit 00e0bfc

Please sign in to comment.