Skip to content

Commit

Permalink
Merge branch 'main' into ai-overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 4, 2023
2 parents 8d58ab8 + 3ac6aec commit b53a90c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
8 changes: 8 additions & 0 deletions BotNet.Services/BotCommands/Art.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using BotNet.Services.RateLimit;
using BotNet.Services.Stability.Models;
using BotNet.Services.ThisXDoesNotExist;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot;
Expand Down Expand Up @@ -50,6 +51,13 @@ await botClient.SendPhotoAsync(
photo: new InputFileStream(imageStream, "art.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch (ContentFilteredException) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Content filtered</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
Expand Down
47 changes: 32 additions & 15 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using BotNet.Services.OpenAI.Models;
using BotNet.Services.OpenAI.Skills;
using BotNet.Services.RateLimit;
using BotNet.Services.Stability.Models;
using BotNet.Services.Stability.Skills;
using Microsoft.Extensions.DependencyInjection;
using RG.Ninja;
Expand Down Expand Up @@ -851,26 +852,35 @@ await serviceProvider.GetRequiredService<FriendlyBot>().StreamChatAsync(
// prompt: message.Text!,
// cancellationToken: cancellationToken
//);
byte[] generatedImage = await serviceProvider.GetRequiredService<Stability.Skills.ImageGenerationBot>().GenerateImageAsync(
prompt: message.Text!,
cancellationToken: cancellationToken
);
using MemoryStream generatedImageStream = new(generatedImage);
try {
await botClient.DeleteMessageAsync(
byte[] generatedImage = await serviceProvider.GetRequiredService<Stability.Skills.ImageGenerationBot>().GenerateImageAsync(
prompt: message.Text!,
cancellationToken: cancellationToken
);
using MemoryStream generatedImageStream = new(generatedImage);
try {
await botClient.DeleteMessageAsync(
chatId: busyMessage.Chat.Id,
messageId: busyMessage.MessageId,
cancellationToken: cancellationToken
);
} catch (OperationCanceledException) {
throw;
}
await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileStream(generatedImageStream, "art.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken
);
} catch (ContentFilteredException) {
await botClient.EditMessageTextAsync(
chatId: busyMessage.Chat.Id,
messageId: busyMessage.MessageId,
cancellationToken: cancellationToken
text: "<code>Content filtered.</code>",
parseMode: ParseMode.Html
);
} catch (OperationCanceledException) {
throw;
}
await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputFileStream(generatedImageStream, "art.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken
);
break;
}
}
Expand Down Expand Up @@ -901,6 +911,13 @@ await botClient.SendTextMessageAsync(
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch (HttpRequestException exc) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Unknown error.</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch (OperationCanceledException) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
Expand Down
5 changes: 5 additions & 0 deletions BotNet.Services/Stability/Models/ContentFilteredException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

namespace BotNet.Services.Stability.Models {
public sealed class ContentFilteredException : Exception { }
}
3 changes: 1 addition & 2 deletions BotNet.Services/Stability/Models/TextToImageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ List<Artifact> Artifacts

internal sealed record Artifact(
string Base64,
string FinishReason,
int Seed
string FinishReason
);
}
4 changes: 4 additions & 0 deletions BotNet.Services/Stability/StabilityClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ CancellationToken cancellationToken

TextToImageResponse? responseData = JsonSerializer.Deserialize<TextToImageResponse>(responseJson, CAMEL_CASE_SERIALIZER_OPTIONS);

if (responseData is { Artifacts: [Artifact { FinishReason: "CONTENT_FILTERED" }] }) {
throw new ContentFilteredException();
}

if (responseData is not { Artifacts: [Artifact { FinishReason: "SUCCESS", Base64: var base64 }] }) {
throw new HttpRequestException();
}
Expand Down

0 comments on commit b53a90c

Please sign in to comment.