Coder Social home page Coder Social logo

Comments (1)

meodai avatar meodai commented on June 9, 2024

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

from color-names.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.