EurekaPotato commited on
Commit
1b04728
Β·
verified Β·
1 Parent(s): ea2573a

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. handler.py +15 -2
handler.py CHANGED
@@ -242,8 +242,9 @@ class AudioFeatureExtractorEndpoint:
242
  # FastAPI handler for deployment (HF Spaces / Cloud Run / Lambda)
243
  # ──────────────────────────────────────────────────────────────────────── #
244
 
245
- from fastapi import FastAPI, File, UploadFile, Form
246
  from fastapi.middleware.cors import CORSMiddleware
 
247
  from pydantic import BaseModel
248
  from typing import Optional
249
  import base64
@@ -256,6 +257,17 @@ app.add_middleware(
256
  allow_methods=["*"], allow_headers=["*"],
257
  )
258
 
 
 
 
 
 
 
 
 
 
 
 
259
  extractor = AudioFeatureExtractorEndpoint()
260
 
261
  DEFAULT_AUDIO_FEATURES = {
@@ -331,8 +343,9 @@ async def extract_audio_features_base64(data: AudioBase64Request):
331
  print(f"[WARN] soundfile failed ({sf_err}), trying librosa...")
332
  y, sr = librosa.load(io.BytesIO(audio_bytes), sr=16000, mono=True)
333
 
334
- if len(y.shape) > 1:
335
  y = np.mean(y, axis=1)
 
336
  if sr != 16000:
337
  y = librosa.resample(y, orig_sr=sr, target_sr=16000)
338
  y = y.astype(np.float32)
 
242
  # FastAPI handler for deployment (HF Spaces / Cloud Run / Lambda)
243
  # ──────────────────────────────────────────────────────────────────────── #
244
 
245
+ from fastapi import FastAPI, File, UploadFile, Form, Request
246
  from fastapi.middleware.cors import CORSMiddleware
247
+ from fastapi.responses import JSONResponse
248
  from pydantic import BaseModel
249
  from typing import Optional
250
  import base64
 
257
  allow_methods=["*"], allow_headers=["*"],
258
  )
259
 
260
+
261
+ @app.exception_handler(Exception)
262
+ async def global_exception_handler(request: Request, exc: Exception):
263
+ """Catch any unhandled exceptions and return defaults instead of 500."""
264
+ print(f"[GLOBAL ERROR] {request.url}: {exc}")
265
+ traceback.print_exc()
266
+ return JSONResponse(
267
+ status_code=200,
268
+ content={**DEFAULT_AUDIO_FEATURES, "_error": str(exc), "_handler": "global"},
269
+ )
270
+
271
  extractor = AudioFeatureExtractorEndpoint()
272
 
273
  DEFAULT_AUDIO_FEATURES = {
 
343
  print(f"[WARN] soundfile failed ({sf_err}), trying librosa...")
344
  y, sr = librosa.load(io.BytesIO(audio_bytes), sr=16000, mono=True)
345
 
346
+ if hasattr(y, 'shape') and len(y.shape) > 1:
347
  y = np.mean(y, axis=1)
348
+ y = np.asarray(y, dtype=np.float32)
349
  if sr != 16000:
350
  y = librosa.resample(y, orig_sr=sr, target_sr=16000)
351
  y = y.astype(np.float32)