-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-vulnerabilities
- Loading branch information
Showing
23 changed files
with
339 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
using SemanticKernel.Service.Options; | ||
|
||
namespace SemanticKernel.Service.CopilotChat.Options; | ||
|
||
/// <summary> | ||
/// Configuration options for Azure Form Recognizer OCR support. | ||
/// </summary> | ||
public sealed class AzureFormRecognizerOptions | ||
{ | ||
public const string PropertyName = "AzureFormRecognizer"; | ||
|
||
/// <summary> | ||
/// The endpoint for accessing a provisioned Azure Form Recognizer instance | ||
/// </summary> | ||
[Required, NotEmptyOrWhitespace] | ||
public string Endpoint { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// The provisioned Azure Form Recognizer access key | ||
/// </summary> | ||
[Required, NotEmptyOrWhitespace] | ||
public string Key { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.AI.FormRecognizer; | ||
using Azure.AI.FormRecognizer.Models; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace SemanticKernel.Service.Services; | ||
|
||
/// <summary> | ||
/// Wrapper for the Azure.AI.FormRecognizer. This allows Form Recognizer to be used as the OCR engine for reading text from files with an image MIME type. | ||
/// </summary> | ||
public class AzureFormRecognizerOcrEngine : IOcrEngine | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the AzureFormRecognizerOcrEngine passing in the Form Recognizer endpoint and key. | ||
/// </summary> | ||
/// <param name="endpoint">The endpoint for accessing a provisioned Azure Form Recognizer instance</param> | ||
/// <param name="credential">The AzureKeyCredential containing the provisioned Azure Form Recognizer access key</param> | ||
public AzureFormRecognizerOcrEngine(string endpoint, AzureKeyCredential credential) | ||
{ | ||
this.FormRecognizerClient = new FormRecognizerClient(new Uri(endpoint), credential); | ||
} | ||
|
||
public FormRecognizerClient FormRecognizerClient { get; } | ||
|
||
///<inheritdoc/> | ||
public async Task<string> ReadTextFromImageFileAsync(IFormFile imageFile) | ||
{ | ||
await using (var imgStream = new MemoryStream()) | ||
{ | ||
await imageFile.CopyToAsync(imgStream); | ||
imgStream.Position = 0; | ||
|
||
// Start the OCR operation | ||
RecognizeContentOperation operation = await this.FormRecognizerClient.StartRecognizeContentAsync(imgStream); | ||
|
||
// Wait for the result | ||
Response<FormPageCollection> operationResponse = await operation.WaitForCompletionAsync(); | ||
FormPageCollection formPages = operationResponse.Value; | ||
|
||
StringBuilder text = new(); | ||
foreach (FormPage page in formPages) | ||
{ | ||
foreach (FormLine line in page.Lines) | ||
{ | ||
string lineText = string.Join(" ", line.Words.Select(word => word.Text)); | ||
text.AppendLine(lineText); | ||
} | ||
} | ||
return text.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace SemanticKernel.Service.Services; | ||
|
||
/// <summary> | ||
/// An OCR engine that can read in text from image MIME type files. | ||
/// </summary> | ||
public interface IOcrEngine | ||
{ | ||
|
||
/// <summary> | ||
/// Reads all text from the image file. | ||
/// </summary> | ||
/// <param name="imageFile">A file that is expected to be an image MIME type</param> | ||
/// <returns></returns> | ||
Task<string> ReadTextFromImageFileAsync(IFormFile imageFile); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace SemanticKernel.Service.Services; | ||
|
||
/// <summary> | ||
/// Used as a placeholder implementation when "none" is set in the OcrSupport:Type field in the configuration. | ||
/// </summary> | ||
public class NullOcrEngine : IOcrEngine | ||
{ | ||
/// <summary> | ||
/// Throws an exception to let the user know they need to specify a valid OcrSupport type in order to use the image upload feature. | ||
/// </summary> | ||
/// <param name="imageFile">Not used</param> | ||
/// <returns>This will always throw a NotImplementedException</returns> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public Task<string> ReadTextFromImageFileAsync(IFormFile imageFile) | ||
{ | ||
throw new NotImplementedException("You must specify a \"Type\" other than \"none\" within the \"OcrSupport\" application settings to use the image upload feature. See the README.md"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.