kopia lustrzana https://github.com/thinkst/zippy
Added a localStorage-based local prelude feature
Signed-off-by: Jacob Torrey <jacob@thinkst.com>pull/6/head
rodzic
3b9cd7269a
commit
16606ca089
|
@ -8,7 +8,8 @@
|
|||
<h1>LZMA-based LLM text classifier</h1>
|
||||
<textarea id="text_input" placeholder="Put text to classify in here..." style="width: 75%; height: 25%;"></textarea><br /><br />
|
||||
<input type="text" id="preset_value" size="1" disabled hidden/><button onclick="time_do_detect();" id="processbtn">Click here to classify!</button><br /><br />
|
||||
<span id="output_span">The results will appear here!</span>
|
||||
<span id="output_span">The results will appear here!</span><br /><br />
|
||||
<button onclick="add_to_lp()" id="addlp" hidden>That should have been classified as AI! Be better!</button>
|
||||
</div>
|
||||
<script type="application/javascript">
|
||||
async function time_do_detect() {
|
||||
|
@ -21,6 +22,7 @@
|
|||
const end = Date.now();
|
||||
document.getElementById("processbtn").disabled = false;
|
||||
document.getElementById("processbtn").innerText = "Click here to classify!";
|
||||
document.getElementById("addlp").hidden = false;
|
||||
console.log("Processing " + ilen + " bytes took " + (end - start) + " ms");
|
||||
}
|
||||
</script>
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue