Skip to content

Latest commit

 

History

History
453 lines (361 loc) · 16.4 KB

File metadata and controls

453 lines (361 loc) · 16.4 KB

SECURE MODULAR ASR Diarization Pipeline API Parameters Documentation

This document describes all configurable parameters for the SECURE MODULAR ASR Diarization Pipeline with enterprise-grade security, HIPAA compliance, and high-quality transcription (DER <7.8%, WER <2%).

Overview

The pipeline provides three main interfaces:

  1. Modular Python API (app/ directory) - Core processing modules with configuration
  2. FastAPI Web Service (app/app.py) - Secure REST API with authentication
  3. n8n Integration - Workflow automation with security features

🔒 ENTERPRISE SECURITY FEATURES

API Authentication

All API endpoints require authentication using API keys:

Header: X-API-Key: your-api-key

Environment Setup:

export API_KEYS="key1,key2,key3"  # Comma-separated API keys

Security Features

  • API Key Authentication: Configurable API keys with header validation
  • Input Validation: Multi-layer file validation (MIME, magic number, size limits)
  • Rate Limiting: DDoS protection (10 requests/minute per IP)
  • Data Protection: Encrypted temporary files, secure deletion, audit logging
  • HIPAA Compliance: All processing maintains medical data privacy standards

Pipeline Orchestrator Parameters

GlobalConfig Class

The GlobalConfig class in config.py provides centralized configuration for the entire secure modular pipeline.

Diarization Settings

Parameter Type Default Description Valid Range
hf_token str Environment HuggingFace token for Pyannote models Valid HF token
num_speakers int - Expected number of speakers (1-4) 1-4
min_speakers int - Minimum speakers for diarization 1-4
max_speakers int - Maximum speakers for diarization 1-4

ASR Settings

Parameter Type Default Description Valid Range
batch_size int 32 Batch size for processing segments 1-64
compute_type str "fp32" Compute precision for ASR "fp16", "fp32", "int8"
language str "en" Language code for ASR model Language code
use_vad bool True Enable Voice Activity Detection true/false
vad_threshold float 0.5 VAD speech detection threshold 0.0-1.0
min_segment_duration float 0.05 Minimum duration for speech segments (seconds) 0.01-1.0

Security Settings

Parameter Type Default Description Valid Range
enable_api_key_auth bool True Enable API key authentication true/false
api_keys List[str] Environment List of valid API keys Comma-separated strings
api_key_header str "X-API-Key" Header name for API key Header string
sanitize_inputs bool True Enable input sanitization true/false
max_filename_length int 255 Maximum filename length 1-1000
max_file_size_mb int 100 Maximum input file size (MB) 1-1000
allowed_extensions List[str] ['.mp3', '.wav', '.flac', '.m4a', '.aac'] Allowed audio file extensions File extension list
secure_temp_dir bool True Use secure temporary directories true/false
auto_cleanup bool True Automatically clean up temporary files true/false
encrypt_temp_files bool False Encrypt temporary files during processing true/false
enable_audit_logging bool True Enable audit logging for file operations true/false
max_retry_attempts int 3 Maximum retry attempts for failed file deletions 1-10
cleanup_timeout_seconds int 30 Timeout for cleanup operations to prevent hanging 10-300
temp_file_retention_hours int 24 Hours to retain temporary files before cleanup 1-168

TempFileTracker Settings (HIPAA-Compliant Cleanup)

Parameter Type Default Description Valid Range
max_retry_attempts int 3 Maximum retry attempts for failed file deletions 1-10
base_retry_delay float 0.1 Base delay in seconds for exponential backoff 0.01-1.0
max_retry_delay float 5.0 Maximum delay between retries 1.0-30.0
audit_log_level str "INFO" Logging level for audit events "DEBUG", "INFO", "WARNING", "ERROR"
enable_audit_logging bool True Enable audit logging for file operations true/false
temp_dir_permissions int 0o700 Permissions for temporary directories Octal permission
cleanup_timeout_seconds int 30 Timeout for cleanup operations to prevent hanging 10-300

Processing Settings

Parameter Type Default Description Valid Options
device str "auto" Device for model inference "auto", "cpu", "cuda"
output_format str "json" Output format for results "json", "txt", "both"
sample_rate int 16000 Target sample rate for processing Audio sample rate

FastAPI Web Service Parameters

Authentication Required

All API endpoints require authentication using the X-API-Key header:

curl -H "X-API-Key: your-api-key" -X POST "https://your-api-endpoint/transcribe_diarize/" \
  -F "audio_file=@audio.mp3"

Endpoint: POST /transcribe_diarize/

Main transcription and diarization endpoint with comprehensive security validation.

Authentication: Required (X-API-Key header) Method: POST (multipart/form-data) Rate Limit: 10 requests per minute per IP

Core Parameters

