last ...
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xAiModels.Models;
|
||||
using System.Threading.Tasks;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
|
||||
namespace xAiApi.Providers.Extractors
|
||||
{
|
||||
public class XImageFileContentExtractor : IXImageFileContentExtractor
|
||||
{
|
||||
/// <summary>
|
||||
/// Supported MIME Types ...
|
||||
/// </summary>
|
||||
private static readonly string[] SupportedMimeTypes =
|
||||
[
|
||||
"image/png",
|
||||
"image/jpeg",
|
||||
"image/jpg",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"image/bmp"
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// آیا OCR فعال است (برای مدلهای غیر Vision) ...
|
||||
/// </summary>
|
||||
private readonly bool _enableOcrFallback;
|
||||
|
||||
public XImageFileContentExtractor(bool enableOcrFallback = true)
|
||||
{
|
||||
_enableOcrFallback = enableOcrFallback;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if this extractor supports the specified MIME type ...
|
||||
/// </summary>
|
||||
public bool CanExtract(string mimeType)
|
||||
{
|
||||
//
|
||||
return SupportedMimeTypes.Contains(
|
||||
mimeType?.ToLowerInvariant() ?? string.Empty
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract text content from file stream ...
|
||||
/// </summary>
|
||||
public async Task<string> ExtractAsync(
|
||||
Stream fileStream,
|
||||
string mimeType,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await ExtractRichAsync(
|
||||
fileStream,
|
||||
"image",
|
||||
mimeType,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result.Text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract content from stream as Rich Result ...
|
||||
/// </summary>
|
||||
/// <param name="fileStream"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XFileExtractionResult> ExtractRichAsync(
|
||||
Stream fileStream,
|
||||
string fileName,
|
||||
string mimeType,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XFileExtractionResult
|
||||
{
|
||||
FileName = fileName,
|
||||
MimeType = mimeType
|
||||
};
|
||||
|
||||
//
|
||||
using var memoryStream = new MemoryStream();
|
||||
await fileStream.CopyToAsync(memoryStream, cancellationToken);
|
||||
var imageBytes = memoryStream.ToArray();
|
||||
|
||||
//
|
||||
result.Images.Add(new XExtractedImage
|
||||
{
|
||||
Bytes = imageBytes,
|
||||
MimeType = mimeType,
|
||||
Description = $"Attached image: {fileName}"
|
||||
});
|
||||
|
||||
// OCR Fallback برای مدلهای متنی
|
||||
if (_enableOcrFallback)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var ocrText = await PerformOcrAsync(imageBytes, cancellationToken);
|
||||
if (!string.IsNullOrWhiteSpace(ocrText))
|
||||
{
|
||||
result.Text = ocrText;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// OCR failed, but image is still available for Vision
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Do OCR on Image for Text Extraction ...
|
||||
/// </summary>
|
||||
private async Task<string> PerformOcrAsync(
|
||||
byte[] imageBytes,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
// TODO:
|
||||
// پیادهسازی با Tesseract یا هر کتابخانه OCR دیگر
|
||||
// در حال حاضر یک placeholder است
|
||||
return await Task.FromResult(string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user