CNum 0.2.1
CPU-optimized ML library for C++
Loading...
Searching...
No Matches
Deploy.h
Go to the documentation of this file.
1#ifndef DEPLOY_H
2#define DEPLOY_H
4#include "crow.h"
5#include <functional>
6#include <string.h>
7
12namespace CNum::Deploy {
14 constexpr size_t MAX_URL_LEN = 50;
15
16
24 struct PathString {
26
27 constexpr PathString(const char (&s)[MAX_URL_LEN]) {
28 for (size_t i = 0; i < MAX_URL_LEN; i++) {
29 str[i] = s[i];
30 }
31 }
32
33 constexpr operator crow::black_magic::const_str() const {
34 return crow::black_magic::const_str(str);
35 }
36
37 constexpr operator ::std::string() const {
38 return ::std::string(str);
39 }
40 };
41
49 template <typename ModelType, typename Storage>
51 using PreprocessFunctionType = CNum::DataStructs::Matrix<double>(crow::json::rvalue &,
52 crow::json::wvalue &,
53 Storage &);
54 using PreprocessFunction = ::std::function< PreprocessFunctionType >;
55
56 using PostprocessFunctionType = void(CNum::DataStructs::Matrix<double> &,
57 crow::json::wvalue &,
58 Storage &);
59 using PostprocessFunction = ::std::function< PostprocessFunctionType >;
60
61 using InferenceRouteFunctionType = void(const crow::request &, crow::response &, ModelType *);
62 using InferenceRouteFunction = ::std::function< InferenceRouteFunctionType >;
63 using RegularRouteFunctionType = void(const crow::request &, crow::response &);
64 using RegularRouteFunction = ::std::function< InferenceRouteFunctionType >;
65
66 private:
68 crow::App<crow::CORSHandler> _app;
69 PreprocessFunction _preprocess;
70 PostprocessFunction _postprocess;
71 unsigned short _port;
72
73 public:
83 InferenceAPI(::std::string path,
84 PreprocessFunction preprocess,
85 PostprocessFunction postprocess,
86 ::std::string allowed_origins = "*",
87 size_t n_models = 20,
88 unsigned short port = 18080);
89
92
94 template <PathString Path>
95 constexpr void add_inference_route(crow::HTTPMethod method, InferenceRouteFunction route);
96
98
101 template <PathString Path>
102 constexpr void add_regular_route(crow::HTTPMethod method, RegularRouteFunction route);
103
105 void start();
106 };
107
108 #include "CNum/Deploy/Deploy.tpp"
109};
110
111#endif
2d array abstraction
Definition Matrix.h:43
void start()
Start the backend.
Definition Deploy.h:95
constexpr void add_regular_route(crow::HTTPMethod method, RegularRouteFunction route)
Add a route that doesn't use a model.
Definition Deploy.h:87
constexpr void add_inference_route(crow::HTTPMethod method, InferenceRouteFunction route)
Add a route to the API that uses a model.
Definition Deploy.h:59
InferenceAPI(::std::string path, PreprocessFunction preprocess, PostprocessFunction postprocess, ::std::string allowed_origins="*", size_t n_models=20, unsigned short port=18080)
Constructor.
Definition Deploy.h:3
A pool of trained models.
Definition ModelPool.h:13
Tools for creating REST APIs.
constexpr size_t MAX_URL_LEN
The maximum length of a path for a route in the REST API.
Definition Deploy.h:14
constexpr PathString(const char(&s)[MAX_URL_LEN])
Definition Deploy.h:27
char str[MAX_URL_LEN]
Definition Deploy.h:25