2024-10-25 13:48:42 +08:00
|
|
|
#!node
|
2024-10-25 13:34:26 +08:00
|
|
|
import { program } from 'commander';
|
|
|
|
|
import * as fs from 'fs/promises';
|
2024-10-25 14:23:29 +08:00
|
|
|
import { awaitProdiaJob, defaultProdiaParams, fetchProdia } from './prodia';
|
2024-10-25 13:34:26 +08:00
|
|
|
|
|
|
|
|
program
|
|
|
|
|
.name('prodia-scripts')
|
|
|
|
|
.description('cli for prodia usage')
|
|
|
|
|
.version('0.1.0');
|
|
|
|
|
|
|
|
|
|
program
|
|
|
|
|
.command('sdxl')
|
|
|
|
|
.description('generate an image with sdxl')
|
2024-10-25 13:48:42 +08:00
|
|
|
.argument('[config]', 'path to the config json', './sdxl.gen.json')
|
|
|
|
|
.option('--init', 'create a default sdxl.gen.json')
|
2024-10-25 14:23:29 +08:00
|
|
|
.option('--append [where]', 'saves generated url to a text file')
|
|
|
|
|
.option('--queue [amount]', 'queues jobs without waiting for them')
|
2024-10-25 13:48:42 +08:00
|
|
|
.action(async (config, flags) => {
|
|
|
|
|
if (flags.init) {
|
|
|
|
|
console.log('Creating sdxl.gen.json...');
|
|
|
|
|
await fs.writeFile(
|
|
|
|
|
'./sdxl.gen.json',
|
|
|
|
|
JSON.stringify(defaultProdiaParams, null, 4)
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Reading config=${config}...`);
|
|
|
|
|
const txt = await fs.readFile(config, { encoding: 'utf8' });
|
|
|
|
|
const json = JSON.parse(txt);
|
|
|
|
|
|
|
|
|
|
console.log(`Fetching prodia api...`);
|
2024-10-25 14:23:29 +08:00
|
|
|
if (flags.queue) {
|
|
|
|
|
const queue = parseInt(flags.queue);
|
|
|
|
|
if (isNaN(queue) || queue < 1)
|
|
|
|
|
throw new Error(`invalid queue number=${flags.queue}`);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < queue; i++) {
|
|
|
|
|
console.log(`Fetching ${i + 1}/${queue}...`);
|
|
|
|
|
const job = await fetchProdia(json);
|
|
|
|
|
if (flags.append)
|
|
|
|
|
await fs.appendFile(
|
|
|
|
|
flags.append,
|
|
|
|
|
`https://images.prodia.xyz/${job.job}.png\n`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
console.log('Done!');
|
|
|
|
|
} else {
|
|
|
|
|
const job = await fetchProdia(json);
|
|
|
|
|
const status = await awaitProdiaJob(job);
|
|
|
|
|
|
|
|
|
|
console.log('Done: ', status);
|
|
|
|
|
|
|
|
|
|
if (status.imageUrl && flags.append) {
|
|
|
|
|
await fs.appendFile(flags.append, `${status.imageUrl}\n`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-25 13:48:42 +08:00
|
|
|
}
|
2024-10-25 13:34:26 +08:00
|
|
|
});
|
2024-10-25 13:48:42 +08:00
|
|
|
|
|
|
|
|
program.parse();
|