|
| 1 | +using DemoApp.Common; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Data; |
| 8 | +using TensorStack.Common; |
| 9 | +using TensorStack.WPF; |
| 10 | +using TensorStack.WPF.Controls; |
| 11 | + |
| 12 | +namespace DemoApp.Controls |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Interaction logic for TranscribeModelControl.xaml |
| 16 | + /// </summary> |
| 17 | + public partial class TranscribeModelControl : BaseControl |
| 18 | + { |
| 19 | + private ListCollectionView _modelCollectionView; |
| 20 | + private Device _selectedDevice; |
| 21 | + private TranscribeModel _selectedModel; |
| 22 | + private Device _currentDevice; |
| 23 | + private TranscribeModel _currentModel; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="TranscribeModelControl"/> class. |
| 27 | + /// </summary> |
| 28 | + public TranscribeModelControl() |
| 29 | + { |
| 30 | + LoadCommand = new AsyncRelayCommand(LoadAsync, CanLoad); |
| 31 | + UnloadCommand = new AsyncRelayCommand(UnloadAsync, CanUnload); |
| 32 | + InitializeComponent(); |
| 33 | + } |
| 34 | + |
| 35 | + public static readonly DependencyProperty SettingsProperty = DependencyProperty.Register(nameof(Settings), typeof(Settings), typeof(TranscribeModelControl), new PropertyMetadata<TranscribeModelControl>((c) => c.OnSettingsChanged())); |
| 36 | + public static readonly DependencyProperty IsSelectionValidProperty = DependencyProperty.Register(nameof(IsSelectionValid), typeof(bool), typeof(TranscribeModelControl)); |
| 37 | + public static readonly DependencyProperty CurrentPipelineProperty = DependencyProperty.Register(nameof(CurrentPipeline), typeof(PipelineModel), typeof(TranscribeModelControl), new PropertyMetadata<TranscribeModelControl>((c) => c.OnPipelineChanged())); |
| 38 | + |
| 39 | + public event EventHandler<PipelineModel> SelectionChanged; |
| 40 | + public AsyncRelayCommand LoadCommand { get; } |
| 41 | + public AsyncRelayCommand UnloadCommand { get; } |
| 42 | + |
| 43 | + public Settings Settings |
| 44 | + { |
| 45 | + get { return (Settings)GetValue(SettingsProperty); } |
| 46 | + set { SetValue(SettingsProperty, value); } |
| 47 | + } |
| 48 | + |
| 49 | + public bool IsSelectionValid |
| 50 | + { |
| 51 | + get { return (bool)GetValue(IsSelectionValidProperty); } |
| 52 | + set { SetValue(IsSelectionValidProperty, value); } |
| 53 | + } |
| 54 | + |
| 55 | + public PipelineModel CurrentPipeline |
| 56 | + { |
| 57 | + get { return (PipelineModel)GetValue(CurrentPipelineProperty); } |
| 58 | + set { SetValue(CurrentPipelineProperty, value); } |
| 59 | + } |
| 60 | + |
| 61 | + public Device SelectedDevice |
| 62 | + { |
| 63 | + get { return _selectedDevice; } |
| 64 | + set { SetProperty(ref _selectedDevice, value); } |
| 65 | + } |
| 66 | + |
| 67 | + public TranscribeModel SelectedModel |
| 68 | + { |
| 69 | + get { return _selectedModel; } |
| 70 | + set { SetProperty(ref _selectedModel, value); } |
| 71 | + } |
| 72 | + |
| 73 | + public ListCollectionView ModelCollectionView |
| 74 | + { |
| 75 | + get { return _modelCollectionView; } |
| 76 | + set { SetProperty(ref _modelCollectionView, value); } |
| 77 | + } |
| 78 | + |
| 79 | + private async Task LoadAsync() |
| 80 | + { |
| 81 | + if (!await IsModelValidAsync()) |
| 82 | + return; |
| 83 | + |
| 84 | + _currentDevice = SelectedDevice; |
| 85 | + _currentModel = SelectedModel; |
| 86 | + CurrentPipeline = new PipelineModel |
| 87 | + { |
| 88 | + Device = _currentDevice, |
| 89 | + TranscribeModel = _currentModel |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + private bool CanLoad() |
| 95 | + { |
| 96 | + var isReloadRequired = SelectedDevice is not null |
| 97 | + && SelectedModel is not null |
| 98 | + && HasCurrentChanged(); |
| 99 | + |
| 100 | + var isSelectionValid = !isReloadRequired; |
| 101 | + if (IsSelectionValid != isSelectionValid) |
| 102 | + IsSelectionValid = isSelectionValid; |
| 103 | + |
| 104 | + return isReloadRequired; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + private Task UnloadAsync() |
| 109 | + { |
| 110 | + _currentModel = default; |
| 111 | + CurrentPipeline = new PipelineModel |
| 112 | + { |
| 113 | + Device = _selectedDevice |
| 114 | + }; |
| 115 | + |
| 116 | + return Task.CompletedTask; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private bool CanUnload() |
| 121 | + { |
| 122 | + return _currentModel is not null; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + private bool HasCurrentChanged() |
| 127 | + { |
| 128 | + return _currentDevice != SelectedDevice |
| 129 | + || _currentModel != SelectedModel; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + private Task OnSettingsChanged() |
| 134 | + { |
| 135 | + ModelCollectionView = new ListCollectionView(Settings.TranscribeModels); |
| 136 | + ModelCollectionView.Filter = (obj) => |
| 137 | + { |
| 138 | + if (obj is not TranscribeModel viewModel) |
| 139 | + return false; |
| 140 | + |
| 141 | + if (_selectedDevice == null) |
| 142 | + return false; |
| 143 | + |
| 144 | + return viewModel.SupportedDevices?.Contains(_selectedDevice.Type) ?? false; |
| 145 | + }; |
| 146 | + |
| 147 | + SelectedDevice = Settings.DefaultDevice; |
| 148 | + return Task.CompletedTask; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + private void Device_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) |
| 153 | + { |
| 154 | + ModelCollectionView?.Refresh(); |
| 155 | + if (ModelCollectionView is not null) |
| 156 | + { |
| 157 | + SelectedModel = ModelCollectionView.Cast<TranscribeModel>().FirstOrDefault(x => x == _currentModel || x.IsDefault); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + private Task OnPipelineChanged() |
| 163 | + { |
| 164 | + SelectedDevice = CurrentPipeline?.Device ?? Settings.DefaultDevice; |
| 165 | + if (CurrentPipeline?.TranscribeModel is not null) |
| 166 | + { |
| 167 | + SelectedModel = CurrentPipeline.TranscribeModel; |
| 168 | + } |
| 169 | + |
| 170 | + _currentDevice = CurrentPipeline?.Device; |
| 171 | + _currentModel = CurrentPipeline?.TranscribeModel; |
| 172 | + SelectionChanged?.Invoke(this, CurrentPipeline); |
| 173 | + return Task.CompletedTask; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + private async Task<bool> IsModelValidAsync() |
| 178 | + { |
| 179 | + if (_selectedModel is null) |
| 180 | + return false; |
| 181 | + |
| 182 | + if (_selectedModel.IsValid) |
| 183 | + return true; |
| 184 | + |
| 185 | + return await _selectedModel.DownloadAsync(Path.Combine(Settings.DirectoryModel, "Transcribe")); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments