← back to Wallco Ai
scripts/audit-stoned-animals-mood.js
204 lines
#!/usr/bin/env node
/**
* audit-stoned-animals-mood.js — Gemini vision pass over the live
* stoned-animals cohort flagging designs where the animal isn't recognizable
* OR isn't actually in a relaxed/sleepy/hazy mood (the collection's intent).
*
* Steve 2026-05-31: same playbook as audit-drunk-animals-abstraction.js
* but tuned for stoned-animals which uses "Slumber/Idyll/Daydream/Haze/
* Repose/Drift/Lullaby" verbs — NO prop required (no joint/pipe/drink),
* the mood IS the motif. Failure modes: AI-stretched shapes that don't
* read as an animal, or an animal with alert/active eyes that contradicts
* the "stoned" theme.
*
* Bonus signal: title-vs-visual mismatch. The snapshot title declares a
* species ("Foxes / Otters / Sloths / Deer / Bears / Owls / Rabbits") but
* the image may show something else entirely — captured as TITLE_DRIFT in
* the audit row for Steve to spot-check.
*
* Outputs:
* /tmp/stoned-animals-audit-results.jsonl (resumable, every row)
* /tmp/stoned-animals-audit-kill-list.json (KILL ids only)
* /tmp/stoned-animals-audit-keep-list.json (KEEP ids only)
*
* Model: gemini-2.5-flash (~$0.12 for ~700 published designs).
*/
'use strict';
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
if (!API_KEY) { console.error('GEMINI_API_KEY not set'); process.exit(1); }
const MODEL = process.env.AUDIT_MODEL || 'gemini-2.5-flash';
const SERVER = process.env.AUDIT_SERVER || 'http://127.0.0.1:9905';
const CATEGORY = process.env.AUDIT_CATEGORY || 'stoned-animals';
const OUT_JSONL = '/tmp/stoned-animals-audit-results.jsonl';
const KILL_OUT = '/tmp/stoned-animals-audit-kill-list.json';
const KEEP_OUT = '/tmp/stoned-animals-audit-keep-list.json';
const CONCURRENCY = parseInt(process.env.AUDIT_CONCURRENCY, 10) || 6;
const PROMPT = `Describe what you see in this wallpaper pattern. Answer in EXACTLY this format on two lines, nothing else:
ANIMAL: name the animal species you see (e.g. fox, owl, sloth, otter, bear, rabbit, deer, raccoon, lemur), or "indistinct" if it's just smeared/stretched shapes you can't identify without being told. Be honest — if the body looks AI-stretched, dismembered, elongated, or amorphous, say "indistinct".
MOOD: pick ONE single word that best describes the animal's expression/pose: "sleepy" (closed/half-closed eyes, lying down, lounging, calm, languid, drowsy, dreamy), "neutral" (no strong mood signal either way), "alert" (wide-open eyes, staring, ears pricked, standing upright, hunting/active pose), or "indistinct" (can't tell because the figure is too abstract).`;
// Verdict logic — applied in JS so we can tune the threshold without rebuilding
// the audit. The collection is "stoned-animals" so mood matters but neutral is
// acceptable (many stylized illustrations have placid expressions Gemini labels
// "neutral"); only clear "alert"/"active" contradicts the theme.
function computeVerdict(animal, mood) {
const a = (animal || '').toLowerCase();
const m = (mood || '').toLowerCase();
if (!a || a === 'indistinct' || a === 'none') return { verdict: 'KILL', reason: 'animal indistinct' };
if (m === 'alert' || m === 'active' || m.includes('alert') || m.includes('active') ||
m.includes('hunting') || m.includes('aggressive')) {
return { verdict: 'KILL', reason: 'mood:' + m };
}
if (m === 'indistinct') return { verdict: 'KILL', reason: 'mood indistinct' };
return { verdict: 'KEEP', reason: '' };
}
function loadQueue() {
const snap = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'data', 'designs.json'), 'utf8'));
return snap.filter(d => d.category === CATEGORY && d.is_published !== false && !d.user_removed)
.map(d => ({ id: d.id, title: d.title || '', created_at: d.created_at || null }));
}
function loadDone() {
if (!fs.existsSync(OUT_JSONL)) return new Set();
const lines = fs.readFileSync(OUT_JSONL, 'utf8').split('\n').filter(Boolean);
const done = new Set();
for (const l of lines) {
try { const r = JSON.parse(l); if (r.id) done.add(r.id); } catch {}
}
return done;
}
function fetchImage(id) {
return new Promise((resolve, reject) => {
const req = http.get(`${SERVER}/designs/img/by-id/${id}`, res => {
if (res.statusCode !== 200) { res.resume(); return reject(new Error(`img HTTP ${res.statusCode}`)); }
const c = []; res.on('data', x => c.push(x));
res.on('end', () => resolve({ buf: Buffer.concat(c), mime: res.headers['content-type'] || 'image/png' }));
res.on('error', reject);
});
req.on('error', reject);
req.setTimeout(15000, () => req.destroy(new Error('img timeout')));
});
}
function callGemini(imgB64, mime) {
return new Promise((resolve, reject) => {
const body = JSON.stringify({
contents: [{ parts: [
{ inline_data: { mime_type: mime.split(';')[0].trim(), data: imgB64 } },
{ text: PROMPT },
]}],
generationConfig: { temperature: 0.1, maxOutputTokens: 100 },
});
const req = https.request({
hostname: 'generativelanguage.googleapis.com',
path: `/v1beta/models/${MODEL}:generateContent?key=${API_KEY}`,
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) },
}, res => {
const c = []; res.on('data', x => c.push(x));
res.on('end', () => {
const raw = Buffer.concat(c).toString('utf8');
if (res.statusCode !== 200) return reject(new Error(`gemini HTTP ${res.statusCode}: ${raw.slice(0,200)}`));
try {
const j = JSON.parse(raw);
const text = j?.candidates?.[0]?.content?.parts?.[0]?.text || '';
resolve({ text, usage: j?.usageMetadata });
} catch (e) { reject(new Error('gemini parse: ' + e.message)); }
});
});
req.on('error', reject);
req.setTimeout(45000, () => req.destroy(new Error('gemini timeout')));
req.write(body); req.end();
});
}
function parseVerdict(text, title) {
const animal = (text.match(/ANIMAL:\s*([^\n]+)/i) || [, ''])[1].trim().slice(0, 80);
const mood = (text.match(/MOOD:\s*([^\n]+)/i) || [, ''])[1].trim().slice(0, 80);
// Strip parenthetical hedges Gemini sometimes adds, e.g. "Sleepy (eyes half-closed)"
const moodClean = mood.replace(/\s*\(.+\)$/, '').trim();
const { verdict, reason } = computeVerdict(animal, moodClean);
// Title-drift detector
const titleAnimal = (title.match(/^([A-Z][a-z]+)s?\s+/) || [, ''])[1].toLowerCase();
const visualAnimal = animal.toLowerCase();
const titleDrift = titleAnimal && visualAnimal && visualAnimal !== 'indistinct' &&
!visualAnimal.includes(titleAnimal) && !titleAnimal.includes(visualAnimal.split(/[\s,]/)[0]);
return { verdict, kill_reason: reason, animal, mood: moodClean, title_drift: titleDrift, title_animal: titleAnimal };
}
async function auditOne(item) {
const t0 = Date.now();
try {
const { buf, mime } = await fetchImage(item.id);
const { text, usage } = await callGemini(buf.toString('base64'), mime);
const v = parseVerdict(text, item.title);
return { id: item.id, title: item.title, ...v, raw: text.slice(0, 200),
tokens_in: usage?.promptTokenCount || 0, tokens_out: usage?.candidatesTokenCount || 0,
ms: Date.now() - t0 };
} catch (e) {
return { id: item.id, title: item.title, verdict: 'ERROR', error: String(e.message || e).slice(0, 160), ms: Date.now() - t0 };
}
}
async function main() {
const queue = loadQueue();
const done = loadDone();
const work = queue.filter(it => !done.has(it.id));
console.log(`category: ${CATEGORY}, queue: ${queue.length}, done: ${done.size}, to do: ${work.length}`);
console.log(`model: ${MODEL}, concurrency: ${CONCURRENCY}`);
const out = fs.createWriteStream(OUT_JSONL, { flags: 'a' });
let idx = 0, processed = 0;
const total = work.length;
const tally = { KEEP: 0, KILL: 0, ERROR: 0, UNKNOWN: 0 };
let drifts = 0, tin = 0, tout = 0;
const t0 = Date.now();
async function worker() {
while (true) {
const i = idx++;
if (i >= work.length) break;
const r = await auditOne(work[i]);
tally[r.verdict] = (tally[r.verdict] || 0) + 1;
if (r.title_drift) drifts++;
tin += r.tokens_in || 0; tout += r.tokens_out || 0;
out.write(JSON.stringify(r) + '\n');
processed++;
if (processed % 25 === 0 || processed === total) {
const elapsed = ((Date.now() - t0) / 1000).toFixed(0);
const rate = (processed / Math.max(1, (Date.now() - t0) / 1000)).toFixed(2);
const cost = (tin * 0.10 / 1e6) + (tout * 0.40 / 1e6);
console.log(` [${processed}/${total}] ${elapsed}s @ ${rate}/s — KEEP=${tally.KEEP} KILL=${tally.KILL} ERR=${tally.ERROR} drifts=${drifts} | $${cost.toFixed(4)}`);
}
}
}
await Promise.all(Array.from({ length: CONCURRENCY }, worker));
out.end();
const allRows = fs.readFileSync(OUT_JSONL, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l));
const kill = allRows.filter(r => r.verdict === 'KILL');
const keep = allRows.filter(r => r.verdict === 'KEEP');
fs.writeFileSync(KILL_OUT, JSON.stringify(kill, null, 2));
fs.writeFileSync(KEEP_OUT, JSON.stringify(keep, null, 2));
console.log(`\n=== DONE ===`);
console.log(`kill: ${KILL_OUT} — ${kill.length} ids`);
console.log(`keep: ${KEEP_OUT} — ${keep.length} ids`);
console.log(`errors: ${allRows.filter(r => r.verdict === 'ERROR').length}`);
console.log(`title drifts: ${allRows.filter(r => r.title_drift).length}`);
console.log(`cost: $${((tin * 0.10 / 1e6) + (tout * 0.40 / 1e6)).toFixed(4)}`);
}
main().catch(e => { console.error('FATAL:', e); process.exit(1); });