meodai / color-names

Large list of handpicked color names 🌈
https://meodai.github.io/color-names/
MIT License
2.39k stars 180 forks source link

Use AI to write a description for every single color #160

Open meodai opened 1 year ago

meodai commented 1 year ago
const fs = require('fs');
const openai = require('openai'); // Import the OpenAI API package

const openaiApiKey = 'YOUR_API_KEY'; // Replace this with your actual API key
const model = 'text-davinci-002'; // Replace this with the desired GPT-3 model

const inputFilePath = '/path/to/your/color-names.csv'; // Replace this with the path to your actual CSV file
const outputFilePath = '/path/to/your/descriptions.csv'; // Replace this with the desired output file path

const csvData = fs.readFileSync(inputFilePath, 'utf-8'); // Read the CSV file
const colorNames = csvData.split('\n'); // Split the CSV data into an array of color names

// Initialize the OpenAI API with your API key
openai.api_key = openaiApiKey;

const descriptions = [];

// Iterate through each color name and generate a description using the OpenAI API
async function generateDescriptions() {
  for (let i = 0; i < colorNames.length; i++) {
    const colorName = colorNames[i].trim();

    // Generate a description for the color name using the OpenAI API and the GPT-3 model
    const response = await openai.complete({
      engine: model,
      prompt: `Describe the color "${colorName}" in a single sentence.`,
      max_tokens: 50,
      temperature: 0.5,
      n: 1,
      stop: '\n',
    });

    const description = response.data.choices[0].text.trim();

    descriptions.push(description);
  }

  // Write the descriptions to a new CSV file
  const outputData = descriptions.join('\n');
  fs.writeFileSync(outputFilePath, outputData, 'utf-8');
}

generateDescriptions();
meodai commented 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.');
  });