Skip to content

Commit e8067a8

Browse files
authored
ui: build-time gzip compression (ggml-org#24571)
* ui: keep original file name and path * fix nocache * ui: build-time gzip compression
1 parent 341babc commit e8067a8

4 files changed

Lines changed: 63 additions & 7 deletions

File tree

scripts/ui-assets.cmake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(HF_VERSION "" CACHE STRING "Version to download (empty = resolve from
1616
set(HF_ENABLED "" CACHE STRING "Whether to allow HF Bucket download (ON/OFF)")
1717
set(BUILD_UI "" CACHE STRING "Build UI via npm (ON/OFF)")
1818
set(LLAMA_UI_EMBED "" CACHE STRING "Path to llama-ui-embed helper")
19+
set(LLAMA_UI_GZIP "" CACHE STRING "Apply gzip compress to assets to save bandwidth")
1920

2021
set(DIST_DIR "${UI_BINARY_DIR}/dist")
2122
set(SRC_DIST_DIR "${UI_SOURCE_DIR}/dist")
@@ -225,6 +226,33 @@ function(hf_download version out_var out_resolved)
225226
endfunction()
226227

227228
function(emit_files dist_dir)
229+
# If gzip is requested, compress every asset into a parallel _gzip/ tree
230+
# the structure stays the same; for ex: /abc/def --> /_gzip/abc/def
231+
# embed.cpp will check for _gzip and will pick it up
232+
if(LLAMA_UI_GZIP AND EXISTS "${dist_dir}/index.html")
233+
find_program(GZIP_EXECUTABLE gzip)
234+
if(NOT GZIP_EXECUTABLE)
235+
message(WARNING "UI: LLAMA_UI_GZIP requested but gzip not found, embedding uncompressed")
236+
else()
237+
set(gzip_dir "${dist_dir}/_gzip")
238+
file(REMOVE_RECURSE "${gzip_dir}")
239+
file(GLOB_RECURSE all_files RELATIVE "${dist_dir}" "${dist_dir}/*")
240+
foreach(f ${all_files})
241+
get_filename_component(dst_dir "${gzip_dir}/${f}" DIRECTORY)
242+
file(MAKE_DIRECTORY "${dst_dir}")
243+
execute_process(
244+
COMMAND "${GZIP_EXECUTABLE}" -c "${dist_dir}/${f}"
245+
OUTPUT_FILE "${gzip_dir}/${f}"
246+
RESULT_VARIABLE gz_rc
247+
)
248+
if(NOT gz_rc EQUAL 0)
249+
message(FATAL_ERROR "UI: gzip failed for ${f}")
250+
endif()
251+
endforeach()
252+
message(STATUS "UI: gzip compression applied (${gzip_dir})")
253+
endif()
254+
endif()
255+
228256
set(args "${UI_CPP}" "${UI_H}")
229257
if(EXISTS "${dist_dir}/index.html")
230258
list(APPEND args "${dist_dir}")

tools/server/server-http.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,26 @@ bool server_http_context::init(const common_params & params) {
319319
}
320320
} else {
321321
#if defined(LLAMA_UI_HAS_ASSETS)
322+
static auto handle_gzip_header = [](const httplib::Request & req, httplib::Response & res) {
323+
if (!llama_ui_use_gzip()) {
324+
// no gzip build, skip
325+
return true;
326+
}
327+
if (req.get_header_value("Accept-Encoding").find("gzip") == std::string::npos) {
328+
res.status = 415; // unsupported media type
329+
res.set_content("Error: gzip is not supported by this browser", "text/plain");
330+
return false;
331+
} else {
332+
res.set_header("Content-Encoding", "gzip");
333+
}
334+
return true;
335+
};
336+
322337
auto serve_asset_cached = [](const std::string & name, bool isolation) {
323338
return [name, isolation](const httplib::Request & req, httplib::Response & res) {
339+
if (!handle_gzip_header(req, res)) {
340+
return true; // returns error message
341+
}
324342
const llama_ui_asset * a = llama_ui_find_asset(name);
325343
if (!a) { res.status = 404; return false; }
326344
res.set_header("ETag", a->etag);
@@ -340,7 +358,10 @@ bool server_http_context::init(const common_params & params) {
340358
};
341359

342360
auto serve_asset_nocache = [](const std::string & name) {
343-
return [name](const httplib::Request & /*req*/, httplib::Response & res) {
361+
return [name](const httplib::Request & req, httplib::Response & res) {
362+
if (!handle_gzip_header(req, res)) {
363+
return true; // returns error message
364+
}
344365
const llama_ui_asset * a = llama_ui_find_asset(name);
345366
if (!a) {
346367
res.status = 404;

tools/ui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(TARGET llama-ui)
22

33
set(LLAMA_UI_HF_BUCKET "ggml-org/llama-ui" CACHE STRING "Hugging Face bucket name for prebuilt UI assets")
4+
set(LLAMA_UI_GZIP ON CACHE BOOL "Apply gzip compress to assets to save bandwidth")
45

56
# Backward compat: forward old var to new one
67
if(DEFINED LLAMA_BUILD_WEBUI)
@@ -83,6 +84,7 @@ add_custom_target(llama-ui-assets ALL
8384
"-DHF_ENABLED=${LLAMA_USE_PREBUILT_UI}"
8485
"-DBUILD_UI=${LLAMA_BUILD_UI}"
8586
"-DLLAMA_UI_EMBED=${LLAMA_UI_EMBED_EXE}"
87+
"-DLLAMA_UI_GZIP=${LLAMA_UI_GZIP}"
8688
-P "${PROJECT_SOURCE_DIR}/scripts/ui-assets.cmake"
8789
COMMENT "Provisioning UI assets"
8890
VERBATIM

tools/ui/embed.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// llama-ui-embed: generate ui.cpp / ui.h that embed UI assets as C arrays.
22
//
33
// Usage:
4-
// llama-ui-embed <out_cpp> <out_h> [<asset_dir>]
4+
// llama-ui-embed <out_cpp> <out_h> <asset_dir>
55
//
66
// Recursively embeds every regular file under <asset_dir>.
77
// Asset names are relative paths from <asset_dir> (e.g. "_app/immutable/bundle.HASH.js").
@@ -145,12 +145,15 @@ int main(int argc, char ** argv) {
145145
return 1;
146146
}
147147

148-
const std::string out_cpp = argv[1];
149-
const std::string out_h = argv[2];
150-
const std::string in_dir = argv[3];
148+
const std::string out_cpp = argv[1];
149+
const std::string out_h = argv[2];
150+
const std::string asset_dir = argv[3];
151+
152+
const bool use_gzip = std::filesystem::exists(asset_dir + "/_gzip");
153+
const std::string in_dir = use_gzip ? (asset_dir + "/_gzip") : asset_dir;
151154

152155
std::vector<asset_entry> assets;
153-
if (argc == 4) {
156+
if (!in_dir.empty()) {
154157
const std::filesystem::path dir = in_dir;
155158

156159
std::error_code ec;
@@ -238,7 +241,8 @@ int main(int argc, char ** argv) {
238241
" std::string etag;\n"
239242
" std::string type;\n"
240243
"};\n\n"
241-
"const llama_ui_asset * llama_ui_find_asset(const std::string & name);\n";
244+
"const llama_ui_asset * llama_ui_find_asset(const std::string & name);\n"
245+
"bool llama_ui_use_gzip();\n";
242246
h += fmt("const std::array<llama_ui_asset, %d> & llama_ui_get_assets();\n", n_assets);
243247

244248
std::string cpp;
@@ -294,6 +298,7 @@ int main(int argc, char ** argv) {
294298
" return empty;\n"
295299
"}\n";
296300
}
301+
cpp += fmt("bool llama_ui_use_gzip() { return %s; }\n", use_gzip ? "true" : "false");
297302

298303
bool ok = true;
299304
ok = write_if_different(out_h, h) && ok;

0 commit comments

Comments
 (0)