Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
meodai opened this issue Feb 22, 2023 · 1 comment
Open

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

meodai opened this issue Feb 22, 2023 · 1 comment

Comments

@meodai
Copy link
Owner

meodai commented Feb 22, 2023

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
Copy link
Owner Author

meodai commented Feb 22, 2023

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.');
  });

@meodai meodai changed the title Use OpenAI to write a description for every single color Use AI to write a description for every single color Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant