Merge branch 'SciSharp:master' into master
This commit is contained in:
commit
08ecff3ea5
|
|
@ -11,6 +11,7 @@ public class ConversationSetting
|
|||
public bool EnableExecutionLog { get; set; }
|
||||
public bool EnableContentLog { get; set; }
|
||||
public bool EnableStateLog { get; set; }
|
||||
public bool EnableTranslationMemory { get; set; }
|
||||
public CleanConversationSetting CleanSetting { get; set; } = new CleanConversationSetting();
|
||||
public RateLimitSetting RateLimit { get; set; } = new RateLimitSetting();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
namespace BotSharp.Abstraction.Files.Converters;
|
||||
|
||||
public interface IPdf2ImageConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert pdf pages to images, and return a list of image file paths
|
||||
/// </summary>
|
||||
/// <param name="pdfLocation">Pdf file location</param>
|
||||
/// <param name="imageFolderLocation">Image folder location</param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<string>> ConvertPdfToImages(string pdfLocation, string imageFolderLocation);
|
||||
}
|
||||
|
|
@ -181,7 +181,11 @@
|
|||
<PackageReference Include="Fluid.Core" Version="2.8.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
|
||||
<PackageReference Include="Nanoid" Version="3.0.0" />
|
||||
<PackageReference Include="PdfiumViewer" Version="2.13.0" />
|
||||
<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="RedLock.net" Version="2.3.2" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
using BotSharp.Abstraction.Browsing;
|
||||
using BotSharp.Abstraction.Browsing.Models;
|
||||
using BotSharp.Abstraction.Files.Converters;
|
||||
using BotSharp.Core.Files.Converters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
|
@ -51,18 +51,8 @@ public partial class BotSharpFileService
|
|||
|
||||
try
|
||||
{
|
||||
var msgInfo = new MessageInfo
|
||||
{
|
||||
ContextId = Guid.NewGuid().ToString()
|
||||
};
|
||||
var web = _services.GetRequiredService<IWebBrowser>();
|
||||
var preFixPath = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER);
|
||||
|
||||
if (isNeedScreenShot)
|
||||
{
|
||||
await web.LaunchBrowser(msgInfo);
|
||||
}
|
||||
|
||||
foreach (var messageId in messageIds)
|
||||
{
|
||||
var dir = Path.Combine(preFixPath, messageId, source);
|
||||
|
|
@ -91,40 +81,40 @@ public partial class BotSharpFileService
|
|||
var screenShotDir = Path.Combine(subDir, SCREENSHOT_FILE_FOLDER);
|
||||
if (ExistDirectory(screenShotDir) && Directory.GetFiles(screenShotDir).Any())
|
||||
{
|
||||
file = Directory.GetFiles(screenShotDir).First();
|
||||
contentType = GetFileContentType(file);
|
||||
|
||||
var model = new MessageFileModel()
|
||||
foreach (var screenShot in Directory.GetFiles(screenShotDir))
|
||||
{
|
||||
MessageId = messageId,
|
||||
FileStorageUrl = file,
|
||||
ContentType = contentType
|
||||
};
|
||||
files.Add(model);
|
||||
contentType = GetFileContentType(screenShot);
|
||||
if (!_allowedImageTypes.Contains(contentType)) continue;
|
||||
|
||||
var model = new MessageFileModel()
|
||||
{
|
||||
MessageId = messageId,
|
||||
FileStorageUrl = screenShot,
|
||||
ContentType = contentType
|
||||
};
|
||||
files.Add(model);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await web.GoToPage(msgInfo, new PageActionArgs { Url = file });
|
||||
var path = Path.Combine(subDir, SCREENSHOT_FILE_FOLDER, $"{Guid.NewGuid()}.png");
|
||||
await web.ScreenshotAsync(msgInfo, path);
|
||||
contentType = GetFileContentType(path);
|
||||
var screenShotPath = Path.Combine(subDir, SCREENSHOT_FILE_FOLDER);
|
||||
var images = await ConvertPdfToImages(file, screenShotPath);
|
||||
|
||||
var model = new MessageFileModel()
|
||||
foreach (var image in images)
|
||||
{
|
||||
MessageId = messageId,
|
||||
FileStorageUrl = path,
|
||||
ContentType = contentType
|
||||
};
|
||||
files.Add(model);
|
||||
contentType = GetFileContentType(image);
|
||||
var model = new MessageFileModel()
|
||||
{
|
||||
MessageId = messageId,
|
||||
FileStorageUrl = image,
|
||||
ContentType = contentType
|
||||
};
|
||||
files.Add(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isNeedScreenShot)
|
||||
{
|
||||
await web.CloseBrowser(msgInfo.ContextId);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -227,9 +217,13 @@ public partial class BotSharpFileService
|
|||
Directory.CreateDirectory(subDir);
|
||||
}
|
||||
|
||||
using var fs = new FileStream(Path.Combine(subDir, file.FileName), FileMode.Create);
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
fs.Flush(true);
|
||||
using (var fs = new FileStream(Path.Combine(subDir, file.FileName), FileMode.Create))
|
||||
{
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
fs.Flush(true);
|
||||
fs.Close();
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -318,5 +312,20 @@ public partial class BotSharpFileService
|
|||
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId);
|
||||
return dir;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLoc, string imageLoc)
|
||||
{
|
||||
var converters = _services.GetServices<IPdf2ImageConverter>();
|
||||
if (converters.IsNullOrEmpty()) return Enumerable.Empty<string>();
|
||||
|
||||
var converter = converters.FirstOrDefault(x => x.GetType().Name != typeof(PdfiumConverter).Name);
|
||||
if (converter == null)
|
||||
{
|
||||
converter = converters.FirstOrDefault(x => x.GetType().Name == typeof(PdfiumConverter).Name);
|
||||
if (converter == null) return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
return await converter.ConvertPdfToImages(pdfLoc, imageLoc);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
using BotSharp.Abstraction.Files.Converters;
|
||||
using PdfiumViewer;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Files.Converters;
|
||||
|
||||
public class PdfiumConverter : IPdf2ImageConverter
|
||||
{
|
||||
public async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLocation, string imageFolderLocation)
|
||||
{
|
||||
var paths = new List<string>();
|
||||
if (string.IsNullOrWhiteSpace(imageFolderLocation)) return paths;
|
||||
|
||||
if (Directory.Exists(imageFolderLocation))
|
||||
{
|
||||
Directory.Delete(imageFolderLocation, true);
|
||||
}
|
||||
Directory.CreateDirectory(imageFolderLocation);
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
using (var document = PdfDocument.Load(pdfLocation))
|
||||
{
|
||||
var pages = document.PageCount;
|
||||
|
||||
for (var page = 0; page < pages; page++)
|
||||
{
|
||||
var size = document.PageSizes[page];
|
||||
using (var image = document.Render(page, (int)size.Width, (int)size.Height, 96, 96, true))
|
||||
{
|
||||
var imagePath = Path.Combine(imageFolderLocation, $"{guid}_pg_{page + 1}.png");
|
||||
image.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);
|
||||
paths.Add(imagePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return await Task.FromResult(paths);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Files.Converters;
|
||||
using BotSharp.Core.Files.Converters;
|
||||
using BotSharp.Core.Files.Hooks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
|
|
@ -16,7 +18,8 @@ public class FilePlugin : IBotSharpPlugin
|
|||
{
|
||||
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
|
||||
|
||||
services.AddScoped<IAgentHook, AttachmentProcessingHook>();
|
||||
services.AddScoped<IAgentHook, FileAnalyzerHook>();
|
||||
services.AddScoped<IAgentToolHook, FileAnalyzerToolHook>();
|
||||
services.AddScoped<IPdf2ImageConverter, PdfiumConverter>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
namespace BotSharp.Core.Files.Hooks;
|
||||
|
||||
public class AttachmentProcessingHook : AgentHookBase
|
||||
public class FileAnalyzerHook : AgentHookBase
|
||||
{
|
||||
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
|
||||
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
|
||||
public FileAnalyzerHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ public class TranslationService : ITranslationService
|
|||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IBotSharpRepository _db;
|
||||
private readonly ConversationSetting _convSettings;
|
||||
private readonly ILogger<TranslationService> _logger;
|
||||
private readonly BotSharpOptions _options;
|
||||
private Agent _router;
|
||||
|
|
@ -22,11 +23,13 @@ public class TranslationService : ITranslationService
|
|||
public TranslationService(
|
||||
IServiceProvider services,
|
||||
IBotSharpRepository db,
|
||||
ConversationSetting convSettings,
|
||||
ILogger<TranslationService> logger,
|
||||
BotSharpOptions options)
|
||||
{
|
||||
_services = services;
|
||||
_db = db;
|
||||
_convSettings = convSettings;
|
||||
_logger = logger;
|
||||
_options = options;
|
||||
}
|
||||
|
|
@ -69,18 +72,23 @@ public class TranslationService : ITranslationService
|
|||
HashText = Utilities.HashTextSha256(x),
|
||||
Language = language
|
||||
}).ToList();
|
||||
var memories = _db.GetTranslationMemories(queries);
|
||||
var memoryHashes = memories.Select(x => x.HashText).ToList();
|
||||
var outOfMemoryList = queries;
|
||||
|
||||
foreach (var memory in memories)
|
||||
if (_convSettings.EnableTranslationMemory)
|
||||
{
|
||||
map[memory.OriginalText] = memory.TranslatedText;
|
||||
}
|
||||
var memories = _db.GetTranslationMemories(queries);
|
||||
var memoryHashes = memories.Select(x => x.HashText).ToList();
|
||||
|
||||
var outOfMemoryList = queries.Where(x => !memoryHashes.Contains(x.HashText)).ToList();
|
||||
foreach (var memory in memories)
|
||||
{
|
||||
map[memory.OriginalText] = memory.TranslatedText;
|
||||
}
|
||||
|
||||
outOfMemoryList = queries.Where(x => !memoryHashes.Contains(x.HashText)).ToList();
|
||||
}
|
||||
#endregion
|
||||
|
||||
var texts = outOfMemoryList.ToArray()
|
||||
var texts = outOfMemoryList
|
||||
.Select((text, i) => new TranslationInput
|
||||
{
|
||||
Id = i + 1,
|
||||
|
|
@ -123,7 +131,10 @@ public class TranslationService : ITranslationService
|
|||
});
|
||||
}
|
||||
|
||||
_db.SaveTranslationMemories(memoryInputs);
|
||||
if (_convSettings.EnableTranslationMemory)
|
||||
{
|
||||
_db.SaveTranslationMemories(memoryInputs);
|
||||
}
|
||||
}
|
||||
|
||||
clonedData = Assign(clonedData, map);
|
||||
|
|
|
|||
|
|
@ -136,6 +136,7 @@
|
|||
"EnableExecutionLog": true,
|
||||
"EnableContentLog": true,
|
||||
"EnableStateLog": true,
|
||||
"EnableTranslationMemory": false,
|
||||
"CleanSetting": {
|
||||
"Enable": true,
|
||||
"BatchSize": 50,
|
||||
|
|
|
|||
Loading…
Reference in a new issue