Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 4f25f97

Browse files
committed
Basic UI integration
1 parent bc09f68 commit 4f25f97

File tree

10 files changed

+769
-10
lines changed

10 files changed

+769
-10
lines changed

OnnxStack.UI/MainWindow.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@
8282
<views:VideoToVideoView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
8383
</TabItem>
8484

85+
<!--ControlNet-->
86+
<TabItem>
87+
<TabItem.Header>
88+
<StackPanel Orientation="Horizontal" Margin="5">
89+
<StackPanel Orientation="Horizontal">
90+
<userControls:FontAwesome Icon="&#xf1c8;" IconStyle="Light"/>
91+
<userControls:FontAwesome Icon="&#xf054;" IconStyle="Regular" Size="8" Margin="3"/>
92+
<userControls:FontAwesome Icon="&#xf1c8;" IconStyle="Light"/>
93+
</StackPanel>
94+
<TextBlock Text="ControlNet" Margin="5,0,0,0"/>
95+
</StackPanel>
96+
</TabItem.Header>
97+
<views:ControlNetView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
98+
</TabItem>
8599

86100
<!--Upscale-->
87101
<TabItem>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using OnnxStack.StableDiffusion.Config;
2+
using System.ComponentModel;
3+
using System.Runtime.CompilerServices;
4+
using System.Text.Json.Serialization;
5+
6+
namespace OnnxStack.UI.Models
7+
{
8+
public class ControlNetModelSetViewModel : INotifyPropertyChanged
9+
{
10+
private string _name;
11+
private bool _isLoaded;
12+
private bool _isLoading;
13+
14+
public string Name
15+
{
16+
get { return _name; }
17+
set { _name = value; NotifyPropertyChanged(); }
18+
}
19+
20+
[JsonIgnore]
21+
public bool IsLoaded
22+
{
23+
get { return _isLoaded; }
24+
set { _isLoaded = value; NotifyPropertyChanged(); }
25+
}
26+
27+
[JsonIgnore]
28+
public bool IsLoading
29+
{
30+
get { return _isLoading; }
31+
set { _isLoading = value; NotifyPropertyChanged(); }
32+
}
33+
34+
public ControlNetModelSet ModelSet { get; set; }
35+
36+
#region INotifyPropertyChanged
37+
public event PropertyChangedEventHandler PropertyChanged;
38+
public void NotifyPropertyChanged([CallerMemberName] string property = "")
39+
{
40+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
41+
}
42+
#endregion
43+
}
44+
}

OnnxStack.UI/Models/OnnxStackUIConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class OnnxStackUIConfig : IConfigSection
1717
public ExecutionProvider SupportedExecutionProvider => GetSupportedExecutionProvider();
1818
public ObservableCollection<UpscaleModelSetViewModel> UpscaleModelSets { get; set; } = new ObservableCollection<UpscaleModelSetViewModel>();
1919
public ObservableCollection<StableDiffusionModelSetViewModel> StableDiffusionModelSets { get; set; } = new ObservableCollection<StableDiffusionModelSetViewModel>();
20-
20+
public ObservableCollection<ControlNetModelSetViewModel> ControlNetModelSets { get; set; } = new ObservableCollection<ControlNetModelSetViewModel>();
2121

2222
public ExecutionProvider GetSupportedExecutionProvider()
2323
{

OnnxStack.UI/Models/SchedulerOptionsModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class SchedulerOptionsModel : INotifyPropertyChanged
3232
private float _maximumBeta = 0.999f;
3333
private int _originalInferenceSteps = 100;
3434
private SchedulerType _schedulerType;
35+
private float _conditioningScale = 7f;
3536
private bool _hasChanged;
3637

3738
/// <summary>
@@ -201,6 +202,13 @@ public SchedulerType SchedulerType
201202
set { _schedulerType = value; NotifyPropertyChanged(); }
202203
}
203204

