'Azure (C#) System.IO.FileNotFoundException: Could not find file

I've created a ML model with Visual Studio. I uploaded the web app to Azure with Visual Studio too. However, when I fill the fields for my ML model and click "run" on the website, I get this error which I copied directly from Azure App Service Editor.

I only get this error while trying to run the ML model on Azure website, if I run the web app on my computer I have no errors at all.

Thank you :)

The error:

2020-07-18 01:12:59.138 +00:00 [Error] Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request.
System.IO.FileNotFoundException: Could not find file 'C:\Users\X\X\X\fileML.Model\MLModel.zip'.
File name: 'C:\Users\X\X\X\fileML.Model\MLModel.zip'

____________________
My code:

// This file was auto-generated by ML.NET Model Builder. 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.ML;
using fileML.Model;

namespace fileML.Model
{
    public class ConsumeModel
    {
        private static readonly 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 = @"C:\Users\X\X\X\fileML.Model\MLModel.zip";
            
            ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

            return predEngine;
        }
    }
}


Solution 1:[1]

you are trying to load file MLModel.zip from C:\Users\X\X\X\fileML.Model. Now that's your local computer path. That path not exists into Azure Web App.

There are 2 ways you can do if you really want to store in local directory:

  1. HOMe environment variable in your Azure Web App that resolves to the equivalent of inetpub for your site. Your app data folder is located at %HOME%\site\wwwroot\AppData.

    TEMP environment both on Azure Web Apps and on your local machine.

Solution 2:[2]

public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
    {
        // Create new MLContext
        MLContext mlContext = new MLContext();

        // Load model & create prediction engine
        string directoryPath = Directory.GetCurrentDirectory();
        string modelPath = Path.Combine(@"C:\Users\Admin\source\repos\ShanuASPMLNETML.Model\MLModel.zip","MLModel.zip");
        
        ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
        var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

        return predEngine;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shirin
Solution 2 Nathan Akl