-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Startup.MachineLearning.cs
131 lines (117 loc) · 5.43 KB
/
Startup.MachineLearning.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using Bet.AspNetCore.Sample;
using Bet.Extensions.ML.Azure.ModelLoaders;
using Bet.Extensions.ML.DataLoaders.ModelLoaders;
using Bet.Extensions.ML.Sentiment.Models;
using Bet.Extensions.ML.Spam.Models;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ModelPredictionEngineExtensions
{
public static IServiceCollection AddSpamModelPrediction(
this IServiceCollection services,
IConfiguration configuration,
string containerName = "models")
{
var isLocal = configuration.GetValue<bool>("LocalMLModels");
var buildModels = configuration.GetValue<bool>("BuildModels");
if (isLocal)
{
if (buildModels)
{
// add sentiment model
services.AddSentimentModelCreationService<FileModelLoader>(
MLModels.SpamModel,
configure: options =>
{
options.ModelName = MLModels.SpamModel;
options.WatchForChanges = false;
options.ModelResultFileName = $"MLContent/{MLModels.SpamModel}.json";
options.ModelFileName = $"MLContent/{MLModels.SpamModel}.zip";
});
}
// add spam model from local ml.net models
services.AddModelPredictionEngine<SpamInput, SpamPrediction>(
MLModels.SpamModel)
.From<SpamInput, SpamPrediction, FileModelLoader>(
options =>
{
options.ModelFileName = "MLContent/SpamModel.zip";
options.WatchForChanges = true;
options.ReloadInterval = TimeSpan.FromSeconds(50);
});
}
else
{
if (buildModels)
{
// add sentiment model
services.AddSentimentModelCreationService<AzureStorageModelLoader>(MLModels.SpamModel);
}
// adds spam model used default azure configurations
services.AddAzureStorageAccount(MLModels.SpamModel)
.AddAzureBlobContainer(MLModels.SpamModel, containerName);
// register model loader with reload interval
services.AddModelPredictionEngine<SpamInput, SpamPrediction>(MLModels.SpamModel)
.From<SpamInput, SpamPrediction, AzureStorageModelLoader>(options =>
{
options.WatchForChanges = true;
options.ReloadInterval = TimeSpan.FromSeconds(40);
});
}
return services;
}
public static IServiceCollection AddSentimentModelPrediction(
this IServiceCollection services,
IConfiguration configuration,
string containerName = "models")
{
var isLocal = configuration.GetValue<bool>("LocalMLModels");
var buildModels = configuration.GetValue<bool>("BuildModels");
if (isLocal)
{
if (buildModels)
{
// add sentiment model
services.AddSentimentModelCreationService<FileModelLoader>(
MLModels.SentimentModel,
configure: options =>
{
options.ModelName = MLModels.SentimentModel;
options.WatchForChanges = false;
options.ModelResultFileName = $"MLContent/{MLModels.SentimentModel}.json";
options.ModelFileName = $"MLContent/{MLModels.SentimentModel}.zip";
});
}
// add sentiment model
services.AddModelPredictionEngine<SentimentIssue, SentimentPrediction>(MLModels.SentimentModel)
.From<SentimentIssue, SentimentPrediction, FileModelLoader>(options =>
{
options.ModelFileName = "MLContent/SentimentModel.zip";
options.WatchForChanges = true;
options.ReloadInterval = TimeSpan.FromSeconds(120);
});
}
else
{
if (buildModels)
{
// add sentiment model
services.AddSentimentModelCreationService<AzureStorageModelLoader>(MLModels.SentimentModel);
}
// adds spam model used default azure configurations
services.AddAzureStorageAccount(MLModels.SentimentModel)
.AddAzureBlobContainer(MLModels.SentimentModel, containerName);
// register model loader with reload interval
services.AddModelPredictionEngine<SentimentIssue, SentimentPrediction>(MLModels.SentimentModel)
.From<SentimentIssue, SentimentPrediction, AzureStorageModelLoader>(options =>
{
options.WatchForChanges = true;
options.ReloadInterval = TimeSpan.FromSeconds(60);
});
}
return services;
}
}
}