Spaces:
Sleeping
Sleeping
File size: 13,438 Bytes
8d7b4b1 | 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 | from typing import Optional, Dict, Any
import hashlib
import io
import json
import time
import logging
import numpy as np
from fastapi import APIRouter, File, Form, HTTPException, BackgroundTasks, UploadFile
from src.api.schemas import AudioPayload, VideoPayload, TextPayload, MultimodalPayload, PredictionResponse, HealthResponse
from src.features.text_features import TextFeatureExtractor
from src.inference.inferencer import ModelV2Inferencer
router = APIRouter()
_inferencer: Optional[ModelV2Inferencer] = None
_text_extractor: Optional[TextFeatureExtractor] = None
_preprocessors: Dict[str, Any] = {}
logger = logging.getLogger(__name__)
# ββ Route-level prediction cache (TTL = 1 hour, max 200 entries) ββββββββββββββ
_pred_cache: dict = {}
_PRED_CACHE_TTL = 3600
_PRED_CACHE_MAX = 200
def _pred_cache_key(payload_dict: dict) -> str:
try:
raw = json.dumps(payload_dict, sort_keys=True, default=str)
return hashlib.sha256(raw.encode()).hexdigest()
except Exception:
return ""
def _pred_cache_get(key: str) -> Optional[dict]:
entry = _pred_cache.get(key)
if entry and (time.monotonic() - entry["ts"]) < _PRED_CACHE_TTL:
return entry["v"]
return None
def _pred_cache_set(key: str, value: dict) -> None:
_pred_cache[key] = {"v": value, "ts": time.monotonic()}
if len(_pred_cache) > _PRED_CACHE_MAX:
oldest = sorted(_pred_cache.items(), key=lambda x: x[1]["ts"])
for k, _ in oldest[: len(_pred_cache) - _PRED_CACHE_MAX]:
_pred_cache.pop(k, None)
def register_inferencer(
inferencer: ModelV2Inferencer,
text_extractor: Optional[TextFeatureExtractor] = None,
preprocessors: Optional[Dict[str, Any]] = None,
) -> None:
global _inferencer, _text_extractor, _preprocessors
_inferencer = inferencer
_text_extractor = text_extractor
if preprocessors:
_preprocessors = preprocessors
logger.info("Registered preprocessors for modalities: %s", list(preprocessors.keys()))
def _apply_preprocessor(features_list: Optional[list], modality: str) -> Optional[list]:
"""Apply normalizer + PCA to raw features using the training preprocessors."""
if not features_list or modality not in _preprocessors:
return features_list
preproc = _preprocessors[modality]
try:
arr = np.array(features_list, dtype=np.float32)
if arr.ndim == 1:
arr = arr.reshape(1, -1)
normalizer = preproc.get("normalizer") if isinstance(preproc, dict) else getattr(preproc, "normalizer", None)
pca = preproc.get("pca") if isinstance(preproc, dict) else getattr(preproc, "pca", None)
if normalizer is not None:
arr = normalizer.transform(arr)
if pca is not None:
arr = pca.transform(arr)
return arr.tolist()
except Exception as exc:
logger.warning("Preprocessing failed for %s (%s); passing features as-is", modality, exc)
return features_list
def get_inferencer() -> Optional[ModelV2Inferencer]:
return _inferencer
@router.get("/health", response_model=HealthResponse)
async def health() -> HealthResponse:
return HealthResponse(
status="ok",
model_loaded=_inferencer is not None,
description="ModelV2 inference service is available." if _inferencer else "Model is not loaded yet.",
)
@router.post("/predict/audio", response_model=PredictionResponse)
async def predict_audio(payload: AudioPayload, background_tasks: BackgroundTasks) -> PredictionResponse:
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
if not payload.audio_features:
raise HTTPException(status_code=400, detail="Audio features are required.")
start = time.perf_counter()
sample = {
"audio": payload.audio_features,
"audio_mask": [True] * len(payload.audio_features),
}
result = _inferencer.predict_single(sample)
duration = time.perf_counter() - start
result.setdefault("metadata", {})
result["processing_details"] = {"total_seconds": duration, "stages": {"inference": duration}}
logger.info("predict_audio finished: duration=%.3fs classification=%.4f confidence=%.4f", duration, result.get("classification"), result.get("confidence"))
return PredictionResponse(**result)
@router.post("/predict/video", response_model=PredictionResponse)
async def predict_video(payload: VideoPayload) -> PredictionResponse:
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
if not payload.video_features:
raise HTTPException(status_code=400, detail="Video features are required.")
start = time.perf_counter()
sample = {
"video": payload.video_features,
"video_mask": [True] * len(payload.video_features),
}
result = _inferencer.predict_single(sample)
duration = time.perf_counter() - start
result.setdefault("metadata", {})
result["processing_details"] = {"total_seconds": duration, "stages": {"inference": duration}}
logger.info("predict_video finished: duration=%.3fs classification=%.4f confidence=%.4f", duration, result.get("classification"), result.get("confidence"))
return PredictionResponse(**result)
@router.post("/predict/text", response_model=PredictionResponse)
async def predict_text(payload: TextPayload) -> PredictionResponse:
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
if payload.transcript is None and not payload.chunked_transcript:
raise HTTPException(status_code=400, detail="Transcript or chunked_transcript is required.")
if _text_extractor is None:
raise HTTPException(status_code=503, detail="Text feature extractor is not configured.")
chunks = payload.chunked_transcript or [payload.transcript]
start_total = time.perf_counter()
# measure text feature extraction
t0 = time.perf_counter()
features = _text_extractor.encode(chunks)
t_text = time.perf_counter() - t0
# Validate feature dimensionality matches model expectation
expected_dim = _inferencer.model.text_input_dim if _inferencer else None
if expected_dim is not None:
if features.ndim == 1:
feat_dim = features.shape[0]
else:
feat_dim = features.shape[-1]
if feat_dim != expected_dim:
raise HTTPException(status_code=400, detail=f"Text embedding dim ({feat_dim}) does not match model expected dim ({expected_dim}). Ensure the saved text preprocessor (PCA/normalizer) is available in checkpoints/scalers.")
sample = {
"text": features.tolist(),
"text_mask": [True] * len(chunks),
}
# measure model inference
t1 = time.perf_counter()
result = _inferencer.predict_single(sample)
t_infer = time.perf_counter() - t1
total = time.perf_counter() - start_total
result.setdefault("metadata", {})
result["processing_details"] = {"total_seconds": total, "stages": {"text_extraction": t_text, "inference": t_infer}}
logger.info("predict_text finished: total=%.3fs text=%.3fs inference=%.3fs classification=%.4f", total, t_text, t_infer, result.get("classification"))
return PredictionResponse(**result)
@router.post("/predict/multimodal", response_model=PredictionResponse)
async def predict_multimodal(payload: MultimodalPayload) -> PredictionResponse:
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
# Check route-level cache first
cache_key = _pred_cache_key(payload.model_dump())
cached = _pred_cache_get(cache_key)
if cached is not None:
logger.debug("predict_multimodal cache HIT key=%s...", cache_key[:12])
return PredictionResponse(**cached)
start = time.perf_counter()
audio = _apply_preprocessor(payload.audio_features, "audio")
video = _apply_preprocessor(payload.video_features, "video")
text = _apply_preprocessor(payload.text_features, "text")
sample = {
"audio": audio,
"video": video,
"text": text,
"audio_mask": payload.audio_mask if payload.audio_mask is not None else ([True] * len(audio) if audio else [False]),
"video_mask": payload.video_mask if payload.video_mask is not None else ([True] * len(video) if video else [False]),
"text_mask": payload.text_mask if payload.text_mask is not None else ([True] * len(text) if text else [False]),
}
result = _inferencer.predict_single(sample)
duration = time.perf_counter() - start
result.setdefault("metadata", {})
result["processing_details"] = {"total_seconds": duration, "stages": {"inference": duration}}
_pred_cache_set(cache_key, result)
logger.info("predict_multimodal finished: duration=%.3fs classification=%.4f confidence=%.4f modality_scores=%s", duration, result.get("classification"), result.get("confidence"), result.get("modality_scores"))
return PredictionResponse(**result)
@router.post("/predict/question", response_model=PredictionResponse)
async def predict_question(payload: MultimodalPayload) -> PredictionResponse:
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
start = time.perf_counter()
audio = _apply_preprocessor(payload.audio_features, "audio")
video = _apply_preprocessor(payload.video_features, "video")
text = _apply_preprocessor(payload.text_features, "text")
sample = {
"audio": audio,
"video": video,
"text": text,
"audio_mask": payload.audio_mask if payload.audio_mask is not None else ([True] * len(audio) if audio else [False]),
"video_mask": payload.video_mask if payload.video_mask is not None else ([True] * len(video) if video else [False]),
"text_mask": payload.text_mask if payload.text_mask is not None else ([True] * len(text) if text else [False]),
}
result = _inferencer.predict_single(sample)
duration = time.perf_counter() - start
result.setdefault("metadata", {})
result["processing_details"] = {"total_seconds": duration, "stages": {"inference": duration}}
logger.info("predict_question finished: duration=%.3fs classification=%.4f", duration, result.get("classification"))
return PredictionResponse(**result)
@router.post("/predict/audio/raw", response_model=PredictionResponse)
async def predict_audio_raw(
file: UploadFile = File(...),
participant_id: str = Form("unknown"),
) -> PredictionResponse:
"""Accept a raw audio file upload, extract MFCC features with librosa, and run inference.
Used by the backend's predict_extended() call for per-question audio scoring.
Feature pipeline: MFCC(13) + delta + delta2 β [T, 39] β normalizer+PCA β [T, audio_dim].
"""
if _inferencer is None:
raise HTTPException(status_code=503, detail="Inference model is not initialized.")
audio_bytes = await file.read()
if not audio_bytes:
raise HTTPException(status_code=400, detail="Empty audio file received.")
try:
import librosa
waveform, _ = librosa.load(io.BytesIO(audio_bytes), sr=16000, mono=True)
except Exception as exc:
raise HTTPException(status_code=400, detail=f"Failed to decode audio: {exc}")
if len(waveform) < 320:
raise HTTPException(status_code=400, detail="Audio too short β need at least 20ms of audio.")
waveform = librosa.util.normalize(waveform)
mfcc = librosa.feature.mfcc(y=waveform, sr=16000, n_mfcc=13)
delta = librosa.feature.delta(mfcc)
delta2 = librosa.feature.delta(mfcc, order=2)
features = np.vstack([mfcc, delta, delta2]).T.astype(np.float32) # [T, 39]
features = np.nan_to_num(features, nan=1e-6, posinf=1e-6, neginf=-1e-6)
audio_features = _apply_preprocessor(features.tolist(), "audio")
# Ensure output dim matches model's audio_input_dim regardless of preprocessor outcome
target_dim = _inferencer.model.audio_input_dim
arr = np.array(audio_features, dtype=np.float32)
if arr.shape[-1] != target_dim:
logger.warning(
"predict_audio_raw: dim mismatch after preprocessing β got %d, expected %d; adjusting",
arr.shape[-1], target_dim,
)
if arr.shape[-1] > target_dim:
arr = arr[:, :target_dim]
else:
pad = np.zeros((arr.shape[0], target_dim - arr.shape[-1]), dtype=np.float32)
arr = np.concatenate([arr, pad], axis=-1)
audio_features = arr.tolist()
start = time.perf_counter()
sample = {
"audio": audio_features,
"audio_mask": [True] * len(audio_features),
}
result = _inferencer.predict_single(sample)
duration = time.perf_counter() - start
result.setdefault("metadata", {})
result["processing_details"] = {
"total_seconds": duration,
"stages": {"inference": duration},
"participant_id": participant_id,
"audio_frames": len(audio_features),
}
logger.info(
"predict_audio_raw finished: frames=%d duration=%.3fs classification=%.4f confidence=%.4f",
len(audio_features), duration, result.get("classification"), result.get("confidence"),
)
return PredictionResponse(**result)
|