Merge pull request #1453 from valadaptive/caption-router

Use Express router for caption endpoint
This commit is contained in:
Cohee 2023-12-04 21:38:12 +02:00 committed by GitHub
commit c0cf274e6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 29 deletions

View File

@ -3624,7 +3624,7 @@ require('./src/endpoints/translate').registerEndpoints(app, jsonParser);
require('./src/endpoints/classify').registerEndpoints(app, jsonParser);
// Image captioning
require('./src/endpoints/caption').registerEndpoints(app, jsonParser);
app.use('/api/extra/caption', require('./src/endpoints/caption').router);
// Web search extension
require('./src/endpoints/serpapi').registerEndpoints(app, jsonParser);

View File

@ -1,35 +1,32 @@
const express = require('express');
const { jsonParser } = require('../express-common');
const TASK = 'image-to-text';
/**
* @param {import("express").Express} app
* @param {any} jsonParser
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/extra/caption', jsonParser, async (req, res) => {
try {
const { image } = req.body;
const router = express.Router();
const module = await import('../transformers.mjs');
const rawImage = await module.default.getRawImage(image);
router.post('/', jsonParser, async (req, res) => {
try {
const { image } = req.body;
if (!rawImage) {
console.log('Failed to parse captioned image');
return res.sendStatus(400);
}
const module = await import('../transformers.mjs');
const rawImage = await module.default.getRawImage(image);
const pipe = await module.default.getPipeline(TASK);
const result = await pipe(rawImage);
const text = result[0].generated_text;
console.log('Image caption:', text);
return res.json({ caption: text });
} catch (error) {
console.error(error);
return res.sendStatus(500);
if (!rawImage) {
console.log('Failed to parse captioned image');
return res.sendStatus(400);
}
});
}
module.exports = {
registerEndpoints,
};
const pipe = await module.default.getPipeline(TASK);
const result = await pipe(rawImage);
const text = result[0].generated_text;
console.log('Image caption:', text);
return res.json({ caption: text });
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
module.exports = { router };