From dfe31e048417d6dc099ac288b2ba3dd98cad1bc9 Mon Sep 17 00:00:00 2001 From: VJHack Date: Mon, 9 Sep 2024 22:06:56 -0500 Subject: [PATCH 1/7] Adding loading page for '/' server requests --- Makefile | 1 + examples/server/CMakeLists.txt | 1 + examples/server/public/loading.html | 12 ++++++++++++ examples/server/server.cpp | 19 ++++++++++++++++--- 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 examples/server/public/loading.html diff --git a/Makefile b/Makefile index 97ef37c0e..ae2e17c15 100644 --- a/Makefile +++ b/Makefile @@ -1430,6 +1430,7 @@ llama-server: \ examples/server/theme-snowstorm.css.hpp \ examples/server/index.html.hpp \ examples/server/index-new.html.hpp \ + examples/server/loading.html.hpp \ examples/server/index.js.hpp \ examples/server/completion.js.hpp \ examples/server/system-prompts.js.hpp \ diff --git a/examples/server/CMakeLists.txt b/examples/server/CMakeLists.txt index dbe41f1fd..b62f813c8 100644 --- a/examples/server/CMakeLists.txt +++ b/examples/server/CMakeLists.txt @@ -25,6 +25,7 @@ set(PUBLIC_ASSETS theme-snowstorm.css index.html index-new.html + loading.html index.js completion.js system-prompts.js diff --git a/examples/server/public/loading.html b/examples/server/public/loading.html new file mode 100644 index 000000000..5e0a837a6 --- /dev/null +++ b/examples/server/public/loading.html @@ -0,0 +1,12 @@ + + + + + + +
+ The model is loading. Please wait.
+ The user interface will appear soon. +
+ + \ No newline at end of file diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 7495821f9..e10f8fed5 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -23,6 +23,7 @@ #include "theme-snowstorm.css.hpp" #include "index.html.hpp" #include "index-new.html.hpp" +#include "loading.html.hpp" #include "index.js.hpp" #include "completion.js.hpp" #include "system-prompts.js.hpp" @@ -2591,11 +2592,22 @@ int main(int argc, char ** argv) { return false; }; - auto middleware_server_state = [&res_error, &state](const httplib::Request &, httplib::Response & res) { + auto middleware_server_state = [&res_error, &state](const httplib::Request & req, httplib::Response & res) { server_state current_state = state.load(); if (current_state == SERVER_STATE_LOADING_MODEL) { - res_error(res, format_error_response("Loading model", ERROR_TYPE_UNAVAILABLE)); - return false; + httplib::Request & modified_req = (httplib::Request &) req; + const char* path_c = modified_req.path.c_str(); + int path_c_len = strlen(path_c); + char last_five[6]; + strcpy(last_five, path_c + (path_c_len -5)); + + if ((strcmp(path_c, "/") == 0) || (strcmp(last_five, ".html") == 0)) { + modified_req.path = "/loading.html"; + } + else { + res_error(res, format_error_response("Loading model", ERROR_TYPE_UNAVAILABLE)); + return false; + } } return true; }; @@ -3162,6 +3174,7 @@ int main(int argc, char ** argv) { svr->Get("/theme-polarnight.css", handle_static_file(theme_polarnight_css, theme_polarnight_css_len, "text/css; charset=utf-8")); svr->Get("/theme-snowstorm.css", handle_static_file(theme_snowstorm_css, theme_snowstorm_css_len, "text/css; charset=utf-8")); svr->Get("/index-new.html", handle_static_file(index_new_html, index_new_html_len, "text/html; charset=utf-8")); + svr->Get("/loading.html", handle_static_file(loading_html, loading_html_len, "text/html; charset=utf-8")); svr->Get("/system-prompts.js", handle_static_file(system_prompts_js, system_prompts_js_len, "text/javascript; charset=utf-8")); svr->Get("/prompt-formats.js", handle_static_file(prompt_formats_js, prompt_formats_js_len, "text/javascript; charset=utf-8")); From dab4b49f04d1a176fe4510592455fb5424166b56 Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:20:11 -0500 Subject: [PATCH 2/7] set content when model is loading --- examples/server/server.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index e10f8fed5..0b17dd715 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2476,7 +2476,6 @@ int main(int argc, char ** argv) { #endif std::atomic state{SERVER_STATE_LOADING_MODEL}; - svr->set_default_headers({{"Server", "llama.cpp"}}); // CORS preflight @@ -2592,22 +2591,11 @@ int main(int argc, char ** argv) { return false; }; - auto middleware_server_state = [&res_error, &state](const httplib::Request & req, httplib::Response & res) { + auto middleware_server_state = [&res_error, &state](const httplib::Request &, httplib::Response & res) { server_state current_state = state.load(); if (current_state == SERVER_STATE_LOADING_MODEL) { - httplib::Request & modified_req = (httplib::Request &) req; - const char* path_c = modified_req.path.c_str(); - int path_c_len = strlen(path_c); - char last_five[6]; - strcpy(last_five, path_c + (path_c_len -5)); - - if ((strcmp(path_c, "/") == 0) || (strcmp(last_five, ".html") == 0)) { - modified_req.path = "/loading.html"; - } - else { - res_error(res, format_error_response("Loading model", ERROR_TYPE_UNAVAILABLE)); - return false; - } + res.set_content("The model is loading. Please wait.
The user interface will appear soon.", "text/html; charset=utf-8"); + return false; } return true; }; From 19bc86307fc69d5f0892e9e197361b61d1c835ef Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:23:09 -0500 Subject: [PATCH 3/7] removed loading html file --- examples/server/public/loading.html | 12 ------------ examples/server/server.cpp | 2 -- 2 files changed, 14 deletions(-) delete mode 100644 examples/server/public/loading.html diff --git a/examples/server/public/loading.html b/examples/server/public/loading.html deleted file mode 100644 index 5e0a837a6..000000000 --- a/examples/server/public/loading.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - -
- The model is loading. Please wait.
- The user interface will appear soon. -
- - \ No newline at end of file diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 0b17dd715..39f8111f8 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -23,7 +23,6 @@ #include "theme-snowstorm.css.hpp" #include "index.html.hpp" #include "index-new.html.hpp" -#include "loading.html.hpp" #include "index.js.hpp" #include "completion.js.hpp" #include "system-prompts.js.hpp" @@ -3162,7 +3161,6 @@ int main(int argc, char ** argv) { svr->Get("/theme-polarnight.css", handle_static_file(theme_polarnight_css, theme_polarnight_css_len, "text/css; charset=utf-8")); svr->Get("/theme-snowstorm.css", handle_static_file(theme_snowstorm_css, theme_snowstorm_css_len, "text/css; charset=utf-8")); svr->Get("/index-new.html", handle_static_file(index_new_html, index_new_html_len, "text/html; charset=utf-8")); - svr->Get("/loading.html", handle_static_file(loading_html, loading_html_len, "text/html; charset=utf-8")); svr->Get("/system-prompts.js", handle_static_file(system_prompts_js, system_prompts_js_len, "text/javascript; charset=utf-8")); svr->Get("/prompt-formats.js", handle_static_file(prompt_formats_js, prompt_formats_js_len, "text/javascript; charset=utf-8")); From 125737a255c582cb41f449e3b4908913f314990e Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:23:57 -0500 Subject: [PATCH 4/7] updated cmakelist --- examples/server/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/server/CMakeLists.txt b/examples/server/CMakeLists.txt index b62f813c8..dbe41f1fd 100644 --- a/examples/server/CMakeLists.txt +++ b/examples/server/CMakeLists.txt @@ -25,7 +25,6 @@ set(PUBLIC_ASSETS theme-snowstorm.css index.html index-new.html - loading.html index.js completion.js system-prompts.js From 3dd73ca6626f6878699a78c40986d0222588d3de Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:24:39 -0500 Subject: [PATCH 5/7] updated makefile --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index ae2e17c15..97ef37c0e 100644 --- a/Makefile +++ b/Makefile @@ -1430,7 +1430,6 @@ llama-server: \ examples/server/theme-snowstorm.css.hpp \ examples/server/index.html.hpp \ examples/server/index-new.html.hpp \ - examples/server/loading.html.hpp \ examples/server/index.js.hpp \ examples/server/completion.js.hpp \ examples/server/system-prompts.js.hpp \ From 1ff1aa722aa41edeefc5caf6a7ea2a3917ad5c7e Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:25:32 -0500 Subject: [PATCH 6/7] cleaned up whitespace --- examples/server/server.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 39f8111f8..43f061aec 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2475,6 +2475,7 @@ int main(int argc, char ** argv) { #endif std::atomic state{SERVER_STATE_LOADING_MODEL}; + svr->set_default_headers({{"Server", "llama.cpp"}}); // CORS preflight From 9d3424a3c15f9b03bdc409e64ad9cb735ae5cf67 Mon Sep 17 00:00:00 2001 From: VJHack Date: Tue, 10 Sep 2024 22:26:58 -0500 Subject: [PATCH 7/7] cleanup for PR removed error --- examples/server/server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 43f061aec..e03cddf77 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -2591,7 +2591,7 @@ int main(int argc, char ** argv) { return false; }; - auto middleware_server_state = [&res_error, &state](const httplib::Request &, httplib::Response & res) { + auto middleware_server_state = [&state](const httplib::Request &, httplib::Response & res) { server_state current_state = state.load(); if (current_state == SERVER_STATE_LOADING_MODEL) { res.set_content("The model is loading. Please wait.
The user interface will appear soon.", "text/html; charset=utf-8");