Skip to content

Instantly share code, notes, and snippets.

@maZahaca
Created February 3, 2024 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maZahaca/f7f9e630aeeb466de784961ef073b5c3 to your computer and use it in GitHub Desktop.
Save maZahaca/f7f9e630aeeb466de784961ef073b5c3 to your computer and use it in GitHub Desktop.
Code block showing how with JavaScript and HuggingFace API, you can easily convert audio feedback from the customer to text and then understand its sentiment.
const {
HUGGINGFACE_API_KEY,
} = process.env;
async function fetchHuggingFaceModel(model, data) {
const response = await fetch(
`https://api-inference.huggingface.co/models/${model}`,
{
headers: { Authorization: `Bearer ${HUGGINGFACE_API_KEY}` },
method: 'POST',
body: data,
duplex: 'half',
}
);
return response.json();
}
async function getTextFromSource(model, fileSource) {
const getResponse = await fetch(fileSource);
return fetchHuggingFaceModel(model, getResponse.body);
}
(async () => {
// extracting audio to text
const audioToText = await getTextFromSource(
'openai/whisper-medium',
'https://drive.google.com/uc?export=download&id=12l9vffZiE5ui4dLWADf9CmctwDH8KAN9'
);
// audioToText.text = `I really enjoyed visiting this tech conference, we are developers.
// There have been lots of great people to collaborate with and to speak with them.`
// extract sentiment from text
const sentiments = await fetchHuggingFaceModel(
'lxyuan/distilbert-base-multilingual-cased-sentiments-student',
JSON.stringify({
inputs: audioToText.text,
})
);
console.log('results', audioToText, sentiments);
// results {
// text: ' I really enjoyed visiting this tech conference, we are developers. There have been lots of great people to collaborate with and to speak with them.'
// } [
// [
// { label: 'positive', score: 0.8829012513160706 },
// { label: 'neutral', score: 0.06260993331670761 },
// { label: 'negative', score: 0.05448877066373825 }
// ]
// ]
})();
@maZahaca
Copy link
Author

maZahaca commented Feb 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment