-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (171 loc) · 6.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const express = require('express');
const { createServer } = require('http');
const { join } = require('path');
const { Server } = require('socket.io');
const { exec } = require('child_process');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const axios = require('axios');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 5500;
const server = createServer(app);
const io = new Server(server);
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(session({ secret: 'secret', resave: false, saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
const users = [
{ id: 1, username: 'user1', password: 'pass1' },
{ id: 2, username: 'user2', password: 'pass2' }
];
passport.use(new LocalStrategy(
(username, password, done) => {
const user = users.find(u => u.username === username && u.password === password);
if (user) {
return done(null, user);
} else {
return done(null, false, { message: 'Incorrect credentials.' });
}
}
));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
const user = users.find(u => u.id === id);
done(null, user);
});
app.post('/login', passport.authenticate('local'), (req, res) => {
res.send('Logged in');
console.log("logged in");
});
let prompts = [];
let imageUrls = [];
let replicateApiKey = '';
let openaiApiKey = '';
io.on('connection', (socket) => {
console.log('A user connected');
// Listen for the submission of API keys
socket.on('submit api keys', (data) => {
console.log('API Keys received:', data);
replicateApiKey = data.replicateApiKey;
openaiApiKey = data.openaiApiKey;
});
socket.on('submit title', (data) => {
const command = `python3 generate_title.py "${data}" "${replicateApiKey}"`;
exec(command, (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error executing Python script:', error, stderr);
io.emit('error', 'Failed to generate title');
return;
}
io.emit('new title', stdout.trim());
console.log("Title generated by AI:", stdout.trim());
});
});
socket.on('submit bio', (data) => {
const { name, bio } = data;
const command = `python3 generate_bio.py "${name}" "${bio}" "${replicateApiKey}"`;
exec(command, (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error executing Python script:', error, stderr);
io.emit('error', 'Failed to generate bio');
return;
}
io.emit('new bio', stdout.trim());
console.log("Bio:", stdout.trim());
});
});
socket.on('submit header', (data) => {
io.emit('new header', data);
});
socket.on('submit chat', (data) => {
io.emit('new chat', data);
});
socket.on('submit label', (data) => {
const command = `python3 generate_label.py "${data}" "${replicateApiKey}"`;
exec(command, (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error executing Python script:', error, stderr);
io.emit('error', 'Failed to generate label');
return;
}
io.emit('new label', stdout.trim());
console.log("Label:", stdout.trim());
});
});
socket.on('submit concept', (prompt) => {
prompts.push(prompt);
io.emit('submit concept', prompt);
const { theme, imagery } = prompt;
const command_idea = `python3 generate_conceptualart.py "${theme}" "${imagery}" "${replicateApiKey}" "${openaiApiKey}"`;
const command_idea2 = `python3 generate_conceptualart2.py "${theme}" "${imagery}" "${replicateApiKey}" "${openaiApiKey}"`;
exec(command_idea, (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error executing Python script:', error, stderr);
io.emit('error', 'Failed to generate image');
return;
}
try {
const outputParts = stdout.split('\n');
const url = outputParts[0];
const titleLine = outputParts.find(line => line.startsWith('Title:'));
const title = titleLine ? titleLine.split('Title:')[1].trim().replace(/^"|"$/g, '') : 'No title provided';
io.emit("new label and article title", title);
const descriptionStartIndex = outputParts.findIndex(line => line.startsWith('Description:')) + 1;
let description = outputParts.slice(descriptionStartIndex).join('\n').trim();
// Use a regular expression to remove text between [ and ]
description = description.replace(/\[.*?\]/g, '').trim();
console.log("Description:", description);
io.emit('new description', description);
const matches = url.match(/https:\/\/[^"]+/);
if (matches && matches[0]) {
imageUrls.push(matches[0]);
io.emit('new image', matches[0]);
} else {
io.emit('error', 'No image URL found');
console.error('No URL found in Python script output:', stdout);
}
} catch (err) {
console.error('Error processing output:', err);
io.emit('error', 'Error processing image data');
}
});
exec(command_idea2, (error, stdout, stderr) => {
if (error || stderr) {
console.error('Error executing Python script:', error, stderr);
io.emit('error', 'Failed to generate image');
return;
}
try {
const outputParts2 = stdout.split('\n');
const url2 = outputParts2[0];
const titleLine2 = outputParts2.find(line => line.startsWith('Title:'));
const title2 = titleLine2 ? titleLine2.split('Title:')[1].trim().replace(/^"|"$/g, '') : 'No title provided';
const descriptionStartIndex2 = outputParts2.findIndex(line => line.startsWith('Description:')) + 1;
const description2 = outputParts2.slice(descriptionStartIndex2).join('\n').trim();
console.log("Title2:", title2);
const matches2 = url2.match(/https:\/\/[^"]+/);
if (matches2 && matches2[0]) {
io.emit('new image 2', matches2[0]);
} else {
console.error('No URL found in Python script output:', stdout);
io.emit('error', 'No image URL found');
}
} catch (err) {
console.error('Error processing output:', err);
io.emit('error', 'Error processing image data');
}
});
});
});
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'public/home.html'));
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});