-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
510 lines (451 loc) · 14.9 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import { spawn } from "child_process";
import express from "express";
import multer from "multer";
import fs from "fs";
import path from "path";
import { WebSocketServer } from "ws";
import fetch from "node-fetch";
import FormData from "form-data";
import { fileURLToPath } from "url";
import { dirname } from "path";
import ffmpeg from "fluent-ffmpeg";
import { v4 as uuidv4 } from "uuid";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const env = process.env.NODE_ENV;
if (env === "production") {
dotenv.config({ path: ".env.production" });
} else {
dotenv.config({ path: ".env.development" });
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ffmpegPath = process.env.FFMPEG_PATH || "/usr/bin/ffmpeg"; // 使用环境变量或默认路径
ffmpeg.setFfmpegPath(ffmpegPath);
const app = express();
const port = process.env.NEXT_PUBLIC_WS_PORT || 3001; // 从环境变量中读取端口
const upload = multer({ dest: path.join(__dirname, "uploads/") }); // 修改路径
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
// 配置CORS
app.use(cors());
const server = app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
const wss = new WebSocketServer({ server, maxPayload: 40000 * 1024 * 1024 });
wss.on("connection", (ws) => {
console.log("Client connected");
let audioBuffer = [];
// 心跳机制
const interval = setInterval(() => {
if (ws.readyState === ws.OPEN) {
ws.send(JSON.stringify({ type: "ping" }));
}
}, 50000); // 每50秒发送一次心跳
ws.on("message", async (message) => {
const data = JSON.parse(message);
const model = data.model || "Systran/faster-whisper-large-v3";
const language = data.language || "zh";
const operation = data.operation || "transcription"; // 新增操作
const outputLanguage = data.outputLanguage || "fr"; // 新增输出语言
const responseFormat = data.response_format || "json";
const temperature = data.temperature || "0";
const isUpload = data.type === "upload";
if (data.type === "audio" || data.type === "upload") {
const buffer = Uint8Array.from(data.audio);
const audioType = data.fileType || "webm"; // 动态确定文件扩展名
const filePath = path.join(
__dirname,
`uploads/audio_${Date.now()}.${audioType}`
); // 动态确定文件扩展名
console.log("保存路径",filePath,"保存类型",audioType,"保存二进制",buffer)
try {
fs.writeFileSync(filePath, buffer);
}catch(e) {
console.log('保存失败',e)
}
console.log(`Audio file saved to ${filePath}`);
await handleAudioFile(
filePath,
ws,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage,
isUpload
);
} else if (data.type === "stop") {
audioBuffer = [];
console.log("Recording stopped, buffer cleared");
} else if (data.type === "pong") {
console.log("Received pong from client");
}
});
ws.on("close", () => {
clearInterval(interval); // 关闭连接时清除心跳定时器
console.log("Client disconnected");
});
});
async function handleAudioFile(
filePath,
ws,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage,
isUpload
) {
const wavFilePath = filePath.replace(path.extname(filePath), ".wav");
const outputDir = path.join(__dirname, "uploads", "chunks");
console.log("wavFilePath", wavFilePath);
console.log("outputDir", outputDir);
console.log(new Date().toISOString().replace("T", " ").replace("Z", ""));
await new Promise((resolve, reject) => {
ffmpeg(filePath)
.audioChannels(1)
.audioCodec("pcm_s16le")
.toFormat("wav")
.on("end", () => {
console.log(`Audio file successfully converted to WAV: ${wavFilePath}`);
resolve();
})
.on("error", (error) => {
console.error(`Error converting audio to WAV: ${error.message}`);
reject(error);
})
.save(wavFilePath);
});
console.log(new Date().toISOString().replace("T", " ").replace("Z", ""));
try {
if (isUpload) {
const pythonProcess = spawn("python", [
"split_audio_vad.py",
wavFilePath,
outputDir,
"30",
]);
const chunkQueue = [];
pythonProcess.stdout.on("data", (data) => {
const lines = data.toString().split("\n");
lines.forEach((line) => {
const chunkFilePath = line.trim();
if (chunkFilePath) {
chunkQueue.push(chunkFilePath);
}
});
});
pythonProcess.stderr.on("data", (data) => {
console.error(`Python process stderr: ${data}`);
ws.send(
JSON.stringify({
type: "error",
message: `Python process error: ${data}`,
})
);
});
pythonProcess.on("close", async (code) => {
console.log(`Python process exited with code: ${code}`);
if (code !== 0) {
ws.send(
JSON.stringify({
type: "error",
message: `Python process exited with code: ${code}`,
})
);
cleanupFiles([filePath, wavFilePath]);
return;
}
while (chunkQueue.length > 0) {
const chunkFilePath = chunkQueue.shift();
await transcribeOrTranslate(
chunkFilePath,
ws,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage
);
fs.unlinkSync(chunkFilePath);
console.log(`Deleted temporary file: ${chunkFilePath}`);
}
cleanupFiles([filePath, wavFilePath]);
console.log(`Deleted original file: ${filePath}`);
});
} else {
await transcribeOrTranslate(
wavFilePath,
ws,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage
);
cleanupFiles([filePath, wavFilePath]);
}
} catch (error) {
console.error("Error during conversion and sending:", error);
ws.send(
JSON.stringify({
type: "error",
message: "Error converting or sending audio",
})
);
cleanupFiles([filePath, wavFilePath]);
}
}
async function transcribeOrTranslate(
chunkFilePath,
ws,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage
) {
const messageId = uuidv4();
try {
const formData = new FormData();
formData.append("file", fs.createReadStream(chunkFilePath));
formData.append("model", model);
formData.append("language", language);
formData.append("response_format", responseFormat);
formData.append("temperature", temperature);
const transcriptionResponse = await fetch(
`${process.env.TRANSCRIPTION_API_BASE_URL}/v1/audio/transcriptions`,
{
method: "POST",
body: formData,
}
);
const transcriptionResponseText = await transcriptionResponse.text();
console.log("Transcription API response:", transcriptionResponseText);
const transcription = JSON.parse(transcriptionResponseText);
// 读取wav文件并编码为base64
const audioBuffer = fs.readFileSync(chunkFilePath);
const audioBase64 = audioBuffer.toString("base64");
console.log(new Date().toISOString().replace("T", " ").replace("Z", ""));
ws.send(
JSON.stringify({
type: "transcription",
text: transcription.text,
id: messageId,
audio: audioBase64,
})
);
if (operation === "translation" || operation === "conversation") {
const systemPrompt =
operation === "translation"
? "You are a professional, authentic machine translation engine."
: "You are a helpful assistant engaging in a conversation.";
const userPrompt =
operation === "translation"
? `Translate the following source text to ${outputLanguage}, Output translation directly without any additional text.\nSource Text: ${transcription.text}\nTranslated Text:`
: `Respond to the following input in ${outputLanguage} as if you were having a conversation. Keep your responses concise and brief. Output response directly without any additional text.\nInput: ${transcription.text}\nResponse:`;
const LLMResponse = await fetch(
`${process.env.LLM_API_BASE_URL}/v1/chat/completions`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.LLM_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: userPrompt,
},
],
}),
}
);
const LLMResponseText = await LLMResponse.text();
console.log("LLM API response:", LLMResponseText);
const LLM = JSON.parse(LLMResponseText);
ws.send(
JSON.stringify({
type: operation,
text: LLM.choices[0].message.content.trim(),
id: messageId,
audio: audioBase64,
})
);
}
} catch (error) {
console.error("Error during transcription or translation:", error);
ws.send(
JSON.stringify({ type: "error", message: "Error processing audio" })
);
}
}
function cleanupFiles(files) {
files.forEach((file) => {
if (fs.existsSync(file)) {
try {
fs.unlinkSync(file);
console.log(`Deleted temporary file: ${file}`);
} catch (error) {
console.error(`Error deleting temporary file: ${file}`, error);
}
}
});
}
const splitText = (text, maxLength) => {
const chunks = [];
let start = 0;
while (start < text.length) {
const end = Math.min(start + maxLength, text.length);
chunks.push(text.slice(start, end));
start = end;
}
return chunks;
};
const summarizeTextInChinese = async (text) => {
const systemPrompt = `
You are a professional summarizer.
Please summarize the following text in Chinese to ensure that it is clear, concise, and coherent. The specific requirements are as follows:
1. Extract the main points and essential information.
2. Maintain the original meaning and overall logic of the content.
3. Ensure the summary is natural and easy to read, without oversimplifying the original text.
Original text:
${text}
Summary in Chinese:
`;
const response = await fetch(
`${process.env.LLM_API_BASE_URL}/v1/chat/completions`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.LLM_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: systemPrompt,
},
],
}),
}
);
const responseText = await response.text();
return JSON.parse(responseText).choices[0].message.content.trim();
};
app.post("/optimize-text", async (req, res) => {
const { texts } = req.body;
if (!texts || !Array.isArray(texts)) {
return res
.status(400)
.json({ error: "Invalid input. 'texts' should be an array of strings." });
}
try {
const optimizedTexts = [];
const maxLength = 200;
for (const text of texts) {
const systemPrompt = `
You are a professional text optimizer.
Please clean and optimize the following speech recognition text to ensure that it is clear, coherent, and meaningful. The specific requirements are as follows:
1. Correct any recognition errors, such as changing "宗教" to "中交".
2. Remove meaningless parts, such as incomplete sentences, filler words (uh, um, etc.).
3. Retain all meaningful content and ensure sentence structure is clear.
4. Maintain the original coherence and overall logic of the content.
5. Ensure the optimized text is natural and easy to read, without oversimplifying the original text.
6. Preserve the truncation of the text, do not attempt to merge or complete it.
Original text:
${text}
Optimized text:
`;
const response = await fetch(
`${process.env.LLM_API_BASE_URL}/v1/chat/completions`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.LLM_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: text,
},
],
}),
}
);
const responseText = await response.text();
const optimizedText =
JSON.parse(responseText).choices[0].message.content.trim();
optimizedTexts.push(optimizedText);
}
const combinedText = optimizedTexts.join("\n");
const paragraphs = splitText(combinedText, maxLength);
const summaries = [];
for (const paragraph of paragraphs) {
summaries.push(await summarizeTextInChinese(paragraph));
}
const combinedSummary = summaries.join(" ");
if (combinedSummary.length > 5000) {
const finalSummary = await summarizeTextInChinese(combinedSummary);
return res.json({ combinedSummary, finalSummary });
} else {
const finalSummary = combinedSummary;
return res.json({ combinedText, combinedSummary, finalSummary });
}
} catch (error) {
console.error("Error optimizing and summarizing text:", error);
return res
.status(500)
.json({ error: "Error optimizing and summarizing text" });
}
});
app.post("/upload", upload.single("file"), async (req, res) => {
const file = req.file;
const model = req.body.model || "Systran/faster-whisper-large-v3";
const language = req.body.language || "zh";
const operation = req.body.operation || "transcription"; // 新增操作
const outputLanguage = req.body.outputLanguage || "fr"; // 新增输出语言
const responseFormat = req.body.response_format || "json";
const temperature = req.body.temperature || "0";
if (!file) {
return res.status(400).json({ error: "No file uploaded" });
}
const filePath = file.path;
const audioType = file.mimetype.split("/")[1]; // 获取文件类型
const newFilePath = path.join(__dirname, `uploads/audio_${Date.now()}.${audioType}`);
fs.renameSync(filePath, newFilePath); // 重命名文件
await handleAudioFile(
newFilePath,
null,
model,
language,
responseFormat,
temperature,
operation,
outputLanguage,
true
);
res.json({ message: "File uploaded and processed successfully" });
});