Initial commit: Doctor-Patient clinical transcription and structured summary pipeline
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
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';
|
||||
const AUDIO_FILE = 'dummy.wav';
|
||||
|
||||
async function main() {
|
||||
if (!fs.existsSync(AUDIO_FILE)) {
|
||||
console.error("Audio file not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("1. Initialize Batch 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();
|
||||
const jobId = initData.job_id;
|
||||
|
||||
console.log("2. Get Upload Link...");
|
||||
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: [AUDIO_FILE]
|
||||
})
|
||||
});
|
||||
const uploadData = await uploadRes.json();
|
||||
const uploadUrl = uploadData.upload_urls[AUDIO_FILE].file_url;
|
||||
|
||||
console.log("3. Upload File...");
|
||||
const fileContent = fs.readFileSync(path.join(__dirname, AUDIO_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("4. Start Job...");
|
||||
await fetch(`${BASE_URL}/speech-to-text/job/v1/${jobId}/start`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'api-subscription-key': API_KEY,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
console.log("5. Polling 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(` State: ${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("6. Get Download Links for", 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("7. Downloading Result...");
|
||||
const resultRes = await fetch(dlUrl);
|
||||
const resultData = await resultRes.json();
|
||||
|
||||
// Parse words
|
||||
let conversationText = "";
|
||||
if (resultData.timestamps && resultData.timestamps.words) {
|
||||
const words = resultData.timestamps.words;
|
||||
const speakers = resultData.timestamps.speaker || [];
|
||||
|
||||
let currentSpk = null;
|
||||
let currentSentence = [];
|
||||
let convLines = [];
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
const word = words[i];
|
||||
let spk = "Unknown";
|
||||
if (speakers && speakers.length > i) {
|
||||
spk = speakers[i];
|
||||
} else if (resultData.words && resultData.words[i] && resultData.words[i].speaker) {
|
||||
spk = resultData.words[i].speaker; // fallback
|
||||
}
|
||||
|
||||
if (currentSpk === null) currentSpk = spk;
|
||||
|
||||
if (spk !== currentSpk) {
|
||||
convLines.push(`**Speaker ${currentSpk}:** ${currentSentence.join(' ')}`);
|
||||
currentSpk = spk;
|
||||
currentSentence = [word];
|
||||
} else {
|
||||
currentSentence.push(word);
|
||||
}
|
||||
}
|
||||
if (currentSentence.length > 0) {
|
||||
convLines.push(`**Speaker ${currentSpk}:** ${currentSentence.join(' ')}`);
|
||||
}
|
||||
|
||||
conversationText = convLines.join('\n');
|
||||
} else {
|
||||
// Fallback for batch output format
|
||||
if (resultData.transcript) {
|
||||
conversationText = resultData.transcript;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("\n8. Getting Summary from NVIDIA Nemotron (OpenRouter)...");
|
||||
const prompt = `Please summarize the following conversation:\n\n${conversationText}\n\nSummary:`;
|
||||
try {
|
||||
const nemotronRes = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer 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`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'nvidia/nemotron-3-ultra-550b-a55b:free', // Using user provided model
|
||||
messages: [
|
||||
{ role: "user", content: prompt }
|
||||
]
|
||||
})
|
||||
});
|
||||
|
||||
const nemotronData = await nemotronRes.json();
|
||||
console.log("\n--- SUMMARY ---");
|
||||
if (nemotronData.choices && nemotronData.choices.length > 0) {
|
||||
console.log(nemotronData.choices[0].message.content);
|
||||
} else {
|
||||
console.log("No summary generated or error occurred:", nemotronData);
|
||||
}
|
||||
} catch(err) {
|
||||
console.error("Failed to call Nemotron", err);
|
||||
}
|
||||
} else {
|
||||
console.log("Job failed:", statusData);
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user