Malina Claude Fable 5 commited on
Commit
eaa9bb7
·
0 Parent(s):

ARIA Audio: MMS-TTS (pol/eng) + wav2vec2 emotion service

Browse files

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Files changed (4) hide show
  1. Dockerfile +15 -0
  2. README.md +25 -0
  3. app.py +93 -0
  4. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ RUN useradd -m -u 1000 user
4
+ WORKDIR /app
5
+
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu \
8
+ && pip install --no-cache-dir -r requirements.txt
9
+
10
+ COPY app.py .
11
+
12
+ USER user
13
+ ENV HF_HOME=/home/user/.cache/huggingface
14
+ EXPOSE 7860
15
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ARIA Audio
3
+ emoji: 🔊
4
+ colorFrom: purple
5
+ colorTo: blue
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
9
+ license: apache-2.0
10
+ short_description: MMS-TTS (pol/eng) + voice emotion for ARIA AGENT
11
+ ---
12
+
13
+ # ARIA Audio
14
+
15
+ Tiny FastAPI service backing the **ARIA AGENT** app for the models that no
16
+ serverless inference provider hosts:
17
+
18
+ - `POST /tts` `{"text": "...", "lang": "pol" | "eng"}` → `audio/wav`
19
+ (facebook/mms-tts-pol / facebook/mms-tts-eng)
20
+ - `POST /emotion` (body: 16 kHz mono WAV) → `[{"label","score"}, ...]`
21
+ (superb/wav2vec2-base-superb-er)
22
+ - `GET /` → health
23
+
24
+ Models load lazily on first use and stay warm. CPU-basic is enough —
25
+ MMS is a 36M-param VITS and the classifier is a base wav2vec2.
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ARIA Audio — TTS (facebook/mms-tts pol/eng) + voice emotion classifier.
2
+
3
+ Exists because neither model family is hosted by any serverless inference
4
+ provider; the ARIA AGENT app (and its Cloudflare proxy) call this Space.
5
+ """
6
+
7
+ import io
8
+ import threading
9
+
10
+ import numpy as np
11
+ import soundfile as sf
12
+ import torch
13
+ from fastapi import FastAPI, Request, Response
14
+ from fastapi.responses import JSONResponse
15
+
16
+ app = FastAPI()
17
+
18
+ _lock = threading.Lock()
19
+ _tts = {} # lang -> (model, tokenizer)
20
+ _emo = {} # "fe" / "model"
21
+
22
+ TTS_LANGS = ("pol", "eng")
23
+ EMOTION_MODEL = "superb/wav2vec2-base-superb-er"
24
+
25
+
26
+ def tts_pipeline(lang: str):
27
+ with _lock:
28
+ if lang not in _tts:
29
+ from transformers import AutoTokenizer, VitsModel
30
+ name = f"facebook/mms-tts-{lang}"
31
+ _tts[lang] = (VitsModel.from_pretrained(name), AutoTokenizer.from_pretrained(name))
32
+ return _tts[lang]
33
+
34
+
35
+ def emotion_pipeline():
36
+ with _lock:
37
+ if "model" not in _emo:
38
+ from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
39
+ _emo["fe"] = AutoFeatureExtractor.from_pretrained(EMOTION_MODEL)
40
+ _emo["model"] = AutoModelForAudioClassification.from_pretrained(EMOTION_MODEL)
41
+ return _emo["fe"], _emo["model"]
42
+
43
+
44
+ @app.get("/")
45
+ def health():
46
+ return {"ok": True, "endpoints": ["/tts", "/emotion"]}
47
+
48
+
49
+ @app.post("/tts")
50
+ async def tts(request: Request):
51
+ body = await request.json()
52
+ text = str(body.get("text", "")).strip()[:1200]
53
+ lang = body.get("lang", "pol")
54
+ if lang not in TTS_LANGS:
55
+ lang = "pol"
56
+ if not text:
57
+ return JSONResponse({"error": "empty text"}, status_code=400)
58
+
59
+ model, tokenizer = tts_pipeline(lang)
60
+ inputs = tokenizer(text, return_tensors="pt")
61
+ with torch.no_grad():
62
+ waveform = model(**inputs).waveform[0].numpy()
63
+
64
+ buf = io.BytesIO()
65
+ sf.write(buf, waveform, model.config.sampling_rate, format="WAV", subtype="PCM_16")
66
+ return Response(buf.getvalue(), media_type="audio/wav")
67
+
68
+
69
+ @app.post("/emotion")
70
+ async def emotion(request: Request):
71
+ raw = await request.body()
72
+ if len(raw) < 1000:
73
+ return JSONResponse({"error": "no audio"}, status_code=400)
74
+ try:
75
+ data, sr = sf.read(io.BytesIO(raw), dtype="float32")
76
+ except Exception:
77
+ return JSONResponse({"error": "bad audio"}, status_code=400)
78
+ if data.ndim > 1:
79
+ data = data.mean(axis=1)
80
+ if sr != 16000 and len(data) > 1: # the app sends 16 kHz; resample defensively
81
+ idx = np.linspace(0, len(data) - 1, max(1, int(len(data) * 16000 / sr)))
82
+ data = data[idx.astype(np.int64)]
83
+ data = data[: 16000 * 30] # cap at 30 s
84
+
85
+ fe, model = emotion_pipeline()
86
+ inputs = fe(data, sampling_rate=16000, return_tensors="pt")
87
+ with torch.no_grad():
88
+ logits = model(**inputs).logits[0]
89
+ probs = torch.softmax(logits, dim=-1)
90
+ labels = model.config.id2label
91
+ out = [{"label": labels[i], "score": float(p)} for i, p in enumerate(probs)]
92
+ out.sort(key=lambda x: -x["score"])
93
+ return out
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers>=4.44
2
+ fastapi
3
+ uvicorn
4
+ soundfile
5
+ numpy