-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
179 lines (142 loc) · 5.31 KB
/
server.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
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const { exec } = require('child_process');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.use(express.static(__dirname));
app.use(express.json());
app.use('/images', express.static(path.join(__dirname, 'images')));
app.get('/get-images', async (req, res) => {
try {
const prompts = await fs.readdir('./images');
let prompt = prompts[Math.floor(Math.random() * prompts.length)];
const samplers = await fs.readdir(`./images/${prompt}`);
if (samplers.length < 2) {
return res.status(500).json({ error: 'Not enough samplers available' });
}
let selectedSampler1 = samplers[Math.floor(Math.random() * samplers.length)];
let selectedSampler2;
do {
selectedSampler2 = samplers[Math.floor(Math.random() * samplers.length)];
} while (selectedSampler1 === selectedSampler2);
const imagecounts = await fs.readdir(`./images/${prompt}/${selectedSampler1}`);
let imageNumber = 10 + 5 * Math.floor(Math.random() * imagecounts.length);
const imagePath1 = `/images/${prompt}/${selectedSampler1}/image${imageNumber}.png`;
const imagePath2 = `/images/${prompt}/${selectedSampler2}/image${imageNumber}.png`;
res.json({ image1: imagePath1, image2: imagePath2 });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch images' });
}
});
const analyzeResults = async () => {
const resultsPath = path.join(__dirname, 'data', 'results.json');
if (!resultsPath) {
await fs.writeFile(resultsPath);
}
const rawData = await fs.readFile(resultsPath, 'utf-8');
const data = JSON.parse(rawData);
let sampler_scores = {};
let sampler_steps = {};
let sampler_prompt_num = {};
for (let entry of data) {
for (let x of [1, 2]) {
let y = x.toString();
let sampler = entry['sampler' + y];
let score = entry['score' + y];
let steps = entry['steps'];
let promptnum = entry['prompt'];
if (!sampler_scores[sampler]) {
sampler_scores[sampler] = 0;
sampler_steps[sampler] = {steps};
sampler_prompt_num[sampler] = {promptnum};
}
if (sampler_scores[sampler] + score <= 0) {
sampler_scores[sampler] = 0;
}
else {
sampler_scores[sampler] += score;
}
sampler_steps[sampler][steps] += 1;
sampler_prompt_num[sampler][promptnum] = (sampler_prompt_num[sampler][promptnum] || 0) + 1;
}
}
return {
sampler_scores: sampler_scores,
sampler_steps: sampler_steps,
sampler_prompt_num: sampler_prompt_num
};
};
app.get('/get-graph-data', async (req, res) => {
try {
const data = await analyzeResults();
res.json(data);
} catch (error) {
res.status(500).json({ error: 'Failed to get graph data' });
}
});
function extractData(imagePath) {
// Split the path into its components
const parts = imagePath.split('/');
// Extract the sampler, prompt, and steps
const sampler = parts[5];
const prompt = parts[4];
const steps = (parseInt(parts[6].replace('image', '').replace('.png', ''), 10));
return { sampler, prompt, steps };
}
app.post('/record-choice', async (req, res) => {
try {
const { choice, image1Path: image1, image2Path: image2 } = req.body;
console.log("Request body:", req.body); // Log the request body
if (!image1 || !image2) {
return res.status(400).json({ error: 'Image paths are missing' });
}
const resultsPath = path.join(__dirname, 'data', 'results.json');
let results;
try {
const rawData = await fs.readFile(resultsPath, 'utf-8');
results = JSON.parse(rawData);
} catch (error) {
console.log("Error reading results file:", error.message); // Log the error message
results = [];
// If the file doesn't exist, create it with an empty array
if (error.code === 'ENOENT') {
console.log("Creating a new results file.");
await fs.writeFile(resultsPath, JSON.stringify(results, null, 2));
}
}
const data1 = extractData(image1);
const data2 = extractData(image2);
let score1, score2;
if (choice === 'image1') {
score1 = 1;
score2 = -1;
} else if (choice === 'image2') {
score1 = -1;
score2 = 1;
} else if (choice === 'identical') {
score1 = score2 = 1;
} else {
score1 = score2 = -1;
}
results.push({
sampler1: data1.sampler,
score1: score1,
sampler2: data2.sampler,
score2: score2,
prompt: data1.prompt,
steps: data1.steps
});
await fs.writeFile(resultsPath, JSON.stringify(results, null, 2));
res.json({ success: true });
} catch (error) {
console.error("Error recording choice:", error); // Log the entire error object
res.status(500).json({ error: 'Failed to record choice' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});