Use Express router for caption endpoint
This commit is contained in:
parent
414c9bd5fb
commit
d2e1577acb
|
@ -3621,7 +3621,7 @@ require('./src/endpoints/translate').registerEndpoints(app, jsonParser);
|
||||||
require('./src/endpoints/classify').registerEndpoints(app, jsonParser);
|
require('./src/endpoints/classify').registerEndpoints(app, jsonParser);
|
||||||
|
|
||||||
// Image captioning
|
// Image captioning
|
||||||
require('./src/endpoints/caption').registerEndpoints(app, jsonParser);
|
app.use('/api/extra/caption', require('./src/endpoints/caption').router);
|
||||||
|
|
||||||
// Web search extension
|
// Web search extension
|
||||||
require('./src/endpoints/serpapi').registerEndpoints(app, jsonParser);
|
require('./src/endpoints/serpapi').registerEndpoints(app, jsonParser);
|
||||||
|
|
|
@ -1,35 +1,32 @@
|
||||||
|
const express = require('express');
|
||||||
|
const { jsonParser } = require('../express-common');
|
||||||
|
|
||||||
const TASK = 'image-to-text';
|
const TASK = 'image-to-text';
|
||||||
|
|
||||||
/**
|
const router = express.Router();
|
||||||
* @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 module = await import('../transformers.mjs');
|
router.post('/', jsonParser, async (req, res) => {
|
||||||
const rawImage = await module.default.getRawImage(image);
|
try {
|
||||||
|
const { image } = req.body;
|
||||||
|
|
||||||
if (!rawImage) {
|
const module = await import('../transformers.mjs');
|
||||||
console.log('Failed to parse captioned image');
|
const rawImage = await module.default.getRawImage(image);
|
||||||
return res.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pipe = await module.default.getPipeline(TASK);
|
if (!rawImage) {
|
||||||
const result = await pipe(rawImage);
|
console.log('Failed to parse captioned image');
|
||||||
const text = result[0].generated_text;
|
return res.sendStatus(400);
|
||||||
console.log('Image caption:', text);
|
|
||||||
|
|
||||||
return res.json({ caption: text });
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.sendStatus(500);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
const pipe = await module.default.getPipeline(TASK);
|
||||||
registerEndpoints,
|
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 };
|
||||||
|
|
Loading…
Reference in New Issue