NodeJS
Learn how to import a phoneme library from a CSV using the Resemble API AI platform with NodeJS.
In order to best use this guide ensure that you have:
- Signed up for a Resemble.ai account and confirmed your e-mail
- Obtained your API key for use with the API - for additional information see the Authentication page
Guide
First, you will need to initialize your Node project
# Create node environment
npm init
With your project setup, we can install the Resemble SDK and CSV Parser via npm install
# Install Resemble SDK
npm install @resemble/node
npm install csv-parser
Create a new index.js
file which will be used for this example.
Import the Resemble
class from the resemble
sdk, the file system module fs
, and csv-parser
module. Next, add the setupResembleAI
function to initialize the Resemble API client.
// resemble-import-phonemes/index.js
import * as Resemble from '@resemble/node'
import fs from 'fs'
import csv from 'csv-parser'
const apiKey = process.env.RESEMBLE_API_KEY
if (!apiKey) {
console.error('Please set the RESEMBLE_API_KEY environment variable.')
process.exit(1)
}
const setupResembleAI = (apiKey) => {
console.log('Setting Resemble API Key...')
Resemble.Resemble.setApiKey(apiKey)
}
setupResembleAI(apiKey)
Notice that the function expects the environment variable RESEMBLE_API_KEY
to be present containing the necessary API key.
We want to build our program in way that the user can specify their desired CSV file using input arguments. For example:
node index.js path/to/csv
To accomplish this, we can use Node’s process.argv
property which returns an array containing the command line arguments passed when the Node process was run. Append the following code snippet to your index.js
file:
import * as Resemble from '@resemble/node'
// -- snipped --
const args = process.argv.slice(2);
// Check if the required number of arguments is provided
if (args.length !== 1) { ①
console.error('Usage: node index.js path/to/csv');
process.exit(1);
}
const [csvFile] = args; ②
// Command-line arguments
const mainArgs = {③
csvFile: csvFile
};
In the above code snippet, we validate that the provided number of arguments is sufficient ① and proceed to de-structure ② and repackage the arguments for our script use ③.
With the argument parsing in place, create a new function called importPhonemes
responsible for parsing the provided CSV file and uploading to the API.
Add the following code snippet below to index.js
file.
// resemble-import-phonemes/index.js
// -- snipped --
async function importPhonemes(csvFilePath) {
try {
fs.createReadStream(csvFilePath)
.pipe(csv())
.on('data', async (row) => {
const word = row.word
const phoneticTranscription = row.phonetic_transcription
try {
// Make a POST request to create a new widget entry
const response = await Resemble.Resemble.v2.phonemes.create(word, phoneticTranscription)
if (response.success) {
console.log(`Imported phoneme pair: ${word} and ${phoneticTranscription}`)
} else {
// Failed to import for some reason
throw new Error(JSON.stringify(response))
}
} catch (e) {
console.error(`ERROR: Could not import phoneme pair: ${word} and ${phoneticTranscription} - ${e.message}`)
}
})
.on('end', () => {
// Print summary
console.log(`\nCompleted`)
})
} catch (error) {
console.error(`ERROR: File not found - ${csvFilePath}`)
}
}
Next, we can add a runMain
function to tie together all the parts of our script:
// -- snipped --
// Main function
async function runMain(args) {
// Run the clip creation function to call the Resemble API
await importPhonemes(args.csvFile)
}
// -- snipped --
const args = process.argv.slice(2)
// Check if the required number of arguments is provided
if (args.length !== 1) {
console.error('Usage: node index.js <path/to/csv>')
process.exit(1)
}
const [csvFile] = args
// Command-line arguments
const mainArgs = {
csvFile: csvFile,
}
runMain(mainArgs)
Finally, test your script by running the command via CLI :
# Be sure to prepend your API key to the command
RESEMBLE_API_KEY=... node index.js example.csv
Imported phoneme pair: vodaphone and voduhphone
Imported phoneme pair: cosmic and kozmik
Completed
Congratulations, you have successfully imported your phoneme library! To check the status of your library, you can programmatically access via the API or use the web application as well.