Add pollinations as image generation source

This commit is contained in:
Cohee
2024-04-04 20:40:47 +03:00
parent 42138ca09b
commit 75ebdf394a
3 changed files with 171 additions and 7 deletions

View File

@ -684,7 +684,7 @@ drawthings.post('/generate', jsonParser, async (request, response) => {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/txt2img';
const body = {...request.body};
const body = { ...request.body };
delete body.url;
const result = await fetch(url, {
@ -710,8 +710,46 @@ drawthings.post('/generate', jsonParser, async (request, response) => {
}
});
const pollinations = express.Router();
pollinations.post('/generate', jsonParser, async (request, response) => {
try {
const promptUrl = new URL(`https://image.pollinations.ai/prompt/${encodeURIComponent(request.body.prompt)}`);
const params = new URLSearchParams({
model: String(request.body.model),
negative_prompt: String(request.body.negative_prompt),
seed: String(Math.floor(Math.random() * 10_000_000)),
enhance: String(request.body.enhance ?? false),
refine: String(request.body.refine ?? false),
width: String(request.body.width ?? 1024),
height: String(request.body.height ?? 1024),
nologo: String(true),
nofeed: String(true),
});
promptUrl.search = params.toString();
console.log('Pollinations request URL:', promptUrl.toString());
const result = await fetch(promptUrl);
if (!result.ok) {
console.log('Pollinations returned an error.', result.status, result.statusText);
throw new Error('Pollinations request failed.');
}
const buffer = await result.buffer();
const base64 = buffer.toString('base64');
return response.send({ image: base64 });
} catch (error) {
console.log(error);
return response.sendStatus(500);
}
});
router.use('/comfy', comfy);
router.use('/together', together);
router.use('/drawthings', drawthings);
router.use('/pollinations', pollinations);
module.exports = { router };