Skip to content

Commit

Permalink
try to add nsfwjs to profile setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-A-Normal-Robot committed May 22, 2024
1 parent 8ada08b commit 8af7431
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions auth/discord-callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ <h1 id="txt">
`${params.get("error")} (${params.get("error_code")}): ${params.get("error_description")}.`
);
}

onSuccess();
}

function onSuccess() {
Expand Down
5 changes: 5 additions & 0 deletions js/profile-pic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
const DEFAULT_PROFILE_PICTURE_PATHS = [

].map(s => `"/data/img/default-pfps/"${s}`);
}
65 changes: 65 additions & 0 deletions js/profile-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { SUPABASE } from "./db.js";
import * as nsfwjs from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
import "https://cdn.jsdelivr.net/npm/models/mobilenet_v2/model.min.js";

let NSFWJSInstance = null;

{ // Counter for character count
for(const counter of document.getElementsByClassName('char-count')) {
Expand Down Expand Up @@ -36,6 +40,7 @@ import { SUPABASE } from "./db.js";
element.textContent = `${currentChars} / ${maxChars}`;
}
}

{ // Handle profile image upload
const profileImageInput = document.getElementById('pfp-input');
profileImageInput?.addEventListener('change', () => {
Expand All @@ -51,4 +56,64 @@ import { SUPABASE } from "./db.js";
};
reader.readAsDataURL(file);
});
}

{ // Handle NSFWJS
const MODEL_TYPE = "MobileNetV2";
const INDEXEDDB_MODEL_KEY = "nsfwjs-model";
async function loadNSFWJS() {
if(!("indexedDB" in window)) {
NSFWJSInstance = await nsfwjs.load(MODEL_TYPE);
return;
}

if(await isModelCached()) {
const cachedInstance = await nsfwjs.load(`indexeddb://${INDEXEDDB_MODEL_KEY}`);
if(cachedInstance) {
NSFWJSInstance = cachedInstance;
return;
}
}

const instance = await nsfwjs.load(MODEL_TYPE);
await instance.model.save(`indexeddb://${INDEXEDDB_MODEL_KEY}`);

NSFWJSInstance = instance;
}

async function isModelCached() {
return new Promise((resolve, reject) => {
const openRequest = indexedDB.open("tensorflowjs");

openRequest.onupgradeneeded = () => {
// The database did not previously exist, so it means the model is not saved
resolve(false);
};

openRequest.onsuccess = () => {
const db = openRequest.result;
const transaction = db.transaction("model_info_store", "readonly"); // tensorflow.js uses this object store name
const store = transaction.objectStore("model_info_store");
const getRequest = store.get(INDEXEDDB_MODEL_KEY);

getRequest.onsuccess = () => {
if (getRequest.result) {
resolve(true);
} else {
resolve(false);
}
};

getRequest.onerror = () => {
reject(getRequest.error);
};
};

openRequest.onerror = () => {
reject(openRequest.error);
};
});
}

loadNSFWJS();
}

0 comments on commit 8af7431

Please sign in to comment.