|
|
|
@ -28,6 +28,8 @@ using StabilityMatrix.Core.Models.Api.Comfy;
|
|
|
|
|
using StabilityMatrix.Core.Models.Api.Comfy.Nodes; |
|
|
|
|
using StabilityMatrix.Core.Models.Api.Comfy.NodeTypes; |
|
|
|
|
using StabilityMatrix.Core.Models.Api.Comfy.WebSocketData; |
|
|
|
|
using StabilityMatrix.Core.Models.Database; |
|
|
|
|
using StabilityMatrix.Core.Models.FileInterfaces; |
|
|
|
|
using StabilityMatrix.Core.Services; |
|
|
|
|
using InferenceTextToImageView = StabilityMatrix.Avalonia.Views.Inference.InferenceTextToImageView; |
|
|
|
|
|
|
|
|
@ -43,6 +45,7 @@ public partial class InferenceTextToImageViewModel : InferenceTabViewModelBase
|
|
|
|
|
private readonly INotificationService notificationService; |
|
|
|
|
private readonly ServiceManager<ViewModelBase> vmFactory; |
|
|
|
|
private readonly IModelIndexService modelIndexService; |
|
|
|
|
private readonly IImageIndexService imageIndexService; |
|
|
|
|
|
|
|
|
|
[JsonIgnore] |
|
|
|
|
public IInferenceClientManager ClientManager { get; } |
|
|
|
@ -103,12 +106,14 @@ public partial class InferenceTextToImageViewModel : InferenceTabViewModelBase
|
|
|
|
|
INotificationService notificationService, |
|
|
|
|
IInferenceClientManager inferenceClientManager, |
|
|
|
|
ServiceManager<ViewModelBase> vmFactory, |
|
|
|
|
IModelIndexService modelIndexService |
|
|
|
|
IModelIndexService modelIndexService, |
|
|
|
|
IImageIndexService imageIndexService |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
this.notificationService = notificationService; |
|
|
|
|
this.vmFactory = vmFactory; |
|
|
|
|
this.modelIndexService = modelIndexService; |
|
|
|
|
this.imageIndexService = imageIndexService; |
|
|
|
|
ClientManager = inferenceClientManager; |
|
|
|
|
|
|
|
|
|
// Get sub view models from service manager |
|
|
|
@ -416,89 +421,103 @@ public partial class InferenceTextToImageViewModel : InferenceTabViewModelBase
|
|
|
|
|
notificationService.Show("No output", "Did not receive any output images"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
// Disconnect progress handler |
|
|
|
|
OutputProgress.Value = 0; |
|
|
|
|
OutputProgress.Text = ""; |
|
|
|
|
ImageGalleryCardViewModel.PreviewImage?.Dispose(); |
|
|
|
|
ImageGalleryCardViewModel.PreviewImage = null; |
|
|
|
|
ImageGalleryCardViewModel.IsPreviewOverlayEnabled = false; |
|
|
|
|
|
|
|
|
|
List<ImageSource> outputImages; |
|
|
|
|
// Use local file path if available, otherwise use remote URL |
|
|
|
|
if (client.OutputImagesDir is { } outputPath) |
|
|
|
|
{ |
|
|
|
|
outputImages = new List<ImageSource>(); |
|
|
|
|
foreach (var image in images) |
|
|
|
|
{ |
|
|
|
|
var filePath = image.ToFilePath(outputPath); |
|
|
|
|
var fileStream = new BinaryReader(filePath.Info.OpenRead()); |
|
|
|
|
var bytes = fileStream.ReadBytes((int)filePath.Info.Length); |
|
|
|
|
var bytesWithMetadata = PngDataHelper.AddMetadata( |
|
|
|
|
bytes, |
|
|
|
|
generationInfo, |
|
|
|
|
smproj |
|
|
|
|
); |
|
|
|
|
fileStream.Close(); |
|
|
|
|
fileStream.Dispose(); |
|
|
|
|
|
|
|
|
|
await using var outputStream = filePath.Info.OpenWrite(); |
|
|
|
|
await outputStream.WriteAsync(bytesWithMetadata); |
|
|
|
|
await outputStream.FlushAsync(); |
|
|
|
|
promptTask?.Dispose(); |
|
|
|
|
client.PreviewImageReceived -= OnPreviewImageReceived; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
outputImages.Add(new ImageSource(filePath)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
private async Task ProcessOutputs(IReadOnlyList<ComfyImage> images) |
|
|
|
|
{ |
|
|
|
|
List<ImageSource> outputImages; |
|
|
|
|
// Use local file path if available, otherwise use remote URL |
|
|
|
|
if (client.OutputImagesDir is { } outputPath) |
|
|
|
|
{ |
|
|
|
|
outputImages = new List<ImageSource>(); |
|
|
|
|
foreach (var image in images) |
|
|
|
|
{ |
|
|
|
|
outputImages = images! |
|
|
|
|
.Select(i => new ImageSource(i.ToUri(client.BaseAddress))) |
|
|
|
|
.ToList(); |
|
|
|
|
} |
|
|
|
|
var filePath = image.ToFilePath(outputPath); |
|
|
|
|
|
|
|
|
|
// Download all images to make grid, if multiple |
|
|
|
|
if (outputImages.Count > 1) |
|
|
|
|
{ |
|
|
|
|
var loadedImages = outputImages |
|
|
|
|
.Select(i => SKImage.FromEncodedData(i.LocalFile?.Info.OpenRead())) |
|
|
|
|
.ToImmutableArray(); |
|
|
|
|
|
|
|
|
|
var grid = ImageProcessor.CreateImageGrid(loadedImages); |
|
|
|
|
var gridBytes = grid.Encode().ToArray(); |
|
|
|
|
var gridBytesWithMetadata = PngDataHelper.AddMetadata( |
|
|
|
|
gridBytes, |
|
|
|
|
var bytesWithMetadata = PngDataHelper.AddMetadata( |
|
|
|
|
await filePath.ReadAllBytesAsync(), |
|
|
|
|
generationInfo, |
|
|
|
|
smproj |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Save to disk |
|
|
|
|
var lastName = outputImages.Last().LocalFile?.Info.Name; |
|
|
|
|
var gridPath = client.OutputImagesDir!.JoinFile($"grid-{lastName}"); |
|
|
|
|
/*await using (var readStream = filePath.Info.OpenWrite()) |
|
|
|
|
{ |
|
|
|
|
using (var reader = new BinaryReader(readStream)) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
await using (var fileStream = gridPath.Info.OpenWrite()) |
|
|
|
|
await using (var outputStream = filePath.Info.OpenWrite()) |
|
|
|
|
{ |
|
|
|
|
await fileStream.WriteAsync(gridBytesWithMetadata, cancellationToken); |
|
|
|
|
await outputStream.WriteAsync(bytesWithMetadata); |
|
|
|
|
await outputStream.FlushAsync(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Insert to start of images |
|
|
|
|
var gridImage = new ImageSource(gridPath); |
|
|
|
|
// Preload |
|
|
|
|
await gridImage.GetBitmapAsync(); |
|
|
|
|
ImageGalleryCardViewModel.ImageSources.Add(gridImage); |
|
|
|
|
outputImages.Add(new ImageSource(filePath)); |
|
|
|
|
|
|
|
|
|
imageIndexService.OnImageAdded(filePath); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
outputImages = images! |
|
|
|
|
.Select(i => new ImageSource(i.ToUri(client.BaseAddress))) |
|
|
|
|
.ToList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Download all images to make grid, if multiple |
|
|
|
|
if (outputImages.Count > 1) |
|
|
|
|
{ |
|
|
|
|
var loadedImages = outputImages |
|
|
|
|
.Select(i => SKImage.FromEncodedData(i.LocalFile?.Info.OpenRead())) |
|
|
|
|
.ToImmutableArray(); |
|
|
|
|
|
|
|
|
|
var grid = ImageProcessor.CreateImageGrid(loadedImages); |
|
|
|
|
var gridBytes = grid.Encode().ToArray(); |
|
|
|
|
var gridBytesWithMetadata = PngDataHelper.AddMetadata( |
|
|
|
|
gridBytes, |
|
|
|
|
generationInfo, |
|
|
|
|
smproj |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Save to disk |
|
|
|
|
var lastName = outputImages.Last().LocalFile?.Info.Name; |
|
|
|
|
var gridPath = client.OutputImagesDir!.JoinFile($"grid-{lastName}"); |
|
|
|
|
|
|
|
|
|
// Add rest of images |
|
|
|
|
foreach (var img in outputImages) |
|
|
|
|
await using (var fileStream = gridPath.Info.OpenWrite()) |
|
|
|
|
{ |
|
|
|
|
// Preload |
|
|
|
|
await img.GetBitmapAsync(); |
|
|
|
|
ImageGalleryCardViewModel.ImageSources.Add(img); |
|
|
|
|
await fileStream.WriteAsync(gridBytesWithMetadata, cancellationToken); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Insert to start of images |
|
|
|
|
var gridImage = new ImageSource(gridPath); |
|
|
|
|
// Preload |
|
|
|
|
await gridImage.GetBitmapAsync(); |
|
|
|
|
ImageGalleryCardViewModel.ImageSources.Add(gridImage); |
|
|
|
|
|
|
|
|
|
imageIndexService.OnImageAdded(gridPath); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
// Disconnect progress handler |
|
|
|
|
OutputProgress.Value = 0; |
|
|
|
|
OutputProgress.Text = ""; |
|
|
|
|
ImageGalleryCardViewModel.PreviewImage?.Dispose(); |
|
|
|
|
ImageGalleryCardViewModel.PreviewImage = null; |
|
|
|
|
ImageGalleryCardViewModel.IsPreviewOverlayEnabled = false; |
|
|
|
|
|
|
|
|
|
promptTask?.Dispose(); |
|
|
|
|
client.PreviewImageReceived -= OnPreviewImageReceived; |
|
|
|
|
// Add rest of images |
|
|
|
|
foreach (var img in outputImages) |
|
|
|
|
{ |
|
|
|
|
// Preload |
|
|
|
|
await img.GetBitmapAsync(); |
|
|
|
|
ImageGalleryCardViewModel.ImageSources.Add(img); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -517,7 +536,6 @@ public partial class InferenceTextToImageViewModel : InferenceTabViewModelBase
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
await GenerateImageImpl(overrides, cancellationToken); |
|
|
|
|
await ImageFolderCardViewModel.OnLoadedAsync(); |
|
|
|
|
} |
|
|
|
|
catch (OperationCanceledException e) |
|
|
|
|
{ |
|
|
|
|