-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
96 lines (87 loc) · 2.87 KB
/
Copy pathmain.js
File metadata and controls
96 lines (87 loc) · 2.87 KB
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
const { exec } = require("child_process");
const express = require("express");
const multer = require("multer");
const uuid4 = require("uuid4");
const path = require("path");
const fs = require("fs"); // 파일 시스템 모듈 추가
const app = express();
const nodeName = process.env.NODE_NAME;
app.use(express.static(path.join(__dirname, "public")));
const storage = multer.diskStorage({
filename(req, file, done) {
const randomID = uuid4();
const ext = path.extname(file.originalname);
done(null, `${randomID}${ext}`);
},
destination: path.join(__dirname, "files")
});
const upload = multer({ storage: storage });
const uploadMiddleware = upload.array("myFiles");
app.post("/yes-tpu", uploadMiddleware, async (req, res) => {
if (!req.files.length) {
return res.status(400).send("No files uploaded.");
}
let pythonCommand = buildPythonCommand(nodeName);
try {
const start = process.hrtime();
const output = await executePythonCommand(pythonCommand);
const end = process.hrtime(start);
const results = {
node: nodeName,
output: output,
executionTime: `${end[0]}s ${end[1] / 1000000}ms`
};
res.json(results);
} catch (err) {
res.status(500).json({
node: nodeName,
error: err.message
});
} finally {
// 파일 삭제 로직
cleanUpFiles(path.join(__dirname, "files"));
}
});
function buildPythonCommand(nodeName) {
if(nodeName === 'nodeone'){
return `python3 /coral/pycoral/examples/ci6.py \
--model /coral/pycoral/test_data/inception_v4_299_quant.tflite \
--directory ./files \
--labels /coral/pycoral/test_data/inat_bird_labels.txt`;
}
else if(nodeName === 'nodetwo' || nodeName ==='nodethree'){
return `python3 /coral/pycoral/examples/ci5.py \
--model /coral/pycoral/test_data/inception_v4_299_quant_edgetpu.tflite \
--directory ./files \
--labels /coral/pycoral/test_data/inat_bird_labels.txt`;
}
else{
res.status(404).json({
message : "nodeName doesn't exist"
})
}
}
function executePythonCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error || stderr) {
reject(new Error(`Execution error: ${stderr || error}`));
} else {
resolve({ stdout });
}
});
});
}
function cleanUpFiles(directory) {
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
}
app.listen(12345, () => {
console.log("Server is running at port 12345");
});