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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# πŸš€ NuralVoiceSTT API Documentation

**Developed by Blink Digital**

Complete API documentation for integrating NuralVoiceSTT into your applications.

## πŸ“‘ API Endpoints

### WebSocket API (Real-time Streaming)

**Endpoint:** `wss://ashishkblink-NuralVoice.hf.space/ws/transcribe`

**Protocol:** WebSocket (WSS for secure connection)

**Best for:** Real-time audio streaming, live transcription, low-latency applications

---

## 🎯 Quick Start

### Prerequisites

- Node.js 14+ installed
- WebSocket library (`ws` package)
- Audio capture capability (microphone or audio file)

### Installation

```bash
npm install ws
```

---

## πŸ“ Node.js Examples

### Example 1: Real-time Microphone Streaming

```javascript
const WebSocket = require('ws');
const { spawn } = require('child_process');

// WebSocket URL
const WS_URL = 'wss://ashishkblink-NuralVoice.hf.space/ws/transcribe';

// Connect to WebSocket
const ws = new WebSocket(WS_URL);

ws.on('open', () => {
    console.log('βœ… Connected to NuralVoiceSTT API');
    
    // Start recording from microphone using arecord (Linux) or sox (macOS/Linux)
    // For macOS, you might need: brew install sox
    const recorder = spawn('sox', [
        '-d',                    // Default audio device (microphone)
        '-t', 'raw',             // Raw audio format
        '-r', '16000',           // Sample rate: 16kHz
        '-c', '1',               // Channels: mono
        '-b', '16',              // Bit depth: 16-bit
        '-e', 'signed-integer',  // Encoding
        '-'                      // Output to stdout
    ]);
    
    // Send audio chunks to WebSocket
    recorder.stdout.on('data', (chunk) => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send(chunk);
        }
    });
    
    recorder.on('error', (error) => {
        console.error('Recording error:', error);
    });
    
    // Stop recording after 10 seconds (example)
    setTimeout(() => {
        recorder.kill();
        ws.send(JSON.stringify({ action: 'stop' }));
    }, 10000);
});

ws.on('message', (data) => {
    try {
        const message = JSON.parse(data.toString());
        
        if (message.status === 'connected') {
            console.log('πŸ“‘ Ready:', message.message);
        } else if (message.text) {
            if (message.is_final) {
                console.log('βœ… Final:', message.text);
            } else if (message.is_partial) {
                console.log('⏳ Partial:', message.text);
            } else {
                console.log('πŸ“ Text:', message.text);
            }
        } else if (message.error) {
            console.error('❌ Error:', message.error);
        }
    } catch (e) {
        console.error('Parse error:', e);
    }
});

ws.on('error', (error) => {
    console.error('WebSocket error:', error);
});

ws.on('close', () => {
    console.log('πŸ”Œ Disconnected from API');
});
```

### Example 2: Audio File Transcription

```javascript
const WebSocket = require('ws');
const fs = require('fs');

const WS_URL = 'wss://ashishkblink-NuralVoice.hf.space/ws/transcribe';
const AUDIO_FILE = 'audio.wav'; // Your audio file path

// Connect to WebSocket
const ws = new WebSocket(WS_URL);

let transcription = '';

ws.on('open', () => {
    console.log('βœ… Connected to NuralVoiceSTT API');
    
    // Read audio file
    const audioBuffer = fs.readFileSync(AUDIO_FILE);
    
    // Convert to 16-bit PCM if needed
    // Note: This assumes the file is already in 16kHz, 16-bit, mono PCM format
    // You may need to convert your audio file first using ffmpeg:
    // ffmpeg -i input.mp3 -ar 16000 -ac 1 -f s16le output.raw
    
    // Send audio in chunks (4000 bytes = ~0.25 seconds at 16kHz)
    const chunkSize = 4000;
    let offset = 0;
    
    const sendChunk = () => {
        if (offset < audioBuffer.length && ws.readyState === WebSocket.OPEN) {
            const chunk = audioBuffer.slice(offset, offset + chunkSize);
            ws.send(chunk);
            offset += chunkSize;
            
            // Send next chunk after a small delay
            setTimeout(sendChunk, 100);
        } else {
            // All chunks sent, request final result
            ws.send(JSON.stringify({ action: 'stop' }));
        }
    };
    
    sendChunk();
});

ws.on('message', (data) => {
    try {
        const message = JSON.parse(data.toString());
        
        if (message.text) {
            if (message.is_final) {
                transcription += ' ' + message.text;
                console.log('βœ… Final transcription:', transcription.trim());
            } else if (message.is_partial) {
                console.log('⏳ Partial:', message.text);
            }
        } else if (message.error) {
            console.error('❌ Error:', message.error);
        }
    } catch (e) {
        // Handle binary data or other formats
    }
});

ws.on('close', () => {
    console.log('\nπŸ“„ Complete Transcription:');
    console.log(transcription.trim());
});
```

### Example 3: Browser Audio Streaming (Node.js Server Proxy)

