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

Commit 1274349

Browse files
committed
Add Load/Unload methods to IStableDiffusionService
1 parent 73d7d00 commit 1274349

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

OnnxStack.StableDiffusion/Common/IStableDiffusionService.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
2+
using OnnxStack.Core.Model;
23
using OnnxStack.StableDiffusion.Config;
34
using SixLabors.ImageSharp;
45
using SixLabors.ImageSharp.PixelFormats;
@@ -12,8 +13,36 @@ namespace OnnxStack.StableDiffusion.Common
1213
{
1314
public interface IStableDiffusionService
1415
{
16+
17+
/// <summary>
18+
/// Gets the models.
19+
/// </summary>
1520
List<ModelOptions> Models { get; }
1621

22+
/// <summary>
23+
/// Loads the model.
24+
/// </summary>
25+
/// <param name="modelOptions">The model options.</param>
26+
/// <returns></returns>
27+
Task<bool> LoadModel(IModelOptions modelOptions);
28+
29+
30+
/// <summary>
31+
/// Unloads the model.
32+
/// </summary>
33+
/// <param name="modelOptions">The model options.</param>
34+
/// <returns></returns>
35+
Task<bool> UnloadModel(IModelOptions modelOptions);
36+
37+
/// <summary>
38+
/// Determines whether the specified model is loaded
39+
/// </summary>
40+
/// <param name="modelOptions">The model options.</param>
41+
/// <returns>
42+
/// <c>true</c> if the specified model is loaded; otherwise, <c>false</c>.
43+
/// </returns>
44+
bool IsModelLoaded(IModelOptions modelOptions);
45+
1746
/// <summary>
1847
/// Generates the StableDiffusion image using the prompt and options provided.
1948
/// </summary>

OnnxStack.StableDiffusion/Config/ModelOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using OnnxStack.Common.Config;
2-
using OnnxStack.StableDiffusion.Common;
1+
using OnnxStack.StableDiffusion.Common;
32
using System.Collections.Immutable;
4-
using System.Linq;
53

64
namespace OnnxStack.StableDiffusion.Config
75
{

OnnxStack.StableDiffusion/Services/StableDiffusionService.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,64 @@ public sealed class StableDiffusionService : IStableDiffusionService
2424
private readonly IDiffuser _imageDiffuser;
2525
private readonly IDiffuser _inpaintDiffuser;
2626
private readonly IDiffuser _inpaintLegacyDiffuser;
27+
private readonly IOnnxModelService _onnxModelService;
2728
private readonly StableDiffusionConfig _configuration;
2829

30+
2931
/// <summary>
3032
/// Initializes a new instance of the <see cref="StableDiffusionService"/> class.
3133
/// </summary>
3234
/// <param name="schedulerService">The scheduler service.</param>
3335
public StableDiffusionService(StableDiffusionConfig configuration, IOnnxModelService onnxModelService, IPromptService promptService)
3436
{
3537
_configuration = configuration;
38+
_onnxModelService = onnxModelService;
3639
_textDiffuser = new TextDiffuser(onnxModelService, promptService);
3740
_imageDiffuser = new ImageDiffuser(onnxModelService, promptService);
3841
_inpaintDiffuser = new InpaintDiffuser(onnxModelService, promptService);
3942
_inpaintLegacyDiffuser = new InpaintLegacyDiffuser(onnxModelService, promptService);
4043
}
4144

45+
46+
/// <summary>
47+
/// Gets the models.
48+
/// </summary>
4249
public List<ModelOptions> Models => _configuration.OnnxModelSets;
4350

4451

52+
/// <summary>
53+
/// Loads the model.
54+
/// </summary>
55+
/// <param name="modelOptions">The model options.</param>
56+
/// <returns></returns>
57+
public async Task<bool> LoadModel(IModelOptions modelOptions)
58+
{
59+
var model = await _onnxModelService.LoadModel(modelOptions);
60+
return model is not null;
61+
}
62+
63+
64+
/// <summary>
65+
/// Unloads the model.
66+
/// </summary>
67+
/// <param name="modelOptions">The model options.</param>
68+
/// <returns></returns>
69+
public async Task<bool> UnloadModel(IModelOptions modelOptions)
70+
{
71+
return await _onnxModelService.UnloadModel(modelOptions);
72+
}
73+
74+
75+
/// <summary>
76+
/// Is the model loaded.
77+
/// </summary>
78+
/// <param name="modelOptions">The model options.</param>
79+
/// <returns></returns>
80+
public bool IsModelLoaded(IModelOptions modelOptions)
81+
{
82+
return _onnxModelService.IsModelLoaded(modelOptions);
83+
}
84+
4585
/// <summary>
4686
/// Generates the StableDiffusion image using the prompt and options provided.
4787
/// </summary>

OnnxStack.UI/UserControls/ModelPickerControl.xaml.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Models;
2-
using OnnxStack.Core.Services;
2+
using OnnxStack.StableDiffusion.Common;
33
using OnnxStack.UI.Commands;
44
using System.ComponentModel;
55
using System.Runtime.CompilerServices;
@@ -14,13 +14,13 @@ namespace OnnxStack.UI.UserControls
1414
/// </summary>
1515
public partial class ModelPickerControl : UserControl, INotifyPropertyChanged
1616
{
17-
private readonly IOnnxModelService _modelService;
17+
private readonly IStableDiffusionService _stableDiffusionService;
1818

1919
/// <summary>Initializes a new instance of the <see cref="ModelPickerControl" /> class.</summary>
2020
public ModelPickerControl()
2121
{
2222
if (!DesignerProperties.GetIsInDesignMode(this))
23-
_modelService = App.GetService<IOnnxModelService>();
23+
_stableDiffusionService = App.GetService<IStableDiffusionService>();
2424

2525
LoadModelCommand = new AsyncRelayCommand(LoadModel);
2626
UnloadModelCommand = new AsyncRelayCommand(UnloadModel);
@@ -46,13 +46,13 @@ public ModelOptionsModel SelectedModel
4646
/// </summary>
4747
private async Task LoadModel()
4848
{
49-
if (_modelService.IsModelLoaded(SelectedModel.ModelOptions))
49+
if (_stableDiffusionService.IsModelLoaded(SelectedModel.ModelOptions))
5050
return;
5151

5252
SelectedModel.IsLoading = true;
53-
await _modelService.LoadModel(SelectedModel.ModelOptions);
53+
var result = await _stableDiffusionService.LoadModel(SelectedModel.ModelOptions);
5454
SelectedModel.IsLoading = false;
55-
SelectedModel.IsLoaded = true;
55+
SelectedModel.IsLoaded = result;
5656
}
5757

5858

@@ -61,11 +61,11 @@ private async Task LoadModel()
6161
/// </summary>
6262
private async Task UnloadModel()
6363
{
64-
if (!_modelService.IsModelLoaded(SelectedModel.ModelOptions))
64+
if (!_stableDiffusionService.IsModelLoaded(SelectedModel.ModelOptions))
6565
return;
6666

6767
SelectedModel.IsLoading = true;
68-
await _modelService.UnloadModel(SelectedModel.ModelOptions);
68+
await _stableDiffusionService.UnloadModel(SelectedModel.ModelOptions);
6969
SelectedModel.IsLoading = false;
7070
SelectedModel.IsLoaded = false;
7171
}

0 commit comments

Comments
 (0)