Open meodai opened 1 year ago
do the same for tags:
const openai = require('openai')('your-api-key-here');
const csv = require('csv-parser');
const fs = require('fs');
const inputFilePath = 'input.csv';
const outputFilePath = 'output.csv';
const categories = ['nature', 'technology', 'popculture', 'gaming', 'music', 'fantasy', 'sci-fi', 'history', 'horror', 'joy', 'feminine', 'masculine'];
const results = [];
const processRow = (row) => {
const name = row.name.trim();
openai.complete({
engine: 'text-davinci-002',
prompt: `Generate tags for the color name "${name}". Categories: ${categories.join(', ')}`,
maxTokens: 50,
n: 1,
stop: '\n',
temperature: 0.7,
presencePenalty: 0.3,
frequencyPenalty: 0.3,
}).then((res) => {
const tags = res.data.choices[0].text.trim().split(',').map((t) => t.trim());
results.push({ name, tags });
if (results.length === count) {
saveResults();
}
}).catch((err) => {
console.error(err);
});
};
const saveResults = () => {
const stream = fs.createWriteStream(outputFilePath, { flags: 'a' });
stream.write(`name,tags\n`);
results.forEach((r) => {
const tagsString = r.tags.join(';');
stream.write(`${r.name},${tagsString}\n`);
});
stream.end();
};
fs.createReadStream(inputFilePath)
.pipe(csv())
.on('data', processRow)
.on('end', () => {
console.log('Finished processing all rows.');
});