From fa82334f4edb38409159c9d3cd897120593442be Mon Sep 17 00:00:00 2001 From: mrq Date: Tue, 4 Oct 2022 21:25:47 +0000 Subject: [PATCH] cum --- README.md | 6 ++ utils/renamer/package.json | 5 ++ utils/renamer/preprocess.js | 118 +++++++++++++++++++----------------- 3 files changed, 73 insertions(+), 56 deletions(-) create mode 100644 utils/renamer/package.json diff --git a/README.md b/README.md index b9b3cee..7350255 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # Textual Inversion Guide w/ E621 Content +An up-to-date repo with all the necessary files can be found here: https://git.coom.tech/mrq/stable-diffusion-utils + +**!**WARNING**!** **!**CAUTION**!** ***DO NOT POST THE REPO'S URL ON 4CHAN*** **!**CAUTION**!** **!**WARNING**!** + +`coom.tech` is an automatic 30-day ban if posted. I am not responsible if you share that URL. Share the [rentry](https://rentry.org/sd-e621-textual-inversion/) instead. + ## Assumptions This guide assumes the following basics: diff --git a/utils/renamer/package.json b/utils/renamer/package.json new file mode 100644 index 0000000..15f58a8 --- /dev/null +++ b/utils/renamer/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "node-fetch": "^2.6.7" + } +} diff --git a/utils/renamer/preprocess.js b/utils/renamer/preprocess.js index c357275..2862bca 100755 --- a/utils/renamer/preprocess.js +++ b/utils/renamer/preprocess.js @@ -18,66 +18,72 @@ for ( let i in csv ) { // for starters, you can also add "anthro", "male", "female", as they're very common tags let filters = [ "female" -] +]; -for ( let i in files ) (async () => { - let file = files[i]; - let md5 = file.match(/^([a-f0-9]{32})/)[1]; - let ext = file.split(".").pop() - console.log(i, files.length, md5, ext); +let parse = async () => { + for ( let i in files ) { + let file = files[i]; + let md5 = file.match(/^([a-f0-9]{32})/); + if ( !md5 ) continue; + md5 = md5[1]; + let ext = file.split(".").pop() + console.log(i, files.length, md5, ext); - let r = await Fetch( `https://e621.net/posts.json?tags=md5:${md5}`, { - headers: { - 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' + let r = await Fetch( `https://e621.net/posts.json?tags=md5:${md5}`, { + headers: { + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' + } + } ); + let json = JSON.parse(await r.text()); + json = json.posts[0]; + if ( !json ) continue; + tags = []; + + let artist = ""; + let content = ""; + switch ( json.rating ) { + case "s": content = "safe content"; break; + case "q": content = "questionable content"; break; + case "e": content = "explict content"; break; } - } ); - let json = JSON.parse(await res.text()); - json = json.posts[0]; - if ( !json ) continue; - tags = []; - let artist = ""; - let content = ""; - switch ( json.rating ) { - case "s": content = "safe content"; break; - case "q": content = "questionable content"; break; - case "e": content = "explict content"; break; - } - - for ( let cat in json.tags ) { - if ( cat === "artist" ) { - let tag = "by " + json.tags["artist"].join(" and ") - if ( !kson.tags[tag] ) continue; - artist = tag; - } else for ( let k in json.tags[cat] ) { - let tag = json.tags[cat][k]; - if ( !kson.tags[tag] ) continue; - if ( tag.indexOf("/") >= 0 ) continue; - if ( filters.includes(tag) ) continue; - tags.push(tag); + for ( let cat in json.tags ) { + if ( cat === "artist" ) { + let tag = "by " + json.tags["artist"].join(" and ") + if ( !kson.tags[tag] ) continue; + artist = tag; + } else for ( let k in json.tags[cat] ) { + let tag = json.tags[cat][k]; + if ( !kson.tags[tag] ) continue; + if ( tag.indexOf("/") >= 0 ) continue; + if ( filters.includes(tag) ) continue; + tags.push(tag); + } } + tags = tags.sort( (a, b) => { + return kson.tags[b] - kson.tags[a] + }) + if ( artist ) tags.unshift(artist); + if ( content ) tags.unshift(content); + + kson.files[md5] = tags; + + let jointmp = ""; + let filtered = []; + for ( let i in tags ) { + if ( (jointmp + " " + tags[i]).length > 192 ) break; // arbitrary limit for filenames and tokens, adjust if you're getting bitched at from the web UI + jointmp += " " + tags[i]; + filtered.push(tags[i]) + } + let joined = filtered.join(" ") + + require("fs").copyFileSync(`./in/${file}`, `./out/${joined}.${ext}`) + + // rate limit + await new Promise( (resolve) => { + setTimeout(resolve, 500) + } ) } - tags = tags.sort( (a, b) => { - return kson.tags[b] - kson.tags[a] - }) - if ( artist ) tags.unshift(artist); - if ( content ) tags.unshift(content); +} - kson.files[md5] = tags; - - let jointmp = ""; - let filtered = []; - for ( let i in tags ) { - if ( (jointmp + " " + tags[i]).length > 192 ) break; // arbitrary limit for filenames and tokens, adjust if you're getting bitched at from the web UI - jointmp += " " + tags[i]; - filtered.push(tags[i]) - } - let joined = filtered.join(" ") - - require("fs").copyFileSync(`./in/${file}`, `./out/${joined}.${ext}`) - - // rate limit - await new Promise( (resolve) => { - setTimeout(resolve, 500) - } ) -})(); \ No newline at end of file +parse(); \ No newline at end of file