Exemple #1
0
        public static ReturnResult <ModelField> InsertUpdate(IDatabase db, ModelField modelField)
        {
            var results = new ReturnResult <ModelField>();

            try
            {
                if (modelField.ModelFieldId == 0)
                {
                    db.Insert(modelField);
                }
                else
                {
                    db.Update(modelField);
                }

                results.Success = true;
                results.Item    = modelField;
            }
            catch (Exception e)
            {
                results.Success      = false;
                results.ErrorMessage = "There was an error saving the model.";
                results.Exception    = e;
            }

            return(results);
        }
Exemple #2
0
        public static ReturnResult <dynamic> PredictModel(IDatabase db, PredictionInput input)
        {
            var results  = new ReturnResult <dynamic>();
            var fileName = Guid.NewGuid().ToString();

            try
            {
                var model     = GetModelById(db, input.ModelId);
                var modelInfo = ModelField.GetFieldsByModelId(db, model.Item.ModelId);
                var modelFile = FileStore.GetById(db, model.Item.FileStoreId.Value);

                MLContext      mlContext = new MLContext();
                DataViewSchema predictionPipelineSchema;

                IDataView LoadedData = null;

                var columnData = new List <TextLoader.Column>();
                var curItem    = 0;
                foreach (var c in modelInfo.Item)
                {
                    var newColData = new TextLoader.Column()
                    {
                        DataKind = (DataKind)c.DataTypeId,
                        Name     = c.Name,
                        Source   = new TextLoader.Range[] { new TextLoader.Range(curItem) }
                    };

                    columnData.Add(newColData);
                    curItem++;
                }

                File.WriteAllText(Path.GetTempPath() + fileName, input.CsvData);

                LoadedData = mlContext.Data.LoadFromTextFile(
                    Path.GetTempPath() + fileName,
                    columnData.ToArray(),
                    separatorChar: ',',
                    hasHeader: false,
                    allowQuoting: true
                    );

                var outputColumn = modelInfo.Item.Single(x => x.IsOutput == true);

                using (MemoryStream ms = new MemoryStream(modelFile.Item.Data))
                {
                    var       predictionPipeline = mlContext.Model.Load(ms, out predictionPipelineSchema);
                    IDataView predictions        = predictionPipeline.Transform(LoadedData);

                    if ((DataKind)outputColumn.DataTypeId == DataKind.Single)
                    {
                        var      hasLabel      = predictions.Schema.GetColumnOrNull("PredictedLabel");
                        Single[] predictionOut = null;
                        if (hasLabel == null)
                        {
                            predictionOut = predictions.GetColumn <Single>("Score").ToArray();
                        }
                        else
                        {
                            predictionOut = predictions.GetColumn <Single>("PredictedLabel").ToArray();
                        }
                        results.Item = predictionOut[0];
                    }
                    else if ((DataKind)outputColumn.DataTypeId == DataKind.String)
                    {
                        var predictionOut = predictions.GetColumn <String>("PredictedLabel").ToArray();
                        results.Item = predictionOut[0];
                    }
                    else if ((DataKind)outputColumn.DataTypeId == DataKind.Boolean)
                    {
                        var predictionOut = predictions.GetColumn <Boolean>("PredictedLabel").ToArray();
                        results.Item = predictionOut[0];
                    }
                };

                results.Success = true;
            }
            catch (Exception e)
            {
                results.Success = false;
            }


            //Delete the prediction file
            try
            {
                if (File.Exists(Path.GetTempPath() + fileName))
                {
                    File.Delete(Path.GetTempPath() + fileName);
                }
            }
            catch (Exception e) {}

            return(results);
        }