0
0
RhSolutions-Api/RhSolutions.Api/Services/ProductTypePredicter.cs

45 lines
1.1 KiB
C#
Raw Permalink Normal View History

2023-09-19 14:56:55 +03:00
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; }
}
}