Created
April 17, 2024 13:26
-
-
Save douglas-henrique/22859a143b9dafdac089794364fa1afb to your computer and use it in GitHub Desktop.
Script to transcribe audio to text using Xenova/whisper-small and transformers.js
This file contains hidden or 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
import fs from 'fs' | |
import { pipeline } from '@xenova/transformers' | |
import { WaveFile } from 'wavefile'; | |
const options = { //Xenova/whisper-small configurations | |
chunk_length_s: 30, | |
stride_length_s: 5, | |
language: 'portuguese', | |
task: 'transcribe', | |
} | |
export async function transcribeAudio(fileName: string) { | |
let data = null; | |
try { | |
console.log('[STARTING TRANSCRIBE]') | |
const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small') //config transformer.js | |
let buffer = fs.readFileSync(fileName) //file buffer | |
let wav = new WaveFile(buffer); // start .wav processing | |
wav.toBitDepth('32f'); | |
wav.toSampleRate(16000); | |
let audioData = wav.getSamples(); | |
if (Array.isArray(audioData)) { | |
audioData = audioData[0]; | |
} | |
let start = performance.now(); | |
data = await transcriber(audioData, options); // process transcribe | |
let end = performance.now(); | |
console.log(`Transcribe execution duration: ${(end - start) / 1000} seconds`); | |
} catch (error) { | |
console.log('[ERROR TRANSCRIBE]', error) | |
throw new Error('Error') | |
} finally { | |
return data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment