-
Notifications
You must be signed in to change notification settings - Fork 0
Chore/image moderation #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,10 @@ | ||||||||||
| using Azure; | ||||||||||
| using Azure.AI.ContentSafety; | ||||||||||
| using Azure.Storage.Blobs; | ||||||||||
| using Azure.Storage.Blobs.Models; | ||||||||||
| using Evently.Server.Common.Domains.Interfaces; | ||||||||||
| using Evently.Server.Common.Domains.Models; | ||||||||||
| using Evently.Server.Common.Extensions; | ||||||||||
| using Microsoft.Extensions.Options; | ||||||||||
|
|
||||||||||
| namespace Evently.Server.Features.Files.Services; | ||||||||||
|
|
@@ -11,6 +13,9 @@ namespace Evently.Server.Features.Files.Services; | |||||||||
| public sealed class ObjectStorageService(IOptions<Settings> settings, ILogger<ObjectStorageService> logger) : IObjectStorageService { | ||||||||||
| private readonly BlobServiceClient _blobServiceClient = | ||||||||||
| new(settings.Value.StorageAccount.AzureStorageConnectionString); | ||||||||||
| private readonly ContentSafetyClient _contentSafetyClient = new( | ||||||||||
| endpoint: new Uri(settings.Value.AzureAiFoundry.ContentSafetyEndpoint), | ||||||||||
| credential: new AzureKeyCredential(settings.Value.AzureAiFoundry.ContentSafetyKey)); | ||||||||||
|
|
||||||||||
| public async Task<Uri> UploadFile(string containerName, string fileName, BinaryData binaryData, | ||||||||||
| string mimeType = "application/octet-stream") { | ||||||||||
|
|
@@ -65,4 +70,23 @@ public async Task<BinaryData> GetFile(string containerName, string fileName) { | |||||||||
| BinaryData data = BinaryData.FromBytes(bytes); | ||||||||||
| return data; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public async Task<bool> PassesContentModeration(BinaryData binaryData) { | ||||||||||
| ContentSafetyImageData image = new(binaryData); | ||||||||||
| AnalyzeImageOptions request = new(image); | ||||||||||
| Response<AnalyzeImageResult> response; | ||||||||||
| try { | ||||||||||
| response = await _contentSafetyClient.AnalyzeImageAsync(request); | ||||||||||
| } catch (RequestFailedException ex) { | ||||||||||
| logger.LogContentModerationError(ex.Status.ToString(), ex.ErrorCode ?? "", ex.Message); | ||||||||||
| throw; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| AnalyzeImageResult result = response.Value; | ||||||||||
| int? score = result.CategoriesAnalysis | ||||||||||
| .Select(v => v.Severity) | ||||||||||
| .Aggregate((a, b) => a + b) | ||||||||||
| ?? 0; | ||||||||||
|
Comment on lines
+88
to
+89
|
||||||||||
| .Aggregate((a, b) => a + b) | |
| ?? 0; | |
| .DefaultIfEmpty(0) | |
| .Aggregate((a, b) => a + b); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The aggregation logic assumes all severity values are integers that can be summed. Consider using
Sum()instead ofAggregate((a, b) => a + b)for clarity and better null handling.