1+ using FluentAssertions ;
2+ using Microsoft . Extensions . DependencyInjection ;
3+ using OnnxStack . Core ;
4+ using OnnxStack . StableDiffusion . Common ;
5+ using OnnxStack . StableDiffusion . Config ;
6+ using OnnxStack . StableDiffusion . Enums ;
7+ using Xunit . Abstractions ;
8+
9+ namespace OnnxStack . IntegrationTests ;
10+
11+ public class StableDiffusionTests
12+ {
13+ private readonly ITestOutputHelper _testOutputHelper ;
14+ private readonly IStableDiffusionService _stableDiffusion ;
15+
16+ public StableDiffusionTests ( ITestOutputHelper testOutputHelper )
17+ {
18+ _testOutputHelper = testOutputHelper ;
19+
20+ var services = new ServiceCollection ( ) ;
21+ services . AddLogging ( ) ;
22+ services . AddOnnxStack ( ) ;
23+ services . AddOnnxStackStableDiffusion ( ) ;
24+ var provider = services . BuildServiceProvider ( ) ;
25+ _stableDiffusion = provider . GetRequiredService < IStableDiffusionService > ( ) ;
26+ }
27+
28+ [ Fact ]
29+ public async Task GivenStableDiffusion15_WhenLoadModel_ThenModelIsLoaded ( )
30+ {
31+ //arrange
32+ var model = _stableDiffusion . Models . Single ( m => m . Name == "StableDiffusion 1.5" ) ;
33+
34+ //act
35+ var isModelLoaded = await _stableDiffusion . LoadModel ( model ) ;
36+
37+ //assert
38+ isModelLoaded . Should ( ) . BeTrue ( ) ;
39+ }
40+
41+ [ Fact ]
42+ public async Task GivenTextToImage_WhenInference_ThenImageGenerated ( )
43+ {
44+ //arrange
45+ var model = _stableDiffusion . Models . Single ( m => m . Name == "StableDiffusion 1.5" ) ;
46+ await _stableDiffusion . LoadModel ( model ) ;
47+
48+ var prompt = new PromptOptions
49+ {
50+ Prompt = "an astronaut riding a horse in space" ,
51+ NegativePrompt = "blurry,ugly,cartoon" ,
52+ BatchCount = 1 ,
53+ SchedulerType = SchedulerType . EulerAncestral ,
54+ DiffuserType = DiffuserType . TextToImage
55+ } ;
56+
57+ var scheduler = new SchedulerOptions
58+ {
59+ Width = 512 ,
60+ Height = 512 ,
61+ InferenceSteps = 10 ,
62+ GuidanceScale = 7.0f ,
63+ Seed = - 1
64+ } ;
65+
66+ var steps = 0 ;
67+
68+ //act
69+ var image = await _stableDiffusion . GenerateAsImageAsync ( model , prompt , scheduler , ( currentStep , totalSteps ) =>
70+ {
71+ _testOutputHelper . WriteLine ( $ "Step { currentStep } /{ totalSteps } ") ;
72+ steps ++ ;
73+ } ) ;
74+
75+ //assert
76+ steps . Should ( ) . Be ( 10 ) ;
77+ image . Should ( ) . NotBeNull ( ) ;
78+ image . Size . IsEmpty . Should ( ) . BeFalse ( ) ;
79+ image . Width . Should ( ) . Be ( 512 ) ;
80+ image . Height . Should ( ) . Be ( 512 ) ;
81+ }
82+ }
0 commit comments