Fix server crash on local captioning
This commit is contained in:
parent
ec6b6ab8d4
commit
2e5bbf0445
|
@ -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;
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue