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

Commit e14d29f

Browse files
committed
ControlNet UI model confinguration
1 parent fed0a58 commit e14d29f

17 files changed

+776
-72
lines changed

OnnxStack.UI/App.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
</ObjectDataProvider.MethodParameters>
107107
</ObjectDataProvider>
108108

109+
<ObjectDataProvider x:Key="ControlNetType" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
110+
<ObjectDataProvider.MethodParameters>
111+
<x:Type TypeName="SD_Enums:ControlNetType"/>
112+
</ObjectDataProvider.MethodParameters>
113+
</ObjectDataProvider>
114+
109115

110116
<!--Crop Image Dialog-->
111117
<SolidColorBrush x:Key="CropFrameBrush" Color="#FF3296FA" />

OnnxStack.UI/App.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public App()
3939
builder.Services.AddTransient<UpdateModelDialog>();
4040
builder.Services.AddTransient<AddUpscaleModelDialog>();
4141
builder.Services.AddTransient<UpdateUpscaleModelDialog>();
42+
builder.Services.AddTransient<AddControlNetModelDialog>();
43+
builder.Services.AddTransient<UpdateControlNetModelDialog>();
44+
4245
builder.Services.AddSingleton<IModelFactory, ModelFactory>();
4346
builder.Services.AddSingleton<IDialogService, DialogService>();
4447
builder.Services.AddSingleton<IDeviceService, DeviceService>();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<Window x:Class="OnnxStack.UI.Dialogs.AddControlNetModelDialog"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:OnnxStack.UI.Dialogs"
7+
xmlns:models="clr-namespace:OnnxStack.UI.Models"
8+
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
9+
mc:Ignorable="d"
10+
Name="UI"
11+
Icon="/Images/Icon.png"
12+
MinWidth="500"
13+
SizeToContent="WidthAndHeight"
14+
WindowStartupLocation="CenterOwner"
15+
SnapsToDevicePixels="True"
16+
UseLayoutRounding="True"
17+
Title="Add ControlNet Model">
18+
<DockPanel DataContext="{Binding ElementName=UI}" Margin="15, 15, 15, 10">
19+
<StackPanel DockPanel.Dock="Top">
20+
21+
<StackPanel>
22+
<TextBlock Text="Model Type"/>
23+
<ComboBox ItemsSource="{Binding Source={StaticResource ControlNetType}}" SelectedItem="{Binding SelectedControlNetType}" />
24+
</StackPanel>
25+
26+
27+
<StackPanel Margin="0,10,0,0">
28+
<TextBlock Text="Control Model File"/>
29+
<userControls:FilePickerTextBox FileName="{Binding ModelFile, Mode=TwoWay}" IsFolderPicker="False" Filter="Onnx Models (*.onnx)|*.onnx" DefaultExt="onnx" />
30+
</StackPanel>
31+
32+
<StackPanel Margin="0,10,0,0">
33+
<TextBlock Text="Annotation Model File"/>
34+
<userControls:FilePickerTextBox FileName="{Binding AnnotationModelFile, Mode=TwoWay}" IsFolderPicker="False" Filter="Onnx Models (*.onnx)|*.onnx" DefaultExt="onnx" />
35+
</StackPanel>
36+
37+
<StackPanel Margin="0,10,0,0">
38+
<TextBlock Text="Model Name"/>
39+
<TextBox Text="{Binding ModelName, UpdateSourceTrigger=PropertyChanged}" />
40+
</StackPanel>
41+
42+
<StackPanel Margin="5">
43+
44+
45+
<ItemsControl ItemsSource="{Binding ValidationResults}">
46+
<ItemsControl.ItemTemplate>
47+
<DataTemplate DataType="{x:Type models:ValidationResult}">
48+
<StackPanel Orientation="Horizontal">
49+
<userControls:FontAwesome >
50+
<userControls:FontAwesome.Style>
51+
<Style TargetType="{x:Type userControls:FontAwesome}">
52+
<Setter Property="Color" Value="Red" />
53+
<Setter Property="Icon" Value="&#xf00d;" />
54+
<Style.Triggers>
55+
<DataTrigger Binding="{Binding IsValid}" Value="True">
56+
<Setter Property="Color" Value="LimeGreen" />
57+
<Setter Property="Icon" Value="&#xf00c;" />
58+
</DataTrigger>
59+
</Style.Triggers>
60+
</Style>
61+
</userControls:FontAwesome.Style>
62+
</userControls:FontAwesome>
63+
<TextBlock Text="{Binding Name}" Margin="5,0,0,3" />
64+
</StackPanel>
65+
</DataTemplate>
66+
</ItemsControl.ItemTemplate>
67+
</ItemsControl>
68+
69+
</StackPanel>
70+
71+
72+
</StackPanel>
73+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,20,0,0">
74+
<UniformGrid Columns="2" Height="30">
75+
<Button Content="Save" Command="{Binding SaveCommand}" IsDefault="True"/>
76+
<Button Content="Cancel" Command="{Binding CancelCommand}" Width="100"/>
77+
</UniformGrid>
78+
</StackPanel>
79+
</DockPanel>
80+
</Window>
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using Microsoft.Extensions.Logging;
2+
using OnnxStack.StableDiffusion.Config;
3+
using OnnxStack.StableDiffusion.Enums;
4+
using OnnxStack.UI.Commands;
5+
using OnnxStack.UI.Models;
6+
using OnnxStack.UI.Services;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Collections.ObjectModel;
10+
using System.ComponentModel;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Runtime.CompilerServices;
14+
using System.Threading.Tasks;
15+
using System.Windows;
16+
17+
namespace OnnxStack.UI.Dialogs
18+
{
19+
/// <summary>
20+
/// Interaction logic for AddControlNetModelDialog.xaml
21+
/// </summary>
22+
public partial class AddControlNetModelDialog : Window, INotifyPropertyChanged
23+
{
24+
private readonly ILogger<AddControlNetModelDialog> _logger;
25+
26+
private readonly List<string> _invalidOptions;
27+
private string _modelName;
28+
private string _modelFile;
29+
private string _annotationModelFile;
30+
private ControlNetType _selectedControlNetType;
31+
private IModelFactory _modelFactory;
32+
private OnnxStackUIConfig _settings;
33+
private ControlNetModelSet _modelSetResult;
34+
35+
public AddControlNetModelDialog(OnnxStackUIConfig settings, IModelFactory modelFactory, ILogger<AddControlNetModelDialog> logger)
36+
{
37+
_logger = logger;
38+
_settings = settings;
39+
_modelFactory = modelFactory;
40+
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
41+
CancelCommand = new AsyncRelayCommand(Cancel);
42+
_invalidOptions = _settings.GetModelNames();
43+
InitializeComponent();
44+
SelectedControlNetType = ControlNetType.Canny;
45+
}
46+
47+
public AsyncRelayCommand SaveCommand { get; }
48+
public AsyncRelayCommand CancelCommand { get; }
49+
public ObservableCollection<ValidationResult> ValidationResults { get; set; } = new ObservableCollection<ValidationResult>();
50+
51+
public string ModelName
52+
{
53+
get { return _modelName; }
54+
set { _modelName = value; _modelName?.Trim(); NotifyPropertyChanged(); CreateModelSet(); }
55+
}
56+
57+
public string ModelFile
58+
{
59+
get { return _modelFile; }
60+
set
61+
{
62+
_modelFile = value;
63+
_modelName = string.IsNullOrEmpty(_modelFile)
64+
? string.Empty
65+
: Path.GetFileNameWithoutExtension(_modelFile);
66+
67+
NotifyPropertyChanged();
68+
NotifyPropertyChanged(nameof(ModelName));
69+
CreateModelSet();
70+
}
71+
}
72+
73+
public string AnnotationModelFile
74+
{
75+
get { return _annotationModelFile; }
76+
set { _annotationModelFile = value; NotifyPropertyChanged(); CreateModelSet(); }
77+
}
78+
79+
public ControlNetType SelectedControlNetType
80+
{
81+
get { return _selectedControlNetType; }
82+
set { _selectedControlNetType = value; NotifyPropertyChanged(); CreateModelSet(); }
83+
84+
}
85+
86+
public ControlNetModelSet ModelSetResult
87+
{
88+
get { return _modelSetResult; }
89+
}
90+
91+
92+
public new bool ShowDialog()
93+
{
94+
return base.ShowDialog() ?? false;
95+
}
96+
97+
98+
private void CreateModelSet()
99+
{
100+
_modelSetResult = null;
101+
ValidationResults.Clear();
102+
if (string.IsNullOrEmpty(_modelFile))
103+
return;
104+
105+
_modelSetResult = _modelFactory.CreateControlNetModelSet(ModelName.Trim(), _selectedControlNetType, _modelFile, _annotationModelFile);
106+
107+
// Validate
108+
ValidationResults.Add(new ValidationResult("Name", !_invalidOptions.Contains(_modelName, StringComparer.OrdinalIgnoreCase) && _modelName.Length > 2 && _modelName.Length < 50));
109+
foreach (var validationResult in _modelSetResult.ModelConfigurations.Select(x => new ValidationResult(x.Type.ToString(), File.Exists(x.OnnxModelPath))))
110+
{
111+
ValidationResults.Add(validationResult);
112+
}
113+
}
114+
115+
116+
private Task Save()
117+
{
118+
DialogResult = true;
119+
return Task.CompletedTask;
120+
}
121+
122+
123+
private bool CanExecuteSave()
124+
{
125+
if (string.IsNullOrEmpty(_modelFile))
126+
return false;
127+
if (_modelSetResult is null)
128+
return false;
129+
130+
return ValidationResults.Count > 0 && ValidationResults.All(x => x.IsValid);
131+
}
132+
133+
134+
private Task Cancel()
135+
{
136+
_modelSetResult = null;
137+
DialogResult = false;
138+
return Task.CompletedTask;
139+
}
140+
141+
#region INotifyPropertyChanged
142+
public event PropertyChangedEventHandler PropertyChanged;
143+
public void NotifyPropertyChanged([CallerMemberName] string property = "")
144+
{
145+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
146+
}
147+
#endregion
148+
}
149+
}

OnnxStack.UI/Dialogs/AddModelDialog.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AddModelDialog(OnnxStackUIConfig settings, IModelFactory modelFactory, IL
3838
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
3939
CancelCommand = new AsyncRelayCommand(Cancel);
4040
ModelTemplates = new List<StableDiffusionModelTemplate>( _modelFactory.GetStableDiffusionModelTemplates());
41-
InvalidOptions = _settings.StableDiffusionModelSets.Select(x => x.Name.ToLower()).ToList();
41+
InvalidOptions = _settings.GetModelNames();
4242
InitializeComponent();
4343
}
4444

OnnxStack.UI/Dialogs/AddUpscaleModelDialog.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public AddUpscaleModelDialog(OnnxStackUIConfig settings, IModelFactory modelFact
3737
SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave);
3838
CancelCommand = new AsyncRelayCommand(Cancel);
3939
ModelTemplates = new List<UpscaleModelTemplate>(_modelFactory.GetUpscaleModelTemplates());
40-
InvalidOptions = _settings.UpscaleModelSets.Select(x => x.Name.ToLower()).ToList();
40+
InvalidOptions = _settings.GetModelNames();
4141
InitializeComponent();
4242
}
4343

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<Window x:Class="OnnxStack.UI.Dialogs.UpdateControlNetModelDialog"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:OnnxStack.UI.Dialogs"
7+
xmlns:models="clr-namespace:OnnxStack.UI.Models"
8+
xmlns:userControls="clr-namespace:OnnxStack.UI.UserControls"
9+
mc:Ignorable="d"
10+
Name="UI"
11+
Icon="/Images/Icon.png"
12+
MinWidth="450"
13+
MaxWidth="450"
14+
SizeToContent="Height"
15+
WindowStartupLocation="CenterOwner"
16+
SnapsToDevicePixels="True"
17+
UseLayoutRounding="True"
18+
Title="Update ControlNet Model">
19+
<DockPanel DataContext="{Binding ElementName=UI}" Margin="15, 15, 15, 10">
20+
<StackPanel DockPanel.Dock="Top">
21+
22+
<Border BorderBrush="Red" BorderThickness="1" CornerRadius="4">
23+
<StackPanel Orientation="Horizontal" Margin="30, 5" HorizontalAlignment="Center">
24+
<userControls:FontAwesome Color="Red" Icon="&#xf00d;" />
25+
<TextBlock Text="{Binding ValidationError}" Margin="5,0,0,3" />
26+
</StackPanel>
27+
<Border.Style>
28+
<Style TargetType="{x:Type Border}">
29+
<Setter Property="Visibility" Value="Visible" />
30+
<Style.Triggers>
31+
<DataTrigger Binding="{Binding ValidationError}" Value="{x:Null}">
32+
<Setter Property="Visibility" Value="Collapsed" />
33+
</DataTrigger>
34+
</Style.Triggers>
35+
</Style>
36+
</Border.Style>
37+
</Border>
38+
39+
40+
<GroupBox Header="Settings" Margin="5,0" >
41+
<StackPanel Margin="5" >
42+
43+
<StackPanel Margin="0,5,0,0">
44+
<TextBlock Text="Model Name"/>
45+
<TextBox Text="{Binding UpdateModelSet.Name, UpdateSourceTrigger=PropertyChanged}" />
46+
</StackPanel>
47+
48+
<StackPanel Margin="0,10,0,0">
49+
<TextBlock Text="ControlNet Model File"/>
50+
<userControls:FilePickerTextBox FileName="{Binding UpdateModelSet.ModelFiles[0].OnnxModelPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Filter="Onnx files (*.onnx)|*.onnx" DefaultExt="onnx" />
51+
</StackPanel>
52+
53+
<StackPanel Margin="0,10,0,0">
54+
<TextBlock Text="Annotation Model File"/>
55+
<userControls:FilePickerTextBox FileName="{Binding UpdateModelSet.ModelFiles[1].OnnxModelPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Filter="Onnx files (*.onnx)|*.onnx" DefaultExt="onnx" />
56+
</StackPanel>
57+
58+
</StackPanel>
59+
</GroupBox>
60+
61+
62+
63+
<GroupBox Header="Performance" Margin="5,10,5,0" >
64+
<StackPanel Margin="5">
65+
<StackPanel Margin="0,0,4,0">
66+
<TextBlock Text="Device" />
67+
<userControls:DevicePickerControl
68+
UISettings="{Binding UISettings}"
69+
DeviceId="{Binding UpdateModelSet.DeviceId, Mode=TwoWay}"
70+
ExecutionProvider="{Binding UpdateModelSet.ExecutionProvider, Mode=TwoWay}" />
71+
</StackPanel>
72+
<StackPanel Margin="0,0,4,0">
73+
<TextBlock Text="Mode" />
74+
<ComboBox ItemsSource="{Binding Source={StaticResource ExecutionModeType}}" SelectedItem="{Binding UpdateModelSet.ExecutionMode}" />
75+
</StackPanel>
76+
<UniformGrid Columns="2" >
77+
<StackPanel Margin="0,0,4,0">
78+
<DockPanel>
79+
<TextBlock DockPanel.Dock="Left" Text="InterOp Threads" />
80+
<TextBlock Text="(0=auto)" FontSize="9" FontStyle="Italic" Margin="6,0,6,0" Opacity="0.5" HorizontalAlignment="Right" VerticalAlignment="Center"/>
81+
</DockPanel>
82+
<TextBox Text="{Binding UpdateModelSet.InterOpNumThreads}" />
83+
</StackPanel>
84+
<StackPanel>
85+
<DockPanel>
86+
<TextBlock DockPanel.Dock="Left" Text="IntraOp Threads" />
87+
<TextBlock Text="(0=auto)" FontSize="9" FontStyle="Italic" Margin="6,0,6,0" Opacity="0.5" HorizontalAlignment="Right" VerticalAlignment="Center"/>
88+
</DockPanel>
89+
<TextBox Text="{Binding UpdateModelSet.IntraOpNumThreads}" />
90+
</StackPanel>
91+
</UniformGrid>
92+
</StackPanel>
93+
</GroupBox>
94+
95+
</StackPanel>
96+
97+
<DockPanel DockPanel.Dock="Bottom" Margin="0,20,0,0" >
98+
<UniformGrid DockPanel.Dock="Right" Columns="2" Height="30">
99+
<Button Content="Save" Command="{Binding SaveCommand}" IsDefault="True"/>
100+
<Button Content="Cancel" Command="{Binding CancelCommand}" Width="100"/>
101+
</UniformGrid>
102+
103+
<TextBlock />
104+
</DockPanel>
105+
106+
</DockPanel>
107+
</Window>

0 commit comments

Comments
 (0)