|
| 1 | +using OnnxStack.Core; |
| 2 | +using OnnxStack.StableDiffusion.Common; |
| 3 | +using OnnxStack.StableDiffusion.Config; |
| 4 | +using OnnxStack.StableDiffusion.Enums; |
| 5 | +using OnnxStack.StableDiffusion.Helpers; |
| 6 | +using SixLabors.ImageSharp; |
| 7 | + |
| 8 | +namespace OnnxStack.Console.Runner |
| 9 | +{ |
| 10 | + public sealed class StableDiffusionBatch : IExampleRunner |
| 11 | + { |
| 12 | + private readonly string _outputDirectory; |
| 13 | + private readonly IStableDiffusionService _stableDiffusionService; |
| 14 | + |
| 15 | + public StableDiffusionBatch(IStableDiffusionService stableDiffusionService) |
| 16 | + { |
| 17 | + _stableDiffusionService = stableDiffusionService; |
| 18 | + _outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionBatch)); |
| 19 | + } |
| 20 | + |
| 21 | + public string Name => "Stable Diffusion Batch Demo"; |
| 22 | + |
| 23 | + public string Description => "Creates a batch of images from the text prompt provided using all Scheduler types"; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Stable Diffusion Demo |
| 27 | + /// </summary> |
| 28 | + public async Task RunAsync() |
| 29 | + { |
| 30 | + Directory.CreateDirectory(_outputDirectory); |
| 31 | + |
| 32 | + var model = _stableDiffusionService.Models.First(); |
| 33 | + await _stableDiffusionService.LoadModel(model); |
| 34 | + |
| 35 | + while (true) |
| 36 | + { |
| 37 | + OutputHelpers.WriteConsole("Please type a prompt and press ENTER", ConsoleColor.Yellow); |
| 38 | + var prompt = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
| 39 | + |
| 40 | + OutputHelpers.WriteConsole("Please type a negative prompt and press ENTER (optional)", ConsoleColor.Yellow); |
| 41 | + var negativePrompt = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
| 42 | + |
| 43 | + OutputHelpers.WriteConsole("Please enter a batch count and press ENTER", ConsoleColor.Yellow); |
| 44 | + var batch = OutputHelpers.ReadConsole(ConsoleColor.Cyan); |
| 45 | + int.TryParse(batch, out var batchCount); |
| 46 | + batchCount = Math.Max(1, batchCount); |
| 47 | + |
| 48 | + var promptOptions = new PromptOptions |
| 49 | + { |
| 50 | + Prompt = prompt, |
| 51 | + NegativePrompt = negativePrompt, |
| 52 | + BatchCount = batchCount |
| 53 | + }; |
| 54 | + |
| 55 | + var schedulerOptions = new SchedulerOptions |
| 56 | + { |
| 57 | + Seed = Random.Shared.Next(), |
| 58 | + |
| 59 | + GuidanceScale = 8, |
| 60 | + InferenceSteps = 22, |
| 61 | + Strength = 0.6f |
| 62 | + }; |
| 63 | + foreach (var schedulerType in Enum.GetValues<SchedulerType>()) |
| 64 | + { |
| 65 | + promptOptions.SchedulerType = schedulerType; |
| 66 | + |
| 67 | + OutputHelpers.WriteConsole("Generating Image...", ConsoleColor.Green); |
| 68 | + await GenerateImage(model, promptOptions, schedulerOptions); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options) |
| 74 | + { |
| 75 | + |
| 76 | + var result = await _stableDiffusionService.GenerateAsync(model, prompt, options); |
| 77 | + if (result == null) |
| 78 | + return false; |
| 79 | + |
| 80 | + var imageTensors = result.Split(prompt.BatchCount); |
| 81 | + for (int i = 0; i < imageTensors.Length; i++) |
| 82 | + { |
| 83 | + var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{prompt.SchedulerType}_{i}.png"); |
| 84 | + var image = imageTensors[i].ToImage(); |
| 85 | + await image.SaveAsPngAsync(outputFilename); |
| 86 | + OutputHelpers.WriteConsole($"{prompt.SchedulerType} Image Created: {Path.GetFileName(outputFilename)}", ConsoleColor.Green); |
| 87 | + } |
| 88 | + |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments