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