// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var instrumentId = Configuration.Bind <ApplicationInsightsOptions>("ApplicationInsights", true); services.AddApplicationInsightsTelemetry(options => { options.InstrumentationKey = instrumentId.InstrumentationKey; }); services.AddDeveloperListRegisteredServices(o => { o.PathOutputOptions = PathOutputOptions.Json; }); services.AddControllers(); services.AddSwaggerGen(options => options.SwaggerDoc("v1", new OpenApiInfo { Title = $"{AppName} API", Version = "v1" })); // add ML.NET Models var spamInMemoryModelStorageProvider = new InMemoryModelStorageProvider(); services.AddSpamDetectionModelBuilder(spamInMemoryModelStorageProvider); var spamName = "SpamModel"; services.AddModelPredictionEngine <SpamInput, SpamPrediction>(spamName) .WithStorageProvider(nameof(SpamModelBuilderService), spamInMemoryModelStorageProvider); var sentimentFileModeStorageProvider = new FileModelStorageProvider(); services.AddSentimentModelBuilder(sentimentFileModeStorageProvider); var sentimentName = "SentimentModel"; services.AddModelPredictionEngine <SentimentIssue, SentimentPrediction>(sentimentName) .WithStorageProvider($"{nameof(SentimentModelBuilderService)}.zip", sentimentFileModeStorageProvider); services.AddScheduler(builder => { builder.AddJob <RebuildMLModelScheduledJob, RebuildMLModelsOptions>(); builder.UnobservedTaskExceptionHandler = null; }); // add healthchecks services.AddHealthChecks() .AddMemoryHealthCheck() .AddMachineLearningModelCheck <SpamInput, SpamPrediction>($"{spamName}_check") .AddMachineLearningModelCheck <SentimentIssue, SentimentPrediction>($"{sentimentName}_check") .AddSigtermCheck("sigterm_check") .AddLoggerPublisher(new List <string> { "sigterm_check" }); }
/// <summary> /// Adds ML.NET based Sentiment model builder with <see cref="FileModelStorageProvider"/> as storage. /// </summary> /// <param name="services">The DI services.</param> /// <param name="modelStorageProvider">The model storage provider.</param> /// <returns></returns> public static IServiceCollection AddSentimentModelBuilder(this IServiceCollection services, IModelStorageProvider modelStorageProvider = null) { services.TryAddSingleton(new MLContext()); if (modelStorageProvider == null) { modelStorageProvider = new FileModelStorageProvider(); } services.TryAddScoped <IModelCreationBuilder <SentimentIssue, SentimentPrediction, BinaryClassificationMetricsResult>, SentimentModelBuilder>(); services.AddScoped <IModelBuilderService, SentimentModelBuilderService>((sp) => { var builder = sp.GetRequiredService <IModelCreationBuilder <SentimentIssue, SentimentPrediction, BinaryClassificationMetricsResult> >(); var logger = sp.GetRequiredService <ILogger <SentimentModelBuilderService> >(); return(new SentimentModelBuilderService(builder, modelStorageProvider, logger)); }); return(services); }
/// <summary> /// Adds ML.NET based Spam detection model builder with <see cref="FileModelStorageProvider"/> as storage. /// </summary> /// <param name="services">The DI services.</param> /// <param name="modelStorageProvider">The model storage provider.</param> /// <returns></returns> public static IServiceCollection AddSpamDetectionModelBuilder(this IServiceCollection services, IModelStorageProvider modelStorageProvider = null) { services.TryAddSingleton(new MLContext()); if (modelStorageProvider == null) { modelStorageProvider = new FileModelStorageProvider(); } services.TryAddScoped <IModelCreationBuilder <SpamInput, SpamPrediction, MulticlassClassificationFoldsAverageMetricsResult>, SpamModelBuilder>(); services.AddScoped <IModelBuilderService, SpamModelBuilderService>((sp) => { var builder = sp.GetRequiredService <IModelCreationBuilder <SpamInput, SpamPrediction, MulticlassClassificationFoldsAverageMetricsResult> >(); var logger = sp.GetRequiredService <ILogger <SpamModelBuilderService> >(); return(new SpamModelBuilderService(builder, modelStorageProvider, logger)); }); return(services); }