Skip to content

Commit

Permalink
server : add request path option(ggerganov#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
eschmidbauer authored and iThalay committed Sep 23, 2024
1 parent bfbc653 commit f579b4b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct server_params
{
std::string hostname = "127.0.0.1";
std::string public_path = "examples/server/public";
std::string request_path = "";

int32_t port = 8080;
int32_t read_timeout = 600;
Expand Down Expand Up @@ -161,6 +162,7 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
fprintf(stderr, " --public PATH, [%-7s] Path to the public folder\n", sparams.public_path.c_str());
fprintf(stderr, " --request-path PATH, [%-7s] Request path for all requests\n", sparams.request_path.c_str());
fprintf(stderr, " --convert, [%-7s] Convert audio to WAV, requires ffmpeg on the server", sparams.ffmpeg_converter ? "true" : "false");
fprintf(stderr, "\n");
}
Expand Down Expand Up @@ -208,6 +210,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
else if ( arg == "--host") { sparams.hostname = argv[++i]; }
else if ( arg == "--public") { sparams.public_path = argv[++i]; }
else if ( arg == "--request-path") { sparams.request_path = argv[++i]; }
else if ( arg == "--convert") { sparams.ffmpeg_converter = true; }
else {
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
Expand Down Expand Up @@ -480,12 +483,12 @@ int main(int argc, char ** argv) {
std::string const default_content = "<html>hello</html>";

// this is only called if no index.html is found in the public --path
svr.Get("/", [&default_content](const Request &, Response &res){
svr.Get(sparams.request_path + "/", [&default_content](const Request &, Response &res){
res.set_content(default_content, "text/html");
return false;
});

svr.Post("/inference", [&](const Request &req, Response &res){
svr.Post(sparams.request_path + "/inference", [&](const Request &req, Response &res){
// acquire whisper model mutex lock
whisper_mutex.lock();

Expand Down Expand Up @@ -722,7 +725,7 @@ int main(int argc, char ** argv) {
// return whisper model mutex lock
whisper_mutex.unlock();
});
svr.Post("/load", [&](const Request &req, Response &res){
svr.Post(sparams.request_path + "/load", [&](const Request &req, Response &res){
whisper_mutex.lock();
if (!req.has_file("model"))
{
Expand Down Expand Up @@ -778,11 +781,11 @@ int main(int argc, char ** argv) {
res.status = 500;
});

svr.set_error_handler([](const Request &, Response &res) {
svr.set_error_handler([](const Request &req, Response &res) {
if (res.status == 400) {
res.set_content("Invalid request", "text/plain");
} else if (res.status != 500) {
res.set_content("File Not Found", "text/plain");
res.set_content("File Not Found (" + req.path + ")", "text/plain");
res.status = 404;
}
});
Expand Down

0 comments on commit f579b4b

Please sign in to comment.