Created
April 23, 2021 02:57
-
-
Save luisquintanilla/a52f75e38bdec353388dea41377149f9 to your computer and use it in GitHub Desktop.
ASP.NET Interactive Extension + ML.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"#r \"nuget:microsoft.dotnet.interactive.aspnetcore,*-*\"\n", | |
"#r \"nuget:Microsoft.ML,1.5.5\"" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "Installed package Microsoft.ML version 1.5.5" | |
}, | |
"execution_count": 1, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "Installed package microsoft.dotnet.interactive.aspnetcore version 1.0.0-beta.21222.2" | |
}, | |
"execution_count": 1, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"using System.IO;\n", | |
"using Microsoft.ML;\n", | |
"using Microsoft.ML.Data;\n", | |
"using System.Text.Json;" | |
], | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"#!aspnet\n", | |
"\n", | |
"public async Task<ITransformer> DownloadModel(MLContext ctx, string url)\n", | |
"{\n", | |
" var model = await HttpClient.GetStreamAsync(url);\n", | |
" return ctx.Model.Load(model, out var x);\n", | |
"}" | |
], | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"public class ModelInput\n", | |
"{\n", | |
" public string SentimentText;\n", | |
"\n", | |
" [ColumnName(\"Label\")]\n", | |
" public bool Sentiment;\n", | |
"}\n", | |
"\n", | |
"public class ModelOutput\n", | |
"{\n", | |
" [ColumnName(\"PredictedLabel\")]\n", | |
" public bool Prediction { get; set; }\n", | |
"\n", | |
" public float Probability { get; set; }\n", | |
"\n", | |
" public float Score { get; set; }\n", | |
"}" | |
], | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"#!aspnet\n", | |
"\n", | |
"MLContext ctx = new MLContext();\n", | |
"var modelStream = await HttpClient.GetStreamAsync(\"https://github.com/dotnet/samples/raw/main/machine-learning/models/sentimentanalysis/sentiment_model.zip\");\n", | |
"var model = ctx.Model.Load(modelStream, out var schema);\n", | |
"var predEngine = ctx.Model.CreatePredictionEngine<ModelInput,ModelOutput>(model);\n", | |
"\n", | |
"Endpoints.MapInteractive(\"/PredictSentiment\", async context => \n", | |
"{\n", | |
" if(context.Request.Method.ToLowerInvariant() == \"post\")\n", | |
" {\n", | |
" var input = await context.Request.ReadFromJsonAsync<ModelInput>();\n", | |
" var prediction = predEngine.Predict(input);\n", | |
" await context.Response.WriteAsJsonAsync(prediction); \n", | |
" }\n", | |
" else\n", | |
" {\n", | |
" context.Response.StatusCode = 405;\n", | |
" await context.Response.CompleteAsync();\n", | |
" }\n", | |
" \n", | |
"});" | |
], | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"#!aspnet\n", | |
"\n", | |
"var input = new ModelInput {SentimentText = \"This was a very bad steak\"};\n", | |
"var reqBody = JsonSerializer.Serialize(input);\n", | |
"var output = await HttpClient.PostAsync(\"/PredictSentiment\", new StringContent(reqBody,System.Text.Encoding.UTF8,\"application/json\"));" | |
], | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"dotnet_interactive": { | |
"language": "csharp" | |
} | |
}, | |
"source": [ | |
"output" | |
], | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": "<div class=\"http-response-message-container\"><style type=\"text/css\">\r\n .http-response-message-container {\r\n display: flex;\r\n flex-wrap: wrap;\r\n }\r\n\r\n .http-response-message-container > div {\r\n margin: .5em;\r\n padding: 1em;\r\n border: 1px solid;\r\n }\r\n\r\n .http-response-message-container > div > h2 {\r\n margin-top: 0;\r\n }\r\n\r\n .http-response-message-container > div > h3 {\r\n margin-bottom: 0;\r\n }\r\n\r\n .aspnet-logs-container {\r\n margin: 0 .5em;\r\n }\r\n\r\n .http-response-message-container summary, .aspnet-logs-container summary {\r\n margin: 1em 0;\r\n font-size: 1.17em;\r\n font-weight: 700;\r\n }</style><div><h2>Request</h2><hr></hr><h3>POST <a href=\"http://127.0.0.1:54460/PredictSentiment\">http://127.0.0.1:54460/PredictSentiment</a> HTTP/1.1</h3><details><summary>Headers</summary><table><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td>Content-Type</td><td>application/json; charset=utf-8</td></tr><tr><td>Content-Length</td><td>2</td></tr></tbody></table></details><details><summary>Body</summary><pre>{}</pre></details></div><div><h2>Response</h2><hr></hr><h3>HTTP/1.1 200 OK</h3><details open=\"True\"><summary>Headers</summary><table><thead><tr><th>Name</th><th>Value</th></tr></thead><tbody><tr><td>Date</td><td>Fri, 23 Apr 2021 02:45:11 GMT</td></tr><tr><td>Server</td><td>Kestrel</td></tr><tr><td>Transfer-Encoding</td><td>chunked</td></tr><tr><td>Content-Type</td><td>application/json; charset=utf-8</td></tr></tbody></table></details><details open=\"True\"><summary>Body</summary><pre>{"prediction":false,"probability":0.5,"score":0}</pre></details></div></div><details class=\"aspnet-logs-container\"><summary>Logs</summary><table><thead><tr><th><i>index</i></th><th>LogLevel</th><th>Category</th><th>EventId</th><th>Message</th><th>Exception</th></tr></thead><tbody><tr><td>0</td><td><span>Information</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Hosting.Diagnostics</div></td><td><div class=\"dni-plaintext\">RequestStarting</div></td><td><div class=\"dni-plaintext\">Request starting HTTP/1.1 POST http://127.0.0.1:54460/PredictSentiment application/json;+charset=utf-8 2</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>1</td><td><span>Trace</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware</div></td><td><div class=\"dni-plaintext\">AllHostsAllowed</div></td><td><div class=\"dni-plaintext\">All hosts are allowed.</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>2</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.Matching.DfaMatcher</div></td><td><div class=\"dni-plaintext\">CandidatesFound</div></td><td><div class=\"dni-plaintext\">2 candidate(s) found for the request path '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>3</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.Matching.DfaMatcher</div></td><td><div class=\"dni-plaintext\">CandiateValid</div></td><td><div class=\"dni-plaintext\">Endpoint '/PredictSentiment' with route pattern '/PredictSentiment' is valid for the request path '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>4</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.Matching.DfaMatcher</div></td><td><div class=\"dni-plaintext\">CandiateValid</div></td><td><div class=\"dni-plaintext\">Endpoint '/PredictSentiment' with route pattern '/PredictSentiment' is valid for the request path '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>5</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware</div></td><td><div class=\"dni-plaintext\">MatchSuccess</div></td><td><div class=\"dni-plaintext\">Request matched endpoint '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>6</td><td><span>Information</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.EndpointMiddleware</div></td><td><div class=\"dni-plaintext\">ExecutingEndpoint</div></td><td><div class=\"dni-plaintext\">Executing endpoint '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>7</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Server.Kestrel</div></td><td><div class=\"dni-plaintext\">RequestBodyStart</div></td><td><div class=\"dni-plaintext\">Connection id "0HM8604MAHVA0", Request id "0HM8604MAHVA0:00000007": started reading request body.</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>8</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Server.Kestrel</div></td><td><div class=\"dni-plaintext\">RequestBodyDone</div></td><td><div class=\"dni-plaintext\">Connection id "0HM8604MAHVA0", Request id "0HM8604MAHVA0:00000007": done reading request body.</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>9</td><td><span>Information</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Routing.EndpointMiddleware</div></td><td><div class=\"dni-plaintext\">ExecutedEndpoint</div></td><td><div class=\"dni-plaintext\">Executed endpoint '/PredictSentiment'</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>10</td><td><span>Debug</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Server.Kestrel</div></td><td><div class=\"dni-plaintext\">ConnectionKeepAlive</div></td><td><div class=\"dni-plaintext\">Connection id "0HM8604MAHVA0" completed keep alive response.</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr><tr><td>11</td><td><span>Information</span></td><td><div class=\"dni-plaintext\">Microsoft.AspNetCore.Hosting.Diagnostics</div></td><td><div class=\"dni-plaintext\">RequestFinished</div></td><td><div class=\"dni-plaintext\">Request finished HTTP/1.1 POST http://127.0.0.1:54460/PredictSentiment application/json;+charset=utf-8 2 - 200 - application/json;+charset=utf-8 7.5551ms</div></td><td><div class=\"dni-plaintext\"><null></div></td></tr></tbody></table></details>" | |
}, | |
"execution_count": 1, | |
"metadata": {} | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".NET (C#)", | |
"language": "C#", | |
"name": ".net-csharp" | |
}, | |
"language_info": { | |
"file_extension": ".cs", | |
"mimetype": "text/x-csharp", | |
"name": "C#", | |
"pygments_lexer": "csharp", | |
"version": "8.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment