Fix server crash on local captioning

This commit is contained in:
Cohee 2023-09-22 23:04:26 +03:00
parent ec6b6ab8d4
commit 2e5bbf0445
2 changed files with 16 additions and 6 deletions

View File

@ -10,7 +10,13 @@ function registerEndpoints(app, jsonParser) {
const { image } = req.body; const { image } = req.body;
const module = await import('./transformers.mjs'); const module = await import('./transformers.mjs');
const rawImage = module.default.getRawImage(image); const rawImage = await module.default.getRawImage(image);
if (!rawImage) {
console.log('Failed to parse captioned image');
return res.sendStatus(400);
}
const pipe = await module.default.getPipeline(TASK); const pipe = await module.default.getPipeline(TASK);
const result = await pipe(rawImage); const result = await pipe(rawImage);
const text = result[0].generated_text; const text = result[0].generated_text;

View File

@ -31,12 +31,16 @@ const tasks = {
} }
async function getRawImage(image) { async function getRawImage(image) {
const buffer = Buffer.from(image, 'base64'); try {
const byteArray = new Uint8Array(buffer); const buffer = Buffer.from(image, 'base64');
const blob = new Blob([byteArray]); const byteArray = new Uint8Array(buffer);
const blob = new Blob([byteArray]);
const rawImage = await RawImage.fromBlob(blob); const rawImage = await RawImage.fromBlob(blob);
return rawImage; return rawImage;
} catch {
return null;
}
} }
function getModelForTask(task) { function getModelForTask(task) {