-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28da4b9
commit 1b2af2e
Showing
7 changed files
with
229 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
|
||
namespace NovelAIBot.Extensions | ||
{ | ||
public static class WebSocketExtensions | ||
{ | ||
public static async Task SendTextMessageAsync(this WebSocket client, string text, Encoding encoding) | ||
{ | ||
byte[] buffer = encoding.GetBytes(text); | ||
await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); | ||
} | ||
|
||
public static async Task<string> ReceiveTextMessageAsync(this WebSocket client, int bufferSize, Encoding encoding) | ||
{ | ||
byte[] buffer = new byte[bufferSize]; | ||
var result = await client.ReceiveAsync(buffer, CancellationToken.None); | ||
Array.Resize(ref buffer, result.Count); | ||
return encoding.GetString(buffer, 0, buffer.Length) ?? string.Empty; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NovelAIBot.Models | ||
{ | ||
public class BackendQueueStatus | ||
{ | ||
public Guid Id { get; set; } | ||
public int QueuePosition { get; set; } | ||
|
||
public NaiQueueState State { get; set; } | ||
|
||
public BackendQueueStatus() | ||
{ | ||
} | ||
|
||
public BackendQueueStatus(Guid? id, int queuePosition, NaiQueueState state = NaiQueueState.Enqueued) | ||
{ | ||
Id = id ?? Guid.Empty; | ||
QueuePosition = queuePosition; | ||
State = state; | ||
} | ||
} | ||
|
||
public enum NaiQueueState { Enqueued, Processing, CompletedSuccess, CompletedError } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Discord.Interactions; | ||
using System.Text.Json.Serialization; | ||
|
||
|
||
namespace NovelAIBot.Models | ||
{ | ||
internal class BackendRequest : INaiRequest | ||
{ | ||
public Guid Id { get; set; } | ||
public string Prompt { get; private set; } | ||
public string NegativePrompt { get; private set; } | ||
public string AuthKey { get; private set; } | ||
|
||
[JsonIgnore] | ||
public SocketInteractionContext Context { get; set; } | ||
|
||
public int Height { get; set; } | ||
|
||
public int Width { get; set; } | ||
|
||
public BackendRequest(string prompt, string negativePrompt, string authKey, int height, int width) | ||
{ | ||
Id = Guid.Empty; | ||
Prompt = prompt; | ||
NegativePrompt = negativePrompt; | ||
AuthKey = authKey; | ||
Height = height; | ||
Width = width; | ||
} | ||
|
||
public BackendRequest(string prompt, string negativePrompt, string authKey, SocketInteractionContext context, int height, int width) | ||
{ | ||
Id = Guid.Empty; | ||
Prompt = prompt; | ||
NegativePrompt = negativePrompt; | ||
AuthKey = authKey; | ||
Context = context; | ||
Height = height; | ||
Width = width; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using NovelAIBot.Extensions; | ||
using NovelAIBot.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace NovelAIBot.Services | ||
{ | ||
internal class BackendService : IGenerationService | ||
{ | ||
public event EventHandler<BackendQueueStatus> BackendQueueStatusChanged; | ||
|
||
private string WebSocketBaseAddress { get { return _configuration.GetSection("GenerationApi")["BackendUrl"]; } } | ||
private string ImageBaseAddress { get { return _configuration.GetSection("GenerationApi")["BackendImageUrl"]; } } | ||
|
||
|
||
|
||
private readonly IConfiguration _configuration; | ||
private readonly ILogger<BackendService> _logger; | ||
private readonly HttpClient _httpClient; | ||
|
||
public BackendService(IConfiguration configuration, ILogger<BackendService> logger) | ||
{ | ||
_configuration = configuration; | ||
_logger = logger; | ||
_httpClient = new HttpClient(); | ||
_httpClient.BaseAddress = new Uri(ImageBaseAddress); | ||
} | ||
|
||
|
||
public async Task<byte[]> GetImageBytesAsync(INaiRequest request) | ||
{ | ||
using ClientWebSocket client = new ClientWebSocket(); | ||
await client.ConnectAsync(new Uri(WebSocketBaseAddress), CancellationToken.None); | ||
string json = JsonSerializer.Serialize(request as BackendRequest); | ||
|
||
await client.SendTextMessageAsync(json, Encoding.UTF8); | ||
json = await client.ReceiveTextMessageAsync(1024 * 20, Encoding.UTF8); | ||
BackendQueueStatus status = JsonSerializer.Deserialize<BackendQueueStatus>(json); | ||
|
||
do | ||
{ | ||
json = await client.ReceiveTextMessageAsync(1024 * 20, Encoding.UTF8); | ||
BackendQueueStatus newStatus = JsonSerializer.Deserialize<BackendQueueStatus>(json); | ||
if (newStatus.QueuePosition != status.QueuePosition || newStatus.State != status.State) | ||
BackendQueueStatusChanged?.Invoke(this, newStatus); | ||
status = newStatus; | ||
} while (status.State == NaiQueueState.Enqueued || status.State == NaiQueueState.Processing); | ||
|
||
if (status.State == NaiQueueState.CompletedError) | ||
{ | ||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Request complete.", CancellationToken.None); | ||
throw new Exception("Image generation completed in an error state"); | ||
} | ||
|
||
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Request complete.", CancellationToken.None); | ||
|
||
return await _httpClient.GetByteArrayAsync($"/api/nai/getimage/{status.Id}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters