45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
|
using Microsoft.ML;
|
|||
|
using Microsoft.ML.Data;
|
|||
|
|
|||
|
namespace RhSolutions.Api.Services;
|
|||
|
|
|||
|
public class ProductTypePredicter : IProductTypePredicter
|
|||
|
{
|
|||
|
private readonly string _modelPath = @"./MLModels/model.zip";
|
|||
|
private MLContext _mlContext;
|
|||
|
private ITransformer _loadedModel;
|
|||
|
private PredictionEngine<Product, TypePrediction> _predEngine;
|
|||
|
|
|||
|
public ProductTypePredicter()
|
|||
|
{
|
|||
|
_mlContext = new MLContext(seed: 0);
|
|||
|
_loadedModel = _mlContext.Model.Load(_modelPath, out var _);
|
|||
|
_predEngine = _mlContext.Model.CreatePredictionEngine<Product, TypePrediction>(_loadedModel);
|
|||
|
}
|
|||
|
|
|||
|
public string? GetPredictedProductType(string productName)
|
|||
|
{
|
|||
|
Product p = new()
|
|||
|
{
|
|||
|
Name = productName
|
|||
|
};
|
|||
|
var prediction = _predEngine.Predict(p);
|
|||
|
return prediction.Type;
|
|||
|
}
|
|||
|
|
|||
|
public class Product
|
|||
|
{
|
|||
|
[LoadColumn(0)]
|
|||
|
public string? Name { get; set; }
|
|||
|
[LoadColumn(1)]
|
|||
|
public string? Type { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
public class TypePrediction
|
|||
|
{
|
|||
|
[ColumnName("PredictedLabel")]
|
|||
|
public string? Type { get; set; }
|
|||
|
}
|
|||
|
}
|
|||
|
|