dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
8.93k stars 1.86k forks source link

Deep learning scenarios json serialization issue in webApp #6908

Closed zewditu closed 5 months ago

zewditu commented 6 months ago

System Information (please complete the following information): -Windows 11 Enterprise (10.0.22631 Build)

Describe the bug In model builder we generate two sample projects console and minimal webApp to our user to show samples how to consume thier model in application. However for the weApp if we have non-English characters in Json string we passe we have error ,if we explicitly sterilize it works . Here is a solution that contain those generated projects https://microsoft.sharepoint.com/:f:/t/Privetbuild/ElKqZFFcA4VDlR8cQVN_PaEB3fRmuet1WOAt7G39rrwtQA?e=haN4H8

To Reproduce Steps to reproduce the behavior:

  1. Download the solution
  2. Open the solution in VS
  3. Run "MLModel1_WebApi1"
  4. Open "PowerShell "
  5. Provide the sample dataset "$body =@{ >> context ="Beyoncé Giselle Knowles-Carter (/bi:jɒnseɪ/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress. Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R&B girl-group Destiny's Child. Managed by her father, Mathew Knowles, the group became one of the world's best-selling girl groups of all time. Their hiatus saw the release of Beyoncé's debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five Grammy Awards and featured the Billboard Hot 100 number-one singles" >> question ="What areas did Beyonce compete in when she was growing up?" >> answer_start =207 >> }"

Note >> are new lines in powershell

  1. Call the method Invoke-RestMethod "https://localhost:PORT_NUMBER/predict"-Method Post -Body ($body | ConvertTo-Json)-ContentType "application/json"
  2. See error as follows image

Expected behavior A clear and concise description of what you expected to happen.

Screenshots, Code, Sample Projects If applicable, add screenshots, code snippets, or sample projects to help explain your problem.

Additional context

app.MapPost("/predict", async (HttpRequest request) =>
{
    var options = new JsonSerializerOptions
    {
        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default,
        PropertyNameCaseInsensitive = true,
    };
    using (StreamReader reader = new StreamReader(request.Body))
    {
        var jsonString = await reader.ReadToEndAsync();
        var modelInput = JsonSerializer.Deserialize<MLModel1.ModelInput>(jsonString,options);
        var result = MLModel1.Predict(modelInput);

        // To get only the first best answer.
        result.Predicted_Answer = new string[] { result.Predicted_Answer[0] };
        result.Score = new float[] { result.Score[0] };
        return result;
    }
});
zewditu commented 6 months ago

@michaelgsharp , In the above code snippet the string that comes from the web request body has place holder character for non-English characters � "Beyonc� Giselle Knowles-Carter (/bi:j?nse?/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress. Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R\u0026B girl-group Destiny\u0027s Child. Managed by her father, Mathew Knowles, the group became one of the world\u0027s best-selling girl groups of all time. Their hiatus saw the release of Beyonc�\u0027s debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five Grammy Awards and featured the Billboard Hot 100 number-one singles"

michaelgsharp commented 5 months ago

The issue is the invalid characters coming in from the web request and not something on the ML.NET side. CLosing this issue.