Spaces:
Sleeping
Sleeping
Commit ·
dd6dcad
0
Parent(s):
feat: Migrate Speech Emotion Recognition to a dedicated Hugging Face Space and update client-side integration.
Browse files- Dockerfile +24 -0
- app/main.py +91 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Instalar dependencias del sistema necesarias para audio y FunASR
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
libsndfile1 \
|
| 8 |
+
ffmpeg \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
# Instalar dependencias Python
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
# Copiar el código de la app
|
| 16 |
+
COPY app /app
|
| 17 |
+
|
| 18 |
+
# Removed build-time model caching to avoid OOM. Model will download on first boot.
|
| 19 |
+
|
| 20 |
+
# Exponer el puerto de Hugging Face Spaces
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
# Comando para iniciar fastapi
|
| 24 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/main.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
import shutil
|
| 3 |
+
import os
|
| 4 |
+
from funasr import AutoModel
|
| 5 |
+
import uvicorn
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="AInterviewer SER Model", version="emotion2vec_plus_large_interino")
|
| 8 |
+
|
| 9 |
+
# Cargar modelo en memoria al inicio
|
| 10 |
+
print("Cargando modelo emotion2vec_plus_large...")
|
| 11 |
+
try:
|
| 12 |
+
model = AutoModel(model="iic/emotion2vec_plus_large")
|
| 13 |
+
print("Modelo cargado exitosamente.")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error cargando modelo: {e}")
|
| 16 |
+
model = None
|
| 17 |
+
|
| 18 |
+
# Mapeo de 9 clases a 4 clases
|
| 19 |
+
EMOTION_MAP = {
|
| 20 |
+
"happy": "happy",
|
| 21 |
+
"sad": "sad",
|
| 22 |
+
"angry": "angry",
|
| 23 |
+
"disgusted": "angry",
|
| 24 |
+
"neutral": "neutral",
|
| 25 |
+
"other": "neutral",
|
| 26 |
+
"unknown": "neutral",
|
| 27 |
+
"fearful": "neutral", # o descartar
|
| 28 |
+
"surprised": "neutral" # o descartar
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
@app.get("/health")
|
| 32 |
+
def healthcheck():
|
| 33 |
+
return {"status": "ok", "model_loaded": model is not None}
|
| 34 |
+
|
| 35 |
+
@app.get("/version")
|
| 36 |
+
def version():
|
| 37 |
+
return {"version": "emotion2vec_plus_large_interino"}
|
| 38 |
+
|
| 39 |
+
@app.post("/analyze")
|
| 40 |
+
async def analyze_audio(file: UploadFile = File(...)):
|
| 41 |
+
if not model:
|
| 42 |
+
raise HTTPException(status_code=503, detail="Model unvailable")
|
| 43 |
+
|
| 44 |
+
temp_path = f"/tmp/{file.filename}"
|
| 45 |
+
try:
|
| 46 |
+
with open(temp_path, "wb") as buffer:
|
| 47 |
+
shutil.copyfileobj(file.file, buffer)
|
| 48 |
+
|
| 49 |
+
# Inferencia
|
| 50 |
+
result = model.generate(temp_path, granularity="utterance", extract_embedding=False)
|
| 51 |
+
|
| 52 |
+
# Result es una lista de dicts. Tomamos el primer elemento.
|
| 53 |
+
# Format ej: [{'labels': ['happy', 'neutral'], 'scores': [0.8, 0.2]}]
|
| 54 |
+
if not result or not isinstance(result, list):
|
| 55 |
+
raise ValueError("Unexpected model output format")
|
| 56 |
+
|
| 57 |
+
res_data = result[0]
|
| 58 |
+
labels = res_data.get("labels", [])
|
| 59 |
+
scores = res_data.get("scores", [])
|
| 60 |
+
|
| 61 |
+
# Sumar scores para nuestras 4 clases
|
| 62 |
+
mapped_scores = {"happy": 0.0, "sad": 0.0, "angry": 0.0, "neutral": 0.0}
|
| 63 |
+
|
| 64 |
+
for label, score in zip(labels, scores):
|
| 65 |
+
target_class = EMOTION_MAP.get(label, "neutral")
|
| 66 |
+
mapped_scores[target_class] += float(score)
|
| 67 |
+
|
| 68 |
+
# Normalizar si no suma 1 por el descarte
|
| 69 |
+
total = sum(mapped_scores.values())
|
| 70 |
+
if total > 0:
|
| 71 |
+
for k in mapped_scores:
|
| 72 |
+
mapped_scores[k] = round(mapped_scores[k] / total, 4)
|
| 73 |
+
|
| 74 |
+
dominant = max(mapped_scores.items(), key=lambda x: x[1])[0]
|
| 75 |
+
|
| 76 |
+
return {
|
| 77 |
+
"dominant": dominant,
|
| 78 |
+
"distribution": mapped_scores,
|
| 79 |
+
"model": "emotion2vec_plus_large_interino",
|
| 80 |
+
"raw_labels": labels, # útil para debug
|
| 81 |
+
"raw_scores": [round(float(s), 4) for s in scores]
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 86 |
+
finally:
|
| 87 |
+
if os.path.exists(temp_path):
|
| 88 |
+
os.remove(temp_path)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
funasr
|
| 5 |
+
modelscope
|
| 6 |
+
torchaudio
|