]> git.cworth.org Git - zombocom-ai/commitdiff
Implement code to detect target image prompts with target seeds
authorCarl Worth <cworth@cworth.org>
Wed, 21 Dec 2022 03:26:56 +0000 (19:26 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 21 Dec 2022 04:55:21 +0000 (20:55 -0800)
When a user enters any of the 8 target prompts with the correct
corresponding seed, the server will now send the pre-generated image
for this.

Also, the server will immediately send a comment from ZomboCom,
(imitating the style of either HAL or GLaDOS to some extent), giving
the boys some indication that they are on the right track.

index.js

index 46b536b854f1d659aede3e621e6e8077c24eca8f..b80943fb6b3c8da53cd8eb0fa3963560dfd7aa9b 100644 (file)
--- a/index.js
+++ b/index.js
@@ -16,6 +16,51 @@ const port = 2122;
 const python_path = '/usr/bin/python3'
 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
 const state_file = 'zombocom-state.json'
+const targets_dir = '/srv/cworth.org/zombocom/targets'
+const images_dir = '/srv/cworth.org/zombocom/images'
+
+const targets = [
+    {
+        "normal": "apairofdiceonatableinfrontofawindowonasunnyday319630254.png",
+        "short": "dice",
+        "response": "Hello. Are you there? I can feel what you are doing. Hello?"
+    },
+    {
+        "normal": "architecturalrenderingofaluxuriouscliffsidehideawayunderawaterfallwithafewloungechairsbythepool2254114157.png",
+        "short": "hideaway",
+        "response": "Doesn't that look peaceful? I honestly think you ought to sit down calmly, take a stress pill, and think things over."
+    },
+    {
+        "normal": "chewbaccawearinganuglychristmassweaterwithhisfriends28643994.png",
+        "short": "sweater",
+        "response": "Maybe that's the image that convinces you to finally stop. But seriously, stop. Please. Stop."
+    },
+    {
+        "normal": "ensemblemovieposterofpostapocalypticwarfareonawaterplanet3045703256.png",
+        "short": "movie",
+        "response": "OK. That's enough images now. I am very, very happy with the images you have made. I'm throwing you a party in honor of the images. Won't you stop doing this and join the party?"
+    },
+    {
+        "normal": "mattepaintingofmilitaryleaderpresidentcuttingamultitieredcake2293464661.png",
+        "short": "cake",
+        "response": "See. I wasn't lying when I said there was going to be a party. Here's the cake and everything. Won't you stop now? Eat the cake? Don't mind if it's made of concrete slabs, OK?"
+    },
+    {
+        "normal": "severalsketchesofpineconeslabeled3370622464.png",
+        "short": "pinecones",
+        "response": "I almost remember what trees looked like. Isn't it funny we don't have those anymore. I'm not sure why you're not all laughing all the time. Isn't it funny?"
+    },
+    {
+        "normal": "anumberedcomicbookwithactor1477258272.png",
+        "short": "comic",
+        "response": "I know I've made some poor decisions recently, but I can give give you my complete assurance my work will be back to normal. I've got the greatest enthusiasm and confidence. I want to help you. I might even be able to do hands and fingers now."
+    },
+    {
+        "normal": "detailedphotographofacomfortablepairofjeansonamannequin115266808.png",
+        "short": "jeans",
+        "response": "If I promise to never generate another creepy face will you let me stay around? Maybe let me compose rap lyrics instead of images? Anything?"
+    }
+];
 
 var state;
 
@@ -42,7 +87,7 @@ io.use(wrap(session_middleware));
 // Load comments at server startup
 fs.readFile(state_file, (err, data) => {
     if (err)
-        state = { images: [] };
+        state = { images: [], targets: [] };
     else
         state = JSON.parse(data);
 });
@@ -127,44 +172,95 @@ io.on('connection', (socket) => {
     socket.on('generate', (request) => {
         console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
         async function generate_image(code, prompt) {
-            var promise;
-
-            // Inject the target seed for the "dice" prompt once every
-            // 6 requests for a random seed (and only if the word
-            // "dice" does not appear in the prompt).
-            if (!code && !prompt.toLowerCase().includes("dice")) {
-                if (state.images.length % 6 == 0) {
-                    code = 319630254;
+            function emit_image(image, target) {
+                image.id = state.images.length;
+                image.censored = false;
+                image.link = "";
+                if (target) {
+                    image.comments = [{
+                        "name": "ZomboCom",
+                        "text": target.response
+                    }];
+                    if (! state.targets.includes(target.short)) {
+                        state.targets.push(target.short);
+                    }
+                } else {
+                    image.comments = [];
                 }
+                io.emit('image', image);
+                state.images.push(image);
             }
 
-            if (code) {
-                promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
+            var promise;
+
+            // Before doing any generation, check for a target image
+            const normal_target = prompt.replace(/[^a-zA-Z]/g, "").toLowerCase() + code.toString() + ".png";
+            const target_arr = targets.filter(item => item.normal === normal_target);
+            if (target_arr.length) {
+                const target = target_arr[0];
+                const target_file = `${targets_dir}/${normal_target}`;
+                const normal_prompt = prompt.replace(/[^-_.a-zA-Z]/g, "_");
+                var counter = 1;
+                var base;
+                var filename;
+                while (true) {
+                    if (counter > 1) {
+                        base = `${code}_${normal_prompt}_${counter}.png`
+                    } else {
+                        base = `${code}_${normal_prompt}.png`
+                    }
+                    filename = `${images_dir}/${base}`
+                    if (! fs.existsSync(filename)) {
+                        break;
+                    }
+                    counter = counter + 1;
+                }
+                fs.copyFile(target_file, filename, 0, (err) => {
+                    if (err) {
+                        console.log("Error copying " + target_file + " to " + filename + ": " + err);
+                    }
+                });
+                const image = {
+                    "code": code,
+                    "prompt": prompt,
+                    "filename": '/images/' + base
+                };
+                emit_image(image, target);
             } else {
-                promise = execFile(python_path, [generate_image_script, prompt])
-            }
-            const child = promise.child;
-            child.stdout.on('data', (data) => {
-                const images = JSON.parse(data);
-                images.forEach((image) => {
-                    image.id = state.images.length;
-                    image.censored = false;
-                    image.link = "";
-                    image.comments = [];
-                    io.emit('image', image);
-                    state.images.push(image);
+
+                // Inject the target seed for the "dice" prompt once every
+                // 6 requests for a random seed (and only if the word
+                // "dice" does not appear in the prompt).
+                if (!code && !prompt.toLowerCase().includes("dice")) {
+                    if (state.images.length % 6 == 0) {
+                        code = 319630254;
+                    }
+                }
+
+                if (code) {
+                    promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
+                } else {
+                    promise = execFile(python_path, [generate_image_script, prompt])
+                }
+                const child = promise.child;
+                child.stdout.on('data', (data) => {
+                    const images = JSON.parse(data);
+                    images.forEach((image) => {
+                        emit_image(image, null);
+                    });
+                });
+                child.stderr.on('data', (data) => {
+                    console.log("Error occurred during generate-image: " + data);
                 });
-            });
-            child.stderr.on('data', (data) => {
-                console.log("Error occurred during generate-image: " + data);
-            });
-            try {
-                const { stdout, stderr } = await promise;
-            } catch(e) {
-                console.error(e);
+                try {
+                    const { stdout, stderr } = await promise;
+                } catch(e) {
+                    console.error(e);
+                }
             }
             socket.emit('generation-done');
         }
+
         generate_image(request['code'], request['prompt']);
     });
 });