Fix Windows Relation on Audio and PDF Content Extraction ...
This commit is contained in:
@@ -24,5 +24,10 @@ namespace xAiApi.Configurations
|
|||||||
/// Transcript Configuration ...
|
/// Transcript Configuration ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public XAiTransciptDescriptor Transcript { get; set; }
|
public XAiTransciptDescriptor Transcript { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// PDF To Image Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
public XPDFToImageDescriptor PDFToImageDescriptor {get; set;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace xAiApi.Models
|
||||||
|
{
|
||||||
|
public class XPDFToImageDescriptor
|
||||||
|
{
|
||||||
|
public int Dpi { get; set; } = 300;
|
||||||
|
public int Width { get; set; } = 0;
|
||||||
|
public int Height { get; set; } = 0;
|
||||||
|
public bool UseTiling { get; set; } = false;
|
||||||
|
public bool WithFormFill { get; set; } = false;
|
||||||
|
public bool WithAspectRatio { get; set; } = false;
|
||||||
|
public bool WithAnnotations { get; set; } = false;
|
||||||
|
public bool DpiRelativeToBounds { get; set; } = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using FFMpegCore;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Whisper.net;
|
using Whisper.net;
|
||||||
using NAudio.Wave;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xAiModels.Models;
|
using xAiModels.Models;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xAiApi.Configurations;
|
using xAiApi.Configurations;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NAudio.MediaFoundation;
|
|
||||||
using xAiApi.Interfaces.Extractors;
|
using xAiApi.Interfaces.Extractors;
|
||||||
|
|
||||||
namespace xAiApi.Providers.Extractors
|
namespace xAiApi.Providers.Extractors
|
||||||
@@ -161,173 +160,76 @@ namespace xAiApi.Providers.Extractors
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var result = await Task.Run(() =>
|
var tempInput = Path.Combine(
|
||||||
{
|
Path.GetTempPath(),
|
||||||
//
|
$"audio_in_{Guid.NewGuid()}{GetExtensionFromMime(mimeType)}"
|
||||||
var memoryStream = new MemoryStream();
|
);
|
||||||
inputStream.CopyTo(memoryStream);
|
var tempOutput = Path.Combine(
|
||||||
memoryStream.Position = 0;
|
Path.GetTempPath(),
|
||||||
|
$"audio_out_{Guid.NewGuid()}.wav"
|
||||||
//
|
);
|
||||||
var targetFormat = new WaveFormat(16000, 16, 1);
|
|
||||||
string tempFilePath = null;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
MemoryStream result = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
WaveStream reader = GetAudioReader(memoryStream, mimeType, ref tempFilePath);
|
using var fs = File.Create(tempInput);
|
||||||
if (reader == null)
|
await inputStream.CopyToAsync(fs, cancellationToken);
|
||||||
{
|
await FFMpegArguments
|
||||||
//
|
.FromFileInput(tempInput)
|
||||||
XException.NotAllowed.Throw(
|
.OutputToFile(tempOutput, overwrite: true, options => options
|
||||||
$"Unsupported audio format: {mimeType}"
|
.WithAudioCodec("pcm_s16le")
|
||||||
);
|
.WithCustomArgument("-ar 16000")
|
||||||
}
|
.WithCustomArgument("-ac 1")
|
||||||
|
.ForceFormat("wav"))
|
||||||
|
.ProcessAsynchronously();
|
||||||
|
|
||||||
//
|
//
|
||||||
var needsConversion =
|
result = new();
|
||||||
reader.WaveFormat.SampleRate != 16000 ||
|
using var fsTmp = File.OpenRead(tempOutput);
|
||||||
reader.WaveFormat.Channels != 1 ||
|
await fsTmp.CopyToAsync(result, cancellationToken);
|
||||||
reader.WaveFormat.BitsPerSample != 16 ||
|
result.Position = 0;
|
||||||
reader.WaveFormat.Encoding != WaveFormatEncoding.Pcm;
|
|
||||||
var outputStream = new MemoryStream();
|
|
||||||
if (!needsConversion)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
reader.CopyTo(outputStream);
|
|
||||||
reader.Dispose();
|
|
||||||
}
|
}
|
||||||
else
|
catch
|
||||||
{
|
{
|
||||||
//
|
result = null;
|
||||||
MediaFoundationApi.Startup();
|
|
||||||
using var resampler = new MediaFoundationResampler(
|
|
||||||
reader,
|
|
||||||
targetFormat
|
|
||||||
);
|
|
||||||
resampler.ResamplerQuality = 60;
|
|
||||||
|
|
||||||
//
|
|
||||||
WaveFileWriter.WriteWavFileToStream(outputStream, resampler);
|
|
||||||
reader.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
outputStream.Position = 0;
|
|
||||||
return outputStream;
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
if (!string.IsNullOrEmpty(tempFilePath) &&
|
// Remove Temparory Files ...
|
||||||
File.Exists(tempFilePath))
|
if (File.Exists(tempInput)) File.Delete(tempInput);
|
||||||
{
|
if (File.Exists(tempOutput)) File.Delete(tempOutput);
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(tempFilePath);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
memoryStream.Dispose();
|
if (result.IsNull())
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
}
|
}
|
||||||
}, cancellationToken);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get appropriate WaveStream reader based on MIME type ...
|
/// Retrieve File Extension based on mime type ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="stream">Input stream (MemoryStream)</param>
|
/// <param name="mimeType"></param>
|
||||||
/// <param name="mimeType">MIME type of audio file</param>
|
/// <returns></returns>
|
||||||
/// <param name="tempFilePath">Path to temp file (if created)</param>
|
private string GetExtensionFromMime(string mimeType)
|
||||||
/// <returns>WaveStream reader or null if unsupported</returns>
|
|
||||||
private WaveStream GetAudioReader(
|
|
||||||
MemoryStream stream,
|
|
||||||
string mimeType,
|
|
||||||
ref string tempFilePath
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
try
|
return mimeType?.ToLowerInvariant() switch
|
||||||
{
|
|
||||||
//
|
|
||||||
var normalizedMime = mimeType?.ToLowerInvariant() ?? string.Empty;
|
|
||||||
switch (normalizedMime)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
case "audio/wav":
|
|
||||||
case "audio/wave":
|
|
||||||
case "audio/x-wav":
|
|
||||||
return new WaveFileReader(stream);
|
|
||||||
|
|
||||||
//
|
|
||||||
case "audio/mpeg":
|
|
||||||
case "audio/mp3":
|
|
||||||
return new Mp3FileReader(stream);
|
|
||||||
|
|
||||||
//
|
|
||||||
case "audio/m4a":
|
|
||||||
case "audio/mp4":
|
|
||||||
case "audio/aac":
|
|
||||||
case "audio/ogg":
|
|
||||||
case "audio/webm":
|
|
||||||
case "audio/x-m4a":
|
|
||||||
//
|
|
||||||
var extension = normalizedMime switch
|
|
||||||
{
|
{
|
||||||
|
"audio/mpeg" or "audio/mp3" => ".mp3",
|
||||||
"audio/m4a" or "audio/mp4" or "audio/aac" or "audio/x-m4a" => ".m4a",
|
"audio/m4a" or "audio/mp4" or "audio/aac" or "audio/x-m4a" => ".m4a",
|
||||||
"audio/ogg" => ".ogg",
|
"audio/ogg" => ".ogg",
|
||||||
"audio/webm" => ".webm",
|
"audio/webm" => ".webm",
|
||||||
|
"audio/wav" or "audio/wave" or "audio/x-wav" => ".wav",
|
||||||
_ => ".tmp"
|
_ => ".tmp"
|
||||||
};
|
};
|
||||||
tempFilePath = Path.Combine(
|
|
||||||
Path.GetTempPath(),
|
|
||||||
$"audio_{Guid.NewGuid()}{extension}"
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
stream.Position = 0;
|
|
||||||
using (var fileStream = File.Create(tempFilePath))
|
|
||||||
{
|
|
||||||
stream.CopyTo(fileStream);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
MediaFoundationApi.Startup();
|
|
||||||
return new MediaFoundationReader(tempFilePath);
|
|
||||||
|
|
||||||
default:
|
|
||||||
//
|
|
||||||
tempFilePath = Path.Combine(
|
|
||||||
Path.GetTempPath(),
|
|
||||||
$"audio_{Guid.NewGuid()}.tmp"
|
|
||||||
);
|
|
||||||
stream.Position = 0;
|
|
||||||
using (var fileStream = File.Create(tempFilePath))
|
|
||||||
{
|
|
||||||
stream.CopyTo(fileStream);
|
|
||||||
}
|
|
||||||
try
|
|
||||||
{
|
|
||||||
MediaFoundationApi.Startup();
|
|
||||||
return new MediaFoundationReader(tempFilePath);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using SkiaSharp;
|
||||||
|
using PDFtoImage;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using PdfiumViewer;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xAiModels.Models;
|
using xAiModels.Models;
|
||||||
|
using xAiApi.Configurations;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using xAiApi.Interfaces.Extractors;
|
using xAiApi.Interfaces.Extractors;
|
||||||
|
|
||||||
@@ -43,6 +45,16 @@ namespace xAiApi.Providers.Extractors
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private const int MaxImageWidth = 2000;
|
private const int MaxImageWidth = 2000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Api Configuration ...
|
||||||
|
/// </summary>
|
||||||
|
private readonly XAiApiConfiguration configuration;
|
||||||
|
|
||||||
|
public XPdfFileContentExtractor(XAiApiConfiguration configuration)
|
||||||
|
{
|
||||||
|
this.configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if this extractor supports the specified MIME type ...
|
/// Check if this extractor supports the specified MIME type ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -211,18 +223,33 @@ namespace xAiApi.Providers.Extractors
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
using var document = PdfDocument.Load(pdfStream);
|
var images = Conversion.ToImages(
|
||||||
|
pdfStream: pdfStream,
|
||||||
|
options: new RenderOptions
|
||||||
|
{
|
||||||
|
Dpi = configuration.PDFToImageDescriptor.Dpi,
|
||||||
|
Width = configuration.PDFToImageDescriptor.Width,
|
||||||
|
Height = configuration.PDFToImageDescriptor.Height,
|
||||||
|
UseTiling = configuration.PDFToImageDescriptor.UseTiling,
|
||||||
|
WithFormFill = configuration.PDFToImageDescriptor.WithFormFill,
|
||||||
|
WithAspectRatio = configuration.PDFToImageDescriptor.WithAspectRatio,
|
||||||
|
WithAnnotations = configuration.PDFToImageDescriptor.WithAnnotations,
|
||||||
|
DpiRelativeToBounds = configuration.PDFToImageDescriptor.DpiRelativeToBounds,
|
||||||
|
}
|
||||||
|
).ToList();
|
||||||
|
|
||||||
//
|
//
|
||||||
|
var allPagesCount = Conversion.GetPageCount(pdfStream);
|
||||||
var pageCount = Math.Min(
|
var pageCount = Math.Min(
|
||||||
document.PageCount,
|
allPagesCount,
|
||||||
MaxPagesForImageFallback
|
MaxPagesForImageFallback
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
|
for (int pageIndex = 0; pageIndex < allPagesCount; pageIndex++)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
// Check Cancellation Token ...
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -232,52 +259,23 @@ namespace xAiApi.Providers.Extractors
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var pageSize = document.PageSizes[pageIndex];
|
var skBitmap = images[pageIndex];
|
||||||
|
using (skBitmap)
|
||||||
//
|
using (var image = SKImage.FromBitmap(skBitmap))
|
||||||
var scaleFactor = RenderDpi / 72.0;
|
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
|
||||||
var renderWidth = (int)Math.Ceiling(pageSize.Width * scaleFactor);
|
using (var ms = new MemoryStream())
|
||||||
var renderHeight = (int)Math.Ceiling(pageSize.Height * scaleFactor);
|
|
||||||
|
|
||||||
//
|
|
||||||
if (renderWidth > MaxImageWidth)
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
var ratio = (double)MaxImageWidth / renderWidth;
|
data.SaveTo(ms);
|
||||||
renderWidth = MaxImageWidth;
|
|
||||||
renderHeight = (int)(renderHeight * ratio);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
using var bitmap = document.Render(
|
|
||||||
dpiX: RenderDpi,
|
|
||||||
dpiY: RenderDpi,
|
|
||||||
page: pageIndex,
|
|
||||||
width: renderWidth,
|
|
||||||
forPrinting: false,
|
|
||||||
height: renderHeight
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
byte[] imageBytes;
|
|
||||||
using (var memoryStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
//
|
|
||||||
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
|
|
||||||
imageBytes = memoryStream.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
result.Images.Add(new XExtractedImage
|
result.Images.Add(new XExtractedImage
|
||||||
{
|
{
|
||||||
Bytes = imageBytes,
|
Bytes = ms.ToArray(),
|
||||||
MimeType = "image/png",
|
MimeType = "image/png",
|
||||||
PageNumber = pageIndex + 1,
|
Description = $"Page {pageIndex}, rendered from {fileName}"
|
||||||
Description = $"Page {pageIndex + 1} of {fileName} ({renderWidth}x{renderHeight})"
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (Exception)
|
}
|
||||||
{ }
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -88,6 +88,16 @@
|
|||||||
"Transcript": {
|
"Transcript": {
|
||||||
"Language": "fa",
|
"Language": "fa",
|
||||||
"ModelsPath": "Models/ggml-base.bin"
|
"ModelsPath": "Models/ggml-base.bin"
|
||||||
|
},
|
||||||
|
"PDFToImageDescriptor": {
|
||||||
|
"Dpi": 300,
|
||||||
|
"Width": null,
|
||||||
|
"Height": null,
|
||||||
|
"UseTiling": false,
|
||||||
|
"WithFormFill": false,
|
||||||
|
"WithAspectRatio": false,
|
||||||
|
"WithAnnotations": false,
|
||||||
|
"DpiRelativeToBounds": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
|
|||||||
+5
-8
@@ -24,7 +24,8 @@
|
|||||||
|
|
||||||
<!-- Icon Handling -->
|
<!-- Icon Handling -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true" PackagePath="\icon.png" />
|
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true"
|
||||||
|
PackagePath="\icon.png" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Local Modules -->
|
<!-- Local Modules -->
|
||||||
@@ -46,22 +47,18 @@
|
|||||||
|
|
||||||
<!-- Dependencies -->
|
<!-- Dependencies -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NAudio" Version="2.4.0" />
|
|
||||||
<PackageReference Include="Tesseract" Version="5.2.0" />
|
<PackageReference Include="Tesseract" Version="5.2.0" />
|
||||||
|
<PackageReference Include="PDFtoImage" Version="4.1.1" />
|
||||||
|
<PackageReference Include="FFMpegCore" Version="4.8.0" />
|
||||||
<PackageReference Include="Whisper.net" Version="1.9.1" />
|
<PackageReference Include="Whisper.net" Version="1.9.1" />
|
||||||
<PackageReference Include="OllamaSharp" Version="5.4.25" />
|
<PackageReference Include="OllamaSharp" Version="5.4.25" />
|
||||||
<PackageReference Include="PdfiumViewer" Version="2.13.0" />
|
|
||||||
<PackageReference Include="ExcelDataReader" Version="3.9.0" />
|
<PackageReference Include="ExcelDataReader" Version="3.9.0" />
|
||||||
<PackageReference Include="Whisper.net.Runtime" Version="1.9.1" />
|
<PackageReference Include="Whisper.net.Runtime" Version="1.9.1" />
|
||||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
<PackageReference Include="DocumentFormat.OpenXml" Version="3.5.1" />
|
||||||
<PackageReference Include="ExcelDataReader.DataSet" Version="3.9.0" />
|
<PackageReference Include="ExcelDataReader.DataSet" Version="3.9.0" />
|
||||||
<PackageReference Include="UglyToad.PdfPig" Version="1.7.0-custom-5" />
|
|
||||||
<PackageReference Include="PdfiumViewer.Native.x86.v8-xfa" Version="2018.4.8.256" />
|
|
||||||
<PackageReference Include="PdfiumViewer.Native.x86_64.v8-xfa" Version="2018.4.8.256" />
|
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" />
|
||||||
<PackageReference Include="PdfiumViewer.Native.x86.no_v8-no_xfa" Version="2018.4.8.256" />
|
|
||||||
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" />
|
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" />
|
||||||
<PackageReference Include="PdfiumViewer.Native.x86_64.no_v8-no_xfa" Version="2018.4.8.256" />
|
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="3.116.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- For XML Documentation Support -->
|
<!-- For XML Documentation Support -->
|
||||||
|
|||||||
Reference in New Issue
Block a user