Skip to content

Commit

Permalink
Merge pull request #73 from teknologi-umum/ai-overhaul
Browse files Browse the repository at this point in the history
Fix vision bug
  • Loading branch information
ronnygunawan authored Dec 3, 2023
2 parents a32b1b5 + 510305d commit dd743b6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,11 @@ CancellationToken cancellationToken
} else if (message.ReplyToMessage is { Photo.Length: > 0 }
&& message.Text is { }) {
fileId = message.ReplyToMessage.Photo.OrderByDescending(photoSize => photoSize.Width).First().FileId;
prompt = message.Caption;
prompt = message.Text;
} else if (message.ReplyToMessage is { Sticker: { } }
&& message.Text is { }) {
fileId = message.ReplyToMessage.Sticker.FileId;
prompt = message.Caption;
prompt = message.Text;
} else {
fileId = null;
prompt = null;
Expand Down Expand Up @@ -907,7 +907,9 @@ await botClient.GetInfoAndDownloadFileAsync(
if (codecResult != SKCodecResult.Success) {
return (null, "Invalid image");
}
if (codec.EncodedFormat != SKEncodedImageFormat.Jpeg) {

if (codec.EncodedFormat != SKEncodedImageFormat.Jpeg
&& codec.EncodedFormat != SKEncodedImageFormat.Webp) {
return (null, "Image must be compressed image");
}
SKBitmap bitmap = SKBitmap.Decode(codec);
Expand All @@ -917,6 +919,17 @@ await botClient.GetInfoAndDownloadFileAsync(
return (null, "Image larger than 1280x1280");
}

// Handle stickers
if (codec.EncodedFormat == SKEncodedImageFormat.Webp) {
SKImage image = SKImage.FromBitmap(bitmap);
SKData data = image.Encode(SKEncodedImageFormat.Jpeg, 20);
using MemoryStream jpegStream = new();
data.SaveTo(jpegStream);

// Encode image as base64
return (Convert.ToBase64String(jpegStream.ToArray()), null);
}

// Encode image as base64
return (Convert.ToBase64String(originalImage), null);
}
Expand Down

0 comments on commit dd743b6

Please sign in to comment.