Load pollinations models from an endpoint

8948354bf0a353bda2468edabc41b20627a48a4e

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +36 -22Showing whitespace changes
public/scripts/extensions/stable-diffusion/index.js+11 -22
@@ -1692,28 +1692,17 @@ async function loadStabilityModels() {
16921692}
16931693
16941694async function loadPollinationsModels() {
1695- return [
1695+ const result = await fetch('/api/sd/pollinations/models', {
1696- {
1696+ method: 'POST',
1697- value: 'flux',
1697+ headers: getRequestHeaders(),
1698- text: 'FLUX.1 [schnell]',
1698+ });
1699- },
1699+
1700- {
1700+ if (result.ok) {
1701- value: 'flux-realism',
1701+ const data = await result.json();
1702- text: 'FLUX Realism',
1702+ return data;
1703- },
1703+ }
1704- {
1704+
1705- value: 'flux-anime',
1705+ return [];
1706- text: 'FLUX Anime',
1707- },
1708- {
1709- value: 'flux-3d',
1710- text: 'FLUX 3D',
1711- },
1712- {
1713- value: 'turbo',
1714- text: 'SDXL Turbo',
1715- },
1716- ];
17171706}
17181707
17191708async function loadTogetherAIModels() {
src/endpoints/stable-diffusion.js+25 -0
@@ -811,6 +811,31 @@ drawthings.post('/generate', jsonParser, async (request, response) => {
811811
812812const pollinations = express.Router();
813813
814+pollinations.post('/models', jsonParser, async (_request, response) => {
815+ try {
816+ const modelsUrl = new URL('https://image.pollinations.ai/models');
817+ const result = await fetch(modelsUrl);
818+
819+ if (!result.ok) {
820+ console.log('Pollinations returned an error.', result.status, result.statusText);
821+ throw new Error('Pollinations request failed.');
822+ }
823+
824+ const data = await result.json();
825+
826+ if (!Array.isArray(data)) {
827+ console.log('Pollinations returned invalid data.');
828+ throw new Error('Pollinations request failed.');
829+ }
830+
831+ const models = data.map(x => ({ value: x, text: x }));
832+ return response.send(models);
833+ } catch (error) {
834+ console.log(error);
835+ return response.sendStatus(500);
836+ }
837+});
838+
814839pollinations.post('/generate', jsonParser, async (request, response) => {
815840 try {
816841 const promptUrl = new URL(`https://image.pollinations.ai/prompt/${encodeURIComponent(request.body.prompt)}`);