BotSharp/BotSharp.Core/Engines/BotPredictor.cs

87 lines
3 KiB
C#
Raw Normal View History

2018-08-09 21:06:40 +00:00
using BotSharp.Core.Abstractions;
2018-10-03 06:00:07 +00:00
using BotSharp.Platform.Models;
2018-10-02 02:49:01 +00:00
using BotSharp.Platform.Models.AiRequest;
2018-10-03 17:21:19 +00:00
using BotSharp.Platform.Models.MachineLearning;
2018-08-09 21:06:40 +00:00
using DotNetToolkit;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
2018-08-09 21:06:40 +00:00
using System;
using System.Collections.Generic;
using System.Drawing;
2018-08-09 21:06:40 +00:00
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Console = Colorful.Console;
2018-08-09 21:06:40 +00:00
namespace BotSharp.Core.Engines
{
public class BotPredictor
2018-08-09 21:06:40 +00:00
{
2018-10-03 06:00:07 +00:00
public async Task<NlpDoc> Predict(AgentBase agent, AiRequest request)
2018-08-09 21:06:40 +00:00
{
// load model
2018-08-28 21:45:56 +00:00
var dir = Path.Combine(request.AgentDir, request.Model);
2018-08-09 21:06:40 +00:00
Console.WriteLine($"Load model from {dir}");
2018-08-28 21:45:56 +00:00
var metaJson = File.ReadAllText(Path.Combine(dir, "model-meta.json"));
2018-08-09 21:06:40 +00:00
var meta = JsonConvert.DeserializeObject<ModelMetaData>(metaJson);
// Get NLP Provider
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");
var providerPipe = meta.Pipeline.First();
var provider = TypeHelper.GetInstance(providerPipe.Name, assemblies) as INlpProvider;
2018-08-09 21:06:40 +00:00
provider.Configuration = config.GetSection(meta.Platform);
var data = new NlpDoc
2018-08-09 21:06:40 +00:00
{
Sentences = new List<NlpDocSentence>
{
new NlpDocSentence
{
2018-10-02 02:49:01 +00:00
Text = request.Text
}
}
};
2018-08-09 21:06:40 +00:00
await provider.Load(agent, providerPipe);
2018-08-09 21:06:40 +00:00
meta.Pipeline.RemoveAt(0);
var settings = new PipeSettings
{
2018-08-23 12:25:55 +00:00
ModelDir = dir,
ProjectDir = request.AgentDir
};
2018-08-09 21:06:40 +00:00
// pipe process
2018-10-03 19:22:51 +00:00
var pipelines = config.GetValue<String>($"{meta.BotEngine}:pipe")
.Split(',')
.Select(x => x.Trim())
.ToList();
2018-08-29 22:24:12 +00:00
for(int pipeIdx = 0; pipeIdx < pipelines.Count; pipeIdx++)
2018-08-09 21:06:40 +00:00
{
2018-08-29 22:24:12 +00:00
var pipe = TypeHelper.GetInstance(pipelines[pipeIdx], assemblies) as INlpPredict;
2018-10-03 19:22:51 +00:00
pipe.Configuration = config.GetSection(meta.BotEngine).GetSection(pipelines[pipeIdx]);
pipe.Settings = settings;
2018-10-03 19:22:51 +00:00
var pipeModel = meta.Pipeline.FirstOrDefault(x => x.Name == pipelines[pipeIdx]);
await pipe.Predict(agent, data, pipeModel);
2018-08-29 22:24:12 +00:00
}
2018-08-09 21:06:40 +00:00
2018-10-21 14:03:12 +00:00
/*Console.WriteLine($"Prediction result:", Color.Green);
Console.WriteLine(JsonConvert.SerializeObject(data, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
2018-10-21 14:03:12 +00:00
}));*/
return data;
2018-08-09 21:06:40 +00:00
}
}
}