forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sets up PredictRequest callback (microsoft#16)
- Loading branch information
Showing
8 changed files
with
166 additions
and
83 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "environment.h" | ||
#include "http_server.h" | ||
#include "json_handling.h" | ||
|
||
namespace onnxruntime { | ||
namespace hosting { | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
|
||
void BadRequest(HttpContext& context, const std::string& error_message) { | ||
auto json_error = R"({"error_code": 400, "error_message": )" + error_message + " }"; | ||
|
||
http::response<http::string_body> res{http::status::bad_request, context.request.version()}; | ||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
res.set(http::field::content_type, "application/json"); | ||
res.keep_alive(context.request.keep_alive()); | ||
res.body() = std::string(json_error); | ||
res.prepare_payload(); | ||
context.response = res; | ||
} | ||
|
||
// TODO: decide whether this should be a class | ||
void Predict(const std::string& name, | ||
const std::string& version, | ||
const std::string& action, | ||
HttpContext& context, | ||
HostingEnvironment& env) { | ||
PredictRequest predictRequest{}; | ||
auto logger = env.GetLogger(); | ||
|
||
LOGS(logger, VERBOSE) << "Name: " << name | ||
<< "Version: " << version | ||
<< "Action: " << action; | ||
|
||
auto body = context.request.body(); | ||
auto status = GetRequestFromJson(body, predictRequest); | ||
|
||
if (!status.ok()) { | ||
return BadRequest(context, status.error_message()); | ||
} | ||
|
||
http::response<http::string_body> res{std::piecewise_construct, | ||
std::make_tuple(body), | ||
std::make_tuple(http::status::ok, context.request.version())}; | ||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
res.set(http::field::content_type, "application/json"); | ||
res.keep_alive(context.request.keep_alive()); | ||
context.response = res; | ||
}; | ||
|
||
} // namespace hosting | ||
} // namespace onnxruntime |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "http_server.h" | ||
#include "json_handling.h" | ||
|
||
namespace onnxruntime { | ||
namespace hosting { | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
|
||
void BadRequest(HttpContext& context, const std::string& error_message); | ||
|
||
// TODO: decide whether this should be a class | ||
void Predict(const std::string& name, | ||
const std::string& version, | ||
const std::string& action, | ||
HttpContext& context, | ||
HostingEnvironment& env); | ||
|
||
} // namespace hosting | ||
} // namespace onnxruntime |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "environment.h" | ||
#include "http_server.h" | ||
#include "predict_request_handler.h" | ||
#include "server_configuration.h" | ||
#include "environment.h" | ||
|
||
namespace beast = boost::beast; | ||
namespace http = beast::http; | ||
|
||
void test_request(const std::string& name, const std::string& version, | ||
const std::string& action, onnxruntime::hosting::HttpContext& context) { | ||
std::stringstream ss; | ||
|
||
ss << "\tModel Name: " << name << std::endl; | ||
ss << "\tModel Version: " << version << std::endl; | ||
ss << "\tAction: " << action << std::endl; | ||
ss << "\tHTTP method: " << context.request.method() << std::endl; | ||
|
||
http::response<http::string_body> | ||
res{std::piecewise_construct, std::make_tuple(ss.str()), std::make_tuple(http::status::ok, context.request.version())}; | ||
|
||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING); | ||
res.set(http::field::content_type, "plain/text"); | ||
res.keep_alive(context.request.keep_alive()); | ||
context.response = res; | ||
} | ||
namespace hosting = onnxruntime::hosting; | ||
|
||
int main(int argc, char* argv[]) { | ||
onnxruntime::hosting::ServerConfiguration config{}; | ||
hosting::ServerConfiguration config{}; | ||
auto res = config.ParseInput(argc, argv); | ||
|
||
if (res == onnxruntime::hosting::Result::ExitSuccess) { | ||
if (res == hosting::Result::ExitSuccess) { | ||
exit(EXIT_SUCCESS); | ||
} else if (res == onnxruntime::hosting::Result::ExitFailure) { | ||
} else if (res == hosting::Result::ExitFailure) { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
onnxruntime::hosting::HostingEnvironment env; | ||
hosting::HostingEnvironment env; | ||
auto logger = env.GetLogger(); | ||
|
||
// TODO: below code snippet just trying to show case how to use the "env". | ||
// Will be moved to proper place. | ||
// TODO: below code snippet just trying to show case how to use the "env". Move later. | ||
LOGS(logger, VERBOSE) << "Logging manager initialized."; | ||
LOGS(logger, VERBOSE) << "Model path: " << config.model_path; | ||
auto status = env.GetSession()->Load(config.model_path); | ||
LOGS(logger, VERBOSE) << "Load Model Status: " << status.Code() << " ---- Error: [" << status.ErrorMessage() << "]"; | ||
|
||
auto const boost_address = boost::asio::ip::make_address(config.address); | ||
|
||
onnxruntime::hosting::App app{}; | ||
app.Post(R"(/v1/models/([^/:]+)(?:/versions/(\d+))?:(classify|regress|predict))", test_request) | ||
.Bind(boost_address, config.http_port) | ||
.NumThreads(config.num_http_threads) | ||
.Run(); | ||
hosting::App app{}; | ||
app.Post(R"(/v1/models/([^/:]+)(?:/versions/(\d+))?:(classify|regress|predict))", | ||
[&env](const std::string& name, const std::string& version, const std::string& action, hosting::HttpContext& context) { | ||
hosting::Predict(name, version, action, context, env); | ||
}); | ||
|
||
app.Bind(boost_address, config.http_port) | ||
.NumThreads(config.num_http_threads) | ||
.Run(); | ||
|
||
return EXIT_SUCCESS; | ||
} |