Transcripts: check the whisper model is installed, download it (trying each form the service has used), and say what came back if it will not
build-container / build (push) Failing after 6m18s
build-container / build (push) Failing after 6m18s
This commit is contained in:
1 parent
05e6b19bfa
commit
c82bd7cae9
3 files changed
+18
-5
No files matched your search
+11
-4
@@ -69,11 +69,18 @@ const jobs = new Map(); // key -> { state: 'running' | 'error', since, error }
|
||||
let chain = Promise.resolve(); // one at a time: it is heavy
|
||||
const fileOf = key => path.join(cfg.dir, key + '.json');
|
||||
const keyFor = (kind, id, src) => crypto.createHash('sha1').update(kind + '|' + id + '|' + src).digest('hex');
|
||||
let modelReady = null; // the service must have the model on disk before it can use it: ask for it once (a no-op when it is already there)
|
||||
const ensureModel = () => modelReady || (modelReady = fetch(cfg.api + '/v1/models/' + cfg.model, { method: 'POST', signal: AbortSignal.timeout(30 * 60e3) })
|
||||
.then(r => { if (!r.ok && r.status !== 409) throw new Error('status ' + r.status); }).catch(e => { modelReady = null; if (/^status 4\d\d/.test(e.message)) return; throw e; }));
|
||||
let modelReady = null; // the service must have the model on disk before it can use it: check, and ask for it once if it is missing
|
||||
async function ensureModel() {
|
||||
const id = cfg.model, enc = id.split('/').map(encodeURIComponent).join('/'), seen = [];
|
||||
const call = async (method, url, body) => { const r = await fetch(cfg.api + url, { method, headers: body ? { 'content-type': 'application/json' } : {}, body, signal: AbortSignal.timeout(30 * 60e3) }); if (r.ok || r.status === 409) return true; seen.push(`${method} ${url}: ${r.status} ${(await r.text()).slice(0, 120)}`); return false; };
|
||||
if ((await fetch(cfg.api + '/v1/models/' + enc, { signal: AbortSignal.timeout(20000) })).ok) return; // already installed
|
||||
// (the download call has differed between versions of the service, so the known forms are tried in turn)
|
||||
if (await call('POST', '/v1/models/' + enc) || await call('POST', '/v1/models', JSON.stringify({ id })) || await call('POST', '/v1/models?model_name=' + encodeURIComponent(id))) return;
|
||||
throw new Error('The transcription service would not download the model ' + id + ' (' + seen.join('; ') + ')');
|
||||
}
|
||||
const modelOnce = () => modelReady || (modelReady = ensureModel().catch(e => { modelReady = null; throw e; }));
|
||||
async function whisper(file, name) {
|
||||
try { await ensureModel(); } catch (e) { throw new Error(`Could not reach the transcription service at ${cfg.api} (${why(e)}). Is the whisper container running? Check its log.`); }
|
||||
try { await modelOnce(); } catch (e) { throw /would not download/.test(e.message) ? e : new Error(`Could not reach the transcription service at ${cfg.api} (${why(e)}). Is the whisper container running? Check its log.`); }
|
||||
const form = new FormData();
|
||||
form.append('file', await fs.openAsBlob(file), name || 'audio');
|
||||
form.append('model', cfg.model); form.append('response_format', 'verbose_json'); form.append('timestamp_granularities[]', 'segment');
|
||||
|
||||
Reference in new issue
Block a user