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

Commit f29bfa4

Browse files
committed
Add some basic UI settings
1 parent 383f5c5 commit f29bfa4

18 files changed

+300
-28
lines changed

OnnxStack.UI/App.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
</ObjectDataProvider>
9393

9494

95+
<ObjectDataProvider x:Key="ModelCacheMode" MethodName="GetValues" ObjectType="{x:Type system:Enum}">
96+
<ObjectDataProvider.MethodParameters>
97+
<x:Type TypeName="models:ModelCacheMode"/>
98+
</ObjectDataProvider.MethodParameters>
99+
</ObjectDataProvider>
95100

96101

97102

OnnxStack.UI/App.xaml.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Configuration;
7-
using System.Data;
8-
using System.Linq;
9-
using System.Threading.Tasks;
10-
using System.Windows;
114
using OnnxStack.Core;
12-
using OnnxStack.UI.Services;
135
using OnnxStack.UI.Dialogs;
14-
using System.Diagnostics;
15-
using System.Windows.Controls;
6+
using OnnxStack.UI.Models;
7+
using OnnxStack.UI.Services;
8+
using System;
9+
using System.Windows;
1610
using System.Windows.Threading;
17-
using OnnxStack.UI.Views;
18-
using System.IO;
1911

2012
namespace OnnxStack.UI
2113
{

OnnxStack.UI/MainWindow.xaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<TextBlock Text="Text To Image" Margin="5,0,0,0"/>
3737
</StackPanel>
3838
</TabItem.Header>
39-
<views:TextToImageView Margin="0,6,0,0"/>
39+
<views:TextToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
4040
</TabItem>
4141

4242
<!--Image To Image-->
@@ -51,7 +51,7 @@
5151
<TextBlock Text="Image To Image" Margin="5,0,0,0"/>
5252
</StackPanel>
5353
</TabItem.Header>
54-
<views:ImageToImage Margin="0,6,0,0"/>
54+
<views:ImageToImage UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
5555
</TabItem>
5656

5757
<!--Image Inpaint-->
@@ -66,7 +66,7 @@
6666
<TextBlock Text="Image Inpaint" Margin="5,0,0,0"/>
6767
</StackPanel>
6868
</TabItem.Header>
69-
<views:ImageInpaint Margin="0,6,0,0"/>
69+
<views:ImageInpaint UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
7070
</TabItem>
7171

7272
<!--Log Window-->
@@ -78,7 +78,7 @@
7878
</StackPanel>
7979
</StackPanel>
8080
</TabItem.Header>
81-
<views:Logger LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
81+
<views:Logger UISettings="{Binding UISettings}" LogOutput="{Binding OutputLog, Mode=TwoWay}" Margin="0,6,0,0"/>
8282
</TabItem>
8383

8484
<!--Settings Window-->
@@ -90,7 +90,7 @@
9090
</StackPanel>
9191
</StackPanel>
9292
</TabItem.Header>
93-
<views:Settings Margin="0,6,0,0"/>
93+
<views:Settings UISettings="{Binding UISettings, Mode=TwoWay}" Margin="0,6,0,0"/>
9494
</TabItem>
9595

9696

@@ -104,7 +104,7 @@
104104
<TextBlock Text="Models" Margin="5,0,0,0"/>
105105
</StackPanel>
106106
</TabItem.Header>
107-
<views:ModelView ModelOptions="{Binding Models, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Margin="0,6,0,0"/>
107+
<views:ModelView UISettings="{Binding UISettings}" ModelOptions="{Binding Models, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Margin="0,6,0,0"/>
108108
</TabItem>
109109
</TabControl>
110110
</Grid>

OnnxStack.UI/MainWindow.xaml.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public partial class MainWindow : Window, INotifyPropertyChanged
2828
private readonly ILogger<MainWindow> _logger;
2929
private ObservableCollection<ModelOptionsModel> _models;
3030

31-
public MainWindow(StableDiffusionConfig configuration, ILogger<MainWindow> logger)
31+
public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSettings, ILogger<MainWindow> logger)
3232
{
3333
_logger = logger;
34+
UISettings = uiSettings;
3435
SaveImageCommand = new AsyncRelayCommand<ImageResult>(SaveImageFile);
3536
SaveBlueprintCommand = new AsyncRelayCommand<ImageResult>(SaveBlueprintFile);
3637
NavigateTextToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateTextToImage);
@@ -50,14 +51,21 @@ public MainWindow(StableDiffusionConfig configuration, ILogger<MainWindow> logge
5051
public AsyncRelayCommand WindowRestoreCommand { get; }
5152
public AsyncRelayCommand WindowMaximizeCommand { get; }
5253
public AsyncRelayCommand WindowCloseCommand { get; }
53-
5454
public AsyncRelayCommand<ImageResult> SaveImageCommand { get; }
5555
public AsyncRelayCommand<ImageResult> SaveBlueprintCommand { get; }
5656
public AsyncRelayCommand<ImageResult> NavigateTextToImageCommand { get; }
5757
public AsyncRelayCommand<ImageResult> NavigateImageToImageCommand { get; }
5858
public AsyncRelayCommand<ImageResult> NavigateImageInpaintCommand { get; }
5959
public AsyncRelayCommand<ImageResult> NavigateImageUpscaleCommand { get; }
6060

61+
public OnnxStackUIConfig UISettings
62+
{
63+
get { return (OnnxStackUIConfig)GetValue(UISettingsProperty); }
64+
set { SetValue(UISettingsProperty, value); }
65+
}
66+
public static readonly DependencyProperty UISettingsProperty =
67+
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(MainWindow));
68+
6169
public ObservableCollection<ModelOptionsModel> Models
6270
{
6371
get { return _models; }
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
using OnnxStack.Common.Config;
1+
using Microsoft.ML.OnnxRuntime;
2+
using OnnxStack.Common.Config;
3+
using OnnxStack.Core.Config;
4+
using OnnxStack.UI.Views;
25
using System.Collections.Generic;
6+
using System.Text.Json.Serialization;
37

4-
namespace OnnxStack.UI.Views
8+
namespace OnnxStack.UI.Models
59
{
610
public class OnnxStackUIConfig : IConfigSection
711
{
12+
public ModelCacheMode ModelCacheMode { get; set; }
13+
14+
public bool ImageAutoSave { get; set; }
15+
public bool ImageAutoSaveBlueprint { get; set; }
16+
public string ImageAutoSaveDirectory { get; set; }
17+
18+
19+
public int DefaultDeviceId { get; set; }
20+
public int DefaultInterOpNumThreads { get; set; }
21+
public int DefaultIntraOpNumThreads { get; set; }
22+
public ExecutionMode DefaultExecutionMode { get; set; }
23+
public ExecutionProvider DefaultExecutionProvider { get; set; }
24+
25+
826
public List<ModelConfigTemplate> ModelTemplates { get; set; } = new List<ModelConfigTemplate>();
927

1028
public void Initialize()
1129
{
1230
}
1331
}
32+
33+
public enum ModelCacheMode
34+
{
35+
Single = 0,
36+
Multiple = 1
37+
}
1438
}

OnnxStack.UI/UserControls/ModelPickerControl.xaml.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using OnnxStack.StableDiffusion.Common;
55
using OnnxStack.StableDiffusion.Enums;
66
using OnnxStack.UI.Commands;
7+
using OnnxStack.UI.Models;
78
using System;
89
using System.Collections.Generic;
910
using System.Collections.ObjectModel;
@@ -54,6 +55,16 @@ public ObservableCollection<ModelOptionsModel> Models
5455
DependencyProperty.Register("Models", typeof(ObservableCollection<ModelOptionsModel>), typeof(ModelPickerControl), new PropertyMetadata(propertyChangedCallback: OnModelsChanged));
5556

5657

58+
public OnnxStackUIConfig UISettings
59+
{
60+
get { return (OnnxStackUIConfig)GetValue(UISettingsProperty); }
61+
set { SetValue(UISettingsProperty, value); }
62+
}
63+
public static readonly DependencyProperty UISettingsProperty =
64+
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(ModelPickerControl));
65+
66+
67+
5768
/// <summary>
5869
/// Gets or sets the supported diffusers.
5970
/// </summary>
@@ -93,6 +104,15 @@ private async Task LoadModel()
93104

94105
try
95106
{
107+
if (UISettings.ModelCacheMode == ModelCacheMode.Single)
108+
{
109+
foreach (var model in Models.Where(x => x.IsLoaded))
110+
{
111+
_logger.LogInformation($"'{model.Name}' Unloading...");
112+
await _stableDiffusionService.UnloadModel(model.ModelOptions);
113+
}
114+
}
115+
96116
SelectedModel.IsLoaded = await _stableDiffusionService.LoadModel(SelectedModel.ModelOptions);
97117
}
98118
catch (Exception ex)

OnnxStack.UI/Utils.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using OnnxStack.UI.Models;
33
using System;
44
using System.IO;
5-
using System.Runtime.CompilerServices;
65
using System.Text.Json;
76
using System.Text.Json.Serialization;
87
using System.Threading.Tasks;
@@ -75,6 +74,25 @@ public static async Task<bool> SaveBlueprintFile(this ImageResult imageResult, s
7574
}
7675
}
7776

77+
78+
public static async Task<bool> AutoSave(this ImageResult imageResult, string autosaveDirectory, bool includeBlueprint)
79+
{
80+
if (!Directory.Exists(autosaveDirectory))
81+
Directory.CreateDirectory(autosaveDirectory);
82+
83+
var random = RandomString();
84+
var imageFile = Path.Combine(autosaveDirectory, $"image-{imageResult.SchedulerOptions.Seed}-{random}.png");
85+
var blueprintFile = Path.Combine(autosaveDirectory, $"image-{imageResult.SchedulerOptions.Seed}-{random}.json");
86+
if (!await imageResult.SaveImageFile(imageFile))
87+
return false;
88+
89+
if (includeBlueprint)
90+
return await imageResult.SaveBlueprintFile(blueprintFile);
91+
92+
return true;
93+
}
94+
95+
7896
public static SchedulerOptions ToSchedulerOptions(this SchedulerOptionsModel model)
7997
{
8098
return new SchedulerOptions

OnnxStack.UI/Views/ImageInpaint.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<DockPanel>
2323
<StackPanel IsEnabled="{Binding IsGenerating, Converter={StaticResource InverseBoolConverter}}">
2424
<userControls:ModelPickerControl
25+
UISettings="{Binding UISettings}"
2526
SupportedDiffusers="{Binding SupportedDiffusers}"
2627
SelectedModel="{Binding SelectedModel, Mode=TwoWay}" Models="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=Models}" />
2728
<userControls:PromptControl

OnnxStack.UI/Views/ImageInpaint.xaml.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
using System.Collections.ObjectModel;
1111
using System.ComponentModel;
1212
using System.Diagnostics;
13+
using System.IO;
1314
using System.Runtime.CompilerServices;
1415
using System.Threading;
1516
using System.Threading.Tasks;
17+
using System.Windows;
1618
using System.Windows.Controls;
1719

1820
namespace OnnxStack.UI.Views
@@ -63,6 +65,14 @@ public ImageInpaint()
6365
InitializeComponent();
6466
}
6567

