File size: 10,423 Bytes
54fb37d | 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 | import base64
import datetime
import io
import logging
import os
import numpy as np
import soundfile as sf
from faster_whisper import WhisperModel
from typing import Dict, Any, Union
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class EndpointHandler:
def __init__(self, path=""):
"""
Initialize the endpoint handler.
Args:
path: Path to the model directory. In Hugging Face Inference Endpoints,
this will be the directory containing the model files.
"""
logger.info("Initializing EndpointHandler")
if os.environ.get("LOG_DIAGNOSTICS") == "true":
self.__log_diagnostics__()
cudaIsAvailable = os.environ.get("CUDA_VISIBLE_DEVICES")
if cudaIsAvailable:
logger.info("CUDA is available")
else:
logger.info("CUDA is not available, using CPU")
# Load the model with fallback to CPU if CUDA fails
device = "cuda" if cudaIsAvailable else "cpu"
try:
# First attempt with CUDA if available
if device == "cuda":
logger.info("Attempting to load model with CUDA support")
self.model = WhisperModel(
path or ".",
compute_type="float16",
device="cuda",
)
logger.info("Model loaded successfully with CUDA")
else:
# CPU fallback
raise ValueError("CUDA not available, using CPU")
except Exception as e:
# Log the error and fall back to CPU
logger.warning(f"Error loading model with CUDA: {e}")
logger.info("Falling back to CPU model")
try:
self.model = WhisperModel(
path or ".",
compute_type="int8",
device="cpu",
)
logger.info("Model loaded successfully with CPU")
except Exception as cpu_err:
logger.error(f"Error loading CPU model: {cpu_err}")
raise
# Set default parameters
self.sampling_rate = 16000
logger.info("EndpointHandler initialized")
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Process a request.
Args:
data: Request data containing audio input and optional parameters.
Expected format:
- For batch processing: {"inputs": audio_data, ...parameters}
- For streaming: {"inputs": audio_chunk, "stream": true, "session_id": "unique_id", ...parameters}
Returns:
Transcription result or error message.
"""
logger.info("Processing request")
try:
# Extract inputs and parameters
if "inputs" not in data:
return {"error": "No inputs provided"}
inputs = data.pop("inputs")
parameters = data.pop("parameters", {})
# Process audio input
audio, sampling_rate = self._process_audio_input(inputs)
# Get transcription parameters
language = data.pop("language", "da") # Default to Danish
beam_size = data.pop("beam_size", 5)
return self.transcribe(
audio,
sampling_rate,
language,
beam_size,
**parameters,
)
except Exception as e:
logger.error(f"Error processing request: {e}")
return {"error": str(e)}
def _process_audio_input(self, inputs: Union[str, Dict[str, Any]]) -> tuple:
"""
Process audio input in various formats.
Args:
inputs: Audio input as base64 string, URL, or numpy array.
Returns:
Tuple of (audio_array, sampling_rate).
"""
# Handle different input formats
if isinstance(inputs, str):
logger.info("Received audio input as base64 encoded string")
# Base64 encoded audio
if inputs.startswith(("data:", "data%3A")):
# Handle data URI
if "base64," in inputs:
audio_b64 = inputs.split("base64,")[1]
else:
audio_b64 = inputs
# Decode base64
audio_bytes = base64.b64decode(audio_b64)
# Read audio data
with io.BytesIO(audio_bytes) as audio_io:
audio, sampling_rate = sf.read(audio_io)
return audio, sampling_rate
# URL or file path (not implemented for security reasons)
else:
raise ValueError("URL or file path inputs are not supported")
# Dictionary with audio data
elif isinstance(inputs, dict) and "audio" in inputs:
logger.info("Received audio input as dictionary")
if isinstance(inputs["audio"], list):
# Convert list to numpy array
audio = np.array(inputs["audio"], dtype=np.float32)
else:
# Assume it's already a numpy array
audio = inputs["audio"]
# Get sampling rate
sampling_rate = inputs.get("sampling_rate", self.sampling_rate)
return audio, sampling_rate
elif isinstance(inputs, bytes):
logger.info("Received raw bytes input")
# Read audio data from bytes
with io.BytesIO(inputs) as audio_io:
audio, sampling_rate = sf.read(audio_io)
return audio, sampling_rate
# Unsupported input format
else:
raise ValueError(f"Unsupported input format: {type(inputs)}")
def transcribe(
self,
audio: np.ndarray,
sampling_rate: int,
language: str,
beam_size: int,
**kwargs,
) -> Dict[str, Any]:
"""
Perform the transcription
Args:
audio: Audio data as numpy array.
sampling_rate: Sampling rate of the audio.
language: Language code.
beam_size: Beam size for transcription.
**kwargs: Additional parameters.
Returns:
Transcription result.
"""
logger.info(f"Batch transcription: {len(audio)} samples, {sampling_rate} Hz")
# Resample if needed
if sampling_rate != self.sampling_rate:
logger.warning(
f"Sampling rate mismatch: {sampling_rate} Hz vs {self.sampling_rate} Hz"
)
logger.info(f"Parameters: {kwargs}")
# Transcribe
# see parameters here: <https://github.com/SYSTRAN/faster-whisper/blob/master/faster_whisper/transcribe.py>
now = datetime.datetime.now()
segments, info = self.model.transcribe(
audio,
language=language,
beam_size=beam_size,
**kwargs,
)
logger.info(f"Transcription info: {info}")
# Format results
result = {
"text": "", # Full text will be populated below
"segments": [],
"language": info.language,
"language_probability": info.language_probability,
}
# Process segments
# Segments is a generator and the actual transcription is done in the loop below,
all_text = []
for segment in segments:
segment_info = {
"id": segment.id,
"text": segment.text,
"start": segment.start,
"end": segment.end,
"temperature": segment.temperature,
"avg_logprob": segment.avg_logprob,
"compression_ratio": segment.compression_ratio,
"no_speech_prob": segment.no_speech_prob,
}
if kwargs.get("word_timestamps", False):
# Add word timestamps if requested
segment_info["words"] = [
{
"word": word.word,
"start": word.start,
"end": word.end,
}
for word in segment.words
]
all_text.append(segment.text)
result["segments"].append(segment_info)
elapsed_time = datetime.datetime.now() - now
logger.info(f"Transcription time: {elapsed_time}")
logger.info(f"Segments: {len(result['segments'])}")
# Combine text from all segments
result["text"] = " ".join(all_text)
return result
def __log_diagnostics__(self):
"""
Log diagnostics information for debugging.
This includes CUDA availability, library paths, installed packages,
and environment variables.
Very useful as the HF endpoint runtime is rather secretive about its environment.
"""
logger.info("Logging environment diagnostics")
logger.info("LD_LIBRARY_PATH:")
if "LD_LIBRARY_PATH" in os.environ:
logger.info(os.environ["LD_LIBRARY_PATH"])
else:
logger.info("LD_LIBRARY_PATH not set")
# and the files
logger.info("LD_LIBRARY_PATH files:")
if "LD_LIBRARY_PATH" in os.environ:
for ld_path in os.environ["LD_LIBRARY_PATH"].split(":"):
if os.path.exists(ld_path):
logger.info(f" {ld_path}:")
for file in os.listdir(ld_path):
logger.info(f" {file}")
else:
logger.info(f" {ld_path} does not exist")
logger.info(f"Installed Python packages:")
import pkg_resources
for package in pkg_resources.working_set:
logger.info(f" {package.key}=={package.version}, {package.location}")
# dump environment variables
logger.info("Environment variables:")
for key, value in os.environ.items():
logger.info(f" {key}: {value}")
logger.info("NVIDIA environment:")
import subprocess
try:
result = subprocess.run(["nvidia-smi"], capture_output=True, text=True)
logger.info(result.stdout)
except Exception as e:
logger.warning(f"Could not run nvidia-smi: {e}")
|