Ionite
8 months ago
committed by
GitHub
15 changed files with 347 additions and 13 deletions
@ -0,0 +1,48 @@ |
|||||||
|
<Styles |
||||||
|
xmlns="https://github.com/avaloniaui" |
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||||
|
xmlns:controls="using:StabilityMatrix.Avalonia.Controls" |
||||||
|
xmlns:sg="clr-namespace:SpacedGridControl.Avalonia;assembly=SpacedGridControl.Avalonia" |
||||||
|
xmlns:ui="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia" |
||||||
|
xmlns:vmInference="clr-namespace:StabilityMatrix.Avalonia.ViewModels.Inference" |
||||||
|
xmlns:converters="clr-namespace:StabilityMatrix.Avalonia.Converters" |
||||||
|
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" |
||||||
|
x:DataType="vmInference:LayerDiffuseCardViewModel"> |
||||||
|
<Design.PreviewWith> |
||||||
|
<Panel Width="400" Height="200"> |
||||||
|
<StackPanel Width="300" VerticalAlignment="Center"> |
||||||
|
<controls:LayerDiffuseCard DataContext="{x:Static mocks:DesignData.LayerDiffuseCardViewModel}"/> |
||||||
|
</StackPanel> |
||||||
|
</Panel> |
||||||
|
</Design.PreviewWith> |
||||||
|
|
||||||
|
<Style Selector="controls|LayerDiffuseCard"> |
||||||
|
<Setter Property="HorizontalAlignment" Value="Stretch" /> |
||||||
|
<Setter Property="Template"> |
||||||
|
<ControlTemplate> |
||||||
|
<controls:Card Padding="12"> |
||||||
|
<sg:SpacedGrid |
||||||
|
ColumnDefinitions="Auto,*" |
||||||
|
ColumnSpacing="8" |
||||||
|
RowDefinitions="*,*,*,*" |
||||||
|
RowSpacing="0"> |
||||||
|
<!-- Mode Selection --> |
||||||
|
<TextBlock |
||||||
|
Grid.Column="0" |
||||||
|
VerticalAlignment="Center" |
||||||
|
Text="Mode" |
||||||
|
TextAlignment="Left" /> |
||||||
|
|
||||||
|
<ui:FAComboBox |
||||||
|
Grid.Row="0" |
||||||
|
Grid.Column="1" |
||||||
|
HorizontalAlignment="Stretch" |
||||||
|
DisplayMemberBinding="{Binding Converter={x:Static converters:EnumAttributeConverters.DisplayName}}" |
||||||
|
ItemsSource="{Binding AvailableModes}" |
||||||
|
SelectedItem="{Binding SelectedMode}" /> |
||||||
|
</sg:SpacedGrid> |
||||||
|
</controls:Card> |
||||||
|
</ControlTemplate> |
||||||
|
</Setter> |
||||||
|
</Style> |
||||||
|
</Styles> |
@ -0,0 +1,7 @@ |
|||||||
|
using Avalonia.Controls.Primitives; |
||||||
|
using StabilityMatrix.Core.Attributes; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Avalonia.Controls; |
||||||
|
|
||||||
|
[Transient] |
||||||
|
public class LayerDiffuseCard : TemplatedControl; |
@ -0,0 +1,100 @@ |
|||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.ComponentModel.DataAnnotations; |
||||||
|
using System.Linq; |
||||||
|
using CommunityToolkit.Mvvm.ComponentModel; |
||||||
|
using KGySoft.CoreLibraries; |
||||||
|
using StabilityMatrix.Avalonia.Controls; |
||||||
|
using StabilityMatrix.Avalonia.Models.Inference; |
||||||
|
using StabilityMatrix.Avalonia.ViewModels.Base; |
||||||
|
using StabilityMatrix.Core.Attributes; |
||||||
|
using StabilityMatrix.Core.Models.Api.Comfy.Nodes; |
||||||
|
using StabilityMatrix.Core.Models.Inference; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Avalonia.ViewModels.Inference; |
||||||
|
|
||||||
|
[Transient] |
||||||
|
[ManagedService] |
||||||
|
[View(typeof(LayerDiffuseCard))] |
||||||
|
public partial class LayerDiffuseCardViewModel : LoadableViewModelBase, IComfyStep |
||||||
|
{ |
||||||
|
public const string ModuleKey = "LayerDiffuse"; |
||||||
|
|
||||||
|
[ObservableProperty] |
||||||
|
private LayerDiffuseMode selectedMode = LayerDiffuseMode.None; |
||||||
|
|
||||||
|
public IEnumerable<LayerDiffuseMode> AvailableModes => Enum<LayerDiffuseMode>.GetValues(); |
||||||
|
|
||||||
|
[ObservableProperty] |
||||||
|
[NotifyDataErrorInfo] |
||||||
|
[Required] |
||||||
|
[Range(-1d, 3d)] |
||||||
|
private double weight = 1; |
||||||
|
|
||||||
|
/// <inheritdoc /> |
||||||
|
public void ApplyStep(ModuleApplyStepEventArgs e) |
||||||
|
{ |
||||||
|
if (SelectedMode == LayerDiffuseMode.None) |
||||||
|
return; |
||||||
|
|
||||||
|
var sdType = SelectedMode switch |
||||||
|
{ |
||||||
|
LayerDiffuseMode.GenerateForegroundWithTransparencySD15 => "SD15", |
||||||
|
LayerDiffuseMode.GenerateForegroundWithTransparencySDXL => "SDXL", |
||||||
|
LayerDiffuseMode.None => throw new ArgumentOutOfRangeException(), |
||||||
|
_ => throw new ArgumentOutOfRangeException() |
||||||
|
}; |
||||||
|
|
||||||
|
// Choose config based on mode |
||||||
|
var config = SelectedMode switch |
||||||
|
{ |
||||||
|
LayerDiffuseMode.GenerateForegroundWithTransparencySD15 |
||||||
|
=> "SD15, Attention Injection, attn_sharing", |
||||||
|
LayerDiffuseMode.GenerateForegroundWithTransparencySDXL => "SDXL, Conv Injection", |
||||||
|
LayerDiffuseMode.None => throw new ArgumentOutOfRangeException(), |
||||||
|
_ => throw new ArgumentOutOfRangeException() |
||||||
|
}; |
||||||
|
|
||||||
|
foreach (var modelConnections in e.Temp.Models.Values) |
||||||
|
{ |
||||||
|
var layerDiffuseApply = e.Nodes.AddTypedNode( |
||||||
|
new ComfyNodeBuilder.LayeredDiffusionApply |
||||||
|
{ |
||||||
|
Name = e.Nodes.GetUniqueName($"LayerDiffuseApply_{modelConnections.Name}"), |
||||||
|
Model = modelConnections.Model, |
||||||
|
Config = config, |
||||||
|
Weight = Weight, |
||||||
|
} |
||||||
|
); |
||||||
|
|
||||||
|
modelConnections.Model = layerDiffuseApply.Output; |
||||||
|
} |
||||||
|
|
||||||
|
// Add pre output action |
||||||
|
e.PreOutputActions.Add(applyArgs => |
||||||
|
{ |
||||||
|
// Use last latent for decode |
||||||
|
var latent = |
||||||
|
applyArgs.Builder.Connections.LastPrimaryLatent |
||||||
|
?? throw new InvalidOperationException("Connections.LastPrimaryLatent not set"); |
||||||
|
|
||||||
|
// Convert primary to image if not already |
||||||
|
var primaryImage = applyArgs.Builder.GetPrimaryAsImage(); |
||||||
|
applyArgs.Builder.Connections.Primary = primaryImage; |
||||||
|
|
||||||
|
// Add a Layer Diffuse Decode |
||||||
|
var decode = applyArgs.Nodes.AddTypedNode( |
||||||
|
new ComfyNodeBuilder.LayeredDiffusionDecodeRgba |
||||||
|
{ |
||||||
|
Name = applyArgs.Nodes.GetUniqueName("LayerDiffuseDecode"), |
||||||
|
Samples = latent, |
||||||
|
Images = primaryImage, |
||||||
|
SdVersion = sdType |
||||||
|
} |
||||||
|
); |
||||||
|
|
||||||
|
// Set primary to decode output |
||||||
|
applyArgs.Builder.Connections.Primary = decode.Output; |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
using StabilityMatrix.Avalonia.Models.Inference; |
||||||
|
using StabilityMatrix.Avalonia.Services; |
||||||
|
using StabilityMatrix.Avalonia.ViewModels.Base; |
||||||
|
using StabilityMatrix.Core.Attributes; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Avalonia.ViewModels.Inference.Modules; |
||||||
|
|
||||||
|
[ManagedService] |
||||||
|
[Transient] |
||||||
|
public class LayerDiffuseModule : ModuleBase |
||||||
|
{ |
||||||
|
/// <inheritdoc /> |
||||||
|
public LayerDiffuseModule(ServiceManager<ViewModelBase> vmFactory) |
||||||
|
: base(vmFactory) |
||||||
|
{ |
||||||
|
Title = "Layer Diffuse"; |
||||||
|
AddCards(vmFactory.Get<LayerDiffuseCardViewModel>()); |
||||||
|
} |
||||||
|
|
||||||
|
/// <inheritdoc /> |
||||||
|
protected override void OnApplyStep(ModuleApplyStepEventArgs e) |
||||||
|
{ |
||||||
|
var card = GetCard<LayerDiffuseCardViewModel>(); |
||||||
|
card.ApplyStep(e); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
using System.ComponentModel.DataAnnotations; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Core.Models.Inference; |
||||||
|
|
||||||
|
public enum LayerDiffuseMode |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// The layer diffuse mode is not set. |
||||||
|
/// </summary> |
||||||
|
[Display(Name = "None")] |
||||||
|
None, |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Generate foreground only with transparency. SD1.5 |
||||||
|
/// </summary> |
||||||
|
[Display(Name = "(SD 1.5) Generate Foreground with Transparency")] |
||||||
|
GenerateForegroundWithTransparencySD15, |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Generate foreground only with transparency. SDXL |
||||||
|
/// </summary> |
||||||
|
[Display(Name = "(SDXL) Generate Foreground with Transparency")] |
||||||
|
GenerateForegroundWithTransparencySDXL, |
||||||
|
} |
Loading…
Reference in new issue