]> git.cworth.org Git - zombocom-ai/blob - index.js
Add exception handling to execution of image generation script
[zombocom-ai] / index.js
1 const fs = require('fs');
2
3 const util = require('util');
4 const execFile = util.promisify(require('child_process').execFile);
5
6 const express = require('express');
7 const app = express();
8 const http = require('http');
9 const server = http.createServer(app);
10 const { Server } = require("socket.io");
11 const io = new Server(server);
12 const port = 2122;
13
14 const python_path = '/usr/bin/python3'
15 const generate_image_script = '/home/cworth/src/zombocom-ai/generate-image.py'
16 const state_file = 'zombocom-state.json'
17
18 var state;
19
20 // Load comments at server startup
21 fs.readFile(state_file, (err, data) => {
22     if (err)
23         state = { images: [], comments: [] };
24     else
25         state = JSON.parse(data);
26 });
27
28 // Save comments when server is shutting down
29 function cleanup() {
30     fs.writeFileSync('zombocom-state.json', JSON.stringify(state), (error) => {
31         if (error)
32             throw error;
33     })
34 }
35
36 // And connect to that on either clean exit...
37 process.on('exit', cleanup);
38
39 // ... or on a SIGINT (control-C)
40 process.on('SIGINT', () => {
41     cleanup();
42     process.exit();
43 });
44
45 app.get('/index.html', (req, res) => {
46     res.sendFile(__dirname + '/index.html');
47 });
48
49 io.on('connection', (socket) => {
50
51     // Replay old comments and images to a newly-joining client
52     state.comments.forEach((comment) => {
53         socket.emit('comment', comment)
54     });
55     state.images.forEach((image) => {
56         socket.emit('image', image)
57     });
58
59     // When any client comments, send that to all clients (including sender)
60     socket.on('comment', (comment) => {
61         io.emit('comment', comment);
62         state.comments.push(comment);
63     });
64
65     // Generate an image when requested
66     socket.on('generate', (request) => {
67         console.log(`Generating image with code=${request['code']} and prompt=${request['prompt']}`);
68         async function generate_image(code, prompt) {
69             var promise;
70             if (code) {
71                 promise = execFile(python_path, [generate_image_script, `--seed=${code}`, prompt])
72             } else {
73                 promise = execFile(python_path, [generate_image_script, prompt])
74             }
75             const child = promise.child;
76             child.stdout.on('data', (data) => {
77                 const images = JSON.parse(data);
78                 images.forEach((image) => {
79                     console.log(`Emitting image to clients: ${image}`);
80                     io.emit('image', image);
81                     state.images.push(image);
82                 });
83             });
84             child.stderr.on('data', (data) => {
85                 console.log("Error occurred during generate-image: " + data);
86             });
87             try {
88                 const { stdout, stderr } = await promise;
89             } catch(e) {
90                 console.error(e);
91             }
92             socket.emit('generation-done');
93         }
94         generate_image(request['code'], request['prompt']);
95     });
96 });
97
98 server.listen(port, () => {
99     console.log(`listening on *:${port}`);
100 });