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

Commit cb2f211

Browse files
committed
Add image links to Blueprint output file
1 parent 10111e3 commit cb2f211

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

OnnxStack.WebUI/Hubs/StableDiffusionHub.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class StableDiffusionHub : Hub<IStableDiffusionClient>
1414
private readonly ILogger<StableDiffusionHub> _logger;
1515
private readonly IStableDiffusionService _stableDiffusionService;
1616

17-
1817
/// <summary>
1918
/// Initializes a new instance of the <see cref="StableDiffusionHub"/> class.
2019
/// </summary>
@@ -122,7 +121,10 @@ private async Task<StableDiffusionResult> GenerateImageToImageResult(PromptOptio
122121
return new StableDiffusionResult("Failed to copy input image");
123122

124123
//3. Generate blueprint
125-
var blueprint = new ImageBlueprint(promptOptions, schedulerOptions);
124+
var inputImageLink = await _fileService.CreateOutputUrl(inputImage, false);
125+
var outputImageLink = await _fileService.CreateOutputUrl(outputImage, false);
126+
promptOptions.InputImage = await _fileService.CreateOutputUrl(inputImageFile.Filename, false);
127+
var blueprint = new ImageBlueprint(promptOptions, schedulerOptions, outputImageLink, inputImageLink);
126128
var bluprintFile = await _fileService.SaveBlueprintFile(blueprint, outputBlueprint);
127129
if (bluprintFile is null)
128130
return new StableDiffusionResult("Failed to save blueprint");
@@ -159,7 +161,8 @@ private async Task<StableDiffusionResult> GenerateTextToImageResult(PromptOption
159161
var outputImageFile = await _fileService.UrlToPhysicalPath(outputImageUrl);
160162

161163
//2. Generate blueprint
162-
var blueprint = new ImageBlueprint(promptOptions, schedulerOptions);
164+
var outputImageLink = await _fileService.CreateOutputUrl(outputImage, false);
165+
var blueprint = new ImageBlueprint(promptOptions, schedulerOptions, outputImageLink);
163166
var bluprintFile = await _fileService.SaveBlueprintFile(blueprint, outputBlueprint);
164167
if (bluprintFile is null)
165168
return new StableDiffusionResult("Failed to save blueprint");

OnnxStack.WebUI/Models/ImageBlueprint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace OnnxStack.WebUI.Models
44
{
5-
public record ImageBlueprint(PromptOptions Prompt, SchedulerOptions Options)
5+
public record ImageBlueprint(PromptOptions Prompt, SchedulerOptions Options, string OutputImageUrl, string InputImageUrl = null)
66
{
77
public DateTime Timestamp { get; } = DateTime.UtcNow;
88
}

OnnxStack.WebUI/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using AspNetCore.Unobtrusive.Ajax;
2+
using Microsoft.AspNetCore.Hosting.Server;
3+
using Microsoft.AspNetCore.Hosting.Server.Features;
24
using OnnxStack.Core;
35
using OnnxStack.Web.Hubs;
46
using Services;
@@ -23,6 +25,7 @@ public static void Main(string[] args)
2325

2426
builder.Services.AddOnnxStackStableDiffusion();
2527
builder.Services.AddSingleton<IFileService, FileService>();
28+
builder.Services.AddSingleton((s) => s.GetService<IServer>().Features.Get<IServerAddressesFeature>());
2629

2730
var app = builder.Build();
2831

OnnxStack.WebUI/Services/FileService.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OnnxStack.Web.Models;
1+
using Microsoft.AspNetCore.Hosting.Server.Features;
2+
using OnnxStack.Web.Models;
23
using OnnxStack.WebUI.Models;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
@@ -10,16 +11,18 @@ public class FileService : IFileService
1011
private readonly ILogger<FileService> _logger;
1112
private readonly IWebHostEnvironment _webHostEnvironment;
1213
private readonly JsonSerializerOptions _serializerOptions;
14+
private readonly IServerAddressesFeature _serverAddressesFeature;
1315

1416
/// <summary>
1517
/// Initializes a new instance of the <see cref="FileService"/> class.
1618
/// </summary>
1719
/// <param name="logger">The logger.</param>
1820
/// <param name="webHostEnvironment">The web host environment.</param>
19-
public FileService(ILogger<FileService> logger, IWebHostEnvironment webHostEnvironment)
21+
public FileService(ILogger<FileService> logger, IWebHostEnvironment webHostEnvironment, IServerAddressesFeature serverAddressesFeature)
2022
{
2123
_logger = logger;
2224
_webHostEnvironment = webHostEnvironment;
25+
_serverAddressesFeature = serverAddressesFeature;
2326
_serializerOptions = new JsonSerializerOptions { WriteIndented = true, Converters = { new JsonStringEnumConverter() } };
2427
}
2528

@@ -155,9 +158,11 @@ public Task<string> UrlToPhysicalPath(string url)
155158
/// <param name="folder">The folder.</param>
156159
/// <param name="file">The file.</param>
157160
/// <returns></returns>
158-
public Task<string> CreateOutputUrl(string file)
161+
public Task<string> CreateOutputUrl(string file, bool relative = true)
159162
{
160-
return Task.FromResult($"/images/results/{file}");
163+
return relative
164+
? Task.FromResult($"/images/results/{file}")
165+
: Task.FromResult($"{GetServerUrl()}/images/results/{file}");
161166
}
162167

163168

@@ -169,6 +174,17 @@ public Task<string> CreateRandomName()
169174
{
170175
return Task.FromResult(Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
171176
}
177+
178+
179+
private string GetServerUrl()
180+
{
181+
var address = _serverAddressesFeature.Addresses.FirstOrDefault(x=> x.StartsWith("https"))
182+
?? _serverAddressesFeature.Addresses.FirstOrDefault();
183+
if (string.IsNullOrEmpty(address))
184+
return string.Empty;
185+
186+
return address;
187+
}
172188
}
173189

174190
public record FileServiceResult(string Filename, string FileUrl, string FilePath);

OnnxStack.WebUI/Services/IFileService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public interface IFileService
5353
/// </summary>
5454
/// <param name="file">The file.</param>
5555
/// <returns></returns>
56-
Task<string> CreateOutputUrl(string file);
56+
Task<string> CreateOutputUrl(string file, bool relative = true);
5757
}
5858
}

0 commit comments

Comments
 (0)