|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using OnnxStack.FeatureExtractor.Common; |
| 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 AddFeatureExtractorModelDialog.xaml |
| 21 | + /// </summary> |
| 22 | + public partial class AddFeatureExtractorModelDialog : Window, INotifyPropertyChanged |
| 23 | + { |
| 24 | + private readonly ILogger<AddFeatureExtractorModelDialog> _logger; |
| 25 | + |
| 26 | + private readonly List<string> _invalidOptions; |
| 27 | + private string _modelName; |
| 28 | + private string _modelFile; |
| 29 | + private bool _normalize; |
| 30 | + private int _sampleSize = 512; |
| 31 | + private int _channels = 1; |
| 32 | + private string _controlNetFilter = "N/A"; |
| 33 | + private IModelFactory _modelFactory; |
| 34 | + private OnnxStackUIConfig _settings; |
| 35 | + private FeatureExtractorModelSet _modelSetResult; |
| 36 | + |
| 37 | + public AddFeatureExtractorModelDialog(OnnxStackUIConfig settings, IModelFactory modelFactory, ILogger<AddFeatureExtractorModelDialog> logger) |
| 38 | + { |
| 39 | + _logger = logger; |
| 40 | + _settings = settings; |
| 41 | + _modelFactory = modelFactory; |
| 42 | + SaveCommand = new AsyncRelayCommand(Save, CanExecuteSave); |
| 43 | + CancelCommand = new AsyncRelayCommand(Cancel); |
| 44 | + _invalidOptions = _settings.FeatureExtractorModelSets.Select(x => x.Name).ToList(); |
| 45 | + ControlNetFilters = new List<string> { "N/A" }; |
| 46 | + ControlNetFilters.AddRange(Enum.GetNames<ControlNetType>()); |
| 47 | + InitializeComponent(); |
| 48 | + } |
| 49 | + |
| 50 | + public AsyncRelayCommand SaveCommand { get; } |
| 51 | + public AsyncRelayCommand CancelCommand { get; } |
| 52 | + public ObservableCollection<ValidationResult> ValidationResults { get; set; } = new ObservableCollection<ValidationResult>(); |
| 53 | + |
| 54 | + public List<string> ControlNetFilters { get; set; } |
| 55 | + |
| 56 | + public string ModelName |
| 57 | + { |
| 58 | + get { return _modelName; } |
| 59 | + set { _modelName = value; _modelName?.Trim(); NotifyPropertyChanged(); CreateModelSet(); } |
| 60 | + } |
| 61 | + |
| 62 | + public string ModelFile |
| 63 | + { |
| 64 | + get { return _modelFile; } |
| 65 | + set |
| 66 | + { |
| 67 | + _modelFile = value; |
| 68 | + _modelName = string.IsNullOrEmpty(_modelFile) |
| 69 | + ? string.Empty |
| 70 | + : Path.GetFileNameWithoutExtension(_modelFile); |
| 71 | + |
| 72 | + NotifyPropertyChanged(); |
| 73 | + NotifyPropertyChanged(nameof(ModelName)); |
| 74 | + CreateModelSet(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public string ControlNetFilter |
| 79 | + { |
| 80 | + get { return _controlNetFilter; } |
| 81 | + set { _controlNetFilter = value; NotifyPropertyChanged(); } |
| 82 | + } |
| 83 | + |
| 84 | + public int SampleSize |
| 85 | + { |
| 86 | + get { return _sampleSize; } |
| 87 | + set { _sampleSize = value; NotifyPropertyChanged(); CreateModelSet(); } |
| 88 | + } |
| 89 | + |
| 90 | + public bool Normalize |
| 91 | + { |
| 92 | + get { return _normalize; } |
| 93 | + set { _normalize = value; NotifyPropertyChanged(); CreateModelSet(); } |
| 94 | + } |
| 95 | + |
| 96 | + public int Channels |
| 97 | + { |
| 98 | + get { return _channels; } |
| 99 | + set { _channels = value; NotifyPropertyChanged(); CreateModelSet(); } |
| 100 | + } |
| 101 | + |
| 102 | + public FeatureExtractorModelSet ModelSetResult |
| 103 | + { |
| 104 | + get { return _modelSetResult; } |
| 105 | + } |
| 106 | + |
| 107 | + public ControlNetType? ControlNetType |
| 108 | + { |
| 109 | + get { return Enum.TryParse<ControlNetType>(_controlNetFilter, out var result) ? result : null; } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public new bool ShowDialog() |
| 114 | + { |
| 115 | + return base.ShowDialog() ?? false; |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + private void CreateModelSet() |
| 120 | + { |
| 121 | + _modelSetResult = null; |
| 122 | + ValidationResults.Clear(); |
| 123 | + if (string.IsNullOrEmpty(_modelFile)) |
| 124 | + return; |
| 125 | + |
| 126 | + _modelSetResult = _modelFactory.CreateFeatureExtractorModelSet(ModelName.Trim(), _normalize, _sampleSize, _channels, _modelFile); |
| 127 | + |
| 128 | + // Validate |
| 129 | + ValidationResults.Add(new ValidationResult("Name", !_invalidOptions.Contains(_modelName, StringComparer.OrdinalIgnoreCase) && _modelName.Length > 2 && _modelName.Length < 50)); |
| 130 | + ValidationResults.Add(new ValidationResult("Model", File.Exists(_modelSetResult.FeatureExtractorConfig.OnnxModelPath))); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private Task Save() |
| 135 | + { |
| 136 | + DialogResult = true; |
| 137 | + return Task.CompletedTask; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private bool CanExecuteSave() |
| 142 | + { |
| 143 | + if (string.IsNullOrEmpty(_modelFile)) |
| 144 | + return false; |
| 145 | + if (_modelSetResult is null) |
| 146 | + return false; |
| 147 | + |
| 148 | + return ValidationResults.Count > 0 && ValidationResults.All(x => x.IsValid); |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + private Task Cancel() |
| 153 | + { |
| 154 | + _modelSetResult = null; |
| 155 | + DialogResult = false; |
| 156 | + return Task.CompletedTask; |
| 157 | + } |
| 158 | + |
| 159 | + #region INotifyPropertyChanged |
| 160 | + public event PropertyChangedEventHandler PropertyChanged; |
| 161 | + public void NotifyPropertyChanged([CallerMemberName] string property = "") |
| 162 | + { |
| 163 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); |
| 164 | + } |
| 165 | + #endregion |
| 166 | + } |
| 167 | +} |
0 commit comments