205+
public float ConditioningScale
206+
{
207+
get { return _conditioningScale; }
208+
set { _conditioningScale = value; NotifyPropertyChanged(); }
209+
}
210+
211+
204212
public bool HasChanged
205213
{
206214
get { return _hasChanged; }
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<UserControl x:Class="OnnxStack.UI.UserControls.ControlNetPickerControl"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:OnnxStack.UI.UserControls"
7+
mc:Ignorable="d"
8+
d:DesignWidth="500" Name="UI">
9+
<UserControl.Resources>
10+
<Style TargetType="{x:Type Label}">
11+
<Setter Property="Margin" Value="-4,0,0,-4"/>
12+
</Style>
13+
<Storyboard x:Key="LoadingAnimation" >
14+
<DoubleAnimation Storyboard.TargetName="SpinnerIcon" Storyboard.TargetProperty="(Image.RenderTransform).(RotateTransform.Angle)" To="360" Duration="0:0:1" RepeatBehavior="Forever" />
15+
</Storyboard>
16+
</UserControl.Resources>
17+
18+
<DockPanel DataContext="{Binding ElementName=UI}">
19+
<TextBlock DockPanel.Dock="Top" Text="ControlNet"/>
20+
<DockPanel>
21+
<StackPanel DockPanel.Dock="Right" Orientation="Horizontal" Visibility="{Binding SelectedModel, Converter={StaticResource NullVisibilityConverter}}">
22+
<Button BorderThickness="0,1,1,1" ToolTip="Load Model" Width="80"
23+
Command="{Binding LoadCommand}"
24+
Visibility="{Binding SelectedModel.IsLoaded, FallbackValue=Visible, TargetNullValue=Visible, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
25+
IsEnabled="{Binding SelectedModel.IsLoading, Converter={StaticResource InverseBoolConverter}}">
26+
<StackPanel Orientation="Horizontal">
27+
<local:FontAwesome x:Name="SpinnerIcon" Icon="&#xf110;" Size="12" Margin="0,0,4,0" Width="12" Height="12"
28+
RenderTransformOrigin="0.5,0.5"
29+
Visibility="{Binding SelectedModel.IsLoading, Converter={StaticResource BooleanToVisibilityConverter}}">
30+
<local:FontAwesome.RenderTransform>
31+
<RotateTransform Angle="0" />
32+
</local:FontAwesome.RenderTransform>
33+
</local:FontAwesome>
34+
<TextBlock Text="Load"/>
35+
</StackPanel>
36+
<Button.Triggers>
37+
<EventTrigger RoutedEvent="Loaded">
38+
<BeginStoryboard x:Name="SpinnerIconAnimation" Storyboard="{StaticResource LoadingAnimation}" />
39+
</EventTrigger>
40+
<EventTrigger RoutedEvent="Unloaded">
41+
<StopStoryboard BeginStoryboardName="SpinnerIconAnimation" />
42+
</EventTrigger>
43+
</Button.Triggers>
44+
</Button>
45+
<Button BorderThickness="0,1,1,1" ToolTip="Unload Model" Width="80"
46+
Command="{Binding UnloadCommand}"
47+
Visibility="{Binding SelectedModel.IsLoaded, FallbackValue=Collapsed, TargetNullValue=Collapsed, Converter={StaticResource BooleanToVisibilityConverter}}"
48+
IsEnabled="{Binding SelectedModel.IsLoading, Converter={StaticResource InverseBoolConverter}}">
49+
<TextBlock Text="Unload"/>
50+
</Button>
51+
</StackPanel>
52+
53+
<Grid>
54+
<ComboBox
55+
DisplayMemberPath="Name"
56+
ItemsSource="{Binding UISettings.ControlNetModelSets}"
57+
SelectedItem="{Binding SelectedModel}"
58+
IsEnabled="{Binding SelectedModel.IsLoading, FallbackValue=True, TargetNullValue=True, Converter={StaticResource InverseBoolConverter}}" >
59+
</ComboBox>
60+
<TextBlock Text="-- Select Model --" Visibility="{Binding SelectedModel, Converter={StaticResource InverseNullVisibilityConverter}}" IsHitTestVisible="False" Margin="3,2" FontStyle="Italic" Opacity=".7" />
61+
</Grid>
62+
63+
</DockPanel>
64+
</DockPanel>
65+
66+
</UserControl>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Microsoft.Extensions.Logging;
2+
using OnnxStack.Core;
3+
using OnnxStack.ImageUpscaler.Services;
4+
using OnnxStack.StableDiffusion.Common;
5+
using OnnxStack.UI.Commands;
6+
using OnnxStack.UI.Models;
7+
using System;
8+
using System.ComponentModel;
9+
using System.Linq;
10+
using System.Runtime.CompilerServices;
11+
using System.Threading.Tasks;
12+
using System.Windows;
13+
using System.Windows.Controls;
14+
15+
namespace OnnxStack.UI.UserControls
16+
{
17+
/// <summary>
18+
/// Interaction logic for ControlNetPickerControl.xaml
19+
/// </summary>
20+
public partial class ControlNetPickerControl : UserControl, INotifyPropertyChanged
21+
{
22+
private readonly ILogger<ControlNetPickerControl> _logger;
23+
private readonly IStableDiffusionService _stableDiffusionService;
24+
25+
/// <summary>Initializes a new instance of the <see cref="ControlNetPickerControl" /> class.</summary>
26+
public ControlNetPickerControl()
27+
{
28+
if (!DesignerProperties.GetIsInDesignMode(this))
29+
{
30+
_logger = App.GetService<ILogger<ControlNetPickerControl>>();
31+
_stableDiffusionService = App.GetService<IStableDiffusionService>();
32+
}
33+
34+
LoadCommand = new AsyncRelayCommand(LoadModel);
35+
UnloadCommand = new AsyncRelayCommand(UnloadModel);
36+
InitializeComponent();
37+
}
38+
39+
public AsyncRelayCommand LoadCommand { get; set; }
40+
public AsyncRelayCommand UnloadCommand { get; set; }
41+
42+
public OnnxStackUIConfig UISettings
43+
{
44+
get { return (OnnxStackUIConfig)GetValue(UISettingsProperty); }
45+
set { SetValue(UISettingsProperty, value); }
46+
}
47+
public static readonly DependencyProperty UISettingsProperty =
48+
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(ControlNetPickerControl));
49+
50+
51+
/// <summary>
52+
/// Gets or sets the selected model.
53+
/// </summary>
54+
public ControlNetModelSetViewModel SelectedModel
55+
{
56+
get { return (ControlNetModelSetViewModel)GetValue(SelectedModelProperty); }
57+
set { SetValue(SelectedModelProperty, value); }
58+
}
59+
public static readonly DependencyProperty SelectedModelProperty =
60+
DependencyProperty.Register("SelectedModel", typeof(ControlNetModelSetViewModel), typeof(ControlNetPickerControl));
61+
62+
63+
64+
/// <summary>
65+
/// Loads the model.
66+
/// </summary>
67+
private async Task LoadModel()
68+
{
69+
if (_stableDiffusionService.IsModelLoaded(SelectedModel.ModelSet))
70+
return;
71+
72+
var elapsed = _logger.LogBegin($"'{SelectedModel.Name}' Loading...");
73+
SelectedModel.IsLoaded = false;
74+
SelectedModel.IsLoading = true;
75+
76+
try
77+
{
78+
foreach (var model in UISettings.ControlNetModelSets.Where(x => x.IsLoaded))
79+
{
80+
_logger.LogInformation($"'{model.Name}' Unloading...");
81+
await _stableDiffusionService.UnloadModelAsync(model.ModelSet);
82+
model.IsLoaded = false;
83+
}
84+
SelectedModel.IsLoaded = await _stableDiffusionService.LoadModelAsync(SelectedModel.ModelSet);
85+
}
86+
catch (Exception ex)
87+
{
88+
_logger.LogError($"An error occured while loading model '{SelectedModel.Name}' \n {ex}");
89+
}
90+
91+
SelectedModel.IsLoading = false;
92+
_logger.LogEnd($"'{SelectedModel.Name}' Loaded.", elapsed);
93+
}
94+
95+
96+
/// <summary>
97+
/// Unloads the model.
98+
/// </summary>
99+
private async Task UnloadModel()
100+
{
101+
if (!_stableDiffusionService.IsModelLoaded(SelectedModel.ModelSet))
102+
return;
103+
104+
_logger.LogInformation($"'{SelectedModel.Name}' Unloading...");
105+
SelectedModel.IsLoading = true;
106+
await _stableDiffusionService.UnloadModelAsync(SelectedModel.ModelSet);
107+
SelectedModel.IsLoading = false;
108+
SelectedModel.IsLoaded = false;
109+
_logger.LogInformation($"'{SelectedModel.Name}' Unloaded.");
110+
}
111+
112+
#region INotifyPropertyChanged
113+
public event PropertyChangedEventHandler PropertyChanged;
114+
public void NotifyPropertyChanged([CallerMemberName] string property = "")
115+
{
116+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
117+
}
118+
#endregion
119+
}
120+
}