```javascript
// server.js - Node.js server that proxies browser audio to HF Space
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const cors = require('cors');

const app = express();
app.use(cors());
app.use(express.json());

const server = http.createServer(app);
const wss = new WebSocket.Server({ server, path: '/ws' });

const HF_WS_URL = 'wss://ashishkblink-NuralVoice.hf.space/ws/transcribe';

wss.on('connection', (clientWs) => {
    console.log('βœ… Client connected');
    
    // Connect to HF Space WebSocket
    const hfWs = new WebSocket(HF_WS_URL);
    
    hfWs.on('open', () => {
        console.log('βœ… Connected to HF Space');
        clientWs.send(JSON.stringify({ 
            type: 'status', 
            message: 'Connected to STT service' 
        }));
    });
    
    // Forward audio from client to HF Space
    clientWs.on('message', (data) => {
        if (hfWs.readyState === WebSocket.OPEN) {
            // If data is JSON, parse it
            try {
                const message = JSON.parse(data.toString());
                if (message.type === 'audio') {
                    // Convert array to buffer
                    const buffer = Buffer.from(message.data);
                    hfWs.send(buffer);
                } else if (message.action === 'stop') {
                    hfWs.send(JSON.stringify({ action: 'stop' }));
                }
            } catch (e) {
                // Binary data - send directly
                hfWs.send(data);
            }
        }
    });
    
    // Forward transcription from HF Space to client
    hfWs.on('message', (data) => {
        try {
            const message = JSON.parse(data.toString());
            clientWs.send(JSON.stringify({
                type: 'transcription',
                text: message.text || '',
                isFinal: message.is_final || false,
                isPartial: message.is_partial || false
            }));
        } catch (e) {
            // Handle non-JSON messages
        }
    });
    
    hfWs.on('error', (error) => {
        console.error('HF WebSocket error:', error);
        clientWs.send(JSON.stringify({ 
            type: 'error', 
            message: error.message 
        }));
    });
    
    clientWs.on('close', () => {
        hfWs.close();
        console.log('❌ Client disconnected');
    });
});

const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
    console.log(`πŸš€ Server running on port ${PORT}`);
    console.log(`πŸ“‘ WebSocket: ws://localhost:${PORT}/ws`);
});
```

### Example 4: Complete Client-Server Application

```javascript
// client-example.js - Complete example with error handling
const WebSocket = require('ws');

class NuralVoiceClient {
    constructor(wsUrl = 'wss://ashishkblink-NuralVoice.hf.space/ws/transcribe') {
        this.wsUrl = wsUrl;
        this.ws = null;
        this.isConnected = false;
        this.transcription = '';
        this.onTranscription = null;
        this.onError = null;
    }
    
    connect() {
        return new Promise((resolve, reject) => {
            this.ws = new WebSocket(this.wsUrl);
            
            this.ws.on('open', () => {
                this.isConnected = true;
                console.log('βœ… Connected to NuralVoiceSTT');
                resolve();
            });
            
            this.ws.on('message', (data) => {
                try {
                    const message = JSON.parse(data.toString());
                    
                    if (message.status === 'connected') {
                        console.log('πŸ“‘ Ready:', message.message);
                    } else if (message.text) {
                        if (message.is_final) {
                            this.transcription += ' ' + message.text;
                            if (this.onTranscription) {
                                this.onTranscription(message.text, true);
                            }
                        } else if (message.is_partial) {
                            if (this.onTranscription) {
                                this.onTranscription(message.text, false);
                            }
                        }
                    } else if (message.error) {
                        console.error('❌ Error:', message.error);
                        if (this.onError) {
                            this.onError(message.error);
                        }
                    }
                } catch (e) {
                    console.error('Parse error:', e);
                }
            });
            
            this.ws.on('error', (error) => {
                this.isConnected = false;
                if (this.onError) {
                    this.onError(error.message);
                }
                reject(error);
            });
            
            this.ws.on('close', () => {
                this.isConnected = false;
                console.log('πŸ”Œ Disconnected');
            });
        });
    }
    
    sendAudio(audioBuffer) {
        if (this.ws && this.isConnected && this.ws.readyState === WebSocket.OPEN) {
            this.ws.send(audioBuffer);
            return true;
        }
        return false;
    }
    
    stop() {
        if (this.ws && this.isConnected) {
            this.ws.send(JSON.stringify({ action: 'stop' }));
        }
    }
    
    close() {
        if (this.ws) {
            this.ws.close();
        }
    }
    
    getTranscription() {
        return this.transcription.trim();
    }
}

// Usage example
async function main() {
    const client = new NuralVoiceClient();
    
    client.onTranscription = (text, isFinal) => {
        if (isFinal) {
            console.log('βœ… Final:', text);
        } else {
            console.log('⏳ Partial:', text);
        }
    };
    
    client.onError = (error) => {
        console.error('Error:', error);
    };
    
    try {
        await client.connect();
        
        // Send audio chunks (example)
        // In real usage, you'd get audio from microphone or file
        const audioChunk = Buffer.alloc(4000); // Example chunk
        client.sendAudio(audioChunk);
        
        // Stop after some time
        setTimeout(() => {
            client.stop();
            console.log('πŸ“„ Complete:', client.getTranscription());
            client.close();
        }, 5000);
        
    } catch (error) {
        console.error('Connection failed:', error);
    }
}

