mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-04 19:21:53 +00:00
wip
This commit is contained in:
+29
-68
@@ -63,89 +63,57 @@ function capitalizeFirst(str) {
|
||||
}
|
||||
|
||||
//https://github.com/soniox/soniox_examples/blob/master/speech_to_text/nodejs/soniox_async.js
|
||||
// soniox sdk is deplicated and nolonger work to validate api key.
|
||||
// soniox sdk is deprecated and no longer works to validate api key.
|
||||
const testSonioxStt = async(logger, credentials) => {
|
||||
const {api_key} = credentials;
|
||||
const https = require('https');
|
||||
|
||||
// Helper function for Soniox API calls
|
||||
const apiFetch = async(endpoint, options = {}) => {
|
||||
const res = await fetch(`https://api.soniox.com${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Authorization': `Bearer ${api_key}`,
|
||||
...options.headers
|
||||
}
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
return options.method === 'DELETE' ? null : res.json();
|
||||
};
|
||||
|
||||
try {
|
||||
// Upload file using form-data with https module
|
||||
// Upload file using form-data with buffer
|
||||
const FormData = require('form-data');
|
||||
const form = new FormData();
|
||||
form.append('file', fs.createReadStream('data/test_audio.wav'));
|
||||
const audioBuffer = fs.readFileSync('data/test_audio.wav');
|
||||
form.append('file', audioBuffer, 'test_audio.wav');
|
||||
|
||||
const fileId = await new Promise((resolve, reject) => {
|
||||
const req = https.request({
|
||||
hostname: 'api.soniox.com',
|
||||
path: '/v1/files',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${api_key}`,
|
||||
...form.getHeaders()
|
||||
}
|
||||
}, (res) => {
|
||||
let data = '';
|
||||
res.on('data', (chunk) => data += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode >= 400) {
|
||||
reject(new Error(`HTTP ${res.statusCode}: ${data}`));
|
||||
} else {
|
||||
try {
|
||||
const result = JSON.parse(data);
|
||||
resolve(result.id);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on('error', reject);
|
||||
form.pipe(req);
|
||||
const { id: fileId } = await apiFetch('/v1/files', {
|
||||
method: 'POST',
|
||||
headers: form.getHeaders(),
|
||||
body: form.getBuffer()
|
||||
});
|
||||
|
||||
// Create transcription
|
||||
const transcriptionRes = await fetch('https://api.soniox.com/v1/transcriptions', {
|
||||
const { id: transcriptionId } = await apiFetch('/v1/transcriptions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${api_key}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ model: 'stt-async-v3', file_id: fileId })
|
||||
});
|
||||
|
||||
if (!transcriptionRes.ok) throw new Error(`HTTP ${transcriptionRes.status}: ${await transcriptionRes.text()}`);
|
||||
const { id: transcriptionId } = await transcriptionRes.json();
|
||||
|
||||
// Wait for transcription to complete
|
||||
let attempts = 0;
|
||||
const maxAttempts = 30; // 30 seconds max wait
|
||||
|
||||
while (attempts < maxAttempts) {
|
||||
const statusRes = await fetch(`https://api.soniox.com/v1/transcriptions/${transcriptionId}`, {
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
});
|
||||
if (!statusRes.ok) throw new Error(`HTTP ${statusRes.status}: ${await statusRes.text()}`);
|
||||
const status = await statusRes.json();
|
||||
const status = await apiFetch(`/v1/transcriptions/${transcriptionId}`);
|
||||
|
||||
if (status.status === 'completed') {
|
||||
// Get the transcript
|
||||
const transcriptRes = await fetch(`https://api.soniox.com/v1/transcriptions/${transcriptionId}/transcript`, {
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
});
|
||||
|
||||
if (!transcriptRes.ok) throw new Error(`HTTP ${transcriptRes.status}: ${await transcriptRes.text()}`);
|
||||
const transcript = await transcriptRes.json();
|
||||
const transcript = await apiFetch(`/v1/transcriptions/${transcriptionId}/transcript`);
|
||||
|
||||
// Clean up
|
||||
await fetch(`https://api.soniox.com/v1/files/${fileId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
});
|
||||
|
||||
await fetch(`https://api.soniox.com/v1/transcriptions/${transcriptionId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
});
|
||||
await apiFetch(`/v1/files/${fileId}`, { method: 'DELETE' });
|
||||
await apiFetch(`/v1/transcriptions/${transcriptionId}`, { method: 'DELETE' });
|
||||
|
||||
return transcript;
|
||||
} else if (status.status === 'error') {
|
||||
@@ -158,15 +126,8 @@ const testSonioxStt = async(logger, credentials) => {
|
||||
}
|
||||
|
||||
// Timeout reached - clean up resources before throwing error
|
||||
await fetch(`https://api.soniox.com/v1/files/${fileId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
}).catch(() => {}); // Ignore cleanup errors
|
||||
|
||||
await fetch(`https://api.soniox.com/v1/transcriptions/${transcriptionId}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': `Bearer ${api_key}` }
|
||||
}).catch(() => {}); // Ignore cleanup errors
|
||||
await apiFetch(`/v1/files/${fileId}`, { method: 'DELETE' }).catch(() => {});
|
||||
await apiFetch(`/v1/transcriptions/${transcriptionId}`, { method: 'DELETE' }).catch(() => {});
|
||||
|
||||
throw new Error('Transcription timeout after 30 seconds');
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user