Skip to content

Commit

Permalink
🎉 Add Azure Content Moderator service for image analysis (microsoft#143)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the copilot-chat repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
This pull request adds content moderation and image handling features to
the project. Specifically, it adds a new ContentModeratorController with
endpoints for detecting sensitive image content and getting the status
of content moderation. It also adds image text validation to the
DocumentImportController, throwing an exception if an image does not
contain text.

Additionally, this pull request adds an AzureContentModerator service
and a ContentModeratorOptions class for configuring content moderation.
The Azure Content Moderator service allows for the analysis of images to
detect harmful content, such as hate speech, sexual content, self-harm,
and violence.

The pull request also includes updates to the ChatInput and DocumentsTab
components in the webapp to support the new features, including the use
of the new useContentModerator hook for content moderation and the
useFile hook for image upload handling.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
webapp
- Add image moderation and import handling to useFile hook: Add
loadImage and handleImport functions to useFile hook.
- loadImage function loads an image from a URL and returns a promise.
- handleImport function handles importing a file and returns a promise.
-  Add ContentModerationService for image moderation. 
- analyzeImageAsync method analyzes an image for content moderation.
- getContentModerationStatusAsync method checks the status of the
content moderation service.

webapi
- The `ChatSkill` now includes an instance of `AzureContentModerator`
which can be used to moderate user input when an image is uploaded.
- The `appsettings.json` file has been updated to include configuration
options for the service.
- The `ChatInput` component has been updated to include a new hook
`useContentModerator` which handles document import, including using the
Azure Content Moderator API to analyze image content for offensive or
unwanted elements. - The `handleImport` function in `ChatInput` has been
removed and replaced with a shared function in `useFile` that handles
file imports. Also refactored document import in DocumentsTab component
to use useFile hook.


![image](https://github.com/microsoft/semantic-kernel/assets/125500434/7d9b3ae0-9974-4733-831a-a980bffb43f5)


![image](https://github.com/microsoft/semantic-kernel/assets/125500434/712137cb-72f8-4e8f-ad85-d63fad6ecc87)

![AzureContentSafety](https://github.com/microsoft/semantic-kernel/assets/125500434/3fd59bae-2a9c-4a7f-af04-85367be3631e)

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [Contribution
Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
~~- [ ] All unit tests pass, and I have added new tests where possible~~
- [x] I didn't break anyone 😄
  • Loading branch information
teresaqhoang authored Aug 18, 2023
1 parent b072121 commit b2e92fb
Show file tree
Hide file tree
Showing 17 changed files with 533 additions and 99 deletions.
55 changes: 47 additions & 8 deletions webapi/Controllers/DocumentImportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI;
using Microsoft.SemanticKernel.Text;
using UglyToad.PdfPig;
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
Expand Down Expand Up @@ -79,6 +80,7 @@ private enum SupportedFileType
private const string GlobalDocumentUploadedClientCall = "GlobalDocumentUploaded";
private const string ReceiveMessageClientCall = "ReceiveMessage";
private readonly IOcrEngine _ocrEngine;
private readonly IContentSafetyService? _contentSafetyService = null;

/// <summary>
/// Initializes a new instance of the <see cref="DocumentImportController"/> class.
Expand All @@ -91,7 +93,8 @@ public DocumentImportController(
ChatMemorySourceRepository sourceRepository,
ChatMessageRepository messageRepository,
ChatParticipantRepository participantRepository,
IOcrEngine ocrEngine)
IOcrEngine ocrEngine,
IContentSafetyService? contentSafety = null)
{
this._logger = logger;
this._options = documentMemoryOptions.Value;
Expand All @@ -101,6 +104,19 @@ public DocumentImportController(
this._messageRepository = messageRepository;
this._participantRepository = participantRepository;
this._ocrEngine = ocrEngine;
this._contentSafetyService = contentSafety;
}

/// <summary>
/// Gets the status of content safety.
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("contentSafety/status")]
[ProducesResponseType(StatusCodes.Status200OK)]
public bool ContentSafetyStatus()
{
return this._contentSafetyService!.ContentSafetyStatus(this._logger);
}

/// <summary>
Expand Down Expand Up @@ -265,7 +281,7 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm
throw new ArgumentException($"File {formFile.FileName} size exceeds the limit.");
}

// Make sure the file type is supported.
// Make sure the file type is supported and validate any images if ContentSafety is enabled.
var fileType = this.GetFileType(Path.GetFileName(formFile.FileName));
switch (fileType)
{
Expand All @@ -276,16 +292,38 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm
case SupportedFileType.Jpg:
case SupportedFileType.Png:
case SupportedFileType.Tiff:
{
if (this._ocrSupportOptions.Type != OcrSupportOptions.OcrSupportType.None)
{
if (documentImportForm.UseContentSafety)
{
if (!this._contentSafetyService!.ContentSafetyStatus(this._logger))
{
throw new ArgumentException("Unable to analyze image. Content Safety is currently disabled in the backend.");
}

var violations = new List<string>();
try
{
// Call the content safety controller to analyze the image
var imageAnalysisResponse = await this._contentSafetyService!.ImageAnalysisAsync(formFile, default);
violations = this._contentSafetyService.ParseViolatedCategories(imageAnalysisResponse, this._contentSafetyService!.Options!.ViolationThreshold);
}
catch (Exception ex) when (!ex.IsCriticalException())
{
this._logger.LogError(ex, "Failed to analyze image {0} with Content Safety. ErrorCode: {{1}}", formFile.FileName, (ex as AIException)?.ErrorCode);
throw new AggregateException($"Failed to analyze image {formFile.FileName} with Content Safety.", ex);
}

if (violations.Count > 0)
{
throw new ArgumentException($"Unable to upload image {formFile.FileName}. Detected undesirable content with potential risk: {string.Join(", ", violations)}");
}
}
break;
}

throw new ArgumentException($"Unsupported image file type: {fileType} when " +
$"{OcrSupportOptions.PropertyName}:{nameof(OcrSupportOptions.Type)} is set to " +
nameof(OcrSupportOptions.OcrSupportType.None));
}
default:
throw new ArgumentException($"Unsupported file type: {fileType}");
}
Expand Down Expand Up @@ -315,11 +353,12 @@ private async Task<ImportResult> ImportDocumentHelperAsync(IKernel kernel, IForm
case SupportedFileType.Jpg:
case SupportedFileType.Png:
case SupportedFileType.Tiff:
{
documentContent = await this.ReadTextFromImageFileAsync(formFile);
if (documentContent.Trim().Length == 0)
{
throw new ArgumentException($"Image {{{formFile.FileName}}} does not contain text.");
}
break;
}

default:
// This should never happen. Validation should have already caught this.
return ImportResult.Fail();
Expand Down
24 changes: 8 additions & 16 deletions webapi/CopilotChatWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,14 @@
<PackageReference Include="Azure.AI.FormRecognizer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.35.3" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Planning.StepwisePlanner"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Chroma"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Postgres"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.MsGraph"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.OpenAPI"
Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Planning.StepwisePlanner" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Chroma" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Postgres" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.MsGraph" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.OpenAPI" Version="0.19.230804.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Skills.Web" Version="0.19.230804.2-preview" />
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
Expand Down
20 changes: 20 additions & 0 deletions webapi/Extensions/SemanticKernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using System.Threading.Tasks;
using CopilotChat.WebApi.Hubs;
using CopilotChat.WebApi.Options;
using CopilotChat.WebApi.Services;
using CopilotChat.WebApi.Skills.ChatSkills;
using CopilotChat.WebApi.Storage;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -61,6 +63,9 @@ internal static IServiceCollection AddSemanticKernelServices(this IServiceCollec
// Semantic memory
services.AddSemanticTextMemory();

// Azure Content Safety
services.AddContentSafety();

// Register skills
services.AddScoped<RegisterSkillsWithKernel>(sp => RegisterSkillsAsync);

Expand Down Expand Up @@ -103,6 +108,7 @@ public static IKernel RegisterChatSkill(this IKernel kernel, IServiceProvider sp
messageRelayHubContext: sp.GetRequiredService<IHubContext<MessageRelayHub>>(),
promptOptions: sp.GetRequiredService<IOptions<PromptsOptions>>(),
documentImportOptions: sp.GetRequiredService<IOptions<DocumentMemoryOptions>>(),
contentSafety: sp.GetService<AzureContentSafety>(),
planner: sp.GetRequiredService<CopilotChatPlanner>(),
logger: sp.GetRequiredService<ILogger<ChatSkill>>()),
nameof(ChatSkill));
Expand Down Expand Up @@ -253,6 +259,20 @@ private static void AddSemanticTextMemory(this IServiceCollection services)
.ToTextEmbeddingsService(logger: sp.GetRequiredService<ILogger<AIServiceOptions>>())));
}

/// <summary>
/// Adds Azure Content Safety
/// </summary>
internal static void AddContentSafety(this IServiceCollection services)
{
IConfiguration configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
ContentSafetyOptions options = configuration.GetSection(ContentSafetyOptions.PropertyName).Get<ContentSafetyOptions>();

if (options.Enabled)
{
services.AddSingleton<IContentSafetyService, AzureContentSafety>(sp => new AzureContentSafety(new Uri(options.Endpoint), options.Key, options));
}
}

/// <summary>
/// Add the completion backend to the kernel config
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions webapi/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public static IServiceCollection AddOptions(this IServiceCollection services, Co
.ValidateOnStart()
.PostConfigure(TrimStringProperties);

// Content safety options
services.AddOptions<ContentSafetyOptions>()
.Bind(configuration.GetSection(ContentSafetyOptions.PropertyName))
.ValidateOnStart()
.PostConfigure(TrimStringProperties);

return services;
}

Expand Down
5 changes: 5 additions & 0 deletions webapi/Models/Request/DocumentImportForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public enum DocumentScopes
/// Will be used to create the chat message representing the document upload.
/// </summary>
public string UserName { get; set; } = string.Empty;

/// <summary>
/// Flag indicating whether user has content safety enabled from the client.
/// </summary>
public bool UseContentSafety { get; set; } = false;
}
37 changes: 37 additions & 0 deletions webapi/Models/Response/ImageAnalysisResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json.Serialization;
using CopilotChat.WebApi.Services;

