66using OnnxStack . StableDiffusion . Helpers ;
77using OnnxStack . UI . Commands ;
88using OnnxStack . UI . Models ;
9- using OnnxStack . UI . UserControls ;
109using System ;
1110using System . Collections . Generic ;
1211using System . Collections . ObjectModel ;
1817using System . Threading . Tasks ;
1918using System . Windows ;
2019using System . Windows . Controls ;
21- using System . Windows . Media . Imaging ;
2220
2321namespace OnnxStack . UI . Views
2422{
@@ -201,8 +199,11 @@ private async Task Generate()
201199 if ( resultImage != null )
202200 {
203201 ResultImage = resultImage ;
204- ImageResults . Add ( resultImage ) ;
205202 HasResult = true ;
203+ if ( BatchOptions . IsAutomationEnabled && BatchOptions . DisableHistory )
204+ continue ;
205+
206+ ImageResults . Add ( resultImage ) ;
206207 }
207208 }
208209 }
@@ -295,6 +296,9 @@ private void Reset()
295296 /// <returns></returns>
296297 private async IAsyncEnumerable < ImageResult > ExecuteStableDiffusion ( IModelOptions modelOptions , PromptOptions promptOptions , SchedulerOptions schedulerOptions , BatchOptions batchOptions )
297298 {
299+ if ( ! IsExecuteOptionsValid ( PromptOptions ) )
300+ yield break ;
301+
298302 _cancelationTokenSource = new CancellationTokenSource ( ) ;
299303 if ( ! BatchOptions . IsAutomationEnabled )
300304 {
@@ -320,21 +324,23 @@ private async IAsyncEnumerable<ImageResult> ExecuteStableDiffusion(IModelOptions
320324 while ( ! _cancelationTokenSource . IsCancellationRequested )
321325 {
322326 var refreshTimestamp = Stopwatch . GetTimestamp ( ) ;
323- var realtimePromptOptions = GetPromptOptions ( PromptOptions ) ;
324- var realtimeSchedulerOptions = SchedulerOptions . ToSchedulerOptions ( ) ;
325- if ( IsLiveOptionsValid ( realtimePromptOptions ) )
327+ if ( SchedulerOptions . HasChanged || PromptOptions . HasChanged )
326328 {
329+ PromptOptions . HasChanged = false ;
330+ SchedulerOptions . HasChanged = false ;
331+ var realtimePromptOptions = GetPromptOptions ( PromptOptions ) ;
332+ var realtimeSchedulerOptions = SchedulerOptions . ToSchedulerOptions ( ) ;
333+
327334 var timestamp = Stopwatch . GetTimestamp ( ) ;
328- var result = await _stableDiffusionService . GenerateAsBytesAsync ( modelOptions , realtimePromptOptions , realtimeSchedulerOptions , ProgressCallback ( ) , _cancelationTokenSource . Token ) ;
335+ var result = await _stableDiffusionService . GenerateAsBytesAsync ( modelOptions , realtimePromptOptions , realtimeSchedulerOptions , RealtimeProgressCallback ( ) , _cancelationTokenSource . Token ) ;
329336 yield return await GenerateResultAsync ( result , promptOptions , schedulerOptions , timestamp ) ;
330337 }
331- await RealtimeRefreshDelay ( refreshTimestamp , _cancelationTokenSource . Token ) ;
338+ await Utils . RefreshDelay ( refreshTimestamp , BatchOptions . RealtimeRefreshRate , _cancelationTokenSource . Token ) ;
332339 }
333340 }
334341 }
335342
336-
337- private bool IsLiveOptionsValid ( PromptOptions prompt )
343+ private bool IsExecuteOptionsValid ( PromptOptionsModel prompt )
338344 {
339345 if ( string . IsNullOrEmpty ( prompt . Prompt ) )
340346 return false ;
@@ -353,16 +359,6 @@ private PromptOptions GetPromptOptions(PromptOptionsModel promptOptionsModel)
353359 } ;
354360 }
355361
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-
366362 private async Task < ImageResult > GenerateResultAsync ( byte [ ] imageBytes , PromptOptions promptOptions , SchedulerOptions schedulerOptions , long timestamp )
367363 {
368364 var image = Utils . CreateBitmap ( imageBytes ) ;
@@ -428,6 +424,22 @@ private Action<int, int, int, int> ProgressBatchCallback()
428424 } ;
429425 }
430426
427+ private Action < int , int > RealtimeProgressCallback ( )
428+ {
429+ return ( value , maximum ) =>
430+ {
431+ App . UIInvoke ( ( ) =>
432+ {
433+ if ( _cancelationTokenSource . IsCancellationRequested )
434+ return ;
435+
436+ if ( BatchOptions . StepValue != value )
437+ BatchOptions . StepValue = value ;
438+ if ( BatchOptions . StepsValue != maximum )
439+ BatchOptions . StepsValue = maximum ;
440+ } ) ;
441+ } ;
442+ }
431443
432444
433445 #region INotifyPropertyChanged
0 commit comments