forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWanSamplerCardViewModel.cs
More file actions
139 lines (126 loc) · 5.15 KB
/
WanSamplerCardViewModel.cs
File metadata and controls
139 lines (126 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Injectio.Attributes;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Models.Inference;
using StabilityMatrix.Avalonia.Services;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.ViewModels.Inference.Modules;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Models.Api.Comfy;
using StabilityMatrix.Core.Models.Api.Comfy.Nodes;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Avalonia.ViewModels.Inference;
[View(typeof(SamplerCard))]
[ManagedService]
[RegisterTransient<WanSamplerCardViewModel>]
public class WanSamplerCardViewModel : SamplerCardViewModel
{
public WanSamplerCardViewModel(
IInferenceClientManager clientManager,
IServiceManager<ViewModelBase> vmFactory,
ISettingsManager settingsManager,
TabContext tabContext
)
: base(clientManager, vmFactory, settingsManager, tabContext)
{
EnableAddons = false;
IsLengthEnabled = true;
SelectedSampler = ComfySampler.UniPC;
SelectedScheduler = ComfyScheduler.Simple;
Length = 33;
}
public override void ApplyStep(ModuleApplyStepEventArgs e)
{
if (EnableAddons)
{
foreach (var module in ModulesCardViewModel.Cards.OfType<ModuleBase>())
{
module.ApplyStep(e);
}
}
// Set primary sampler and scheduler
var primarySampler = SelectedSampler ?? throw new ValidationException("Sampler not selected");
e.Builder.Connections.PrimarySampler = primarySampler;
var primaryScheduler = SelectedScheduler ?? throw new ValidationException("Scheduler not selected");
e.Builder.Connections.PrimaryScheduler = primaryScheduler;
// for later inheritance if needed
e.Builder.Connections.PrimaryCfg = CfgScale;
e.Builder.Connections.PrimarySteps = Steps;
e.Builder.Connections.PrimaryModelType = SelectedModelType;
e.Temp = e.CreateTempFromBuilder();
var conditioning = e.Temp.Base.Conditioning.Unwrap();
var isImgToVid = IsDenoiseStrengthEnabled;
if (isImgToVid)
{
var clipVisionEncode = e.Nodes.AddTypedNode(
new ComfyNodeBuilder.CLIPVisionEncode
{
Name = e.Nodes.GetUniqueName(nameof(ComfyNodeBuilder.CLIPVisionEncode)),
ClipVision =
e.Builder.Connections.BaseClipVision
?? throw new ValidationException("BaseClipVision not set"),
Image = e.Builder.GetPrimaryAsImage(),
Crop = "none",
}
);
var wanImageToVideo = e.Nodes.AddTypedNode(
new ComfyNodeBuilder.WanImageToVideo
{
Name = e.Nodes.GetUniqueName(nameof(ComfyNodeBuilder.WanImageToVideo)),
Positive = conditioning.Positive,
Negative = conditioning.Negative,
Vae = e.Builder.Connections.GetDefaultVAE(),
ClipVisionOutput = clipVisionEncode.Output,
StartImage = e.Builder.GetPrimaryAsImage(),
Width = Width,
Height = Height,
Length = Length,
BatchSize = e.Builder.Connections.BatchSize,
}
);
var kSampler = e.Nodes.AddTypedNode(
new ComfyNodeBuilder.KSampler
{
Name = "Sampler",
Model = e.Temp.Base.Model!.Unwrap(),
Seed = e.Builder.Connections.Seed,
SamplerName = primarySampler.Name,
Scheduler = primaryScheduler.Name,
Steps = Steps,
Cfg = CfgScale,
Positive = wanImageToVideo.Output1,
Negative = wanImageToVideo.Output2,
LatentImage = wanImageToVideo.Output3,
Denoise = DenoiseStrength,
}
);
e.Builder.Connections.Primary = kSampler.Output;
}
else
{
var primaryLatent = e.Builder.GetPrimaryAsLatent(
e.Temp.Primary!.Unwrap(),
e.Builder.Connections.GetDefaultVAE()
);
var kSampler = e.Nodes.AddTypedNode(
new ComfyNodeBuilder.KSampler
{
Name = "Sampler",
Model = e.Temp.Base.Model!.Unwrap(),
Seed = e.Builder.Connections.Seed,
SamplerName = primarySampler.Name,
Scheduler = primaryScheduler.Name,
Steps = Steps,
Cfg = CfgScale,
Positive = conditioning.Positive,
Negative = conditioning.Negative,
LatentImage = primaryLatent,
Denoise = DenoiseStrength,
}
);
e.Builder.Connections.Primary = kSampler.Output;
}
}
}