@@ -35,6 +35,7 @@ public partial class TextToImageView : UserControl, INavigatable, INotifyPropert
3535 private int _progressValue ;
3636 private bool _isGenerating ;
3737 private int _selectedTabIndex ;
38+ private bool _isControlsEnabled ;
3839 private ImageResult _resultImage ;
3940 private ModelOptionsModel _selectedModel ;
4041 private PromptOptionsModel _promptOptionsModel ;
@@ -62,6 +63,7 @@ public TextToImageView()
6263 BatchOptions = new BatchOptionsModel ( ) ;
6364 ImageResults = new ObservableCollection < ImageResult > ( ) ;
6465 ProgressMax = SchedulerOptions . InferenceSteps ;
66+ IsControlsEnabled = true ;
6567 InitializeComponent ( ) ;
6668 }
6769
@@ -141,6 +143,11 @@ public int SelectedTabIndex
141143 }
142144
143145
146+ public bool IsControlsEnabled
147+ {
148+ get { return _isControlsEnabled ; }
149+ set { _isControlsEnabled = value ; NotifyPropertyChanged ( ) ; }
150+ }
144151
145152
146153 /// <summary>
@@ -175,6 +182,7 @@ private async Task Generate()
175182 {
176183 HasResult = false ;
177184 IsGenerating = true ;
185+ IsControlsEnabled = false ;
178186 ResultImage = null ;
179187 var promptOptions = new PromptOptions
180188 {
@@ -274,6 +282,7 @@ private bool CanExecuteClearHistory()
274282 private void Reset ( )
275283 {
276284 IsGenerating = false ;
285+ IsControlsEnabled = true ;
277286 ProgressValue = 0 ;
278287 }
279288
@@ -295,16 +304,65 @@ private async IAsyncEnumerable<ImageResult> ExecuteStableDiffusion(IModelOptions
295304 }
296305 else
297306 {
298- var timestamp = Stopwatch . GetTimestamp ( ) ;
299- await foreach ( var batchResult in _stableDiffusionService . GenerateBatchAsync ( modelOptions , promptOptions , schedulerOptions , batchOptions , ProgressBatchCallback ( ) , _cancelationTokenSource . Token ) )
307+ if ( _batchOptions . BatchType != BatchOptionType . Realtime )
300308 {
301- yield return await GenerateResultAsync ( batchResult . ImageResult . ToImageBytes ( ) , promptOptions , batchResult . SchedulerOptions , timestamp ) ;
302- timestamp = Stopwatch . GetTimestamp ( ) ;
309+ var timestamp = Stopwatch . GetTimestamp ( ) ;
310+ await foreach ( var batchResult in _stableDiffusionService . GenerateBatchAsync ( modelOptions , promptOptions , schedulerOptions , batchOptions , ProgressBatchCallback ( ) , _cancelationTokenSource . Token ) )
311+ {
312+ yield return await GenerateResultAsync ( batchResult . ImageResult . ToImageBytes ( ) , promptOptions , batchResult . SchedulerOptions , timestamp ) ;
313+ timestamp = Stopwatch . GetTimestamp ( ) ;
314+ }
315+ yield break ;
316+ }
317+
318+ // Realtime Diffusion
319+ IsControlsEnabled = true ;
320+ while ( ! _cancelationTokenSource . IsCancellationRequested )
321+ {
322+ var refreshTimestamp = Stopwatch . GetTimestamp ( ) ;
323+ var realtimePromptOptions = GetPromptOptions ( PromptOptions ) ;
324+ var realtimeSchedulerOptions = SchedulerOptions . ToSchedulerOptions ( ) ;
325+ if ( IsLiveOptionsValid ( realtimePromptOptions ) )
326+ {
327+ var timestamp = Stopwatch . GetTimestamp ( ) ;
328+ var result = await _stableDiffusionService . GenerateAsBytesAsync ( modelOptions , realtimePromptOptions , realtimeSchedulerOptions , ProgressCallback ( ) , _cancelationTokenSource . Token ) ;
329+ yield return await GenerateResultAsync ( result , promptOptions , schedulerOptions , timestamp ) ;
330+ }
331+ await RealtimeRefreshDelay ( refreshTimestamp , _cancelationTokenSource . Token ) ;
303332 }
304333 }
305334 }
306335
307336
337+ private bool IsLiveOptionsValid ( PromptOptions prompt )
338+ {
339+ if ( string . IsNullOrEmpty ( prompt . Prompt ) )
340+ return false ;
341+
342+ return true ;
343+ }
344+
345+
346+ private PromptOptions GetPromptOptions ( PromptOptionsModel promptOptionsModel )
347+ {
348+ return new PromptOptions
349+ {
350+ Prompt = promptOptionsModel . Prompt ,
351+ NegativePrompt = promptOptionsModel . NegativePrompt ,
352+ DiffuserType = DiffuserType . TextToImage
353+ } ;
354+ }
355+
356+
357+ private async Task RealtimeRefreshDelay ( long startTime , CancellationToken cancellationToken )
358+ {
359+ var endTime = Stopwatch . GetTimestamp ( ) ;
360+ var elapsedMilliseconds = ( endTime - startTime ) * 1000.0 / Stopwatch . Frequency ;
361+ int adjustedDelay = Math . Max ( 0 , BatchOptions . RealtimeRefreshRate - ( int ) elapsedMilliseconds ) ;
362+ await Task . Delay ( adjustedDelay , cancellationToken ) . ConfigureAwait ( false ) ;
363+ }
364+
365+
308366 private async Task < ImageResult > GenerateResultAsync ( byte [ ] imageBytes , PromptOptions promptOptions , SchedulerOptions schedulerOptions , long timestamp )
309367 {
310368 var image = Utils . CreateBitmap ( imageBytes ) ;
0 commit comments