BotSharp/BotSharp.Core/Engines/BotTrainer.cs

135 lines
4.6 KiB
C#
Raw Normal View History

2018-06-13 12:23:03 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2018-08-08 21:39:00 +00:00
using System.IO;
2018-06-14 15:03:01 +00:00
using System.Linq;
2018-06-13 12:23:03 +00:00
using System.Text;
2018-08-08 21:39:00 +00:00
using System.Threading.Tasks;
2018-06-14 15:03:01 +00:00
using BotSharp.Core.Abstractions;
2019-03-17 00:55:55 +00:00
using BotSharp.Platform.Abstractions;
2018-10-01 17:15:17 +00:00
using BotSharp.Platform.Models;
2018-10-03 17:21:19 +00:00
using BotSharp.Platform.Models.MachineLearning;
2018-06-13 12:23:03 +00:00
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
2018-08-02 21:14:46 +00:00
using Microsoft.Extensions.Configuration;
2018-08-08 21:39:00 +00:00
using Newtonsoft.Json;
2018-06-14 15:03:01 +00:00
using Newtonsoft.Json.Linq;
2018-08-08 21:39:00 +00:00
using Newtonsoft.Json.Serialization;
2018-06-13 12:23:03 +00:00
namespace BotSharp.Core.Engines
{
public class BotTrainer
{
private readonly Database dc;
2018-06-13 12:23:03 +00:00
private readonly string agentId;
2018-10-13 04:12:21 +00:00
private readonly IPlatformSettings settings;
2018-06-13 12:23:03 +00:00
2018-10-13 04:12:21 +00:00
public BotTrainer(IPlatformSettings setting)
{
2018-10-13 04:12:21 +00:00
this.settings = setting;
}
public BotTrainer(string agentId, Database dc)
2018-06-13 12:23:03 +00:00
{
this.dc = dc;
this.agentId = agentId;
}
2018-10-03 06:00:07 +00:00
public async Task<ModelMetaData> Train(AgentBase agent, BotTrainOptions options)
2018-06-13 12:23:03 +00:00
{
var data = new NlpDoc();
2018-06-14 15:03:01 +00:00
2018-06-13 12:23:03 +00:00
// Get NLP Provider
2018-08-02 21:14:46 +00:00
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
2018-10-11 13:24:50 +00:00
var platform = config.GetSection($"platformModuleName").Value;
2018-10-13 04:12:21 +00:00
var engine = this.settings.BotEngine;
2018-10-25 22:17:51 +00:00
string providerName = config.GetSection($"{engine}_{agent.Language}:Provider").Value;
var provider = TypeHelper.GetInstance(providerName, assemblies) as INlpProvider;
2018-10-25 22:17:51 +00:00
provider.Configuration = config.GetSection($"{engine}_{agent.Language}");
2018-06-13 12:23:03 +00:00
2018-08-08 21:39:00 +00:00
var pipeModel = new PipeModel
{
Name = providerName,
Class = provider.ToString(),
Meta = new JObject(),
Time = DateTime.UtcNow
};
await provider.Load(agent, pipeModel);
2018-08-08 21:39:00 +00:00
var settings = new PipeSettings
{
ProjectDir = options.AgentDir
};
2018-08-28 21:45:56 +00:00
settings.ModelDir = Path.Combine(options.AgentDir, options.Model);
if (!Directory.Exists(settings.ProjectDir))
{
Directory.CreateDirectory(settings.ProjectDir);
}
if (!Directory.Exists(settings.ModelDir))
{
Directory.CreateDirectory(settings.ModelDir);
}
var meta = new ModelMetaData
{
2018-10-03 17:21:19 +00:00
Platform = platform,
BotEngine = engine,
Language = agent.Language,
TrainingDate = DateTime.UtcNow,
Version = config.GetValue<String>($"Version"),
Pipeline = new List<PipeModel>() { pipeModel },
Model = settings.ModelDir
};
2018-06-13 22:17:35 +00:00
// pipe process
2018-09-27 01:55:21 +00:00
var pipelines = provider.Configuration.GetValue<String>($"pipe")
2018-06-14 15:03:01 +00:00
.Split(',')
.Select(x => x.Trim())
.ToList();
for (int pipeIdx = 0; pipeIdx < pipelines.Count; pipeIdx++)
2018-06-14 15:03:01 +00:00
{
2019-03-01 04:33:21 +00:00
Console.WriteLine("");
Console.WriteLine($"Executing {pipeModel.Name}");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var pipe = TypeHelper.GetInstance(pipelines[pipeIdx], assemblies) as INlpTrain;
// set configuration to current section
pipe.Configuration = provider.Configuration.GetSection(pipelines[pipeIdx]);
pipe.Settings = settings;
2018-08-08 21:39:00 +00:00
pipeModel = new PipeModel
{
Name = pipelines[pipeIdx],
2018-08-08 21:39:00 +00:00
Class = pipe.ToString(),
Time = DateTime.UtcNow
};
meta.Pipeline.Add(pipeModel);
2019-03-01 04:33:21 +00:00
Console.WriteLine(JsonConvert.SerializeObject(pipeModel, Formatting.Indented));
2018-08-08 21:39:00 +00:00
await pipe.Train(agent, data, pipeModel);
stopwatch.Stop();
Console.WriteLine($"Executed pipe {pipeModel.Name} elapsed {stopwatch.Elapsed}");
}
2018-08-08 21:39:00 +00:00
// save model meta data
var metaJson = JsonConvert.SerializeObject(meta, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
2018-06-14 15:03:01 +00:00
});
2018-08-28 21:45:56 +00:00
File.WriteAllText(Path.Combine(settings.ModelDir, "model-meta.json"), metaJson);
2018-06-13 12:23:03 +00:00
return meta;
2018-06-13 12:23:03 +00:00
}
}
}