namespace CopilotChat.WebApi.Models.Response;

/// <summary>
/// Response definition to the /contentsafety/image:analyze
/// endpoint made by the AzureContentSafety.
/// </summary>
public class ImageAnalysisResponse
{
/// <summary>
/// Gets or sets the AnalysisResult related to hate.
/// </summary>
[JsonPropertyName("hateResult")]
public AnalysisResult? HateResult { get; set; }

/// <summary>
/// Gets or sets the AnalysisResult related to self-harm.
/// </summary>
[JsonPropertyName("selfHarmResult")]
public AnalysisResult? SelfHarmResult { get; set; }

/// <summary>
/// Gets or sets the AnalysisResult related to sexual content.
/// </summary>
[JsonPropertyName("sexualResult")]
public AnalysisResult? SexualResult { get; set; }

/// <summary>
/// Gets or sets the AnalysisResult related to violence.
/// </summary>
[JsonPropertyName("violenceResult")]
public AnalysisResult? ViolenceResult { get; set; }
}
38 changes: 38 additions & 0 deletions webapi/Options/ContentSafetyOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.ComponentModel.DataAnnotations;

namespace CopilotChat.WebApi.Options;

/// <summary>
/// Configuration options for content safety.
/// </summary>
public class ContentSafetyOptions
{
public const string PropertyName = "ContentSafety";

/// <summary>
/// Whether to enable content safety.
/// </summary>
[Required, NotEmptyOrWhitespace]
public bool Enabled { get; set; } = false;

/// <summary>
/// Azure Content Safety endpoints
/// </summary>
[RequiredOnPropertyValue(nameof(Enabled), true)]
public string Endpoint { get; set; } = string.Empty;

/// <summary>
/// Key to access the content safety service.
/// </summary>
[RequiredOnPropertyValue(nameof(Enabled), true)]
public string Key { get; set; } = string.Empty;

/// <summary>
/// Set the violation threshold. See https://learn.microsoft.com/en-us/azure/ai-services/content-safety/quickstart-image for details.
/// </summary>
[Range(0, 6)]
public short ViolationThreshold { get; set; } = 4;
}
Loading

0 comments on commit b2e92fb

Please sign in to comment.