-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ada08b
commit 8af7431
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) { | ||
|
@@ -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', () => { | ||
|
@@ -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(); | ||
} |