extract shared whisper transcribe helper, use in file-browser and pi stt routes
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,8 @@ import { readSttConfig } from '../server-settings/stt';
|
||||
import { listPiModels } from './list-models';
|
||||
import { getHomeDirForRole } from '../../data-path';
|
||||
import { logger } from './logger';
|
||||
import { transcribeAudio } from '../stt/transcribe';
|
||||
import { getUserSettings } from 'officerdb';
|
||||
|
||||
export const piRestRouter = createRouter();
|
||||
|
||||
@@ -72,30 +74,21 @@ piRestRouter.post('/pi/stt', async (ctx: Context) => {
|
||||
return ctx.json({ error: 'Whisper not configured — set it up in Settings → Speech to Text' }, 400);
|
||||
}
|
||||
|
||||
const user = ctx.get('user');
|
||||
const body = await ctx.req.parseBody();
|
||||
const file = body['file'];
|
||||
if (!file || !(file instanceof File)) {
|
||||
return ctx.json({ error: 'file is required' }, 400);
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file, 'recording.wav');
|
||||
formData.append('temperature', String(body['temperature'] ?? '0.0'));
|
||||
formData.append('temperature_inc', String(body['temperature_inc'] ?? '0.2'));
|
||||
formData.append('response_format', String(body['response_format'] ?? 'json'));
|
||||
const settings = (await getUserSettings(user.id)) as { languages?: { spoken?: string[] } };
|
||||
const spokenLanguages = settings.languages?.spoken ?? [];
|
||||
|
||||
try {
|
||||
const res = await fetch(`${sttConfig.url.replace(/\/+$/, '')}/inference`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
if (!res.ok) {
|
||||
return ctx.json({ error: `Whisper returned ${res.status}` }, 502);
|
||||
}
|
||||
const json = await res.json();
|
||||
return ctx.json(json);
|
||||
const result = await transcribeAudio({ file, whisperUrl: sttConfig.url, spokenLanguages });
|
||||
return ctx.json(result);
|
||||
} catch (err) {
|
||||
logger.error('STT proxy failed', { error: String(err) });
|
||||
return ctx.json({ error: 'Failed to reach Whisper server' }, 502);
|
||||
return ctx.json({ error: err instanceof Error ? err.message : 'Failed to reach Whisper server' }, 502);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user