Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 58df719

Browse files
committed
Add diffusion information to result output
1 parent 4288f94 commit 58df719

File tree

10 files changed

+116
-43
lines changed

10 files changed

+116
-43
lines changed

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task RunAsync()
3535
{
3636
Prompt = prompt,
3737
NegativePrompt = negativePrompt,
38-
SchedulerType = SchedulerType.LMSScheduler
38+
SchedulerType = SchedulerType.LMS
3939
};
4040

4141
var schedulerOptions = new SchedulerOptions
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
namespace OnnxStack.StableDiffusion.Enums
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace OnnxStack.StableDiffusion.Enums
24
{
35
public enum SchedulerType
46
{
5-
LMSScheduler = 0,
6-
EulerAncestralScheduler = 1,
7-
DDPMScheduler = 3
7+
[Display(Name = "LMS")]
8+
LMS = 0,
9+
10+
[Display(Name = "Euler Ancestral")]
11+
EulerAncestral = 1,
12+
13+
[Display(Name = "DDPM")]
14+
DDPM = 3
815
}
916
}

OnnxStack.StableDiffusion/Services/SchedulerService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ private static IScheduler GetScheduler(PromptOptions prompt, SchedulerOptions op
242242
{
243243
return prompt.SchedulerType switch
244244
{
245-
SchedulerType.LMSScheduler => new LMSScheduler(options),
246-
SchedulerType.EulerAncestralScheduler => new EulerAncestralScheduler(options),
247-
SchedulerType.DDPMScheduler => new DDPMScheduler(options),
245+
SchedulerType.LMS => new LMSScheduler(options),
246+
SchedulerType.EulerAncestral => new EulerAncestralScheduler(options),
247+
SchedulerType.DDPM => new DDPMScheduler(options),
248248
_ => default
249249
};
250250
}

OnnxStack.WebUI/Extensions.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace OnnxStack.WebUI
44
{
5-
public static class Extensions
6-
{
5+
public static class Extensions
6+
{
77
public static MvcForm AjaxBeginFormModal(this IHtmlHelper htmlHelper, string handler)
88
{
99
return htmlHelper.AjaxBeginForm(new AjaxOptions
@@ -16,14 +16,14 @@ public static MvcForm AjaxBeginFormModal(this IHtmlHelper htmlHelper, string han
1616
}
1717

1818
public static MvcForm AjaxBeginFormModal(this IHtmlHelper htmlHelper, string page, string handler)
19-
{
20-
return htmlHelper.AjaxBeginForm(new AjaxOptions
21-
{
22-
Url = $"{page}?handler={handler}",
23-
HttpMethod = "POST",
24-
UpdateTargetId = "simplemodal-data",
25-
InsertionMode = InsertionMode.Replace
26-
});
27-
}
28-
}
19+
{
20+
return htmlHelper.AjaxBeginForm(new AjaxOptions
21+
{
22+
Url = $"{page}?handler={handler}",
23+
HttpMethod = "POST",
24+
UpdateTargetId = "simplemodal-data",
25+
InsertionMode = InsertionMode.Replace
26+
});
27+
}
28+
}
2929
}

OnnxStack.WebUI/Hubs/StableDiffusionHub.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OnnxStack.StableDiffusion.Common;
33
using OnnxStack.StableDiffusion.Config;
44
using OnnxStack.WebUI.Models;
5+
using System.Diagnostics;
56
using System.Runtime.CompilerServices;
67
using System.Text.Json;
78
using System.Text.Json.Serialization;
@@ -83,6 +84,7 @@ public async IAsyncEnumerable<TextToImageResult> OnExecuteTextToImage(TextToImag
8384
/// <returns></returns>
8485
private async Task<TextToImageResult> GenerateTextToImageResult(TextToImageOptions options, CancellationToken cancellationToken)
8586
{
87+
var timestamp = Stopwatch.GetTimestamp();
8688
options.Seed = GenerateSeed(options.Seed);
8789
var promptOptions = new PromptOptions
8890
{
@@ -102,14 +104,16 @@ private async Task<TextToImageResult> GenerateTextToImageResult(TextToImageOptio
102104
InitialNoiseLevel = options.InitialNoiseLevel
103105
};
104106

107+
105108
var fileInfo = CreateFileInfo(promptOptions, schedulerOptions);
106109
if (!await SaveOptionsFile(fileInfo, options))
107110
return null;
108111

109112
if (!await RunStableDiffusion(promptOptions, schedulerOptions, fileInfo, cancellationToken))
110113
return null;
111114

112-
return new TextToImageResult(fileInfo.OutputImage, fileInfo.OutputImageUrl, options, fileInfo.OutputOptionsUrl);
115+
var elapsed = (int)Stopwatch.GetElapsedTime(timestamp).TotalSeconds;
116+
return new TextToImageResult(fileInfo.OutputImage, fileInfo.OutputImageUrl, options, fileInfo.OutputOptionsUrl, elapsed);
113117
}
114118

115119

@@ -241,6 +245,6 @@ private string CreateOutputUrl(string folder, string file)
241245
}
242246

243247
public record ProgressResult(int Progress, int Total);
244-
public record TextToImageResult(string OutputImage, string OutputImageUrl, TextToImageOptions OutputOptions, string OutputOptionsUrl);
248+
public record TextToImageResult(string OutputImage, string OutputImageUrl, TextToImageOptions OutputOptions, string OutputOptionsUrl, int Elapsed);
245249
public record FileInfoResult(string OutputImage, string OutputImageUrl, string OutputImageFile, string OutputOptions, string OutputOptionsUrl, string OutputOptionsFile);
246250
}

OnnxStack.WebUI/Pages/StableDiffusion/TextToImage.cshtml

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
resize: none;
1212
font-size: smaller !important
1313
}
14+
15+
.result-info{
16+
font-style: italic;
17+
padding-right: 3px;
18+
opacity: 0.6;
19+
font-size:x-small;
20+
}
1421
</style>
1522
@Html.AntiForgeryToken()
1623

@@ -135,19 +142,35 @@
135142
<div style="overflow:hidden;text-align:center">
136143
<img id="img-result" width="{{width}}" height="{{height}}" src="{{outputImageUrl}}" />
137144
</div>
138-
<div class="d-flex flex-column pt-2">
139-
<div class="progress">
140-
<div class="progress-bar" role="progressbar" style="width: 100%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">Completed</div>
141-
</div>
145+
<div class="d-flex flex-row flex-wrap-reverse justify-content-around pt-2" style="max-width:{{width}}px">
146+
<div class="d-flex flex-row align-items-center">
147+
<small class="result-info">Scheduler:</small>
148+
<span>{{outputOptions.schedulerType}}</span>
149+
</div>
150+
<div class="d-flex flex-row align-items-center">
151+
<small class="result-info">Steps:</small>
152+
<span>{{outputOptions.inferenceSteps}}</span>
153+
</div>
154+
<div class="d-flex flex-row align-items-center">
155+
<small class="result-info">Guidance:</small>
156+
<span>{{outputOptions.guidanceScale}}</span>
157+
</div>
158+
<div class="d-flex flex-row align-items-center">
159+
<small class="result-info">Seed:</small>
160+
<span>{{outputOptions.seed}}</span>
161+
</div>
162+
<div class="d-flex flex-row align-items-center">
163+
<small class="result-info">Elapsed:</small>
164+
<span>{{elapsed}}s</span>
165+
</div>
142166
</div>
143167
<div class="d-flex flex-row gap-2 pt-2">
144168
<div class="btn-group dropend w-100">
145-
<a class="btn btn-sm btn-success w-100" href="{{outputImageUrl}}" download="{{OutputImage}}">Download</a>
169+
<a class="btn btn-sm btn-success w-100" href="{{outputImageUrl}}" download="{{outputImage}}">Download</a>
146170
<button type="button" class="btn btn-sm btn-success dropdown-toggle dropdown-toggle-split w-25" data-bs-toggle="dropdown" aria-expanded="false">
147-
148171
</button>
149172
<ul class="dropdown-menu">
150-
<li><a class="dropdown-item" href="{{outputImageUrl}}" download="{{OutputImage}}">Download PNG</a></li>
173+
<li><a class="dropdown-item" href="{{outputImageUrl}}" download="{{outputImage}}">Download PNG</a></li>
151174
<li><a class="dropdown-item" href="{{outputOptionsUrl}}" download="{{OutputOptions}}">Download JSON</a></li>
152175
</ul>
153176
</div>

OnnxStack.WebUI/Pages/StableDiffusion/UploadImageModal.cshtml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@using (Html.AjaxBeginFormModal("Index", "UploadImage"))
77
{
88
@Html.AntiForgeryToken()
9-
<div class="modal-dialog" role="document" >
9+
<div class="modal-dialog" role="document">
1010
<div class="modal-content">
1111
<div class="modal-header">
1212
<h5 class="modal-title">Example Modal</h5>
@@ -18,10 +18,10 @@
1818
@Html.HiddenFor(m => m.Width)
1919
@Html.HiddenFor(m => m.Height)
2020
@Html.HiddenFor(m => m.ImageBase64)
21-
<div id="uploadImageContainer" >
21+
<div id="uploadImageContainer">
2222
</div>
2323
<div class="d-flex flex-row gap-1">
24-
<input id="ImageFile" type="file" class="form-control w-100" />
24+
<input id="ImageFile" name="ImageFile" type="file" class="form-control w-100" />
2525
<button id="btn-crop" type="button" class="btn btn-info w-50" disabled>Crop Image</button>
2626
</div>
2727
@Html.ValidationMessageFor(m => m.ImageBase64, "", new { @class = "text-danger" })
@@ -36,29 +36,27 @@
3636
var uploadImageHeight = +$('#Height').val();
3737
var uploadImageInput = $('#ImageFile');
3838
var uploadImageContainer = $('#uploadImageContainer');
39-
var scaleFactor = uploadImageHeight >= 512 || uploadImageWidth >= 512 ? 0.8 : 1;
39+
var uploadScaleFactor = scaleFactor(uploadImageHeight);
4040
4141
var croppie = uploadImageContainer.croppie({
4242
url: '/images/placeholder.jpg',
4343
viewport: {
44-
width: uploadImageWidth * scaleFactor, // Set your desired width
45-
height: uploadImageHeight * scaleFactor, // Set your desired height
46-
type: 'square' // You can use 'square', 'circle', or 'rectangle'
44+
width: uploadImageWidth * uploadScaleFactor,
45+
height: uploadImageHeight * uploadScaleFactor,
46+
type: uploadImageWidth == uploadImageHeight ? 'square' : 'rectangle'
4747
},
4848
boundary: {
49-
width: (uploadImageWidth + 50) * scaleFactor, // Set the width of the boundary container
50-
height: (uploadImageHeight + 50) * scaleFactor // Set the height of the boundary container
49+
width: (uploadImageWidth + 50) * uploadScaleFactor,
50+
height: (uploadImageHeight + 50) * uploadScaleFactor
5151
}
5252
});
5353
54-
5554
uploadImageInput.on('change', function (e) {
5655
$('#btn-crop, #submit').attr('disabled', 'disabled');
5756
const files = e.target.files;
5857
if (files.length > 0) {
5958
const reader = new FileReader();
6059
reader.onload = function (e) {
61-
// Set the image source for Croppie
6260
croppie.croppie('bind', {
6361
url: e.target.result
6462
});
@@ -74,7 +72,6 @@
7472
$('#submit').removeAttr('disabled');
7573
});
7674
});
77-
7875
</script>
7976
</div>
8077
}

OnnxStack.WebUI/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using OnnxStack.Web.Hubs;
22
using OnnxStack.Core;
33
using AspNetCore.Unobtrusive.Ajax;
4+
using MathNet.Numerics;
5+
using System.Text.Json.Serialization;
46

57
namespace OnnxStack.WebUI
68
{
@@ -13,7 +15,11 @@ public static void Main(string[] args)
1315
// Add services to the container.
1416
builder.Services.AddRazorPages();
1517
builder.Services.AddUnobtrusiveAjax();
16-
builder.Services.AddSignalR();
18+
builder.Services.AddSignalR()
19+
.AddJsonProtocol(options =>
20+
{
21+
options.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
22+
});
1723

1824
builder.Services.AddOnnxStackStableDiffusion();
1925

OnnxStack.WebUI/wwwroot/css/site.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ button.accept-policy {
9595

9696

9797
.modal-dialog {
98-
display: block;
98+
margin-top: 40px !important;
99+
margin-left: auto;
100+
margin-right: auto;
101+
display: table;
99102
}
100103

101104
.simplemodal-overlay{

OnnxStack.WebUI/wwwroot/js/site.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ const getJson = (url, vars) => {
3131
});
3232
}
3333

34+
35+
const scaleFactor = (value) => {
36+
if (value == 512) {
37+
return 0.75;
38+
}
39+
else if (value == 576) {
40+
return 0.7;
41+
}
42+
else if (value == 640) {
43+
return 0.65;
44+
}
45+
else if (value == 704) {
46+
return 0.6;
47+
}
48+
else if (value == 768) {
49+
return 0.55;
50+
}
51+
else if (value == 832) {
52+
return 0.5;
53+
}
54+
else if (value == 896) {
55+
return 0.45;
56+
}
57+
else if (value == 960) {
58+
return 0.4;
59+
}
60+
else if (value == 1024) {
61+
return 0.35;
62+
}
63+
return 1;
64+
}
65+
66+
3467
const Enums = {
3568
ProcessResult: Object.freeze({
3669
Progress: 0,

0 commit comments

Comments
 (0)