-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (83 loc) · 2.5 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
import process from "node:process";
import path from "node:path";
import express from "express";
import { execa } from "execa";
const app = express();
import { config } from "dotenv";
import { condaPython } from "./utils.js";
config();
const portArgIndex = process.argv.indexOf('--port');
let currentPort = 3000;
if (portArgIndex !== -1 && process.argv.length > portArgIndex + 1) {
const parsedPort = parseInt(process.argv[portArgIndex + 1], 10);
if (!isNaN(parsedPort)) {
currentPort = parsedPort;
}
}
app.use(express.json());
// Host local files
app.use("/uploads", express.static("results"));
// Host video player page
app.get("/files/:fileName", async (request, response) => {
console.log(request.params);
response.send(`<video controls src="/uploads/${request.params.fileName}"/>`);
});
/**
* @typedef GenerateObject
* @property {string} browser
* @property {string} download
* @property {string} fileName
* @property {string} filePath
*/
/**
*
* @param {string} audio
* @param {string} image
* @param {number} [batchSize=2]
* @returns {Promise<GenerateObject>}
*/
async function generate({ audio, image, batchSize = 2 }) {
const pythonScript = "inference.py";
const args = [
"--driven_audio",
audio,
"--source_image",
image,
"--batch_size",
batchSize
].join(' ');
const command = condaPython('expresssadtalker', `${pythonScript} ${args}`);
try {
const result = await execa(command, {shell: true});
console.log(result.stdout);
console.log(result.stderr);
const parts = result.stdout.split("\n");
const relativePath = parts[3]
.replace("The generated video is named:", "")
.trim();
const filePath = path.join(process.cwd(), relativePath);
const fileName = relativePath.replace("./results\\", "");
return {
filePath,
fileName,
download: `http://127.0.0.1:${currentPort}/uploads/${fileName}`,
browser: `http://127.0.0.1:${currentPort}/files/${fileName}`
};
} catch (error) {
console.log(error);
throw error;
}
}
app.post("/generate", async (request, response) => {
const { audio, image, batchSize } = request.body;
console.log(request.body);
try {
const result = await generate({ audio, image, batchSize });
response.status(201).json(result);
} catch (error) {
response.status(500).json({ message: error.message });
}
});
app.listen(currentPort, async () => {
console.log(`Server is running on port ${currentPort}`);
});