2018-08-09 21:06:40 +00:00
|
|
|
|
using BotSharp.Core.Abstractions;
|
|
|
|
|
|
using BotSharp.Core.Agents;
|
2018-10-02 02:49:01 +00:00
|
|
|
|
using BotSharp.Platform.Models.AiRequest;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
using DotNetToolkit;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-08-10 20:10:53 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2018-08-31 17:52:52 +00:00
|
|
|
|
using System.Drawing;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2018-08-31 17:52:52 +00:00
|
|
|
|
using Console = Colorful.Console;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Engines
|
|
|
|
|
|
{
|
2018-08-13 04:14:02 +00:00
|
|
|
|
public class BotPredictor
|
2018-08-09 21:06:40 +00:00
|
|
|
|
{
|
2018-10-02 02:49:01 +00:00
|
|
|
|
public async Task<NlpDoc> Predict(Agent 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();
|
2018-08-13 04:14:02 +00:00
|
|
|
|
var provider = TypeHelper.GetInstance(providerPipe.Name, assemblies) as INlpProvider;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
provider.Configuration = config.GetSection(meta.Platform);
|
|
|
|
|
|
|
2018-08-10 20:10:53 +00:00
|
|
|
|
var data = new NlpDoc
|
2018-08-09 21:06:40 +00:00
|
|
|
|
{
|
2018-08-10 20:10:53 +00:00
|
|
|
|
Sentences = new List<NlpDocSentence>
|
|
|
|
|
|
{
|
|
|
|
|
|
new NlpDocSentence
|
|
|
|
|
|
{
|
2018-10-02 02:49:01 +00:00
|
|
|
|
Text = request.Text
|
2018-08-10 20:10:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2018-08-09 21:06:40 +00:00
|
|
|
|
|
2018-08-13 04:14:02 +00:00
|
|
|
|
await provider.Load(agent, providerPipe);
|
2018-08-09 21:06:40 +00:00
|
|
|
|
meta.Pipeline.RemoveAt(0);
|
|
|
|
|
|
|
2018-08-09 21:54:52 +00:00
|
|
|
|
var settings = new PipeSettings
|
|
|
|
|
|
{
|
2018-08-23 12:25:55 +00:00
|
|
|
|
ModelDir = dir,
|
2018-09-20 00:19:04 +00:00
|
|
|
|
ProjectDir = request.AgentDir
|
2018-08-09 21:54:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2018-08-13 04:14:02 +00:00
|
|
|
|
|
2018-08-09 21:06:40 +00:00
|
|
|
|
// pipe process
|
2018-09-27 01:55:21 +00:00
|
|
|
|
var pipelines = provider.Configuration.GetValue<String>($"Pipe")
|
2018-08-13 04:14:02 +00:00
|
|
|
|
.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-09-20 00:19:04 +00:00
|
|
|
|
pipe.Configuration = provider.Configuration.GetSection(pipelines[pipeIdx]);
|
2018-08-09 21:54:52 +00:00
|
|
|
|
pipe.Settings = settings;
|
2018-08-29 22:24:12 +00:00
|
|
|
|
await pipe.Predict(agent, data, meta.Pipeline.FirstOrDefault(x => x.Name == pipelines[pipeIdx]));
|
|
|
|
|
|
}
|
2018-08-09 21:06:40 +00:00
|
|
|
|
|
2018-08-31 17:52:52 +00:00
|
|
|
|
Console.WriteLine($"Prediction result:", Color.Green);
|
2018-08-10 20:10:53 +00:00
|
|
|
|
Console.WriteLine(JsonConvert.SerializeObject(data, new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
2018-08-09 21:06:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|