// Uncomment to run
// main();
```

---

## πŸ“‹ API Protocol

### Connection

1. **Connect** to `wss://ashishkblink-NuralVoice.hf.space/ws/transcribe`
2. **Wait** for connection confirmation message
3. **Send** audio data as binary (16-bit PCM, 16kHz, mono)
4. **Receive** transcription results as JSON

### Audio Format Requirements

- **Sample Rate:** 16,000 Hz (16kHz)
- **Channels:** Mono (1 channel)
- **Bit Depth:** 16-bit
- **Encoding:** Signed integer PCM
- **Format:** Raw binary data (no headers)

### Converting Audio Files

Use `ffmpeg` to convert audio files to the required format:

```bash
# Convert MP3 to required format
ffmpeg -i input.mp3 -ar 16000 -ac 1 -f s16le output.raw

# Convert WAV to required format
ffmpeg -i input.wav -ar 16000 -ac 1 -f s16le output.raw

# Record from microphone directly
ffmpeg -f avfoundation -i ":0" -ar 16000 -ac 1 -f s16le output.raw
```

### Message Format

#### Client β†’ Server (Send Audio)

Send raw binary audio data (16-bit PCM):
```javascript
ws.send(audioBuffer); // Buffer containing 16-bit PCM audio
```

Send stop command:
```javascript
ws.send(JSON.stringify({ action: 'stop' }));
```

#### Server β†’ Client (Receive Transcription)

**Status Message:**
```json
{
  "status": "connected",
  "message": "Ready to receive audio. Send 16-bit PCM mono audio at 16kHz sample rate.",
  "sample_rate": 16000
}
```

**Partial Transcription:**
```json
{
  "text": "hello world",
  "is_final": false,
  "is_partial": true
}
```

**Final Transcription:**
```json
{
  "text": "hello world",
  "is_final": true,
  "words": [
    {
      "word": "hello",
      "start": 0.5,
      "end": 1.2,
      "conf": 0.95
    },
    {
      "word": "world",
      "start": 1.3,
      "end": 2.0,
      "conf": 0.92
    }
  ]
}
```

**Error Message:**
```json
{
  "error": "Error description",
  "status": "error"
}
```

---

## πŸ”§ Integration Examples

### Express.js Server

```javascript
const express = require('express');
const WebSocket = require('ws');
const http = require('http');

const app = express();
const server = http.createServer(app);

// WebSocket endpoint
const wss = new WebSocket.Server({ server, path: '/api/transcribe' });

wss.on('connection', (ws) => {
    const hfWs = new WebSocket('wss://ashishkblink-NuralVoice.hf.space/ws/transcribe');
    
    ws.on('message', (data) => {
        if (hfWs.readyState === WebSocket.OPEN) {
            hfWs.send(data);
        }
    });
    
    hfWs.on('message', (data) => {
        ws.send(data);
    });
});

server.listen(3000, () => {
    console.log('Server running on port 3000');
});
```

### React Integration

```javascript
// In your React component
import { useEffect, useRef, useState } from 'react';

function SpeechToText() {
    const [transcription, setTranscription] = useState('');
    const wsRef = useRef(null);
    
    useEffect(() => {
        const ws = new WebSocket('wss://ashishkblink-NuralVoice.hf.space/ws/transcribe');
        wsRef.current = ws;
        
        ws.onmessage = (event) => {
            const data = JSON.parse(event.data);
            if (data.text) {
                setTranscription(prev => prev + ' ' + data.text);
            }
        };
        
        return () => ws.close();
    }, []);
    
    const sendAudio = (audioBuffer) => {
        if (wsRef.current?.readyState === WebSocket.OPEN) {
            wsRef.current.send(audioBuffer);
        }
    };
    
    return <div>{transcription}</div>;
}
```

---

## ⚠️ Important Notes

1. **Rate Limiting:** Be mindful of API usage. Don't send too many requests simultaneously.

2. **Connection Management:** Always close WebSocket connections when done to free resources.

3. **Error Handling:** Implement proper error handling for network issues and API errors.

4. **Audio Quality:** Better audio quality = better transcription accuracy. Use noise reduction when possible.

5. **Latency:** WebSocket provides low-latency streaming. For best results, send audio in small chunks (2000-4000 bytes).

---

## πŸ› Troubleshooting

### Connection Refused
- Check if the Space is running
- Verify the WebSocket URL is correct
- Ensure you're using `wss://` (secure WebSocket)

### No Transcription
- Verify audio format (16kHz, 16-bit, mono PCM)
- Check if audio is being sent correctly
- Ensure WebSocket connection is open

### Poor Accuracy
- Use better quality audio
- Reduce background noise
- Speak clearly and at moderate pace

---

## πŸ“ž Support

For issues or questions:
- Check the [Space page](https://huggingface.co/spaces/ashishkblink/NuralVoice)
- Review error messages in WebSocket responses
- Ensure your audio format matches requirements

---

**Developed by Blink Digital** | [Model Repository](https://huggingface.co/ashishkblink/NuralVoiceSTT)