Create ai/audio-transcription.js
Browse files- ai/audio-transcription.js +45 -0
ai/audio-transcription.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require('axios');
|
| 2 |
+
|
| 3 |
+
const FormData = require('form-data');
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
async function transcribe(buffer) {
|
| 8 |
+
|
| 9 |
+
try {
|
| 10 |
+
|
| 11 |
+
if (!buffer || !Buffer.isBuffer(buffer)) throw new Error('Audio buffer is required');
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
const form = new FormData();
|
| 16 |
+
|
| 17 |
+
form.append('file', buffer, `${Date.now()}_rynn.mp3`);
|
| 18 |
+
|
| 19 |
+
const { data } = await axios.post('https://audio-transcription-api.752web.workers.dev/api/transcribe', form, {
|
| 20 |
+
|
| 21 |
+
headers: form.getHeaders()
|
| 22 |
+
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
return data.transcription;
|
| 28 |
+
|
| 29 |
+
} catch (error) {
|
| 30 |
+
|
| 31 |
+
throw new Error(error.message);
|
| 32 |
+
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
// Usage:
|
| 40 |
+
|
| 41 |
+
const fs = require('fs');
|
| 42 |
+
|
| 43 |
+
const resp = await transcribe(fs.readFileSync('./audio.mp3'));
|
| 44 |
+
|
| 45 |
+
console.log(resp);
|