bare minimum: Express, 2 роута, 0 лишнего
This commit is contained in:
@@ -1,81 +1,8 @@
|
||||
/**
|
||||
* Say — минимальная версия (только Whisper).
|
||||
* Работает: загрузка аудио → транскрибация → текст.
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const multer = require('multer');
|
||||
const fetch = require('node-fetch');
|
||||
const FormData = require('form-data');
|
||||
const path = require('path');
|
||||
|
||||
const PORT = parseInt(process.env.PORT, 10) || 3000;
|
||||
|
||||
// --- Конфигурация Whisper ---
|
||||
const WHISPER_URL = process.env.WHISPER_URL || 'https://api.aillm.ru/v1/audio/transcriptions';
|
||||
const WHISPER_MODEL = process.env.WHISPER_MODEL || 'whisper-large-v3';
|
||||
const API_KEY = process.env.API_KEY || '';
|
||||
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
const app = express();
|
||||
|
||||
// --- Статика ---
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.get('/', (_req, res) => res.send('Say OK'));
|
||||
app.get('/health', (_req, res) => res.json({ status: 'ok' }));
|
||||
|
||||
// --- Роуты ---
|
||||
|
||||
// Главная
|
||||
app.get('/', (_req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
||||
});
|
||||
|
||||
// Health
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
// Whisper: аудио → текст
|
||||
app.post('/api/speech', upload.single('audio'), async (req, res) => {
|
||||
try {
|
||||
if (!req.file || !req.file.buffer || req.file.size === 0) {
|
||||
return res.status(400).json({ error: 'Нет аудио' });
|
||||
}
|
||||
|
||||
const form = new FormData();
|
||||
form.append('file', req.file.buffer, {
|
||||
filename: 'audio.webm',
|
||||
contentType: req.file.mimetype || 'audio/webm',
|
||||
});
|
||||
form.append('model', WHISPER_MODEL);
|
||||
form.append('language', 'ru');
|
||||
|
||||
console.log(`Whisper: отправка ${req.file.size} байт`);
|
||||
|
||||
const resp = await fetch(WHISPER_URL, {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${API_KEY}` },
|
||||
body: form,
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
const errText = await resp.text();
|
||||
console.error(`Whisper error ${resp.status}: ${errText}`);
|
||||
return res.status(500).json({ error: `Whisper: ${resp.status}` });
|
||||
}
|
||||
|
||||
const data = await resp.json();
|
||||
const text = (data.text || '').trim();
|
||||
|
||||
console.log(`Whisper: распознано "${text}"`);
|
||||
res.json({ transcription: text });
|
||||
|
||||
} catch (err) {
|
||||
console.error('ERROR:', err.message);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// --- Запуск ---
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Say (Whisper only) listening on :${PORT}`);
|
||||
});
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => console.log(`:${PORT}`));
|
||||
|
||||
Reference in New Issue
Block a user