From 16606ca089dd3247e55da4aeb38fba555732b0ef Mon Sep 17 00:00:00 2001 From: Jacob Torrey Date: Tue, 16 May 2023 15:22:46 -0600 Subject: [PATCH] Added a localStorage-based local prelude feature Signed-off-by: Jacob Torrey --- nlzmadetect/aidetect.html | 4 +++- nlzmadetect/src/nlzmadetect.nim | 31 +++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/nlzmadetect/aidetect.html b/nlzmadetect/aidetect.html index f85635d..fed66a5 100644 --- a/nlzmadetect/aidetect.html +++ b/nlzmadetect/aidetect.html @@ -8,7 +8,8 @@

LZMA-based LLM text classifier





- The results will appear here! + The results will appear here!

+ diff --git a/nlzmadetect/src/nlzmadetect.nim b/nlzmadetect/src/nlzmadetect.nim index 89eddd4..7d36071 100644 --- a/nlzmadetect/src/nlzmadetect.nim +++ b/nlzmadetect/src/nlzmadetect.nim @@ -9,10 +9,12 @@ import strutils when isMainModule and defined(c): import std/[parseopt, os] +when defined(js): + var local_prelude : string = "" + const COMPRESSION_PRESET = 1.int32 const SHORT_SAMPLE_THRESHOLD = 350 - const PRELUDE_FILE = "../../ai-generated.txt" const PRELUDE_STR = staticRead(PRELUDE_FILE) proc compress_str(s : string, preset = COMPRESSION_PRESET): float64 @@ -20,6 +22,9 @@ var PRELUDE_RATIO = compress_str("") when defined(js): var console {.importc, nodecl.}: JsObject + proc getLocalStorageItem(key : cstring) : JsObject {.importjs: "localStorage.getItem(#)".} + proc setLocalStorageItem(key : cstring, val : cstring) {.importjs: "localStorage.setItem(#, #)".} + proc compress(str : cstring, mode : int) : seq[byte] {.importjs: "LZMA.compress(#, #)".} console.log("Initialized with a prelude compression ratio of: " & $PRELUDE_RATIO) @@ -31,12 +36,13 @@ proc ti_compress(input : cstring, preset: int32, check: int32): seq[byte] = return compress(input, preset) proc compress_str(s : string, preset = COMPRESSION_PRESET): float64 = - let - in_len = PRELUDE_STR.len + s.len - var combined : string = PRELUDE_STR & s when defined(c): + let in_len = PRELUDE_STR.len + s.len + var combined : string = PRELUDE_STR & s combined = convert(PRELUDE_STR & s, "us-ascii", "UTF-8").replace(re"[^\x00-\x7F]") when defined(js): + let in_len = PRELUDE_STR.len + local_prelude.len + s.len + var combined : string = PRELUDE_STR & local_prelude & s let nonascii = newRegExp(r"[^\x00-\x7F]") combined = $combined.cstring.replace(nonascii, "") let out_len = ti_compress(combined.cstring, preset, 0.int32).len @@ -149,6 +155,21 @@ when defined(c) and isMainModule: when defined(js) and isMainModule: document.getElementById("preset_value").value = ($COMPRESSION_PRESET).cstring + if getLocalStorageItem("local_prelude") != jsNull: + local_prelude = $(getLocalStorageItem("local_prelude").to(cstring)) + PRELUDE_RATIO = compress_str("") + console.log("New prelude compression ratio of: " & $PRELUDE_RATIO) + + proc add_to_lp() {.exportc.} = + let new_text = document.getElementById("text_input").value + local_prelude = local_prelude & "\n" & ($new_text) + if getLocalStorageItem("local_prelude") != jsNull: + let existing = getLocalStorageItem("local_prelude").to(cstring) + setLocalStorageItem("local_prelude", existing & "\n" & new_text) + else: + setLocalStorageItem("local_prelude", new_text) + PRELUDE_RATIO = compress_str("") + console.log("New prelude compression ratio of: " & $PRELUDE_RATIO) proc do_detect() {.exportc.} = let @@ -159,3 +180,5 @@ when defined(js) and isMainModule: color = "rgba(0, 255, 0, " & $(s.round(3) * 10.0) & ")" document.getElementById("output_span").textContent = d.cstring & ", confidence score of: " & ($s.round(6)).cstring document.getElementById("output_span").style.backgroundColor = color.cstring + + #window.onload = on_load