Created
February 3, 2024 11:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
// ] | |
// ] | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shared on twitter: https://x.com/AndrewRedUK/status/1753752365847441752?s=20