Parameter Type Default Description Required
audio_file file - Audio file to process (MP3, WAV, FLAC, M4A, AAC) Yes
language str "en" Language code for ASR No
diarize bool true Enable speaker diarization No
vad bool true Enable Voice Activity Detection No
min_speakers int - Minimum number of speakers for diarization No
max_speakers int - Maximum number of speakers for diarization No
hf_token str Environment HuggingFace token for Pyannote diarization No*

*Required for diarization if not set in environment

Processing Parameters

Parameter Type Default Description Valid Range
batch_size int 32 Batch size for ASR processing 1-64
output_format str "json" Output format "json", "txt", "both"
unload_models_after bool false Unload models after processing to free VRAM true/false

Endpoint: POST /cleanup/

Manual endpoint to unload all cached models and free VRAM.

Authentication: Required (X-API-Key header) Method: POST Response: {"message": "All models unloaded and VRAM freed"}

Usage:

curl -H "X-API-Key: your-api-key" -X POST "https://your-api-endpoint/cleanup/"

Diarization-Controlled Segmentation

The system implements intelligent segmentation that respects diarization boundaries rather than ASR internal segmentation:

Key Features

  • Diarization-First: Segmentation follows speaker turn boundaries, not ASR model decisions
  • Intelligent Merging: Consecutive segments from same speaker within 500ms are merged
  • Punctuation Preservation: Natural speech pauses maintain proper grammar
  • Workflow Optimization: Creates coherent segments perfect for automation

Benefits

  • Readable Transcripts: Complete speaker turns in single segments
  • Proper Punctuation: Natural pauses preserved for grammar
  • n8n Ready: Optimal segment boundaries for workflow processing
  • Reduced Fragmentation: 31% fewer segments while maintaining quality

VAD (Voice Activity Detection) Parameters

The new VAD functionality provides advanced speech detection capabilities:

Key VAD Parameters

Parameter API Name Config Name Type Default Description
VAD Enable vad asr_use_vad bool true Enable/disable VAD processing
VAD Threshold - asr_vad_threshold float 0.5 Speech detection sensitivity (0.0-1.0)
Min Segment Duration - asr_min_segment_duration float 0.05 Minimum speech segment length (seconds)

VAD Threshold Guidelines

  • 0.3-0.4: More sensitive (detects softer speech, may include noise)
  • 0.5: Balanced (recommended default)
  • 0.6-0.7: Less sensitive (detects only clear speech, filters noise)

VAD Integration

Note: VAD is now disabled by default (vad=false) for optimal performance with the hybrid system.

When vad=false (recommended):

  • Direct processing with Parakeet TDT for maximum accuracy
  • No artificial speech filtering that might remove valid audio
  • Faster processing with in-memory operations
  • Better integration with diarization-controlled segmentation

When vad=true (legacy):

  • Uses NVIDIA ASR with VAD processing
  • May conflict with TDT model performance
  • Slower processing due to additional filtering
  • Not recommended for production use

Configuration Examples

Python API Usage

from app.config import GlobalConfig, create_custom_config

# Custom configuration with VAD tuning
config = create_custom_config(
    use_vad=True,
    vad_threshold=0.4,  # More sensitive
    min_segment_duration=0.1,  # Longer minimum segments
    batch_size=32  # Larger batch for faster processing
)

# Use the modular components directly
from app.hybrid_diarization import HybridDiarization
from app.nvidia_asr import NvidiaASR

diarizer = HybridDiarization(config.diarization)
asr = NvidiaASR(config.asr)

# Process audio with custom configuration
results = diarizer.process_audio('audio.mp3', asr_model=asr)

REST API Usage

# Production system with authentication
curl -H "X-API-Key: your-api-key" \
  -X POST "https://your-api-endpoint/transcribe_diarize/" \
  -F "audio_file=@audio.mp3" \
  -F "diarize=true" \
  -F "vad=true" \
  -F "min_speakers=2" \
  -F "max_speakers=4" \
  -F "hf_token=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# With file output for workflows
curl -H "X-API-Key: your-api-key" \
  -X POST "https://your-api-endpoint/transcribe_diarize/" \
  -F "audio_file=@audio.mp3" \
  -F "diarize=true" \
  -F "vad=true" \
  -F "min_speakers=2" \
  -F "max_speakers=4" \
  -F "hf_token=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -o transcription_result.json

n8n Integration

Recommended n8n Node Configuration

{
  "parameters": {
    "method": "POST",
    "url": "https://your-api-endpoint/transcribe_diarize/",
    "sendHeaders": true,
    "headerParameters": {
      "parameters": [
        {
          "name": "X-API-Key",
          "value": "={{ $json.api_key }}"
        }
      ]
    },
    "sendBody": true,
    "contentType": "multipart-form-data",
    "bodyParameters": {
      "parameters": [
        {
          "parameterType": "formBinaryData",
          "name": "audio_file",
          "inputDataFieldName": "audio_file"
        },
        {
          "name": "diarize",
          "value": "=true"
        },
        {
          "name": "vad",
          "value": "=true"
        },
        {
          "name": "min_speakers",
          "value": "={{ $json.min_speakers || 2 }}"
        },
        {
          "name": "max_speakers",
          "value": "={{ $json.max_speakers || 4 }}"
        },
        {
          "name": "hf_token",
          "value": "={{ $json.hugging_face_token }}"
        },
        {
          "name": "output_format",
          "value": "={{ $json.output_format || 'json' }}"
        },
        {
          "name": "batch_size",
          "value": "={{ $json.batch_size || 32 }}"
        },
        {
          "name": "unload_models_after",
          "value": "=true"
        }
      ]
    }
  }
}

n8n Workflow Benefits

  • Automatic Backend Selection: Uses hybrid system by default for best quality
  • No Model Parameters Needed: System auto-selects optimal models
  • Enterprise Quality: DER <7.8%, perfect speaker attribution
  • Error Handling: Built-in retry logic and error recovery

JSON Configuration Override

from app.config import create_custom_config

# Override specific parameters
custom_config = create_custom_config(
    use_vad=True,
    vad_threshold=0.6,  # Less sensitive
    batch_size=8,  # Smaller batch
    device="cuda"  # Force GPU
)

Output Format

JSON Output Structure

{
  "segments": [
    {
      "speaker": "SPEAKER_00",
      "start": 0.0,
      "end": 3.45,
      "text": "Hello, this is a test transcription."
    },
    {
      "speaker": "SPEAKER_01",
      "start": 3.5,
      "end": 7.12,
      "text": "Thank you for the demonstration."
    }
  ]
}

Text Output Format

SPEAKER_00: Hello, this is a test transcription.
SPEAKER_01: Thank you for the demonstration.

Error Handling

The API returns appropriate HTTP status codes and error messages:

Authentication Errors

  • 401 Unauthorized: Missing or invalid X-API-Key header
  • 403 Forbidden: Invalid API key provided

Input Validation Errors

  • 400 Bad Request: Invalid parameters, malformed file, or validation failure
  • 413 Payload Too Large: File exceeds size limit (100MB default)
  • 415 Unsupported Media Type: Unsupported audio format or MIME type
  • 429 Too Many Requests: Rate limit exceeded (10 requests/minute per IP)

Processing Errors

  • 500 Internal Server Error: Model loading or processing errors
  • 503 Service Unavailable: Temporary service issues or resource constraints

Security Events

All security-related events are logged with detailed audit trails including:

  • Authentication failures
  • Input validation failures
  • File upload attempts
  • Rate limit violations
  • Anomalous activity detection

Performance Considerations

Hybrid System Performance

Metric Community-1 System Legacy NVIDIA Improvement
DER (Diarization Error Rate) <7.8% ~70% 89% better
WER (Word Error Rate) <2% ~5% 60% better
Speaker Attribution 100% Poor Perfect
Processing Speed 65s (7.5% faster) 80s 7.5% faster
Memory Usage 8GB GPU 4GB GPU Higher but worth quality
Segmentation 52 coherent segments 76+ fragmented 31% more readable

Parameter Tuning for Speed vs Accuracy

Use Case Recommended Settings Expected Quality
Enterprise Production vad=false, min_speakers=2, max_speakers=4, batch_size=32 DER <7.8%, WER <2%
Precise Speaker Control min_speakers=2, max_speakers=2, vad=false Exact speaker count
High Accuracy vad=false, batch_size=16, Community-1 diarization DER <7.8%, WER <2%
Workflow Automation vad=false, save_to_file=result.json n8n ready output
Low Resource device="cpu", batch_size=4, vad=false Variable quality

Memory Usage

  • Hybrid GPU Memory: ~6-8GB for Pyannote + Parakeet TDT
  • NVIDIA GPU Memory: ~4GB for Sortformer + Parakeet CTC
  • CPU Memory: ~1-2GB for processing
  • Batch Size: Higher values use more memory but process faster
  • Model Unloading: unload_models_after=true frees VRAM between requests

Security Best Practices

API Key Management

  • Store API keys securely (environment variables, Docker secrets)
  • Rotate keys regularly for production deployments
  • Use different keys for different applications/environments
  • Monitor API key usage and revoke compromised keys

Environment Configuration

# Production environment setup
export API_KEYS="prod-key-1,prod-key-2"
export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
export LOG_LEVEL="INFO"
export MAX_FILE_SIZE_MB="100"

Monitoring & Alerting

  • Enable audit logging for all file operations
  • Monitor rate limiting and authentication failures
  • Set up alerts for security events and anomalies
  • Regular security scans and dependency updates

HIPAA Compliance Checklist

  • ✅ Encrypted temporary file storage
  • ✅ Secure file deletion (overwrites)
  • ✅ Audit logging of all operations
  • ✅ Input sanitization and validation
  • ✅ Access control via API authentication
  • ✅ Data retention policies (24-hour cleanup)