Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Improve highlighting in new web ui
Justine Tunneyd25fa3a3d91e
diff --git a/llamafile/server/www/chatbot.png b/llamafile/server/www/chatbot.pngindex 20557f4f..0e96111c 100644Binary files a/llamafile/server/www/chatbot.png and b/llamafile/server/www/chatbot.png differdiff --git a/llamafile/server/www/highlight_cmake.js b/llamafile/server/www/highlight_cmake.jsnew file mode 100644index 00000000..733026a7--- /dev/null+++ b/llamafile/server/www/highlight_cmake.js@@ -0,0 +1,261 @@+// Copyright 2024 Mozilla Foundation+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++const CMAKE_KEYWORDS = new Set([+ 'block',+ 'else',+ 'elseif',+ 'endblock',+ 'endforeach',+ 'endfunction',+ 'endif',+ 'endmacro',+ 'endwhile',+ 'foreach',+ 'function',+ 'if',+ 'macro',+ 'while',+]);++class HighlightCmake extends Highlighter {++ static NORMAL = 0;+ static BACKSLASH = 1;+ static WORD = 2;+ static WORD_SPACE = 3;+ static DOLLAR = 4;+ static VAR = 5;+ static COMMENT = 6;+ static DQUOTE = 7;+ static DQUOTE_BACKSLASH = 8;+ static DQUOTE_DOLLAR = 9;+ static DQUOTE_VAR = 10;++ constructor(delegate) {+ super(delegate);+ this.spaces = 0;+ this.word = '';+ }++ feed(input) {+ for (let i = 0; i < input.length; i += this.delta) {+ this.delta = 1;+ let c = input[i];+ switch (this.state) {++ case HighlightCmake.NORMAL:+ if (!isascii(c) || isalpha(c) || c == '_') {+ this.state = HighlightCmake.WORD;+ this.word += c;+ } else if (c == '#') {+ this.state = HighlightCmake.COMMENT;+ this.push("span", "comment");+ this.append('#');+ } else if (c == '"') {+ this.state = HighlightCmake.DQUOTE;+ this.push("span", "string");+ this.append('"');+ } else if (c == '$') {+ this.state = HighlightCmake.DOLLAR;+ } else if (c == '\\') {+ this.state = HighlightCmake.BACKSLASH;+ this.push("span", "escape");+ this.append('\\');+ } else {+ this.append(c);+ }+ break;++ case HighlightCmake.BACKSLASH:+ this.append(c);+ this.pop();+ this.state = HighlightCmake.NORMAL;+ break;++ case HighlightCmake.WORD:+ if (!isascii(c) || isalnum(c) || c == '_') {+ this.word += c;+ } else {+ this.spaces = 0;+ this.epsilon(HighlightCmake.WORD_SPACE);+ }+ break;++ case HighlightCmake.WORD_SPACE:+ if (c == ' ') {+ ++this.spaces;+ } else if (c == '(') {+ if (CMAKE_KEYWORDS.has(this.word.toLowerCase())) {+ this.push("span", "keyword");+ this.append(this.word);+ this.pop();+ } else {+ this.push("span", "builtin");+ this.append(this.word);+ this.pop();+ }+ this.word = '';+ for (let i = 0; i < this.spaces; ++i)+ this.append(' ');+ this.epsilon(HighlightCmake.NORMAL);+ } else {+ this.append(this.word);+ this.word = '';+ for (let i = 0; i < this.spaces; ++i)+ this.append(' ');+ this.epsilon(HighlightCmake.NORMAL);+ }+ break;++ case HighlightCmake.COMMENT:+ this.append(c);+ if (c == '\n') {+ this.pop();+ this.state = HighlightCmake.NORMAL;+ }+ break;++ case HighlightCmake.DOLLAR:+ if (c == '{') {+ this.state = HighlightCmake.VAR;+ } else if (c == '$') {+ this.append('$');+ } else {+ this.append('$');+ this.epsilon(HighlightCmake.NORMAL);+ }+ break;++ case HighlightCmake.VAR:+ if (isalnum(c) || c == '_') {+ this.word += c;+ } else if (c == '}') {+ this.append("${");+ this.push("span", "var");+ this.append(this.word);+ this.pop();+ this.append('}');+ this.word = '';+ this.state = HighlightCmake.NORMAL;+ } else {+ this.append("${");+ this.append(this.word);+ this.word = '';+ this.epsilon(HighlightCmake.NORMAL);+ }+ break;++ case HighlightCmake.DQUOTE:+ if (c == '"') {+ this.append('"');+ this.pop();+ this.state = HighlightCmake.NORMAL;+ } else if (c == '\\') {+ this.append('\\');+ this.state = HighlightCmake.DQUOTE_BACKSLASH;+ } else if (c == '$') {+ this.state = HighlightCmake.DQUOTE_DOLLAR;+ } else {+ this.append(c);+ }+ break;++ case HighlightCmake.DQUOTE_BACKSLASH:+ this.append(c);+ this.state = HighlightCmake.DQUOTE;+ break;++ case HighlightCmake.DQUOTE_DOLLAR:+ if (c == '{') {+ this.state = HighlightCmake.DQUOTE_VAR;+ } else if (c == '$') {+ this.append('$');+ } else {+ this.append('$');+ this.epsilon(HighlightCmake.DQUOTE);+ }+ break;++ case HighlightCmake.DQUOTE_VAR:+ if (isalnum(c) || c == '_') {+ this.word += c;+ } else if (c == '}') {+ this.append("${");+ this.push("span", "var");+ this.append(this.word);+ this.pop();+ this.append('}');+ this.word = '';+ this.state = HighlightCmake.DQUOTE;+ } else {+ this.append("${");+ this.append(this.word);+ this.word = '';+ this.epsilon(HighlightCmake.DQUOTE);+ }+ break;++ default:+ throw new Error('Invalid state');+ }+ }+ }++ flush() {+ switch (this.state) {+ case HighlightCmake.WORD:+ this.append(this.word);+ this.word = '';+ break;+ case HighlightCmake.WORD_SPACE:+ this.append(this.word);+ this.word = '';+ for (let i = 0; i < this.spaces; ++i)+ this.append(' ');+ break;+ case HighlightCmake.DOLLAR:+ this.append('$');+ break;+ case HighlightCmake.DQUOTE_DOLLAR:+ this.append('$');+ this.pop();+ break;+ case HighlightCmake.DQUOTE_VAR:+ this.append("${");+ this.append(this.word);+ this.pop();+ this.word = '';+ break;+ case HighlightCmake.VAR:+ this.append("${");+ this.append(this.word);+ this.word = '';+ break;+ case HighlightCmake.DQUOTE:+ case HighlightCmake.COMMENT:+ case HighlightCmake.BACKSLASH:+ case HighlightCmake.DQUOTE_BACKSLASH:+ this.pop();+ break;+ default:+ break;+ }+ this.state = HighlightCmake.NORMAL;+ this.delegate.flush();+ this.delta = 1;+ }+}++Highlighter.REGISTRY['cmake'] = HighlightCmake;diff --git a/llamafile/server/www/highlight_make.js b/llamafile/server/www/highlight_make.jsindex 01747b01..1f37f49c 100644--- a/llamafile/server/www/highlight_make.js+++ b/llamafile/server/www/highlight_make.js@@ -72,22 +72,6 @@ const MAKE_BUILTINS = new Set(['words',]);-function is_automatic_variable(c) {- switch (c) {- case '@':- case '%':- case '<':- case '?':- case '^':- case '+':- case '|':- case '*':- return true;- default:- return false;- }-}-class HighlightMake extends Highlighter {static NORMAL = 0;@@ -99,6 +83,22 @@ class HighlightMake extends Highlighter {static VARIABLE = 6;static BACKSLASH = 7;+ static is_automatic_variable(c) {+ switch (c) {+ case '@':+ case '%':+ case '<':+ case '?':+ case '^':+ case '+':+ case '|':+ case '*':+ return true;+ default:+ return false;+ }+ }+constructor(delegate) {super(delegate);this.word = '';@@ -154,7 +154,7 @@ class HighlightMake extends Highlighter {break;case HighlightMake.DOLLAR:- if (isdigit(c) || is_automatic_variable(c)) {+ if (isdigit(c) || HighlightMake.is_automatic_variable(c)) {this.push("span", "var");this.append(c);this.pop();@@ -191,7 +191,7 @@ class HighlightMake extends Highlighter {c == '@' || //c == '_') {this.word += c;- } else if (c == '$' && this.word.empty()) {+ } else if (c == '$' && !this.word) {this.state = HighlightMake.DOLLAR;this.append('$');} else if (c == ')' || //diff --git a/llamafile/server/www/highlight_markdown.js b/llamafile/server/www/highlight_markdown.jsindex c0682f80..129392c0 100644--- a/llamafile/server/www/highlight_markdown.js+++ b/llamafile/server/www/highlight_markdown.js@@ -35,6 +35,13 @@ class HighlightMarkdown extends Highlighter {static NEWLINE = 18;static EAT_NEWLINE = 19;static INCODE2_TICK2 = 20;+ static LSB = 21;+ static LSB_BACKSLASH = 22;+ static LSB_RSB = 23;+ static LSB_RSB_LPAREN = 24;+ static LSB_RSB_LPAREN_BACKSLASH = 25;+ static LT = 26;+ static LT_BACKSLASH = 27;constructor(delegate) {super(delegate);@@ -43,6 +50,8 @@ class HighlightMarkdown extends Highlighter {this.lang = '';this.highlighter = null;this.newlines = 0;+ this.text = '';+ this.href = '';}feed(input) {@@ -58,6 +67,10 @@ class HighlightMarkdown extends Highlighter {} else if (c == '*') {this.state = HighlightMarkdown.STAR;break;+ } else if (c == '[') {+ this.state = HighlightMarkdown.LSB;+ } else if (c == '<') {+ this.state = HighlightMarkdown.LT;} else if (c == '\\') {// handle \*\*not bold\*\* etc.this.state = HighlightMarkdown.BACKSLASH;@@ -108,10 +121,12 @@ class HighlightMarkdown extends Highlighter {// handle *emphasized* text// inverted because \e[3m has a poorly supported western biasthis.push('span', 'italic');- this.append(c);- this.state = HighlightMarkdown.EMPHASIS;- if (c == '\\')+ if (c == '\\') {this.state = HighlightMarkdown.EMPHASIS_BACKSLASH;+ } else {+ this.state = HighlightMarkdown.EMPHASIS;+ this.append(c);+ }}this.bol = false;break;@@ -287,6 +302,72 @@ class HighlightMarkdown extends Highlighter {}break;+ case HighlightMarkdown.LT:+ if (c == '>') {+ let a = this.push('a', '');+ a.innerText = this.href;+ a.href = this.href;+ this.pop();+ this.href = '';+ this.state = HighlightMarkdown.NORMAL;+ } else if (c == '\\') {+ this.state = HighlightMarkdown.LT_BACKSLASH;+ } else {+ this.href += c;+ }+ break;++ case HighlightMarkdown.LT_BACKSLASH:+ this.href += c;+ this.state = HighlightMarkdown.LT;+ break;++ case HighlightMarkdown.LSB:+ if (c == ']') {+ this.state = HighlightMarkdown.LSB_RSB;+ } else if (c == '\\') {+ this.state = HighlightMarkdown.LSB_BACKSLASH;+ } else {+ this.text += c;+ }+ break;++ case HighlightMarkdown.LSB_BACKSLASH:+ this.text += c;+ this.state = HighlightMarkdown.LSB;+ break;++ case HighlightMarkdown.LSB_RSB:+ if (c == '(') {+ this.state = HighlightMarkdown.LSB_RSB_LPAREN;+ } else {+ this.append('[' + this.text + ']');+ this.text = '';+ this.epsilon(HighlightMarkdown.NORMAL);+ }+ break;++ case HighlightMarkdown.LSB_RSB_LPAREN:+ if (c == ')') {+ let a = this.push('a', '');+ a.innerText = this.text;+ a.href = this.href;+ this.pop();+ this.href = '';+ this.text = '';+ this.state = HighlightMarkdown.NORMAL;+ } else if (c == '\\') {+ this.state = HighlightMarkdown.LSB_RSB_LPAREN_BACKSLASH;+ } else {+ this.href += c;+ }+ break;++ case HighlightMarkdown.LSB_RSB_LPAREN_BACKSLASH:+ this.href += c;+ this.state = HighlightMarkdown.LSB_RSB_LPAREN;+ break;+default:throw new Error('Invalid state');}@@ -335,6 +416,26 @@ class HighlightMarkdown extends Highlighter {this.highlighter = null;this.pop();break;+ case HighlightMarkdown.LT:+ case HighlightMarkdown.LT_BACKSLASH:+ this.append('<' + this.href);+ this.href = '';+ break;+ case HighlightMarkdown.LSB:+ case HighlightMarkdown.LSB_BACKSLASH:+ this.append('[' + this.text);+ this.text = '';+ break;+ case HighlightMarkdown.LSB_RSB:+ this.append('[' + this.text + ']');+ this.text = '';+ break;+ case HighlightMarkdown.LSB_RSB_LPAREN:+ case HighlightMarkdown.LSB_RSB_LPAREN_BACKSLASH:+ this.append('[' + this.text + '](' + this.href);+ this.text = '';+ this.href = '';+ break;default:break;}diff --git a/llamafile/server/www/highlight_ocaml.js b/llamafile/server/www/highlight_ocaml.jsindex f37559ea..61b1d5ba 100644--- a/llamafile/server/www/highlight_ocaml.js+++ b/llamafile/server/www/highlight_ocaml.js@@ -263,7 +263,7 @@ class HighlightOcaml extends Highlighter {this.word += c;} else {this.append('{');- if (this.word.empty()) {+ if (!this.word) {this.epsilon(HighlightOcaml.NORMAL);} else {this.epsilon(HighlightOcaml.WORD);diff --git a/llamafile/server/www/index.html b/llamafile/server/www/index.htmlindex c1df1913..75854d65 100644--- a/llamafile/server/www/index.html+++ b/llamafile/server/www/index.html@@ -8,7 +8,7 @@<div class="chat-container"><div class="chat-header">- <h1><img src="chatbot.png" alt="[logo]"> llamafile</h1>+ <h1><img src="chatbot.png" alt="[logo]" width="64" height="64"> llamafile</h1></div><div class="chat-messages" id="chat-messages"><div class="message system">@@ -72,5 +72,6 @@<script src="highlight_tex.js"></script><script src="highlight_typescript.js"></script><script src="highlight_zig.js"></script>+<script src="highlight_cmake.js"></script><script src="chatbot.js"></script>