ai-chatbot/scripts/prepare-official-model-avatars.mjs

58 lines
2.0 KiB
JavaScript

import fs from 'node:fs/promises';
import path from 'node:path';
import sharp from 'sharp';
const sourceDir = process.argv[2] || '/tmp/official-model-logos';
const outputDir = process.argv[3] || '/tmp/official-model-avatars';
const sources = {
google: path.join(sourceDir, 'google.png'),
openai: path.join(sourceDir, 'openai.svg'),
qwen: path.join(sourceDir, 'qwen.png'),
runway: path.join(sourceDir, 'runway.png'),
bfl: path.join(sourceDir, 'bfl.png'),
stepfun: path.join(sourceDir, 'stepfun.png'),
volcengine: path.join(sourceDir, 'volcengine.png'),
doubao: path.join(sourceDir, 'doubao.png'),
};
const background = Buffer.from(`
<svg width="128" height="128" viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg">
<rect width="128" height="128" rx="28" fill="#F1F2F4"/>
<rect x="0.75" y="0.75" width="126.5" height="126.5" rx="27.25" fill="none" stroke="#D8DADE" stroke-width="1.5"/>
</svg>
`);
async function openAiMark() {
const officialSvg = await fs.readFile(sources.openai, 'utf8');
const blossom = officialSvg.match(/<path d="([^"]+)" fill="black"\/>/);
if (!blossom) throw new Error('OpenAI Blossom path was not found in the official asset');
return Buffer.from(`
<svg width="270" height="267" viewBox="145 226 270 267" xmlns="http://www.w3.org/2000/svg">
<path d="${blossom[1]}" fill="#202226"/>
</svg>
`);
}
async function makeAvatar(name, input) {
const mark = await sharp(input)
.resize(88, 88, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.grayscale()
.modulate({ brightness: 0.62 })
.png()
.toBuffer();
await sharp(background)
.composite([{ input: mark, gravity: 'centre' }])
.png({ compressionLevel: 9 })
.toFile(path.join(outputDir, `${name}.png`));
}
await fs.mkdir(outputDir, { recursive: true });
for (const [name, input] of Object.entries(sources)) {
await makeAvatar(name, name === 'openai' ? await openAiMark() : input);
}
console.log(`Prepared ${Object.keys(sources).length} monochrome official model avatars in ${outputDir}`);