Skip to content

Commit

Permalink
Handle Stability errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 4, 2023
1 parent 6a665f7 commit 3ac6aec
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) {

Check warning on line 914 in BotNet.Services/BotCommands/OpenAI.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'exc' is declared but never used

Check warning on line 914 in BotNet.Services/BotCommands/OpenAI.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'exc' is declared but never used
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 @@ -67,6 +67,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 3ac6aec

Please sign in to comment.