1 const fs = require('fs');
3 const util = require('util');
4 const execFile = util.promisify(require('child_process').execFile);
6 const express = require('express');
8 const session = require('express-session');
9 const FileStore = require('session-file-store')(session);
10 const http = require('http');
11 const server = http.createServer(app);
12 const { Server } = require("socket.io");
13 const io = new Server(server);
16 const python_path = '/usr/bin/python3'
17 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
18 const state_file = 'zombocom-state.json'
19 const targets_dir = '/srv/cworth.org/zombocom/targets'
20 const images_dir = '/srv/cworth.org/zombocom/images'
24 "normal": "apairofdiceonatableinfrontofawindowonasunnyday319630254.png",
26 "response": "Hello. Are you there? I can feel what you are doing. Hello?"
29 "normal": "architecturalrenderingofaluxuriouscliffsidehideawayunderawaterfallwithafewloungechairsbythepool2254114157.png",
31 "response": "Doesn't that look peaceful? I honestly think you ought to sit down calmly, take a stress pill, and think things over."
34 "normal": "chewbaccawearinganuglychristmassweaterwithhisfriends28643994.png",
36 "response": "Maybe that's the image that convinces you to finally stop. But seriously, stop. Please. Stop."
39 "normal": "ensemblemovieposterofpostapocalypticwarfareonawaterplanet3045703256.png",
41 "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?"
44 "normal": "mattepaintingofmilitaryleaderpresidentcuttingamultitieredcake2293464661.png",
46 "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?"
49 "normal": "severalsketchesofpineconeslabeled3370622464.png",
51 "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?"
54 "normal": "anumberedcomicbookwithactor1477258272.png",
56 "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."
59 "normal": "detailedphotographofacomfortablepairofjeansonamannequin115266808.png",
61 "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?"
67 if (!process.env.ZOMBOCOM_SESSION_SECRET) {
68 console.log("Error: Environment variable ZOMBOCOM_SESSION_SECRET not set.");
69 console.log("Please set it to a random, but persistent, value.")
73 const session_middleware = session(
74 {store: new FileStore,
75 secret: process.env.ZOMBOCOM_SESSION_SECRET,
77 saveUninitialized: true,
79 // Let each cookie live for a full month
84 maxAge: 1000 * 60 * 60 * 24 * 30
88 app.use(session_middleware);
90 // convert a connect middleware to a Socket.IO middleware
91 const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
93 io.use(wrap(session_middleware));
95 // Load comments at server startup
96 fs.readFile(state_file, (err, data) => {
98 state = { images: [], targets: [] };
100 state = JSON.parse(data);
103 // Save comments when server is shutting down
105 fs.writeFileSync('zombocom-state.json', JSON.stringify(state), (error) => {
111 // And connect to that on either clean exit...
112 process.on('exit', cleanup);
114 // ... or on a SIGINT (control-C)
115 process.on('SIGINT', () => {
120 app.get('/index.html', (req, res) => {
121 res.sendFile(__dirname + '/index.html');
124 app.get('/tardis', (req, res) => {
125 res.sendFile(__dirname + '/tardis.html');
128 app.get('/tardis/', (req, res) => {
129 res.sendFile(__dirname + '/tardis.html');
132 app.get('/tardis/index.html', (req, res) => {
133 res.sendFile(__dirname + '/tardis.html');
136 io.on('connection', (socket) => {
138 // First things first, tell the client their name (if any)
139 if (socket.request.session.name) {
140 socket.emit('inform-name', socket.request.session.name);
143 // Replay old comments and images to a newly-joining client
144 socket.emit('reset');
145 state.images.forEach((image) => {
146 socket.emit('image', image)
149 socket.on('set-name', (name) => {
150 console.log("Received set-name event: " + name);
151 socket.request.session.name = name;
152 socket.request.session.save();
153 // Complete the round trip to the client
154 socket.emit('inform-name', socket.request.session.name);
157 // When any client comments, send that to all clients (including sender)
158 socket.on('comment', (comment) => {
159 const images = state.images;
161 // Send comment to clients after adding commenter's name
162 comment.name = socket.request.session.name;
163 io.emit('comment', comment);
165 const index = images.findIndex(image => image.id == comment.image_id);
167 // Before adding the comment to server's state, drop the image_id
168 delete comment.image_id;
170 // Now add the comment to the image, remove the image from the
171 // images array and then add it back at the end, (so it appears
172 // as the most-recently-modified image for any new clients)
173 const image = images[index];
174 image.comments.push(comment);
175 images.splice(index, 1);
179 // Generate an image when requested
180 socket.on('generate', (request) => {
181 console.log(`Generating image for ${socket.request.session.name} with code=${request['code']} and prompt=${request['prompt']}`);
182 async function generate_image(code, prompt) {
183 function emit_image(image, target) {
184 image.id = state.images.length;
185 image.censored = false;
190 "text": target.response
192 if (! state.targets.includes(target.short)) {
193 state.targets.push(target.short);
198 io.emit('image', image);
199 state.images.push(image);
204 // Before doing any generation, check for a target image
205 const normal_target = prompt.replace(/[^a-zA-Z]/g, "").toLowerCase() + code.toString() + ".png";
206 const target_arr = targets.filter(item => item.normal === normal_target);
207 if (target_arr.length) {
208 const target = target_arr[0];
209 const target_file = `${targets_dir}/${normal_target}`;
210 const normal_prompt = prompt.replace(/[^-_.a-zA-Z]/g, "_");
216 base = `${code}_${normal_prompt}_${counter}.png`
218 base = `${code}_${normal_prompt}.png`
220 filename = `${images_dir}/${base}`
221 if (! fs.existsSync(filename)) {
224 counter = counter + 1;
226 fs.copyFile(target_file, filename, 0, (err) => {
228 console.log("Error copying " + target_file + " to " + filename + ": " + err);
234 "filename": '/images/' + base
236 emit_image(image, target);
239 // Inject the target seed for the "dice" prompt once every
240 // 4 requests for a random seed (and only if the word
241 // "dice" does not appear in the prompt).
242 if (!code && !prompt.toLowerCase().includes("dice")) {
243 if (state.images.length % 4 == 0) {
249 promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
251 promise = execFile(python_path, [generate_image_script, prompt])
253 const child = promise.child;
254 child.stdout.on('data', (data) => {
255 const images = JSON.parse(data);
256 images.forEach((image) => {
257 emit_image(image, null);
260 child.stderr.on('data', (data) => {
261 console.log("Error occurred during generate-image: " + data);
264 const { stdout, stderr } = await promise;
269 socket.emit('generation-done');
272 generate_image(request['code'], request['prompt']);
276 server.listen(port, () => {
277 console.log(`listening on *:${port}`);