This commit is contained in:
hyper
2024-10-25 13:34:26 +08:00
commit ae6322bcb2
4 changed files with 702 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
import { program } from 'commander';
import * as fs from 'fs/promises';
import { fetchProdia } from './prodia';
program
.name('prodia-scripts')
.description('cli for prodia usage')
.version('0.1.0');
program
.command('sdxl')
.description('generate an image with sdxl')
.argument('<config>', 'path to the config json')
.action(async (config) => {
const txt = await fs.readFile(config, { encoding: 'utf8' });
const json = JSON.parse(txt);
const status = await fetchProdia(json);
console.log('done:', status);
});
+74
View File
@@ -0,0 +1,74 @@
import fetch from 'node-fetch';
type ProdiaParams = {
model: string;
prompt: string;
negative_prompt: string;
style_preset: string;
steps: number;
cfg_scale: number;
seed: number;
sampler: string;
width: number;
height: number;
};
type ProdiaResponse = {
job: string;
status: string;
imageUrl?: string;
};
const defaultParams: ProdiaParams = {
model: 'sd_xl_base_1.0.safetensors [be9edd61]',
prompt: 'puppies in a cloud',
negative_prompt: 'badly drawn',
style_preset: 'anime',
steps: 20,
cfg_scale: 6,
seed: -1,
sampler: 'DPM++ 2M Karras',
width: 1024,
height: 1024,
};
export async function fetchProdia(
params: Partial<ProdiaParams>,
endpoint = 'sdxl/generate'
) {
const resp = await fetch(`https://api.prodia.com/v1/${endpoint}`, {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
'X-Prodia-Key': '6e169a7f-444f-4c22-8353-b9a77d42645c',
},
body: JSON.stringify({
...defaultParams,
...params,
}),
});
const json = (await resp.json()) as ProdiaResponse;
if ('job' in json) {
while (true) {
const query = await fetch(
`https://api.prodia.com/v1/job/${json.job}`,
{
headers: {
accept: 'application/json',
'X-Prodia-Key': '6e169a7f-444f-4c22-8353-b9a77d42645c',
},
}
);
const status = (await query.json()) as ProdiaResponse;
if (status.imageUrl) return status;
console.log(status);
await new Promise((r) => setTimeout(r, 5000));
}
}
return json;
}