RatePlanNameAnalysis/RatePlanNameAnalysis.Model/ConsumeModel.cs
2024-08-18 21:11:29 +08:00

38 lines
1.3 KiB
C#

// This file was auto-generated by ML.NET Model Builder.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ML;
using System.IO;
namespace RatePlanNameAnalysis.Model
{
public class ConsumeModel
{
private static Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);
// For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
// Method for consuming model in your app
public static ModelOutput Predict(ModelInput input)
{
ModelOutput result = PredictionEngine.Value.Predict(input);
return result;
}
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "..", "RatePlanNameAnalysis.ConsoleApp", "Models", "model.zip");
ITransformer loadedModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(loadedModel);
return predEngine;
}
}
}