111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const dotenv = require('dotenv');
|
|
dotenv.config();
|
|
|
|
const API_KEY = process.env.SARVAM_API_KEY;
|
|
const BASE_URL = 'https://api.sarvam.ai';
|
|
|
|
async function main() {
|
|
console.log("1. Initialize Job...");
|
|
const initRes = await fetch(`${BASE_URL}/speech-to-text/job/v1`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'api-subscription-key': API_KEY,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
job_parameters: {
|
|
model: "saaras:v4",
|
|
mode: "transcribe",
|
|
with_diarization: true,
|
|
with_timestamps: true,
|
|
language_code: "unknown"
|
|
}
|
|
})
|
|
});
|
|
const initData = await initRes.json();
|
|
console.log(initData);
|
|
const jobId = initData.job_id;
|
|
|
|
console.log("\n2. Get Upload Links...");
|
|
const uploadRes = await fetch(`${BASE_URL}/speech-to-text/job/v1/upload-files`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'api-subscription-key': API_KEY,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
job_id: jobId,
|
|
files: ["test.wav"]
|
|
})
|
|
});
|
|
const uploadData = await uploadRes.json();
|
|
console.log(uploadData);
|
|
|
|
const uploadUrl = uploadData.upload_urls["test.wav"].file_url;
|
|
|
|
console.log("\n3. Upload File...");
|
|
const fileContent = fs.readFileSync(path.join(__dirname, 'test.wav')); // we need a small wav file
|
|
const putRes = await fetch(uploadUrl, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'x-ms-blob-type': 'BlockBlob',
|
|
'Content-Type': 'audio/wav'
|
|
},
|
|
body: fileContent
|
|
});
|
|
console.log("Upload Status:", putRes.status);
|
|
|
|
console.log("\n4. Start Job...");
|
|
const startRes = await fetch(`${BASE_URL}/speech-to-text/job/v1/${jobId}/start`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'api-subscription-key': API_KEY,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
const startData = await startRes.json();
|
|
console.log(startData);
|
|
|
|
console.log("\n5. Poll Status...");
|
|
let statusData;
|
|
for (let i = 0; i < 30; i++) {
|
|
const statusRes = await fetch(`${BASE_URL}/speech-to-text/job/v1/${jobId}/status`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'api-subscription-key': API_KEY
|
|
}
|
|
});
|
|
statusData = await statusRes.json();
|
|
console.log(`Poll ${i}:`, statusData.job_state);
|
|
if (statusData.job_state === "Completed" || statusData.job_state === "Failed") break;
|
|
await new Promise(r => setTimeout(r, 5000));
|
|
}
|
|
|
|
if (statusData.job_state === 'Completed') {
|
|
const outFile = statusData.job_details[0].outputs[0].file_name;
|
|
console.log("\n6. Get Download Links...", outFile);
|
|
const dlRes = await fetch(`${BASE_URL}/speech-to-text/job/v1/download-files`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'api-subscription-key': API_KEY,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({
|
|
job_id: jobId,
|
|
files: [outFile]
|
|
})
|
|
});
|
|
const dlData = await dlRes.json();
|
|
const dlUrl = dlData.download_urls[outFile].file_url;
|
|
|
|
console.log("\n7. Download Result...");
|
|
const resultRes = await fetch(dlUrl);
|
|
const resultData = await resultRes.json();
|
|
console.log(JSON.stringify(resultData).slice(0, 500) + '...');
|
|
}
|
|
}
|
|
|
|
main().catch(console.error);
|