225 lines
8.9 KiB
Markdown
225 lines
8.9 KiB
Markdown
# AI Clinical Assistant — Doctor-Patient Consultation Transcription & Summary
|
|
|
|
An intelligent, real-time clinical speech-to-text transcription and structured medical summary pipeline tailored for doctor-patient consultations in multilingual environments (Marathi, Hindi, and English).
|
|
|
|
Powered by **Sarvam AI** for multilingual speech recognition, real-time WebSocket streaming, translation, and native speaker diarization, combined with high-performance LLMs (**Groq** / **Sarvam**) for clinical role classification and structured SOAP-format medical documentation.
|
|
|
|
---
|
|
|
|
## Key Features
|
|
|
|
- **Multilingual Speech-to-Text with Automatic English Translation**:
|
|
- Accepts audio spoken in **Marathi**, **Hindi**, or **English**.
|
|
- Always outputs clean, professional **English transcriptions** preserving medical accuracy, context, and intent.
|
|
- Employs Sarvam AI's `saaras:v4` translation mode with automated fallback pipelines.
|
|
- **Speaker Diarization & Role Classification**:
|
|
- Automatically separates distinct speakers (Doctor vs. Patient).
|
|
- Uses Sarvam Native Diarization paired with intelligent LLM context analysis for robust role classification.
|
|
- Hybrid speaker embedding fallback support.
|
|
- **Structured Clinical Summary (SOAP Format)**:
|
|
- Generates comprehensive medical summaries structured into:
|
|
- **Chief Complaint**
|
|
- **Symptoms & Observations**
|
|
- **Medical History**
|
|
- **Clinical Assessment**
|
|
- **Treatment Plan & Follow-up**
|
|
- Multi-tier LLM fallback pipeline (`llama-3.3-70b-versatile`, `openai/gpt-oss-120b`, `sarvam-105b-conversations`).
|
|
- **Real-Time Live Streaming & Batch Upload**:
|
|
- Live audio capture from the browser microphone streamed over WebSocket (16kHz mono linear16 PCM).
|
|
- Chunked file upload processing and full-file batch transcription.
|
|
- **Doctor & Patient Record Management**:
|
|
- Local database storage for consultation sessions, doctor profiles, and patient history.
|
|
|
|
---
|
|
|
|
## Architecture Overview
|
|
|
|
```
|
|
+-------------------------------------------------------------------------+
|
|
| Web Browser |
|
|
| - Live Microphone Audio (16kHz Mono PCM) |
|
|
| - Audio File Uploads (WAV/MP3) |
|
|
| - Real-time Transcript Viewer & SOAP Summary Display |
|
|
+-------------------------------------------------------------------------+
|
|
| ^
|
|
WebSocket / HTTP POST | Server Responses
|
|
v |
|
|
+-------------------------------------------------------------------------+
|
|
| Node.js Backend (server.js) |
|
|
| - Express HTTP Endpoints & WebSocket Server Proxy |
|
|
| - Audio Chunking & Format Normalization |
|
|
| - Multi-model LLM Fallback Coordinator |
|
|
+-------------------------------------------------------------------------+
|
|
| |
|
|
(Speech & Diarization) (LLM Inference)
|
|
v v
|
|
+-------------------------------+ +---------------------------+
|
|
| Sarvam AI APIs | | Groq APIs |
|
|
| - saaras:v4-realtime (WS STT) | | - llama-3.3-70b-versatile |
|
|
| - Batch Audio Diarization | | - openai/gpt-oss-120b |
|
|
| - Mayura v1 (Translation) | | - Clinical Summarization |
|
|
| - Sarvam 105B LLM | +---------------------------+
|
|
+-------------------------------+
|
|
```
|
|
|
|
---
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
.
|
|
├── server.js # Main Express & WebSocket backend server
|
|
├── package.json # Node.js project manifest & dependencies
|
|
├── .env.example # Environment variables template
|
|
├── .gitignore # Git ignore rules for clean repository
|
|
├── db.js # Consultation, doctor, and patient data manager
|
|
├── hybrid_diarization.py # Python hybrid speaker diarization helper
|
|
├── public/ # Web application frontend assets
|
|
│ ├── index.html # Clinical assistant dashboard UI
|
|
│ ├── app.js # Microphone capture, WebSocket client, UI logic
|
|
│ ├── style.css # Modern clinical UI styles & themes
|
|
│ ├── logo.png # Application branding logo
|
|
│ └── favicon.png # Browser favicon
|
|
├── backend/ # Optional standalone Python/FastAPI service
|
|
│ ├── main.py # Starlette/FastAPI implementation
|
|
│ ├── diarization_pipeline.py # Voice embeddings & diarization logic
|
|
│ ├── requirements.txt # Python backend dependencies
|
|
│ └── .env.example # Python backend environment template
|
|
└── data/ # Data directory
|
|
└── db.json # Seed database for patients & consultations
|
|
```
|
|
|
|
---
|
|
|
|
## Prerequisites
|
|
|
|
Before setting up the project, make sure you have:
|
|
|
|
- **Node.js**: `v18.0.0` or higher ([Download Node.js](https://nodejs.org/))
|
|
- **npm**: `v9.0.0` or higher (bundled with Node.js)
|
|
- **Sarvam AI API Key**: Required for STT, translation, and diarization ([Get API Key](https://dashboard.sarvam.ai))
|
|
- **Groq API Key** *(Optional)*: Recommended for high-speed clinical summary generation ([Get API Key](https://console.groq.com))
|
|
- *(Optional)* **Python**: `3.10+` if running the hybrid diarization module or FastAPI backend
|
|
|
|
---
|
|
|
|
## Quick Start & Installation
|
|
|
|
### 1. Clone the Repository
|
|
|
|
```bash
|
|
git clone https://gitea.aiindia.ai/AyushS/Doctor_Patient_Summary.git
|
|
cd Doctor_Patient_Summary
|
|
```
|
|
|
|
### 2. Configure Environment Variables
|
|
|
|
Copy the example configuration file and enter your API keys:
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
```
|
|
|
|
Open `.env` in any text editor and configure your credentials:
|
|
|
|
```env
|
|
# Sarvam AI API Key (Required)
|
|
SARVAM_API_KEY=sk_your_sarvam_api_key_here
|
|
|
|
# Sarvam Chat Model (Default: sarvam-105b-conversations)
|
|
SARVAM_CHAT_MODEL=sarvam-105b-conversations
|
|
|
|
# Groq API Key (Recommended for fast clinical summary generation)
|
|
GROQ_API_KEY=gsk_your_groq_api_key_here
|
|
|
|
# Server Port (Default: 3000)
|
|
PORT=3000
|
|
```
|
|
|
|
### 3. Install Dependencies
|
|
|
|
Install the required Node.js packages:
|
|
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
### 4. Start the Application
|
|
|
|
Start the server:
|
|
|
|
```bash
|
|
# Production mode
|
|
npm start
|
|
|
|
# Development mode (with live reload)
|
|
npm run dev
|
|
```
|
|
|
|
### 5. Access the Web Application
|
|
|
|
Open your browser and navigate to:
|
|
|
|
```
|
|
http://localhost:3000
|
|
```
|
|
|
|
---
|
|
|
|
## Audio Format Requirements
|
|
|
|
When streaming or uploading audio directly:
|
|
- **Encoding**: 16-bit linear PCM (`audio/wav`)
|
|
- **Sample Rate**: 16,000 Hz (16 kHz)
|
|
- **Channels**: 1 (Mono)
|
|
- The frontend microphone capture automatically records and downsamples to 16kHz mono PCM before sending to the backend.
|
|
|
|
---
|
|
|
|
## API Endpoints Reference
|
|
|
|
### HTTP REST Endpoints
|
|
|
|
| Method | Endpoint | Description |
|
|
|---|---|---|
|
|
| `GET` | `/api/health` | Healthcheck endpoint reporting service status. |
|
|
| `POST` | `/api/transcribe` | Batch transcription with Sarvam native speaker diarization. |
|
|
| `POST` | `/api/transcribe-chunk` | Transcribes audio chunks with automatic English translation. |
|
|
| `POST` | `/api/hybrid-transcribe` | Hybrid acoustic + Sarvam STT transcription pipeline. |
|
|
| `POST` | `/api/diarize` | NLP-based speaker role classification (Doctor vs. Patient). |
|
|
| `POST` | `/api/summary` | Generates a structured clinical SOAP summary from transcripts. |
|
|
|
|
### WebSocket Endpoint
|
|
|
|
| Endpoint | Protocol | Description |
|
|
|---|---|---|
|
|
| `/ws/transcribe` | `WSS / WS` | Full-duplex real-time audio streaming proxy to Sarvam AI. |
|
|
|
|
---
|
|
|
|
## How It Works
|
|
|
|
1. **Audio Ingestion**:
|
|
- The user records live via the microphone or uploads an audio file of a doctor-patient conversation.
|
|
2. **Translation & Diarization**:
|
|
- Speech chunks are transcribed by Sarvam AI in `translate` mode, ensuring Marathi, Hindi, or English speech is accurately rendered in English.
|
|
- Speaker diarization partitions the audio timeline into individual speaker turns.
|
|
3. **Role Classification**:
|
|
- The conversational turns are analyzed by an LLM prompt trained to identify medical inquiries and clinical advice from patient complaints.
|
|
4. **Clinical SOAP Summary**:
|
|
- The complete diarized dialogue is passed to the clinical summarization engine, producing structured records ready for medical documentation.
|
|
|
|
---
|
|
|
|
## Troubleshooting
|
|
|
|
- **Microphone Access Denied**: Ensure browser permissions allow microphone input for `http://localhost:3000`.
|
|
- **401 Unauthorized from Sarvam**: Verify that `SARVAM_API_KEY` in `.env` is valid and active.
|
|
- **Summary Generation Timeout**: If Groq is not configured, ensure `SARVAM_API_KEY` has quota for Sarvam chat completions. Adding a `GROQ_API_KEY` provides ultra-fast summary fallbacks.
|
|
|
|
---
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License.
|