-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentimentModel.consumption.cs
100 lines (80 loc) · 3.23 KB
/
SentimentModel.consumption.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This file was auto-generated by ML.NET Model Builder.
using Microsoft.ML.Data;
using Microsoft.Extensions.ML;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.Threading.Tasks;
using System;
using System.IO;
using Microsoft.ML;
namespace WebAppWithModelConsumption
{
public abstract partial class SentimentModel
{
/// <summary>
/// model input class for SentimentModel.
/// </summary>
#region model input class
public class ModelInput
{
[ColumnName(@"Comment")]
public string Comment { get; set; }
[ColumnName(@"Sentiment")]
public string Sentiment { get; set; }
}
#endregion
/// <summary>
/// model output class for SentimentModel.
/// </summary>
#region model output class
public class ModelOutput
{
[ColumnName("PredictedLabel")]
public string Prediction { get; set; }
public float[] Score { get; set; }
}
#endregion
public static SentimentModel Create(string modelPath)
{
MLContext mlContext = new MLContext();
// Load model & create prediction engine
var fullModelPath = Path.GetFullPath(modelPath);
ITransformer mlModel = mlContext.Model.Load(fullModelPath, out var modelInputSchema);
var predictionEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return new SentimentModelPredictionEngine(predictionEngine);
}
public static SentimentModel Create(PredictionEngine<SentimentModel.ModelInput, SentimentModel.ModelOutput> predictionEngine)
{
return new SentimentModelPredictionEngine(predictionEngine);
}
public static SentimentModel Create(PredictionEnginePool<SentimentModel.ModelInput, SentimentModel.ModelOutput> predictionEnginePool)
{
return new SentimentModelPredictionEnginePool(predictionEnginePool);
}
public abstract ModelOutput Predict(ModelInput input);
class SentimentModelPredictionEngine : SentimentModel
{
private readonly PredictionEngine<ModelInput, ModelOutput> _predictionEngine;
public SentimentModelPredictionEngine(PredictionEngine<SentimentModel.ModelInput, SentimentModel.ModelOutput> predictionEngine)
{
this._predictionEngine = predictionEngine;
}
public override ModelOutput Predict(ModelInput input)
{
return this._predictionEngine.Predict(input);
}
}
class SentimentModelPredictionEnginePool : SentimentModel
{
private readonly PredictionEnginePool<ModelInput, ModelOutput> _predictionEnginePool;
public SentimentModelPredictionEnginePool(PredictionEnginePool<SentimentModel.ModelInput, SentimentModel.ModelOutput> predictionEnginePool)
{
this._predictionEnginePool = predictionEnginePool;
}
public override ModelOutput Predict(ModelInput input)
{
return this._predictionEnginePool.Predict(input);
}
}
}
}