68+
public OnnxStackUIConfig UISettings
69+
{
70+
get { return (OnnxStackUIConfig)GetValue(UISettingsProperty); }
71+
set { SetValue(UISettingsProperty, value); }
72+
}
73+
public static readonly DependencyProperty UISettingsProperty =
74+
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(ImageInpaint));
75+
6676
public List<DiffuserType> SupportedDiffusers { get; }
6777
public AsyncRelayCommand CancelCommand { get; }
6878
public AsyncRelayCommand GenerateCommand { get; }
@@ -316,7 +326,7 @@ private async Task<ImageResult> ExecuteStableDiffusion(IModelOptions modelOption
316326
if (image == null)
317327
return null;
318328

319-
return new ImageResult
329+
var imageResult = new ImageResult
320330
{
321331
Image = image,
322332
Model = _selectedModel,
@@ -328,6 +338,11 @@ private async Task<ImageResult> ExecuteStableDiffusion(IModelOptions modelOption
328338
SchedulerOptions = schedulerOptions,
329339
Elapsed = Stopwatch.GetElapsedTime(timestamp).TotalSeconds
330340
};
341+
342+
if (UISettings.ImageAutoSave)
343+
await imageResult.AutoSave(Path.Combine(UISettings.ImageAutoSaveDirectory, "ImageInpaint"), UISettings.ImageAutoSaveBlueprint);
344+
345+
return imageResult;
331346
}
332347
catch (Exception ex)
333348
{

OnnxStack.UI/Views/ImageToImage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<DockPanel>
2323
<StackPanel IsEnabled="{Binding IsGenerating, Converter={StaticResource InverseBoolConverter}}">
2424
<userControls:ModelPickerControl
25+
UISettings="{Binding UISettings}"
2526
SupportedDiffusers="{Binding SupportedDiffusers}"
2627
SelectedModel="{Binding SelectedModel, Mode=TwoWay}" Models="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=Models}"/>
2728
<userControls:PromptControl

0 commit comments

Comments
 (0)