Sort pages by their number, drop repeated photos, mark uncertain words
The model now wraps words it isn't sure of in {word} or {word|other}; the
reader shows the first reading. Pages are ordered by the number on each
photo and a page photographed twice is kept once (fewer uncertain words,
then more text). meta.photos records which photo goes with which page.
New POST /images/:n/transcribe re-reads a single photo for the editor.
Seeded plays get an img/ folder on their first photo.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
parent
3f2cf95f1d
commit
ab842a1d73
8 changed files with 187 additions and 43 deletions
|
|
@ -7,7 +7,7 @@ import { createHash, timingSafeEqual } from "node:crypto";
|
|||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createStore, NotFound } from "./store.mjs";
|
||||
import { splitPage, assemble } from "./transcribe.mjs";
|
||||
import { splitPage, assemble, normalizeSpeakers } from "./transcribe.mjs";
|
||||
import { makeProvider } from "./providers.mjs";
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
|
@ -56,8 +56,8 @@ async function readBody(req, limit) {
|
|||
}
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
async function readJson(req) {
|
||||
try { return JSON.parse((await readBody(req, 64 * 1024)).toString("utf8") || "{}"); }
|
||||
async function readJson(req, limit = 64 * 1024) {
|
||||
try { return JSON.parse((await readBody(req, limit)).toString("utf8") || "{}"); }
|
||||
catch (err) { if (err instanceof HttpError) throw err; throw new HttpError(400, "Neplatný JSON."); }
|
||||
}
|
||||
|
||||
|
|
@ -122,8 +122,9 @@ export async function runJob(store, id, provider) {
|
|||
await Promise.all(Array.from({ length: config.concurrency }, worker));
|
||||
|
||||
const failed = results.filter((r) => r.error).length;
|
||||
await store.setText(id, assemble(results));
|
||||
await store.updateMeta(id, () => ({ status: "draft", progress: null, failed, model: `${provider.name}:${provider.model}` }));
|
||||
const { text, photos, duplicates } = assemble(results);
|
||||
await store.setText(id, text);
|
||||
await store.updateMeta(id, () => ({ status: "draft", progress: null, failed, photos, duplicates, model: `${provider.name}:${provider.model}` }));
|
||||
} catch (err) {
|
||||
console.error(`[${id}] job failed:`, err);
|
||||
await store.updateMeta(id, () => ({ status: "draft", progress: null, jobError: String(err?.message || err) })).catch(() => {});
|
||||
|
|
@ -133,11 +134,16 @@ export async function runJob(store, id, provider) {
|
|||
}
|
||||
|
||||
// ---- API
|
||||
const publicMeta = ({ pages, ...m }) => ({ ...m, failedPages: (pages || []).flatMap((p, i) => (p?.error ? [{ image: i + 1, error: p.error }] : [])) });
|
||||
const publicMeta = ({ pages, ...m }) => ({
|
||||
...m,
|
||||
photos: m.photos || null,
|
||||
duplicates: m.duplicates || [],
|
||||
failedPages: (pages || []).flatMap((p, i) => (p?.error ? [{ image: i + 1, error: p.error }] : [])),
|
||||
});
|
||||
|
||||
async function api(req, res, store, provider, parts) {
|
||||
const method = req.method;
|
||||
const [, , id, sub, n] = parts; // ["", "plays", id, sub, n]
|
||||
const [, , id, sub, n] = parts; // ["", "plays", id, sub, n, action]
|
||||
|
||||
if (parts[1] === "config" && method === "GET")
|
||||
return send(res, 200, { uploads: Boolean(config.password), transcription: Boolean(provider), model: provider?.model || null, maxPages: config.maxPages });
|
||||
|
|
@ -170,11 +176,24 @@ async function api(req, res, store, provider, parts) {
|
|||
if (method === "GET") return send(res, 200, publicMeta(meta));
|
||||
if (method === "PATCH") {
|
||||
await requireAuth(req);
|
||||
const body = await readJson(req);
|
||||
const body = await readJson(req, config.maxTextBytes);
|
||||
const patch = {};
|
||||
if (typeof body.title === "string" && body.title.trim()) patch.title = body.title.trim().slice(0, 200);
|
||||
if (typeof body.note === "string") patch.note = body.note.trim().slice(0, 500);
|
||||
if (typeof body.published === "boolean") patch.published = body.published;
|
||||
// which photo belongs to which page marker of the text (the editor keeps this in step)
|
||||
if (Array.isArray(body.photos)) {
|
||||
if (body.photos.length > 2000 || !body.photos.every((p) => p === null || (Number.isInteger(p) && p >= 1 && p <= meta.images)))
|
||||
throw new HttpError(400, "Neplatné přiřazení fotek.");
|
||||
patch.photos = body.photos;
|
||||
}
|
||||
// left-out repeat photos; the editor swaps them with the page's chosen photo
|
||||
if (Array.isArray(body.duplicates)) {
|
||||
const ok = (d) => d && Number.isInteger(d.image) && d.image >= 1 && d.image <= meta.images && Number.isInteger(d.kept)
|
||||
&& typeof d.page === "string" && d.page.length <= 20 && typeof d.body === "string" && d.body.length <= 50_000;
|
||||
if (body.duplicates.length > config.maxPages || !body.duplicates.every(ok)) throw new HttpError(400, "Neplatný seznam opakovaných fotek.");
|
||||
patch.duplicates = body.duplicates.map(({ image, page, kept, body: text }) => ({ image, page, kept, body: text }));
|
||||
}
|
||||
return send(res, 200, publicMeta(await store.updateMeta(id, () => patch)));
|
||||
}
|
||||
if (method === "DELETE") {
|
||||
|
|
@ -196,7 +215,25 @@ async function api(req, res, store, provider, parts) {
|
|||
}
|
||||
}
|
||||
|
||||
if (sub === "images" && n) {
|
||||
// Transcribe one photo and hand back { num, body } without touching the text:
|
||||
// the editor puts it into the page (re-reading a failed page, adding a page later).
|
||||
if (sub === "images" && n && parts[5] === "transcribe" && method === "POST") {
|
||||
await requireAuth(req);
|
||||
if (!provider) throw new HttpError(503, "Přepis není nastavený: na serveru chybí OPENROUTER_API_KEY nebo ANTHROPIC_API_KEY.");
|
||||
if (running.has(id)) throw new HttpError(409, "Hra se právě přepisuje.");
|
||||
const num = Number(n);
|
||||
const jpeg = await store.getImage(id, num);
|
||||
await takePages(1);
|
||||
let result;
|
||||
try { result = splitPage(await provider.transcribe(jpeg)); }
|
||||
catch (err) { throw new HttpError(502, `Přepis se nepovedl: ${err?.message || err}`); }
|
||||
// spell the speakers the way the rest of the play does
|
||||
const [, body] = normalizeSpeakers([await store.getText(id), result.body]);
|
||||
await store.updateMeta(id, (m) => { const pages = m.pages.slice(); pages[num - 1] = result; return { pages }; });
|
||||
return send(res, 200, { num: result.num, body });
|
||||
}
|
||||
|
||||
if (sub === "images" && n && !parts[5]) {
|
||||
const num = Number(n);
|
||||
if (method === "GET") return send(res, 200, await store.getImage(id, num), "image/jpeg", { "Cache-Control": "private, max-age=3600" });
|
||||
if (method === "PUT") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue