Skip to content

Commit

Permalink
Test out AiServer replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
Layoric committed Jul 12, 2024
1 parent 583c5e3 commit 98b0674
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 35 deletions.
4 changes: 3 additions & 1 deletion BlazorDiffusion.Tests/CreativeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ public override void Configure(Container container)
});

container.Register<IStableDiffusionClient>(
new AiServerClient("https://openai.servicestack.net/",new MemoryVirtualFiles())
new AiServerClient
{
Client = new JsonApiClient("https://openai.servicestack.net/"),
VirtualFiles = new MemoryVirtualFiles(),
OutputPathPrefix = Path.Join(ContentRootDirectory.RealPath.CombineWith("App_Files"),"artifacts")
});
container.AddSingleton<AppUserQuotas>();
Expand Down
92 changes: 84 additions & 8 deletions BlazorDiffusion/AiServer/dtos.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Options:
Date: 2024-07-10 07:46:36
Date: 2024-07-12 04:09:19
Version: 8.31
Tip: To override a DTO option, remove "//" prefix before updating
BaseUrl: https://openai.servicestack.net
Expand Down Expand Up @@ -31,6 +31,7 @@
using System.Runtime.Serialization;
using ServiceStack;
using ServiceStack.DataAnnotations;
using System.IO;
using AiServer.ServiceModel.Types;
using AiServer.ServiceModel;

Expand Down Expand Up @@ -1058,7 +1059,14 @@ public partial class ComfyImageToImage