OnnxStack.UI/UserControls/SchedulerControl.xaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
</i:Interaction.Behaviors>
111111
</Slider>
112112
<StackPanel.Style>
113+
113114
<Style TargetType="{x:Type StackPanel}">
114115
<Style.Triggers>
115116
<DataTrigger Binding="{Binding Path=DiffuserType}" Value="TextToImage">
@@ -118,32 +119,36 @@
118119
<DataTrigger Binding="{Binding Path=DiffuserType}" Value="ImageInpaint">
119120
<Setter Property="Visibility" Value="Collapsed" />
120121
</DataTrigger>
122+
<DataTrigger Binding="{Binding Path=DiffuserType}" Value="ControlNet">
123+
<Setter Property="Visibility" Value="Collapsed" />
124+
</DataTrigger>
121125
</Style.Triggers>
122126
</Style>
123127
</StackPanel.Style>
124128
</StackPanel>
125129

126130

127-
<!--<StackPanel Margin="5,0,0,0">
131+
<StackPanel Margin="5,0,0,0">
128132
<DockPanel>
129-
<Label>Initial Noise</Label>
130-
<TextBlock Text="{Binding ElementName=SliderInitialNoiseLevel, Path=Value, StringFormat={}{0:F2}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
133+
<Label>Conditioning Scale</Label>
134+
<TextBlock Text="{Binding ElementName=SliderInitialConditioningScale, Path=Value, StringFormat={}{0:F2}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="10" Margin="0,0,6,0" FontWeight="Medium" />
131135
</DockPanel>
132-
<Slider Name="SliderInitialNoiseLevel" Value="{Binding SchedulerOptions.InitialNoiseLevel}" Minimum="-1" Maximum="1" TickFrequency="0.1" IsSnapToTickEnabled="true" SmallChange="0.1" LargeChange="0.1">
136+
<Slider Name="SliderInitialConditioningScale" Value="{Binding SchedulerOptions.ConditioningScale}" Minimum="0" Maximum="1" TickFrequency="0.01" IsSnapToTickEnabled="true" SmallChange="0.01" LargeChange="0.01">
133137
<i:Interaction.Behaviors>
134138
<behaviors:SliderMouseWheelBehavior />
135139
</i:Interaction.Behaviors>
136140
</Slider>
137141
<StackPanel.Style>
138142
<Style TargetType="{x:Type StackPanel}">
143+
<Setter Property="Visibility" Value="Collapsed" />
139144
<Style.Triggers>
140-
<DataTrigger Binding="{Binding Path=DiffuserType}" Value="TextToImage">
141-
<Setter Property="Visibility" Value="Collapsed" />
145+
<DataTrigger Binding="{Binding Path=DiffuserType}" Value="ControlNet">
146+
<Setter Property="Visibility" Value="Visible" />
142147
</DataTrigger>
143148
</Style.Triggers>
144149
</Style>
145150
</StackPanel.Style>
146-
</StackPanel>-->
151+
</StackPanel>
147152

148153
</UniformGrid>
149154

OnnxStack.UI/Utils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public static SchedulerOptions ToSchedulerOptions(this SchedulerOptionsModel mod
102102
UseKarrasSigmas = model.UseKarrasSigmas,
103103
VarianceType = model.VarianceType,
104104
OriginalInferenceSteps = model.OriginalInferenceSteps,
105-
SchedulerType = model.SchedulerType
105+
SchedulerType = model.SchedulerType,
106+
ConditioningScale = model.ConditioningScale,
106107
};
107108
}
108109

@@ -133,7 +134,8 @@ public static SchedulerOptionsModel ToSchedulerOptionsModel(this SchedulerOption
133134
UseKarrasSigmas = model.UseKarrasSigmas,
134135
VarianceType = model.VarianceType,
135136
OriginalInferenceSteps = model.OriginalInferenceSteps,
136-
SchedulerType = model.SchedulerType
137+
SchedulerType = model.SchedulerType,
138+
ConditioningScale = model.ConditioningScale
137139
};
138140
}
139141

0 commit comments

Comments
 (0)