feat: references
This commit is contained in:
+33
-13
@@ -16,7 +16,7 @@ try {
|
||||
const API_BASE = 'https://api.banatie.app/api/v1';
|
||||
const API_KEY = process.env.BANATIE_KEY || '';
|
||||
|
||||
async function uploadImage(filePath) {
|
||||
async function uploadImage(filePath, { flowId, alias }) {
|
||||
const absolutePath = resolve(filePath);
|
||||
const fileBuffer = readFileSync(absolutePath);
|
||||
const fileName = basename(absolutePath);
|
||||
@@ -27,8 +27,10 @@ async function uploadImage(filePath) {
|
||||
|
||||
const form = new FormData();
|
||||
form.append('file', new Blob([fileBuffer], { type: mime }), fileName);
|
||||
form.append('alias', alias);
|
||||
if (flowId) form.append('flowId', flowId);
|
||||
|
||||
console.log(`Uploading reference: ${filePath}...`);
|
||||
console.log(`Uploading: ${filePath} as ${alias}${flowId ? ` (flow: ${flowId})` : ''}...`);
|
||||
|
||||
const response = await fetch(`${API_BASE}/images/upload`, {
|
||||
method: 'POST',
|
||||
@@ -48,22 +50,37 @@ async function uploadImage(filePath) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`Uploaded: ${result.data.id}`);
|
||||
return result.data.id;
|
||||
console.log(`Uploaded: ${alias} (${result.data.id})`);
|
||||
return result.data;
|
||||
}
|
||||
|
||||
function makeAlias(filePath, index) {
|
||||
const name = basename(filePath).replace(/\.[^.]+$/, '').replace(/[^a-z0-9-]/gi, '-').toLowerCase();
|
||||
return `@ref-${index + 1}-${name}`;
|
||||
}
|
||||
|
||||
async function resolveRefs(refs) {
|
||||
if (!refs || refs.length === 0) return undefined;
|
||||
|
||||
const ids = [];
|
||||
for (const ref of refs) {
|
||||
if (existsSync(ref)) {
|
||||
ids.push(await uploadImage(ref));
|
||||
const aliases = [];
|
||||
let flowId = null;
|
||||
|
||||
for (let i = 0; i < refs.length; i++) {
|
||||
const ref = refs[i];
|
||||
if (ref.startsWith('@')) {
|
||||
aliases.push(ref);
|
||||
} else if (existsSync(ref)) {
|
||||
const alias = makeAlias(ref, i);
|
||||
const data = await uploadImage(ref, { flowId, alias });
|
||||
if (!flowId) flowId = data.flowId;
|
||||
aliases.push(alias);
|
||||
} else {
|
||||
ids.push(ref);
|
||||
console.error(`Reference not found: ${ref}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
|
||||
return { referenceImages: aliases, flowId };
|
||||
}
|
||||
|
||||
export async function generateImage({ prompt, output, aspectRatio = '1:1', refs }) {
|
||||
@@ -72,11 +89,14 @@ export async function generateImage({ prompt, output, aspectRatio = '1:1', refs
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const referenceImages = await resolveRefs(refs);
|
||||
const resolved = await resolveRefs(refs);
|
||||
const body = { prompt, aspectRatio };
|
||||
if (referenceImages) body.referenceImages = referenceImages;
|
||||
if (resolved) {
|
||||
body.referenceImages = resolved.referenceImages;
|
||||
if (resolved.flowId) body.flowId = resolved.flowId;
|
||||
}
|
||||
|
||||
console.log(`Generating: "${prompt}" (${body.aspectRatio})${referenceImages ? ` with ${referenceImages.length} ref(s)` : ''}...`);
|
||||
console.log(`Generating: "${prompt}" (${body.aspectRatio})${resolved ? ` with ${resolved.referenceImages.length} ref(s)` : ''}...`);
|
||||
|
||||
const response = await fetch(`${API_BASE}/generations`, {
|
||||
method: 'POST',
|
||||
|
||||
Reference in New Issue
Block a user