Browse Source

Make FF use SIMD, load bin blobs locally, fix undeclared variables...

v1
coomdev 2 years ago
parent
commit
1b3f384630
  1. 5
      .gitignore
  2. 9
      build-ff.js
  3. 3269
      firefox/dist/main.js
  4. 2
      firefox/manifest.json
  5. 2
      firefox_update.json
  6. 3
      jkcs-0.27.xpi
  7. 14505
      package-lock.json
  8. 61
      src/main.js
  9. BIN
      src/tfjs-backend-wasm-simd.wasm
  10. BIN
      src/tfjs-backend-wasm-threaded-simd.wasm
  11. BIN
      src/tfjs-backend-wasm.wasm

5
.gitignore

@ -47,3 +47,8 @@ efdb47d2f0e04144bbaa-0.245.xpi
build-test.js
dist/test.js
src/pngv4.ts
firefox/model.weights.bin
firefox/tfjs-backend-wasm-simd.wasm
firefox/tfjs-backend-wasm-threaded-simd.wasm
firefox/tfjs-backend-wasm.wasm
firefox/dist/main.js

9
build-ff.js

@ -66,7 +66,7 @@ const manif = {
},
// inject: ["./esbuild.inject.js"],
plugins: [],
loader: { ".bin": "text" },
loader: { ".bin": "text", '.wasm': 'binary' },
metafile: true,
});
@ -102,7 +102,12 @@ const manif = {
);
*/
writeFileSync("./firefox/manifest.json", JSON.stringify(manif, null, 2));
copyFileSync("./logo.png", "./chrome/1449696017588.png");
copyFileSync("./logo.png", "./firefox/1449696017588.png");
copyFileSync("./src/model.weights.bin", "./firefox/model.weights.bin");
copyFileSync("./src/tfjs-backend-wasm-threaded-simd.wasm", "./firefox/tfjs-backend-wasm-threaded-simd.wasm");
copyFileSync("./src/tfjs-backend-wasm-simd.wasm", "./firefox/tfjs-backend-wasm-simd.wasm");
copyFileSync("./src/tfjs-backend-wasm.wasm", "./firefox/tfjs-backend-wasm.wasm");
res = await webExt.cmd.build({
sourceDir: "./firefox/",

3269
firefox/dist/main.js

File diff suppressed because one or more lines are too long

2
firefox/manifest.json

@ -8,7 +8,7 @@
},
"name": "JannySkillersCaptchaSolver",
"description": "The Janny Skillers Captcha Solver of choice.",
"version": "0.25",
"version": "0.27",
"icons": {
"64": "1449696017588.png"
},

2
firefox_update.json

@ -1 +1 @@
{"addons":{"{[email protected]}":{"updates":[{"version":"0.25","update_link":"https://git.coom.tech/araragi/JKCS/raw/branch/master/jkcs-0.25.xpi"}]}}}
{"addons":{"{[email protected]}":{"updates":[{"version":"0.27","update_link":"https://git.coom.tech/araragi/JKCS/raw/branch/master/jkcs-0.27.xpi"}]}}}

3
jkcs-0.27.xpi

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ce3dd88fb5afe1b809555fbb01b2db2f05435cbc3378d1c03d2c01037f903f46
size 9886879

14505
package-lock.json

File diff suppressed because it is too large

61
src/main.js

@ -2,19 +2,46 @@ import * as tf from '@tensorflow/tfjs'
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm'
import charsetJSON from './charset.json'
import modelJSON from './model.json'
import weightsData from './model.weights.bin'
let weightsData
let model
tf.enableProdMode()
// tf.enableDebugMode()
setWasmPaths({
'tfjs-backend-wasm.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm.wasm',
'tfjs-backend-wasm-simd.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm-simd.wasm',
'tfjs-backend-wasm-threaded-simd.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm-threaded-simd.wasm'
}) // TODO: Figure out how the fuck to load the WASM modules locally from the extension.
// setWasmPaths('https://unpkg.com/@tensorflow/[email protected]/dist/', true)
tf.setBackend('wasm')
const wasmToUrl = wasm => {
const blb = new Blob([tfwasm])
return URL.createObjectURL(blb)
}
const backendloaded = (async () => {
try {
// dead code elimination should occur here
if (execution_mode === 'userscript') {
weightsData = import('./model.weights.bin')
const tfwasmthreadedsimd = await import('./tfjs-backend-wasm-threaded-simd.wasm')
const tfwasmsimd = await import('./tfjs-backend-wasm-simd.wasm')
const tfwasm = await import('./tfjs-backend-wasm.wasm')
setWasmPaths({
'tfjs-backend-wasm.wasm': wasmToUrl(tfwasm),
'tfjs-backend-wasm-simd.wasm': wasmToUrl(tfwasmsimd),
'tfjs-backend-wasm-threaded-simd.wasm': wasmToUrl(tfwasmthreadedsimd)
})
} else {
weightsData = await (await fetch(chrome.runtime.getURL('./model.weights.bin'))).text()
const args = {
'tfjs-backend-wasm.wasm': chrome.runtime.getURL('tfjs-backend-wasm.wasm'),
'tfjs-backend-wasm-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-simd.wasm'),
'tfjs-backend-wasm-threaded-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-threaded-simd.wasm')
}
setWasmPaths(args)
}
const l = await tf.setBackend('wasm')
console.log('tf backend loaded', l)
} catch (err) {
console.log('tf err', err)
}
})()
function toggle (obj, v) {
if (v) obj.style.display = ''
@ -204,19 +231,19 @@ async function predict (img, bg, off) {
if (!model) {
model = await load()
}
image = imageFromCanvas(img, bg, off)
tensor = tf.browser
const image = imageFromCanvas(img, bg, off)
const tensor = tf.browser
.fromPixels(image, 1)
.mul(-1 / 238)
.add(1)
prediction = await model.predict(tensor.expandDims(0)).data()
const prediction = await model.predict(tensor.expandDims(0)).data()
return createSequence(prediction)
}
function createSequence (prediction) {
const csl = charsetJSON.charset.length
sequence = []
const sequence = []
for (let pos = 0; pos < prediction.length; pos += csl) {
const preds = prediction.slice(pos, pos + csl)
@ -240,8 +267,8 @@ function createSequence (prediction) {
}
function postprocess (sequence, overrides) {
csl = charsetJSON.charset.length
possibilities = [{ sequence: [] }]
const csl = charsetJSON.charset.length
let possibilities = [{ sequence: [] }]
sequence.forEach(function (e, i) {
let additions
@ -255,7 +282,7 @@ function postprocess (sequence, overrides) {
if (additions.length === 1 && additions[0].sym === '') return
oldpos = possibilities
const oldpos = possibilities
possibilities = []
oldpos.forEach(function (possibility) {
additions.forEach(function (a) {
@ -381,6 +408,8 @@ async function solve (force) {
const help = document.getElementById('t-help')
if (!help) return
await backendloaded
placeAfter(solveButton, resp)
placeAfter(altsDiv, help)

BIN
src/tfjs-backend-wasm-simd.wasm

Binary file not shown.

BIN
src/tfjs-backend-wasm-threaded-simd.wasm

Binary file not shown.

BIN
src/tfjs-backend-wasm.wasm

Binary file not shown.
Loading…
Cancel
Save