Spaces:
Running
Running
File size: 5,501 Bytes
7938dc3 | 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 | """
Sinama Audio Classifier – Hugging Face Spaces API
--------------------------------------------------
FastAPI app that loads the trained CNN model, accepts audio uploads,
and returns top-5 predicted Cebuano/Sinama words with confidence scores.
Deploy this as a Hugging Face Space (Docker or Gradio SDK).
"""
import json
import os
import tempfile
from contextlib import asynccontextmanager
import librosa
import numpy as np
import tensorflow as tf
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
# ---------------------------------------------------------------------------
# Config – must match training preprocessing
# ---------------------------------------------------------------------------
SAMPLE_RATE = 22050
DURATION = 4.0
N_MELS = 128
N_FFT = 2048
HOP_LENGTH = 512
TARGET_LEN = int(SAMPLE_RATE * DURATION)
# Model files (uploaded to the Space repo)
MODEL_PATH = "best_model.keras"
LABEL_MAP_PATH = "label_map.json"
# ---------------------------------------------------------------------------
# Global state
# ---------------------------------------------------------------------------
model = None
label_map = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Load model and label map on startup."""
global model, label_map
if not os.path.exists(MODEL_PATH):
raise RuntimeError(f"Model file not found: {MODEL_PATH}")
model = tf.keras.models.load_model(MODEL_PATH)
print(f"[app] Model loaded from {MODEL_PATH}")
with open(LABEL_MAP_PATH, "r", encoding="utf-8") as f:
raw = json.load(f)
label_map = {int(k): v for k, v in raw.items()}
print(f"[app] Loaded {len(label_map)} classes")
yield
# Cleanup
model = None
label_map = None
app = FastAPI(
title="Sinama Audio Classifier",
description="Classify spoken Cebuano/Sinama words from audio clips",
version="1.0.0",
lifespan=lifespan,
)
# Allow Flutter app / any frontend to call this API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------------------------
# Audio preprocessing
# ---------------------------------------------------------------------------
def preprocess_audio(audio_bytes: bytes) -> np.ndarray:
"""Convert raw audio bytes → Mel spectrogram feature array."""
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp.write(audio_bytes)
tmp_path = tmp.name
try:
waveform, _ = librosa.load(tmp_path, sr=SAMPLE_RATE, mono=True)
finally:
os.unlink(tmp_path)
# Pad or trim to fixed duration
if len(waveform) < TARGET_LEN:
waveform = np.pad(waveform, (0, TARGET_LEN - len(waveform)))
else:
waveform = waveform[:TARGET_LEN]
# Mel spectrogram
mel = librosa.feature.melspectrogram(
y=waveform, sr=SAMPLE_RATE,
n_mels=N_MELS, n_fft=N_FFT, hop_length=HOP_LENGTH,
)
mel_db = librosa.power_to_db(mel, ref=np.max)
# Normalise
mean, std = mel_db.mean(), mel_db.std()
mel_db = (mel_db - mean) / (std + 1e-9)
# Shape: (1, freq_bins, time_steps, 1)
return mel_db[np.newaxis, ..., np.newaxis]
# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------
@app.get("/health")
async def health():
return {"status": "ok", "classes": len(label_map) if label_map else 0}
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
"""
Accept an audio file and return top-5 predictions.
Returns:
[{"label": "word", "score": 0.95}, ...]
"""
if model is None or label_map is None:
raise HTTPException(status_code=503, detail="Model not loaded")
audio_bytes = await file.read()
if len(audio_bytes) == 0:
raise HTTPException(status_code=400, detail="Empty audio file")
try:
features = preprocess_audio(audio_bytes)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Audio processing failed: {e}")
preds = model.predict(features, verbose=0)[0]
top_indices = np.argsort(preds)[::-1][:5]
results = [
{"label": label_map[int(i)], "score": round(float(preds[i]), 4)}
for i in top_indices
]
return results
@app.post("/predict/raw")
async def predict_raw(file: UploadFile = File(...)):
"""
Same as /predict but returns ALL class probabilities.
Useful for debugging or custom logic in the app.
"""
if model is None or label_map is None:
raise HTTPException(status_code=503, detail="Model not loaded")
audio_bytes = await file.read()
if len(audio_bytes) == 0:
raise HTTPException(status_code=400, detail="Empty audio file")
try:
features = preprocess_audio(audio_bytes)
except Exception as e:
raise HTTPException(status_code=400, detail=f"Audio processing failed: {e}")
preds = model.predict(features, verbose=0)[0]
return {
"predictions": {
label_map[i]: round(float(preds[i]), 4)
for i in range(len(preds))
}
}
|