public partial class ComfyImageToImageResponse
{
public virtual string FilePath { get; set; }
public ComfyImageToImageResponse()
{
Images = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Images { get; set; }
}

public partial class ComfyImageToImageUpscale
Expand All @@ -1071,7 +1079,14 @@ public partial class ComfyImageToImageUpscale

public partial class ComfyImageToImageUpscaleResponse
{
public virtual string FilePath { get; set; }
public ComfyImageToImageUpscaleResponse()
{
Images = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Images { get; set; }
}

public partial class ComfyImageToImageWithMask
Expand All @@ -1094,7 +1109,14 @@ public partial class ComfyImageToImageWithMask

public partial class ComfyImageToImageWithMaskResponse
{
public virtual string FilePath { get; set; }
public ComfyImageToImageWithMaskResponse()
{
Images = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Images { get; set; }
}

public partial class ComfyImageToText
Expand All @@ -1105,7 +1127,9 @@ public partial class ComfyImageToText

public partial class ComfyImageToTextResponse
{
public virtual string Text { get; set; }
public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual ComfyTextOutput TextOutput { get; set; }
}

public enum ComfyMaskSource
Expand Down Expand Up @@ -1163,7 +1187,9 @@ public partial class ComfySpeechToText

public partial class ComfySpeechToTextResponse
{
public virtual string Text { get; set; }
public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual ComfyTextOutput TextOutput { get; set; }
}

public enum ComfyTaskType
Expand Down Expand Up @@ -1200,7 +1226,14 @@ public partial class ComfyTextToAudio

public partial class ComfyTextToAudioResponse
{
public virtual string FilePath { get; set; }
public ComfyTextToAudioResponse()
{
Sounds = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Sounds { get; set; }
}

public partial class ComfyTextToImage
Expand All @@ -1226,6 +1259,8 @@ public ComfyTextToImageResponse()
Images = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Images { get; set; }
}

Expand All @@ -1238,7 +1273,47 @@ public partial class ComfyTextToSpeech

public partial class ComfyTextToSpeechResponse
{
public virtual string FilePath { get; set; }
public ComfyTextToSpeechResponse()
{
Speech = new List<ComfyHostedFileOutput>{};
}

public virtual string PromptId { get; set; }
public virtual ComfyWorkflowRequest Request { get; set; }
public virtual List<ComfyHostedFileOutput> Speech { get; set; }
}

public partial class ComfyWorkflowRequest
{
public virtual long Id { get; set; }
public virtual string Model { get; set; }
public virtual int? Steps { get; set; }
public virtual int BatchSize { get; set; }
public virtual int? Seed { get; set; }
public virtual string PositivePrompt { get; set; }
public virtual string NegativePrompt { get; set; }
public virtual ComfyFileInput Image { get; set; }
public virtual ComfyFileInput Speech { get; set; }
public virtual ComfyFileInput Mask { get; set; }
public virtual Stream ImageInput { get; set; }
public virtual Stream SpeechInput { get; set; }
public virtual Stream MaskInput { get; set; }
public virtual ComfySampler? Sampler { get; set; }
public virtual ArtStyle? ArtStyle { get; set; }
public virtual string Scheduler { get; set; }
public virtual double? CfgScale { get; set; }
public virtual double? Denoise { get; set; }
public virtual string UpscaleModel { get; set; }
public virtual int? Width { get; set; }
public virtual int? Height { get; set; }
public virtual ComfyTaskType TaskType { get; set; }
public virtual string RefId { get; set; }
public virtual string Provider { get; set; }
public virtual string ReplyTo { get; set; }
public virtual string Tag { get; set; }
public virtual string Clip { get; set; }
public virtual double? SampleLength { get; set; }
public virtual ComfyMaskSource MaskChannel { get; set; }
}

public partial class ComfyWorkflowResponse
Expand Down Expand Up @@ -1340,6 +1415,7 @@ public QueueComfyWorkflowResponse()
TextOutputs = new List<ComfyTextOutput>{};
}

public virtual ComfyWorkflowRequest Request { get; set; }
public virtual ComfyWorkflowStatus Status { get; set; }
public virtual ComfyWorkflowResponse WorkflowResponse { get; set; }
public virtual string PromptId { get; set; }
Expand Down
46 changes: 23 additions & 23 deletions BlazorDiffusion/AiServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@
using BlazorDiffusion.ServiceInterface;
using BlazorDiffusion.ServiceModel;
using ServiceStack.IO;
using ServiceStack.Text;
using JsonApiClient = ServiceStack.JsonApiClient;

namespace BlazorDiffusion;

public class AiServerClient: IStableDiffusionClient
{
public JsonApiClient Client { get; }
public IVirtualFiles VirtualFiles { get; }
public JsonApiClient Client { get; set; }
public IVirtualFiles VirtualFiles { get; set; }

public string? OutputPathPrefix { get; set; }

public AiServerClient(string aiServerBaseUrl,IVirtualFiles virtualFiles)
{
Client = new JsonApiClient(aiServerBaseUrl);
VirtualFiles = virtualFiles;
}

public async Task<ImageGenerationResponse> GenerateImageAsync(ImageGeneration request)
{
var req = request.ToComfy();
var res = await Client.PostAsync(req);
var now = DateTime.UtcNow;
var key = $"{now:yyyy/MM/dd}/{(long)now.TimeOfDay.TotalMilliseconds}";

var promptId = Guid.NewGuid().ToString();

var results = new List<ImageGenerationResult>();
var seed = (res.Request.Seed ?? 0).ConvertTo<uint>();
foreach (var item in res.Images)
{
var artifactUrl = $"{Client.BaseUri.TrimEnd('/')}/uploads{item.Url}";
var seed = (uint)Random.Shared.Next();
var output = Path.Join(OutputPathPrefix, key, $"output_{Guid.NewGuid()}.png");
var output = Path.Join(OutputPathPrefix, key, $"output_{seed}.png");
var bytes = await artifactUrl.GetBytesFromUrlAsync();
await VirtualFiles.WriteFileAsync(output, bytes);
var imageDetails = ImageDetails.Calculate(bytes);
Expand All @@ -40,38 +35,44 @@ public async Task<ImageGenerationResponse> GenerateImageAsync(ImageGeneration re
{
Prompt = request.Prompt,
Seed = seed,
AnswerId = promptId,
AnswerId = res.PromptId,
FilePath = $"/artifacts/{key}/output_{seed}.png",
FileName = $"output_{seed}.png",
ContentLength = bytes.Length,
Width = request.Width,
Height = request.Height,
ImageDetails = imageDetails,
});
// Assume incremental seeds for multiple images as comfyui does not provide the specific image seed back
seed++;
}

return new ImageGenerationResponse
{
RequestId = Guid.NewGuid().ToString(),
RequestId = res.PromptId,
EngineId = "comfy",
Key = key,
Results = results,
};
}

public IVirtualFile? GetMetadataFile(Creative creative)
{
throw new NotImplementedException();
}
public string GetMetadataPath(Creative creative) => OutputPathPrefix.CombineWith(creative.Key, "metadata.json");
public IVirtualFile GetMetadataFile(Creative creative) => VirtualFiles.GetFile(GetMetadataPath(creative));

public Task SaveMetadataAsync(Creative entry)
public async Task SaveMetadataAsync(Creative creative)
{
throw new NotImplementedException();
var vfsPathSuffix = creative.Key;
var outputDir = Path.Join(OutputPathPrefix, vfsPathSuffix);
await VirtualFiles.WriteFileAsync(Path.Join(outputDir, "metadata.json"), creative.ToJson().IndentJson());
}

public Task DeleteFolderAsync(Creative entry)
public Task DeleteFolderAsync(Creative creative)
{
throw new NotImplementedException();
var vfsPathSuffix = creative.Key;
var directory = VirtualFiles.GetDirectory(Path.Join(OutputPathPrefix, vfsPathSuffix));
var allFiles = directory.GetAllFiles();
VirtualFiles.DeleteFiles(allFiles);
return Task.CompletedTask;
}
}

Expand All @@ -84,7 +85,6 @@ public static ComfyTextToImage ToComfy(this ImageGeneration request)
Height = request.Height,
Width = request.Width,
Seed = request.Seed ?? Random.Shared.Next(),
Steps = request.Steps,
BatchSize = request.Images,
PositivePrompt = request.Prompt,
};
Expand Down
9 changes: 6 additions & 3 deletions BlazorDiffusion/Configure.AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ public override void Configure(Container container)
transformFile:ImageUtils.TransformAvatarAsync)
));

var aiServerClient = new JsonApiClient("https://openai.servicestack.net/");
if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AI_SERVER_APIKEY")))
aiServerClient.BearerToken = Environment.GetEnvironmentVariable("AI_SERVER_APIKEY");
// Don't use public prefix if working locally
Register<IStableDiffusionClient>(new DreamStudioClient
Register<IStableDiffusionClient>(new AiServerClient
{
ApiKey = Environment.GetEnvironmentVariable("DREAMAI_APIKEY") ?? "<your_api_key>",
Client = aiServerClient,
OutputPathPrefix = "artifacts",
PublicPrefix = appConfig.AssetsBasePath,
VirtualFiles = appFs
//PublicPrefix = appConfig.AssetsBasePath,
});

ScriptContext.Args[nameof(AppData)] = AppData.Instance;
Expand Down

0 comments on commit 98b0674

Please sign in to comment.