Ashu1007 commited on
Commit
4723136
·
1 Parent(s): d17ce0a

feat: complete v0.2.0 rewrite with hybrid prosody pipeline, React UI, and Docker deployment

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .dockerignore +14 -0
  2. .env.example +25 -0
  3. .gitattributes +9 -0
  4. .github/workflows/ci.yml +37 -0
  5. .gitignore +39 -0
  6. Dockerfile +51 -0
  7. README.md +230 -125
  8. app/__init__.py +0 -0
  9. app/__pycache__/config.cpython-312.pyc +0 -0
  10. app/__pycache__/main.cpython-312.pyc +0 -0
  11. app/api/__init__.py +0 -0
  12. app/api/analyze.py +28 -0
  13. app/api/audio.py +30 -0
  14. app/api/deps.py +25 -0
  15. app/api/health.py +35 -0
  16. app/api/synthesize.py +60 -0
  17. app/api/voices.py +16 -0
  18. app/audio/__init__.py +0 -0
  19. app/audio/store.py +104 -0
  20. app/config.py +0 -15
  21. app/core/__init__.py +0 -0
  22. app/core/config.py +52 -0
  23. app/core/errors.py +61 -0
  24. app/core/logging.py +19 -0
  25. app/emotion/__init__.py +0 -0
  26. app/emotion/fusion.py +59 -0
  27. app/emotion/pipeline.py +89 -0
  28. app/emotion/prosody.py +49 -0
  29. app/emotion/vad_mapping.py +29 -0
  30. app/emotion/weights.py +63 -0
  31. app/main.py +76 -22
  32. app/ml/__init__.py +0 -0
  33. app/ml/emotion_model.py +54 -0
  34. app/ml/manager.py +100 -0
  35. app/ml/vad_model.py +61 -0
  36. app/models/__pycache__/emotion_model.cpython-312.pyc +0 -0
  37. app/models/__pycache__/vad_model.cpython-312.pyc +0 -0
  38. app/models/emotion_model.py +0 -22
  39. app/models/vad_model.py +0 -24
  40. app/schemas.py +116 -0
  41. app/services/__pycache__/engine.cpython-312.pyc +0 -0
  42. app/services/engine.py +0 -25
  43. app/static/index.html +0 -76
  44. app/static/script.js +0 -36
  45. app/static/style.css +0 -184
  46. app/static1/static/index.html +0 -115
  47. app/static1/static/script.js +0 -36
  48. app/static1/static/style.css +0 -260
  49. app/tts/__init__.py +0 -0
  50. app/tts/__pycache__/pause_injection.cpython-312.pyc +0 -0
.dockerignore ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .github
3
+ .venv
4
+ venv
5
+ env
6
+ __pycache__
7
+ *.pyc
8
+ .pytest_cache
9
+ .ruff_cache
10
+ audio_outputs/*.mp3
11
+ frontend/node_modules
12
+ frontend/dist
13
+ .cache
14
+ tests
.env.example ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copy to .env and adjust as needed. All values have sensible defaults in
2
+ # app/core/config.py; nothing here is required to run.
3
+
4
+ # --- Server ---
5
+ EMPATHVOICE_HOST=127.0.0.1
6
+ EMPATHVOICE_PORT=8000
7
+
8
+ # --- Models ---
9
+ EMPATHVOICE_EMOTION_MODEL=SamLowe/roberta-base-go_emotions
10
+ EMPATHVOICE_VAD_MODEL=RobroKools/vad-bert
11
+ # Force a device, or leave unset for auto-detect (cuda if available, else cpu).
12
+ # EMPATHVOICE_DEVICE=cpu
13
+ # Max concurrent model inferences (shared model instance). 1 = serialize.
14
+ EMPATHVOICE_INFERENCE_CONCURRENCY=1
15
+
16
+ # --- Audio ---
17
+ EMPATHVOICE_AUDIO_DIR=audio_outputs
18
+ EMPATHVOICE_AUDIO_TTL_SECONDS=3600
19
+ EMPATHVOICE_DEFAULT_VOICE=en-IN-NeerjaNeural
20
+
21
+ # --- Limits ---
22
+ EMPATHVOICE_MAX_TEXT_CHARS=2000
23
+
24
+ # --- Logging ---
25
+ EMPATHVOICE_LOG_LEVEL=INFO
.gitattributes ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Normalize line endings so the working tree stops showing phantom CRLF diffs.
2
+ # Text files are stored with LF in the repo and checked out with LF everywhere.
3
+ * text=auto eol=lf
4
+
5
+ # Binary assets: never touch.
6
+ *.mp3 binary
7
+ *.png binary
8
+ *.pt binary
9
+ *.bin binary
.github/workflows/ci.yml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ backend:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - uses: actions/setup-python@v5
14
+ with:
15
+ python-version: "3.12"
16
+ - name: Install dependencies
17
+ run: pip install -r requirements-dev.txt
18
+ # The default suite mocks models + TTS (integration tests are
19
+ # deselected), so CI never downloads pretrained weights or calls edge-tts.
20
+ - name: Lint
21
+ run: ruff check app tests
22
+ - name: Test
23
+ run: pytest
24
+
25
+ frontend:
26
+ runs-on: ubuntu-latest
27
+ defaults:
28
+ run:
29
+ working-directory: frontend
30
+ steps:
31
+ - uses: actions/checkout@v4
32
+ - uses: actions/setup-node@v4
33
+ with:
34
+ node-version: "20"
35
+ - run: npm ci
36
+ - name: Type-check and build
37
+ run: npm run build
.gitignore ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .venv/
6
+ venv/
7
+ env/
8
+
9
+ # Local environment / secrets
10
+ .env
11
+ .env.*
12
+ !.env.example
13
+
14
+ # Generated audio (runtime artifacts, never committed)
15
+ audio_outputs/
16
+ !audio_outputs/.gitkeep
17
+
18
+ # Hugging Face / Torch caches
19
+ .cache/
20
+ *.pt
21
+ *.bin
22
+
23
+ # Tooling
24
+ .pytest_cache/
25
+ .ruff_cache/
26
+ .mypy_cache/
27
+ htmlcov/
28
+ .coverage
29
+
30
+ # Frontend (added in Stage 4)
31
+ node_modules/
32
+ frontend/dist/
33
+ *.tsbuildinfo
34
+
35
+ # OS / editor
36
+ .DS_Store
37
+ Thumbs.db
38
+ .idea/
39
+ .vscode/
Dockerfile ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Stage 1: Build the React + TypeScript frontend
2
+ FROM node:20-slim AS frontend-builder
3
+ WORKDIR /frontend
4
+
5
+ COPY frontend/package*.json ./
6
+ RUN npm ci
7
+
8
+ COPY frontend/ ./
9
+ RUN npm run build
10
+
11
+ # Stage 2: Python backend + serving
12
+ FROM python:3.12-slim
13
+
14
+ # Hugging Face Spaces runs as user ID 1000
15
+ RUN useradd -m -u 1000 user
16
+ USER user
17
+ ENV HOME=/home/user \
18
+ PATH=/home/user/.local/bin:$PATH \
19
+ PYTHONUNBUFFERED=1 \
20
+ HF_HOME=/home/user/.cache/huggingface \
21
+ EMPATHVOICE_HOST=0.0.0.0 \
22
+ EMPATHVOICE_PORT=7860
23
+
24
+ WORKDIR $HOME/app
25
+
26
+ # Install Python runtime dependencies
27
+ COPY --chown=user:user requirements.txt .
28
+ RUN pip install --no-cache-dir --upgrade pip && \
29
+ pip install --no-cache-dir -r requirements.txt
30
+
31
+ # Pre-cache models into the container image for fast cold-starts
32
+ RUN python -c "from transformers import AutoModelForSequenceClassification, AutoTokenizer; \
33
+ AutoTokenizer.from_pretrained('SamLowe/roberta-base-go_emotions'); \
34
+ AutoModelForSequenceClassification.from_pretrained('SamLowe/roberta-base-go_emotions'); \
35
+ AutoTokenizer.from_pretrained('RobroKools/vad-bert'); \
36
+ AutoModelForSequenceClassification.from_pretrained('RobroKools/vad-bert')"
37
+
38
+ # Copy backend code
39
+ COPY --chown=user:user app ./app
40
+ COPY --chown=user:user run.py .
41
+
42
+ # Copy built frontend assets from builder stage
43
+ COPY --chown=user:user --from=frontend-builder /frontend/dist ./frontend/dist
44
+
45
+ # Create audio outputs directory
46
+ RUN mkdir -p audio_outputs
47
+
48
+ # Hugging Face Spaces exposes port 7860 by default
49
+ EXPOSE 7860
50
+
51
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,181 +1,286 @@
1
  # EmpathVoice
2
 
3
- EmpathVoice is a FastAPI-based text-to-speech service that tries to make generated voice sound emotionally appropriate.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- In simple words:
 
 
 
 
6
 
7
- - You give text.
8
- - The system detects emotion.
9
- - It changes vocal controls (pitch, rate, energy, pauses).
10
- - It returns an MP3 with emotion-aware delivery.
11
 
12
- ## Core Contribution (Quick Overview)
 
13
 
14
- The main idea is a deterministic hybrid emotion-to-prosody controller.
 
 
 
 
 
 
 
15
 
16
- Instead of a black-box end-to-end prosody model, we explicitly combine two emotional views:
 
 
 
 
17
 
18
- 1. Discrete emotion space (28-label GoEmotions probabilities)
19
- 2. Continuous affective space (Valence-Arousal-Dominance)
 
 
20
 
21
- These two state spaces are mapped to the same 4 vocal parameters and fused using confidence-adaptive weights.
 
 
 
22
 
23
- This keeps the system interpretable, controllable, and easy to debug.
24
 
25
- ## Demo (Frontend Preview)
 
 
 
26
 
27
- <!-- Replace with your demo screenshot/svg -->
28
- ![Demo Placeholder](frontend.png)
29
 
30
- ## High-Level Methodology
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- ### 1) Emotion inference from two state spaces
33
 
34
- - Discrete branch: GoEmotions gives a 28-dimensional multi-label probability vector.
35
- - Continuous branch: VAD model gives valence, arousal, dominance in [0, 1].
36
 
37
- ### 2) Deterministic mappings to vocal parameter space
 
 
 
 
38
 
39
- Both branches are mapped to the same prosody state:
 
40
 
41
- $$
42
- \mathbf{s} = [\text{pitch},\,\text{rate},\,\text{energy},\,\text{pauses}]
43
- $$
44
 
45
- - Discrete mapping: handcrafted matrix from 28 emotions to 4 prosody controls.
46
- - Continuous mapping: VAD transform (with nonlinearity) to the same 4 controls.
 
 
47
 
48
- This deterministic mapping layer is the central design choice of this project.
49
 
50
- ### 3) Confidence-adaptive fusion (key integration logic)
 
 
 
51
 
52
- Let:
53
 
54
- - $\mathbf{s}_{disc}$ = prosody from discrete branch
55
- - $\mathbf{s}_{cont}$ = prosody from VAD branch
56
- - $c = (\max_i p_i)^\kappa$ = confidence from top discrete probability
57
 
58
- Fusion is:
 
 
59
 
60
- $$
61
- \mathbf{s}_{final} = \mathbf{w}_d \odot (\mathbf{G}_d \odot \mathbf{s}_{disc}) + \mathbf{w}_c \odot (\mathbf{G}_c \odot \mathbf{s}_{cont})
62
- $$
63
 
64
- with
65
 
66
- $$
67
- \mathbf{w}_d = c\,\boldsymbol{\alpha}, \quad \mathbf{w}_c = 1-\mathbf{w}_d
68
- $$
69
 
70
- Why this matters:
71
 
72
- - High discrete confidence -> stronger categorical influence.
73
- - Lower confidence -> smoother fallback from VAD branch.
74
- - Per-dimension priors allow different trust levels for pitch/rate/energy/pauses.
 
75
 
76
- ## Post-Processing for TTS Control
 
77
 
78
- The fused prosody vector is converted into bounded TTS controls for edge-tts:
79
 
80
- - pitch shift (Hz)
81
- - speaking rate (%)
82
- - energy/volume (%)
83
- - pause control scalar
84
 
85
- Although SSML-level fine control is not integrated, the project still applies prosody modulation and includes an intra-sentence pause strategy module.
 
 
 
86
 
87
- ## Project Structure
88
 
89
- ```text
90
- DarvixProject/
91
- ├── README.md
92
- ├── readme1.md
93
- ├── requirements.txt
94
- ├── run.py
95
- ├── app/
96
- │ ├── config.py
97
- │ ├── main.py
98
- │ ├── models/
99
- │ │ ├── emotion_model.py
100
- │ │ └── vad_model.py
101
- │ ├── services/
102
- │ │ └── engine.py
103
- │ ├── static/
104
- │ │ ├── index.html
105
- │ │ ├── style.css
106
- │ │ └── script.js
107
- │ ├── tts/
108
- │ │ ├── pause_injection.py
109
- │ │ └── tts_engine.py
110
- │ └── utils/
111
- │ ├── emotion_weights.py
112
- │ ├── fusion.py
113
- │ ├── processing.py
114
- │ └── vad_mapping.py
115
- └── audio_outputs/
116
- ```
117
 
118
- ## Setup
119
 
120
- ```bash
121
- cd DarvixProject
122
- pip install -r requirements.txt
123
- ```
124
 
125
- Recommended:
126
 
127
- - Python 3.9+
128
- - CUDA-enabled PyTorch (optional, for faster inference)
129
 
130
- ## Run
 
 
 
 
 
 
 
 
 
 
131
 
132
- ```bash
133
- cd DarvixProject
134
- python run.py
135
- ```
136
 
137
- Service URL:
 
138
 
139
- - http://127.0.0.1:8000
140
 
141
  ## API
142
 
143
- ### POST /speak
144
 
145
- Input:
 
 
 
 
 
 
 
146
 
147
- - query parameter: text
148
-
149
- Example:
150
 
151
  ```bash
152
- curl -X POST "http://127.0.0.1:8000/speak?text=I%20am%20proud%20of%20you"
 
 
 
 
 
 
 
153
  ```
154
 
155
- Response:
156
-
157
- ```json
158
- {
159
- "audio": "audio_out/<uuid>.mp3"
160
- }
161
- ```
162
-
163
- ## Design Rationale
164
-
165
- 1. Hybrid emotion modeling
166
- - Categorical labels capture semantic emotion type.
167
- - VAD captures smooth affective intensity.
168
 
169
- 2. Deterministic mapping instead of black-box prosody generation
170
- - Better interpretability and direct control over behavior.
171
 
172
- 3. Confidence-adaptive fusion
173
- - Robust under ambiguity; branch contribution is input-dependent.
174
-
175
- 4. Practical deployability
176
- - FastAPI + edge-tts pipeline is simple and easy to demo.
177
-
178
- ## Note
179
 
180
- Recent/sample generated audio files can be found in the `audio_outputs/` folder.
 
 
181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # EmpathVoice
2
 
3
+ Emotion-aware text-to-speech with a transparent prosody pipeline.
4
+
5
+ [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-yellow)](https://huggingface.co/spaces)
6
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
7
+ [![React 18](https://img.shields.io/badge/react-18-61dafb.svg)](https://react.dev/)
8
+ [![FastAPI](https://img.shields.io/badge/FastAPI-0.162+-009688.svg)](https://fastapi.tiangolo.com/)
9
+
10
+ EmpathVoice reads a piece of text, estimates its emotional character with two
11
+ complementary models, maps that estimate to concrete speech-delivery parameters
12
+ (pitch, rate, energy, pauses), and synthesizes audio whose delivery reflects the
13
+ text. The thing I care about most here is **transparency**: instead of a
14
+ black-box "expressive TTS", the app shows you *why* it chose a particular
15
+ delivery, directly from the model outputs.
16
+
17
+ > The emotion and VAD numbers are **model estimates**, not calibrated
18
+ > probabilities or a definitive reading of anyone's feelings. The UI and the API
19
+ > describe them that way on purpose.
20
+
21
+ ---
22
+
23
+ ## Preview
24
+
25
+ <p align="center">
26
+ <img src="frontend.png" alt="EmpathVoice UI Screenshot" width="650" />
27
+ </p>
28
+
29
+ ---
30
+
31
+ ## Motivation
32
+
33
+ Most expressive-TTS demos hide their reasoning: text goes in, "emotional" audio
34
+ comes out, and you can't tell what the system thought the text meant. I wanted
35
+ the opposite — a small, honest studio where the emotion analysis is explicit,
36
+ interpretable, and visibly drives the voice. The analysis pipeline is
37
+ deterministic and inspectable, which makes it easy to reason about, debug, and
38
+ explain.
39
+
40
+ ## Features
41
+
42
+ - **Live emotion analysis preview** — as you type, the app shows the estimated
43
+ emotions, the valence/arousal/dominance (VAD) estimate, and the resulting
44
+ pitch/rate/energy/pause adjustments, all computed by the backend.
45
+ - **One-click synthesis** — generate speech that applies those parameters, then
46
+ play, replay, and download the result.
47
+ - **Voice selection** — a set of English edge-tts voices.
48
+ - **Transparent delivery** — every number shown comes from the real pipeline;
49
+ there is no client-side keyword guessing.
50
+ - **Clean failure and warm-up handling** — the UI reports when models are still
51
+ loading or unavailable, and the API returns structured errors.
52
+
53
+ ## Architecture
54
+
55
+ ```mermaid
56
+ flowchart LR
57
+ UI[React + TS UI] -- POST /api/analyze --> API[FastAPI]
58
+ UI -- POST /api/synthesize --> API
59
+ UI -- GET /api/audio/:id --> API
60
+ API --> MGR[Model Manager<br/>lifespan, device, eval, concurrency]
61
+ MGR --> EMO[GoEmotions]
62
+ MGR --> VAD[VAD-BERT]
63
+ API --> PIPE[Emotion→Prosody pipeline<br/>mappings · fusion · clamp]
64
+ API --> TTS[edge-tts]
65
+ API --> STORE[Audio store<br/>TTL · id-based]
66
+ ```
67
 
68
+ The backend is organized by responsibility: HTTP/API layer, request/response
69
+ schemas, configuration, model lifecycle, the emotion→prosody pipeline, TTS, and
70
+ audio storage are each isolated. A fuller write-up — request lifecycle, the
71
+ model-readiness model, the fusion math, and the main design decisions — is in
72
+ [`docs/architecture.md`](docs/architecture.md).
73
 
74
+ ## The AI pipeline
 
 
 
75
 
76
+ The core is a deterministic, interpretable **hybrid emotion-to-prosody
77
+ controller**. Two views of the text's emotion are combined:
78
 
79
+ 1. **Discrete branch** — `SamLowe/roberta-base-go_emotions` produces a
80
+ 28-dimensional multi-label score vector. A handcrafted 28×4 weight matrix maps
81
+ those labels to a prosody shift `[pitch, rate, energy, pauses]`. The matrix is
82
+ aligned to the model's labels *by name*, so it stays correct regardless of
83
+ label ordering.
84
+ 2. **Continuous branch** — `RobroKools/vad-bert` estimates valence, arousal, and
85
+ dominance in `[0, 1]`. These are centered to `[-1, 1]`, passed through a
86
+ `tanh` nonlinearity, and mapped to the same prosody space by a 4×3 matrix.
87
 
88
+ The two shifts are combined with a **confidence-adaptive fusion**: the top
89
+ *non-neutral* discrete score sets how much the categorical branch is trusted
90
+ versus the smoother VAD branch, per prosody dimension. (Excluding `neutral` from
91
+ the confidence term is a deliberate fix — otherwise neutral text, whose discrete
92
+ prosody row is all zeros, would suppress the VAD branch and flatten delivery.)
93
 
94
+ The fused vector is scaled and **clamped** to safe TTS ranges, then applied via
95
+ edge-tts (pitch in Hz, rate and volume in percent). Because edge-tts does not
96
+ expose SSML `<break>` control, pauses are approximated by inserting commas at
97
+ clause boundaries, scaled by the pause level.
98
 
99
+ **Honesty note:** the weight matrices and clamps are hand-tuned heuristics, not
100
+ learned or formally evaluated. I make no claims about emotion-recognition
101
+ accuracy or "emotional quality"; the value here is interpretability and a clean,
102
+ controllable mapping, not state-of-the-art affect modeling.
103
 
104
+ ## Tech stack
105
 
106
+ - **Backend:** Python 3.10+, FastAPI, Pydantic v2, PyTorch, Hugging Face
107
+ Transformers, NumPy, edge-tts.
108
+ - **Frontend:** React 18, TypeScript, Vite.
109
+ - **Testing:** pytest (unit + API with fakes; optional integration tests).
110
 
111
+ ## Project structure
 
112
 
113
+ ```text
114
+ EmpathVoice/
115
+ ├── app/
116
+ │ ├── main.py # app factory, lifespan, routing, SPA serving
117
+ │ ├── core/ # config, logging, error handling
118
+ │ ├── schemas.py # Pydantic request/response contracts
119
+ │ ├── ml/ # model wrappers + lifecycle manager
120
+ │ ├── emotion/ # weights, VAD mapping, fusion, prosody, pipeline
121
+ │ ├── tts/ # pause injection, synthesizer, voices
122
+ │ ├── audio/ # TTL audio store
123
+ │ └── api/ # one module per route group
124
+ ├── frontend/ # React + TypeScript + Vite app
125
+ ├── tests/ # unit, API, and optional integration tests
126
+ ├── docs/architecture.md
127
+ ├── requirements.txt / requirements-dev.txt
128
+ └── run.py
129
+ ```
130
 
131
+ ## Setup
132
 
133
+ ### Backend
 
134
 
135
+ ```bash
136
+ cd EmpathVoice
137
+ python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
138
+ pip install -r requirements.txt
139
+ ```
140
 
141
+ PyTorch and Transformers download the pretrained models on first use (a few
142
+ hundred MB). CUDA is used automatically when available; otherwise it runs on CPU.
143
 
144
+ ### Frontend
 
 
145
 
146
+ ```bash
147
+ cd frontend
148
+ npm install
149
+ ```
150
 
151
+ ## Environment configuration
152
 
153
+ All settings have defaults, so no configuration is required. To override, copy
154
+ `.env.example` to `.env`. Variables use the `EMPATHVOICE_` prefix — model names,
155
+ `DEVICE` (auto-detected if unset), `INFERENCE_CONCURRENCY`, `AUDIO_TTL_SECONDS`
156
+ (default 3600), `DEFAULT_VOICE`, `MAX_TEXT_CHARS`, and `LOG_LEVEL`.
157
 
158
+ ## Running
159
 
160
+ ### Development (two processes, hot reload)
 
 
161
 
162
+ ```bash
163
+ # Terminal 1 — backend on :8000
164
+ python run.py
165
 
166
+ # Terminal 2 — frontend on :5173 (proxies /api and probes to :8000)
167
+ cd frontend && npm run dev
168
+ ```
169
 
170
+ Open http://127.0.0.1:5173.
171
 
172
+ ### Production-style (single process)
 
 
173
 
174
+ Build the frontend; FastAPI then serves it from `frontend/dist` at `/`:
175
 
176
+ ```bash
177
+ cd frontend && npm run build && cd ..
178
+ uvicorn app.main:app --host 0.0.0.0 --port 8000 # no --reload: models load once
179
+ ```
180
 
181
+ Open http://127.0.0.1:8000. If the frontend isn't built, `/` redirects to the
182
+ interactive API docs at `/docs`.
183
 
184
+ ### Docker (Local)
185
 
186
+ Run the entire application in a self-contained container:
 
 
 
187
 
188
+ ```bash
189
+ docker build -t empathvoice .
190
+ docker run -p 7860:7860 empathvoice
191
+ ```
192
 
193
+ Open http://127.0.0.1:7860.
194
 
195
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
+ ## Deployment & Live Demo
198
 
199
+ Because EmpathVoice runs two PyTorch transformer models in memory (`RoBERTa-GoEmotions` and `VAD-BERT`), it requires at least ~1.5 GB of RAM.
 
 
 
200
 
201
+ ### Deploying to Hugging Face Spaces (Recommended — Free 16GB RAM)
202
 
203
+ [Hugging Face Spaces](https://huggingface.co/spaces) is the ideal host for this project:
 
204
 
205
+ 1. **Create a Space**: Go to [huggingface.co/new-space](https://huggingface.co/new-space).
206
+ 2. **Select Space SDK**: Choose **Docker** (Blank template).
207
+ 3. **Hardware Tier**: Select the **Free** tier (2 vCPU, 16 GB RAM).
208
+ 4. **Deploy**:
209
+ - Push this repository directly to the Hugging Face Space Git remote:
210
+ ```bash
211
+ git remote add space https://huggingface.co/spaces/<your-username>/<space-name>
212
+ git push space main
213
+ ```
214
+ - *Or* connect your GitHub repository directly in the Space settings for automatic deployment on push.
215
+ 5. Hugging Face builds the included `Dockerfile` (which pre-caches the models) and hosts your live app at `https://huggingface.co/spaces/<your-username>/<space-name>`.
216
 
217
+ ### Deploying to Railway or Render
 
 
 
218
 
219
+ * **Railway**: Connect your GitHub repository. Railway automatically detects the `Dockerfile` and deploys it with containerized memory allocation.
220
+ * **Render**: Deploy as a Web Service from Git or Docker. Select at least the **Starter tier (2 GB RAM)**, as the Free tier (512 MB RAM) will hit Out-Of-Memory limits when loading PyTorch models.
221
 
222
+ ---
223
 
224
  ## API
225
 
226
+ Interactive docs live at `/docs`. Summary:
227
 
228
+ | Method | Path | Purpose |
229
+ | ------ | -------------------- | --------------------------------------------------- |
230
+ | GET | `/health` | Liveness — process is up (always 200). |
231
+ | GET | `/ready` | Readiness — 200 when models are loaded, else 503. |
232
+ | POST | `/api/analyze` | Emotion → prosody analysis (no audio). |
233
+ | POST | `/api/synthesize` | Analysis + synthesized audio. |
234
+ | GET | `/api/audio/{id}` | Retrieve generated audio by id (expires via TTL). |
235
+ | GET | `/api/voices` | Available voices + default. |
236
 
237
+ Examples:
 
 
238
 
239
  ```bash
240
+ curl -s -X POST http://127.0.0.1:8000/api/analyze \
241
+ -H 'Content-Type: application/json' \
242
+ -d '{"text": "I am so proud of you!"}'
243
+
244
+ curl -s -X POST http://127.0.0.1:8000/api/synthesize \
245
+ -H 'Content-Type: application/json' \
246
+ -d '{"text": "I am so proud of you!", "voice": "en-US-AriaNeural"}'
247
+ # -> { "audio_id": "...", "audio_url": "/api/audio/...", "expires_in_seconds": 3600, "analysis": {...} }
248
  ```
249
 
250
+ Errors are returned as `{"error": {"code", "message"}}` with an appropriate
251
+ status code (e.g. `model_not_ready` → 503, `invalid_request` → 400,
252
+ `audio_not_found` → 404). Input validation errors use FastAPI's 422.
 
 
 
 
 
 
 
 
 
 
253
 
254
+ ## Testing
 
255
 
256
+ ```bash
257
+ pip install -r requirements-dev.txt
 
 
 
 
 
258
 
259
+ pytest # unit + API tests (fakes; no downloads, no network)
260
+ pytest -m integration # optional: loads real models + calls edge-tts
261
+ ```
262
 
263
+ The default suite mocks the models and the TTS service, so it is fast and
264
+ hermetic. The integration tests exercise the real inference and synthesis paths
265
+ and skip cleanly when torch / edge-tts are unavailable.
266
+
267
+ ## Known limitations
268
+
269
+ - Prosody mappings and clamps are hand-tuned heuristics; there is no learned
270
+ prosody model and no formal evaluation of emotional fidelity.
271
+ - A single prosody setting is applied to the whole input — long, mixed-emotion
272
+ passages are not segmented per sentence.
273
+ - Pause control is approximate (comma insertion), because edge-tts does not
274
+ expose SSML break timing.
275
+ - Synthesis depends on the external edge-tts (Microsoft) service and needs
276
+ network access; voice quality is whatever that service provides.
277
+ - Generated audio is intentionally temporary (1-hour TTL) and not persisted
278
+ across restarts.
279
+
280
+ ## Possible future improvements
281
+
282
+ - Per-sentence prosody for mixed-emotion passages (multi-segment synthesis).
283
+ - An expressiveness control (the backend already accepts a global strength
284
+ parameter; it is intentionally not exposed in the current UI).
285
+ - A pluggable TTS backend that supports true SSML prosody/break control.
286
+ - Calibration of the emotion scores if a labelled dataset is introduced.
app/__init__.py ADDED
File without changes
app/__pycache__/config.cpython-312.pyc DELETED
Binary file (535 Bytes)
 
app/__pycache__/main.cpython-312.pyc DELETED
Binary file (1.55 kB)
 
app/api/__init__.py ADDED
File without changes
app/api/analyze.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Emotion-analysis endpoint.
2
+
3
+ Returns the full emotion -> prosody analysis without synthesizing audio, so the
4
+ frontend can preview *why* the voice will sound a certain way before committing
5
+ to a (slower) synthesis call.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from fastapi import APIRouter, Depends
11
+
12
+ from app.api.deps import get_manager
13
+ from app.core.errors import ModelNotReadyError
14
+ from app.ml.manager import ModelManager
15
+ from app.schemas import AnalyzeRequest, AnalyzeResponse
16
+
17
+ router = APIRouter(tags=["analysis"])
18
+
19
+
20
+ @router.post("/analyze", response_model=AnalyzeResponse)
21
+ async def analyze(
22
+ payload: AnalyzeRequest, manager: ModelManager = Depends(get_manager)
23
+ ) -> AnalyzeResponse:
24
+ if not manager.ready:
25
+ raise ModelNotReadyError(
26
+ f"Models are not ready (status: {manager.status.value})."
27
+ )
28
+ return await manager.analyze(payload.text)
app/api/audio.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Controlled audio retrieval.
2
+
3
+ Serves generated audio by opaque id (never by filesystem path) and 404s once a
4
+ file has expired or if the id is malformed. ``Content-Disposition: attachment``
5
+ offers a clean download filename while the same url still streams for replay.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from fastapi import APIRouter, Depends
11
+ from fastapi.responses import FileResponse
12
+
13
+ from app.api.deps import get_store
14
+ from app.audio.store import AudioStore
15
+ from app.core.errors import AudioNotFoundError
16
+
17
+ router = APIRouter(tags=["audio"])
18
+
19
+
20
+ @router.get("/audio/{audio_id}")
21
+ async def get_audio(audio_id: str, store: AudioStore = Depends(get_store)) -> FileResponse:
22
+ path = store.path_for(audio_id)
23
+ if path is None:
24
+ raise AudioNotFoundError("Audio not found or expired.")
25
+ return FileResponse(
26
+ path,
27
+ media_type="audio/mpeg",
28
+ filename=f"empathvoice-{audio_id}.mp3",
29
+ headers={"Cache-Control": "no-store"},
30
+ )
app/api/deps.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """FastAPI dependency accessors for process-wide singletons.
2
+
3
+ The model manager, audio store, and synthesizer are created once in the app
4
+ lifespan and stashed on ``app.state``; these helpers expose them to routes.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from fastapi import Request
10
+
11
+ from app.audio.store import AudioStore
12
+ from app.ml.manager import ModelManager
13
+ from app.tts.synth import Synthesizer
14
+
15
+
16
+ def get_manager(request: Request) -> ModelManager:
17
+ return request.app.state.manager
18
+
19
+
20
+ def get_store(request: Request) -> AudioStore:
21
+ return request.app.state.audio_store
22
+
23
+
24
+ def get_synthesizer(request: Request) -> Synthesizer:
25
+ return request.app.state.synthesizer
app/api/health.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Liveness and readiness endpoints.
2
+
3
+ * /health — process is up and serving. Always 200. For container liveness.
4
+ * /ready — models are loaded and inference can run. 200 when ready, else
5
+ 503 with the current model status. For readiness gating.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from fastapi import APIRouter, Depends, Response
11
+
12
+ from app.api.deps import get_manager
13
+ from app.ml.manager import ModelManager
14
+ from app.schemas import HealthResponse, ReadinessResponse
15
+
16
+ router = APIRouter(tags=["health"])
17
+
18
+
19
+ @router.get("/health", response_model=HealthResponse)
20
+ async def health() -> HealthResponse:
21
+ return HealthResponse(status="ok")
22
+
23
+
24
+ @router.get("/ready", response_model=ReadinessResponse)
25
+ async def ready(
26
+ response: Response, manager: ModelManager = Depends(get_manager)
27
+ ) -> ReadinessResponse:
28
+ if not manager.ready:
29
+ response.status_code = 503
30
+ return ReadinessResponse(
31
+ ready=manager.ready,
32
+ model_status=manager.status.value,
33
+ device=manager.device,
34
+ detail=manager.detail,
35
+ )
app/api/synthesize.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Speech-synthesis endpoint.
2
+
3
+ Runs the same analysis as /analyze, converts the prosody parameters into
4
+ edge-tts controls, injects clause pauses, synthesizes audio, and stores it under
5
+ a temporary id. Returns the audio url plus the analysis that produced it, so the
6
+ client never has to call /analyze separately for a one-shot generation.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from fastapi import APIRouter, Depends
12
+
13
+ from app.api.deps import get_manager, get_store, get_synthesizer
14
+ from app.audio.store import AudioStore
15
+ from app.core.config import get_settings
16
+ from app.core.errors import InvalidRequestError, ModelNotReadyError
17
+ from app.ml.manager import ModelManager
18
+ from app.schemas import SynthesizeRequest, SynthesizeResponse
19
+ from app.tts.pause_injection import inject_pauses
20
+ from app.tts.synth import Synthesizer
21
+ from app.tts.voices import VALID_VOICE_IDS
22
+
23
+ router = APIRouter(tags=["synthesis"])
24
+
25
+
26
+ @router.post("/synthesize", response_model=SynthesizeResponse)
27
+ async def synthesize(
28
+ payload: SynthesizeRequest,
29
+ manager: ModelManager = Depends(get_manager),
30
+ store: AudioStore = Depends(get_store),
31
+ synthesizer: Synthesizer = Depends(get_synthesizer),
32
+ ) -> SynthesizeResponse:
33
+ settings = get_settings()
34
+
35
+ if not manager.ready:
36
+ raise ModelNotReadyError(f"Models are not ready (status: {manager.status.value}).")
37
+
38
+ voice = payload.voice or settings.default_voice
39
+ if voice not in VALID_VOICE_IDS:
40
+ raise InvalidRequestError(f"Unknown voice '{voice}'.")
41
+
42
+ analysis = await manager.analyze(payload.text, expressiveness=payload.expressiveness)
43
+ prosody = analysis.prosody
44
+
45
+ text_for_tts = inject_pauses(payload.text, prosody.pause_level)
46
+ audio = await synthesizer.synthesize(
47
+ text=text_for_tts,
48
+ voice=voice,
49
+ rate=f"{prosody.rate_pct:+.0f}%",
50
+ pitch=f"{prosody.pitch_hz:+.0f}Hz",
51
+ volume=f"{prosody.energy_pct:+.0f}%",
52
+ )
53
+
54
+ audio_id = store.save(audio)
55
+ return SynthesizeResponse(
56
+ audio_id=audio_id,
57
+ audio_url=f"/api/audio/{audio_id}",
58
+ expires_in_seconds=settings.audio_ttl_seconds,
59
+ analysis=analysis,
60
+ )
app/api/voices.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Available voices endpoint for the frontend voice picker."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from fastapi import APIRouter
6
+
7
+ from app.core.config import get_settings
8
+ from app.schemas import VoicesResponse
9
+ from app.tts.voices import VOICES
10
+
11
+ router = APIRouter(tags=["voices"])
12
+
13
+
14
+ @router.get("/voices", response_model=VoicesResponse)
15
+ async def list_voices() -> VoicesResponse:
16
+ return VoicesResponse(default=get_settings().default_voice, voices=VOICES)
app/audio/__init__.py ADDED
File without changes
app/audio/store.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Temporary, TTL-based audio storage.
2
+
3
+ Design goals (no Redis/Celery, nothing to persist across restarts):
4
+ * files are addressed by an opaque uuid id, never by caller-supplied paths;
5
+ * a file stays retrievable for ``ttl_seconds`` (replay + repeated downloads),
6
+ i.e. it is NOT deleted on first download;
7
+ * expiry is derived from file mtime, so no in-memory index must survive;
8
+ * cleanup happens (a) once at startup — wiping any leftovers from a previous
9
+ run — and (b) opportunistically, rate-limited, during normal requests.
10
+
11
+ This is intentionally simple and self-contained; it is the entire "background
12
+ processing" footprint of the app.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import logging
18
+ import time
19
+ import uuid
20
+ from pathlib import Path
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+ _UUID_HEX = frozenset("0123456789abcdef-")
25
+ _SWEEP_MIN_INTERVAL_S = 30.0 # don't scan the dir more often than this
26
+
27
+
28
+ class AudioStore:
29
+ def __init__(self, directory: Path, ttl_seconds: int) -> None:
30
+ self._dir = Path(directory)
31
+ self._ttl = ttl_seconds
32
+ self._last_sweep = 0.0
33
+ self._dir.mkdir(parents=True, exist_ok=True)
34
+
35
+ # --- lifecycle ----------------------------------------------------------
36
+
37
+ def purge_all(self) -> int:
38
+ """Delete every stored file. Called at startup (no cross-run persistence)."""
39
+ removed = 0
40
+ for f in self._dir.glob("*.mp3"):
41
+ try:
42
+ f.unlink()
43
+ removed += 1
44
+ except OSError:
45
+ pass
46
+ if removed:
47
+ logger.info("Startup cleanup removed %d leftover audio file(s)", removed)
48
+ return removed
49
+
50
+ # --- write / read -------------------------------------------------------
51
+
52
+ def save(self, data: bytes) -> str:
53
+ self._maybe_sweep()
54
+ audio_id = uuid.uuid4().hex
55
+ (self._dir / f"{audio_id}.mp3").write_bytes(data)
56
+ return audio_id
57
+
58
+ def path_for(self, audio_id: str) -> Path | None:
59
+ """Return the file path if the id is valid, present, and unexpired."""
60
+ self._maybe_sweep()
61
+ if not self._is_valid_id(audio_id):
62
+ return None
63
+ path = self._dir / f"{audio_id}.mp3"
64
+ if not path.is_file():
65
+ return None
66
+ if self._is_expired(path):
67
+ self._safe_unlink(path)
68
+ return None
69
+ return path
70
+
71
+ # --- cleanup ------------------------------------------------------------
72
+
73
+ def _maybe_sweep(self) -> None:
74
+ now = time.time()
75
+ if now - self._last_sweep < _SWEEP_MIN_INTERVAL_S:
76
+ return
77
+ self._last_sweep = now
78
+ for f in self._dir.glob("*.mp3"):
79
+ if self._is_expired(f):
80
+ self._safe_unlink(f)
81
+
82
+ def _is_expired(self, path: Path) -> bool:
83
+ try:
84
+ return (time.time() - path.stat().st_mtime) > self._ttl
85
+ except OSError:
86
+ return False
87
+
88
+ @staticmethod
89
+ def _safe_unlink(path: Path) -> None:
90
+ try:
91
+ path.unlink()
92
+ except OSError:
93
+ pass
94
+
95
+ @staticmethod
96
+ def _is_valid_id(audio_id: str) -> bool:
97
+ # Reject anything that isn't a bare uuid hex -> no path traversal.
98
+ if not audio_id or not set(audio_id) <= _UUID_HEX:
99
+ return False
100
+ try:
101
+ uuid.UUID(hex=audio_id)
102
+ return True
103
+ except ValueError:
104
+ return False
app/config.py DELETED
@@ -1,15 +0,0 @@
1
- from pathlib import Path
2
-
3
- VOICE = "en-IN-NeerjaNeural"
4
-
5
- OUT_DIR = Path("audio_outputs")
6
- OUT_DIR.mkdir(exist_ok=True)
7
-
8
- PROSODY_KEYS = ["pitch", "rate", "energy", "pauses"]
9
-
10
- PROSODY_CLAMPS = {
11
- "pitch_hz": 40.0,
12
- "rate_pct": 35.0,
13
- "energy_pct": 30.0,
14
- "pause_gain": 0.45,
15
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/core/__init__.py ADDED
File without changes
app/core/config.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Centralized, environment-driven configuration.
2
+
3
+ Every tunable the app needs lives here as a single `Settings` object, read once
4
+ at import and injected where needed. Values come from environment variables
5
+ (prefix ``EMPATHVOICE_``) or an optional ``.env`` file; all have defaults, so
6
+ the app runs with zero configuration.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from functools import lru_cache
12
+ from pathlib import Path
13
+
14
+ from pydantic import Field
15
+ from pydantic_settings import BaseSettings, SettingsConfigDict
16
+
17
+
18
+ class Settings(BaseSettings):
19
+ model_config = SettingsConfigDict(
20
+ env_prefix="EMPATHVOICE_",
21
+ env_file=".env",
22
+ env_file_encoding="utf-8",
23
+ extra="ignore",
24
+ )
25
+
26
+ # --- Server ---
27
+ host: str = "127.0.0.1"
28
+ port: int = 8000
29
+
30
+ # --- Models ---
31
+ emotion_model: str = "SamLowe/roberta-base-go_emotions"
32
+ vad_model: str = "RobroKools/vad-bert"
33
+ # None => auto-detect (cuda if available else cpu). Set "cpu"/"cuda" to force.
34
+ device: str | None = None
35
+ inference_concurrency: int = Field(default=1, ge=1, le=8)
36
+
37
+ # --- Audio ---
38
+ audio_dir: Path = Path("audio_outputs")
39
+ audio_ttl_seconds: int = Field(default=3600, ge=1)
40
+ default_voice: str = "en-IN-NeerjaNeural"
41
+
42
+ # --- Limits ---
43
+ max_text_chars: int = Field(default=2000, ge=1)
44
+
45
+ # --- Logging ---
46
+ log_level: str = "INFO"
47
+
48
+
49
+ @lru_cache
50
+ def get_settings() -> Settings:
51
+ """Return the process-wide settings singleton."""
52
+ return Settings()
app/core/errors.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Application error types and their HTTP translation.
2
+
3
+ Routes raise these semantic errors; a single set of handlers turns them into
4
+ consistent JSON responses ``{"error": {"code", "message"}}`` with the right
5
+ status code. This keeps raw tracebacks out of responses and gives the frontend
6
+ a stable error shape to render.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from fastapi import FastAPI, Request
12
+ from fastapi.responses import JSONResponse
13
+
14
+
15
+ class AppError(Exception):
16
+ """Base class for expected, client-facing errors."""
17
+
18
+ status_code: int = 500
19
+ code: str = "internal_error"
20
+
21
+ def __init__(self, message: str) -> None:
22
+ super().__init__(message)
23
+ self.message = message
24
+
25
+
26
+ class InvalidRequestError(AppError):
27
+ status_code = 400
28
+ code = "invalid_request"
29
+
30
+
31
+ class ModelNotReadyError(AppError):
32
+ status_code = 503
33
+ code = "model_not_ready"
34
+
35
+
36
+ class SynthesisError(AppError):
37
+ status_code = 502
38
+ code = "synthesis_failed"
39
+
40
+
41
+ class AudioNotFoundError(AppError):
42
+ status_code = 404
43
+ code = "audio_not_found"
44
+
45
+
46
+ def _error_response(status_code: int, code: str, message: str) -> JSONResponse:
47
+ return JSONResponse(
48
+ status_code=status_code,
49
+ content={"error": {"code": code, "message": message}},
50
+ )
51
+
52
+
53
+ def register_exception_handlers(app: FastAPI) -> None:
54
+ @app.exception_handler(AppError)
55
+ async def _handle_app_error(_: Request, exc: AppError) -> JSONResponse:
56
+ return _error_response(exc.status_code, exc.code, exc.message)
57
+
58
+ @app.exception_handler(Exception)
59
+ async def _handle_unexpected(_: Request, exc: Exception) -> JSONResponse:
60
+ # Last resort: never leak a traceback to the client.
61
+ return _error_response(500, "internal_error", "An unexpected error occurred.")
app/core/logging.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Minimal, dependency-free logging setup.
2
+
3
+ One place configures the root logger so every module can simply call
4
+ ``logging.getLogger(__name__)`` and get consistent, timestamped output.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import logging
10
+
11
+
12
+ def configure_logging(level: str = "INFO") -> None:
13
+ logging.basicConfig(
14
+ level=getattr(logging, level.upper(), logging.INFO),
15
+ format="%(asctime)s %(levelname)-8s %(name)s | %(message)s",
16
+ datefmt="%H:%M:%S",
17
+ )
18
+ # edge-tts / urllib3 are chatty at DEBUG; keep them at WARNING.
19
+ logging.getLogger("edge_tts").setLevel(logging.WARNING)
app/emotion/__init__.py ADDED
File without changes
app/emotion/fusion.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Confidence-adaptive fusion of the discrete and continuous prosody shifts.
2
+
3
+ The blend is unchanged from the original (a per-dimension weighted *arithmetic*
4
+ combination — the original docstring mislabelled it a "geometric mean"; the
5
+ README's math is the correct description).
6
+
7
+ One technically justified refinement (Stage 2, #1): the confidence term now
8
+ ignores the ``neutral`` label. GoEmotions assigns neutral text a high
9
+ ``neutral`` probability, whose prosody row is all zeros; using it as confidence
10
+ made the (zero) discrete branch dominate and suppressed the VAD branch, flat-
11
+ tening neutral/ambiguous delivery. Excluding neutral lets such text lean on the
12
+ continuous branch as the hybrid design intends. When ``neutral_index`` is None
13
+ the behaviour is identical to the original.
14
+
15
+ c = (max_i, i != neutral, p_i) ** kappa # confidence (neutral excluded)
16
+ w_disc = c * alpha_priors # per-dimension discrete weight
17
+ w_cont = 1 - w_disc
18
+ S_final = w_disc * (G_disc * S_disc) + w_cont * (G_cont * S_cont)
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import numpy as np
24
+
25
+ ALPHA_PRIORS = np.array([0.62, 0.50, 0.52, 0.55])
26
+ DISC_GAIN = np.array([1.35, 1.35, 1.35, 1.20])
27
+ CONT_GAIN = np.array([0.80, 0.80, 0.80, 0.75])
28
+
29
+
30
+ def fuse(
31
+ s_disc: np.ndarray,
32
+ s_cont: np.ndarray,
33
+ emotion_probs: np.ndarray,
34
+ kappa: float = 1.5,
35
+ neutral_index: int | None = None,
36
+ ) -> np.ndarray:
37
+ """Fuse discrete and continuous shifts with a confidence-adaptive blend.
38
+
39
+ If ``neutral_index`` is given, that label is excluded from the confidence
40
+ term (see module docstring). Falls back to the plain max if neutral is the
41
+ only label present.
42
+ """
43
+ if neutral_index is None:
44
+ top_prob = float(np.max(emotion_probs))
45
+ else:
46
+ mask = np.ones(len(emotion_probs), dtype=bool)
47
+ mask[neutral_index] = False
48
+ non_neutral = emotion_probs[mask]
49
+ top_prob = float(np.max(non_neutral)) if non_neutral.size else float(np.max(emotion_probs))
50
+
51
+ confidence = top_prob**kappa
52
+
53
+ w_disc = confidence * ALPHA_PRIORS
54
+ w_cont = 1.0 - w_disc
55
+
56
+ s_disc_eff = DISC_GAIN * s_disc
57
+ s_cont_eff = CONT_GAIN * s_cont
58
+
59
+ return w_disc * s_disc_eff + w_cont * s_cont_eff
app/emotion/pipeline.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """End-to-end emotion -> prosody orchestration (pure, synchronous).
2
+
3
+ This is the heart of the system and is deliberately free of torch, FastAPI, and
4
+ I/O: it takes already-loaded model objects (anything exposing ``infer`` / an
5
+ ``EmotionModel`` with ``.labels``) plus the prebuilt weight matrix, and returns
6
+ the full analysis. That makes it unit-testable with lightweight fakes and no
7
+ model downloads.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from typing import Protocol
13
+
14
+ import numpy as np
15
+
16
+ from app.emotion.fusion import fuse
17
+ from app.emotion.prosody import process_prosody
18
+ from app.emotion.vad_mapping import compute_continuous_shift
19
+ from app.emotion.weights import compute_discrete_shift
20
+ from app.schemas import AnalyzeResponse, EmotionEstimate, VADEstimate
21
+
22
+
23
+ class _Infers(Protocol):
24
+ def infer(self, text: str) -> np.ndarray: ...
25
+
26
+
27
+ def _build_explanation(emotions: list[EmotionEstimate], prosody) -> str:
28
+ top = emotions[0].label if emotions else "neutral"
29
+
30
+ def describe(value: float, up: str, down: str, unit: str) -> str | None:
31
+ if abs(value) < 1e-6:
32
+ return None
33
+ direction = up if value > 0 else down
34
+ return f"{direction} {abs(value):.0f}{unit}"
35
+
36
+ parts = [
37
+ p for p in (
38
+ describe(prosody.pitch_hz, "higher pitch", "lower pitch", " Hz"),
39
+ describe(prosody.rate_pct, "faster rate", "slower rate", "%"),
40
+ describe(prosody.energy_pct, "more energy", "less energy", "%"),
41
+ ) if p
42
+ ]
43
+ pace = "more pauses" if prosody.pause_level > 0.55 else (
44
+ "fewer pauses" if prosody.pause_level < 0.45 else "balanced pauses"
45
+ )
46
+ parts.append(pace)
47
+ detail = ", ".join(parts) if parts else "near-neutral delivery"
48
+ return (
49
+ f"Estimated dominant tone '{top}'. Delivery adjusts toward {detail}. "
50
+ "These are model estimates, not a definitive reading of emotion."
51
+ )
52
+
53
+
54
+ def analyze(
55
+ text: str,
56
+ emotion_model,
57
+ vad_model: _Infers,
58
+ weight_matrix: np.ndarray,
59
+ expressiveness: float = 1.0,
60
+ top_k: int = 5,
61
+ ) -> AnalyzeResponse:
62
+ probs = emotion_model.infer(text)
63
+ vad = vad_model.infer(text)
64
+
65
+ labels = emotion_model.labels
66
+ neutral_index = labels.index("neutral") if "neutral" in labels else None
67
+
68
+ s_disc = compute_discrete_shift(probs, weight_matrix)
69
+ s_cont = compute_continuous_shift(vad)
70
+ s_final = fuse(s_disc, s_cont, probs, neutral_index=neutral_index)
71
+ prosody = process_prosody(s_final, expressiveness=expressiveness)
72
+
73
+ order = np.argsort(probs)[::-1][:top_k]
74
+ top_emotions = [
75
+ EmotionEstimate(label=emotion_model.labels[i], score=round(float(probs[i]), 4))
76
+ for i in order
77
+ ]
78
+
79
+ return AnalyzeResponse(
80
+ text=text,
81
+ top_emotions=top_emotions,
82
+ vad=VADEstimate(
83
+ valence=round(float(vad[0]), 4),
84
+ arousal=round(float(vad[1]), 4),
85
+ dominance=round(float(vad[2]), 4),
86
+ ),
87
+ prosody=prosody,
88
+ explanation=_build_explanation(top_emotions, prosody),
89
+ )
app/emotion/prosody.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Post-processing: fused prosody vector -> bounded, TTS-ready parameters.
2
+
3
+ Scales each fused dimension and clamps it to a safe range. Values are unchanged
4
+ from the original implementation. An optional ``expressiveness`` scalar applies a
5
+ single global gain to the (signed) deltas *before* clamping, so the emotion
6
+ mappings and fusion are untouched — it only dials the overall strength.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import numpy as np
12
+
13
+ from app.schemas import ProsodyParameters
14
+
15
+ # Clamp bounds (previously in config.py PROSODY_CLAMPS; kept with the logic they
16
+ # govern so the mapping is readable in one place).
17
+ PITCH_HZ_CLAMP = 40.0
18
+ RATE_PCT_CLAMP = 35.0
19
+ ENERGY_PCT_CLAMP = 30.0
20
+ PAUSE_GAIN = 0.45
21
+
22
+ # Per-dimension pre-scale (unchanged original factors).
23
+ PITCH_SCALE = 0.60
24
+ RATE_SCALE = 0.85
25
+ ENERGY_SCALE = 1.00
26
+
27
+
28
+ def process_prosody(fused: np.ndarray, expressiveness: float = 1.0) -> ProsodyParameters:
29
+ pitch = np.clip(
30
+ expressiveness * PITCH_SCALE * PITCH_HZ_CLAMP * fused[0],
31
+ -PITCH_HZ_CLAMP, PITCH_HZ_CLAMP,
32
+ )
33
+ rate = np.clip(
34
+ expressiveness * RATE_SCALE * RATE_PCT_CLAMP * fused[1],
35
+ -RATE_PCT_CLAMP, RATE_PCT_CLAMP,
36
+ )
37
+ energy = np.clip(
38
+ expressiveness * ENERGY_SCALE * ENERGY_PCT_CLAMP * fused[2],
39
+ -ENERGY_PCT_CLAMP, ENERGY_PCT_CLAMP,
40
+ )
41
+ # Pause level is centered at 0.5; expressiveness scales the deviation from it.
42
+ pause = np.clip(0.5 + expressiveness * PAUSE_GAIN * fused[3], 0.10, 0.92)
43
+
44
+ return ProsodyParameters(
45
+ pitch_hz=round(float(pitch), 2),
46
+ rate_pct=round(float(rate), 2),
47
+ energy_pct=round(float(energy), 2),
48
+ pause_level=round(float(pause), 3),
49
+ )
app/emotion/vad_mapping.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Continuous affect (VAD) -> prosody mapping.
2
+
3
+ Maps valence/arousal/dominance in [0, 1] to the same 4-D prosody space. Values
4
+ and transform (center to [-1, 1], tanh nonlinearity) are unchanged from the
5
+ original implementation.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import numpy as np
11
+
12
+ # Columns: [Valence, Arousal, Dominance]; rows: [pitch, rate, energy, pauses]
13
+ W_VAD = np.array(
14
+ [
15
+ [0.25, 0.38, -0.18], # pitch
16
+ [0.05, 0.58, -0.08], # rate
17
+ [0.15, 0.48, 0.22], # energy
18
+ [-0.10, -0.55, -0.10], # pauses
19
+ ],
20
+ dtype=np.float64,
21
+ )
22
+
23
+
24
+ def compute_continuous_shift(vad_01: np.ndarray, beta: float = 1.35) -> np.ndarray:
25
+ """Continuous prosody shift from VAD coordinates in [0, 1]."""
26
+ vad_01 = np.clip(np.asarray(vad_01, dtype=np.float64), 0.0, 1.0)
27
+ vad_centered = 2.0 * vad_01 - 1.0
28
+ vad_nonlinear = np.tanh(beta * vad_centered)
29
+ return W_VAD @ vad_nonlinear
app/emotion/weights.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Discrete emotion -> prosody mapping (GoEmotions branch).
2
+
3
+ Handcrafted weights mapping each of the 28 GoEmotions labels to a 4-D prosody
4
+ shift [pitch, rate, energy, pauses]. Values are unchanged from the original
5
+ implementation; only the matrix construction has been decoupled from model
6
+ import so this module can be loaded and tested without downloading a model.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from collections.abc import Sequence
12
+
13
+ import numpy as np
14
+
15
+ # Format: [pitch, rate, energy, pauses]
16
+ EMOTION_WEIGHTS: dict[str, list[float]] = {
17
+ "admiration": [0.15, 0.08, 0.18, -0.10],
18
+ "amusement": [0.25, 0.22, 0.20, -0.20],
19
+ "excitement": [0.45, 0.42, 0.45, -0.45],
20
+ "joy": [0.38, 0.30, 0.35, -0.32],
21
+ "pride": [0.12, -0.08, 0.30, -0.05],
22
+ "love": [0.15, -0.12, 0.08, 0.10],
23
+ "approval": [0.08, 0.05, 0.10, -0.05],
24
+ "caring": [0.05, -0.15, 0.05, 0.15],
25
+ "gratitude": [0.18, 0.05, 0.15, -0.08],
26
+ "optimism": [0.20, 0.12, 0.18, -0.12],
27
+ "relief": [0.05, -0.20, -0.10, 0.18],
28
+ "anger": [0.15, 0.32, 0.50, -0.38],
29
+ "annoyance": [0.05, 0.15, 0.25, -0.12],
30
+ "disgust": [-0.12, -0.10, 0.18, 0.20],
31
+ "fear": [0.25, 0.28, 0.10, 0.18],
32
+ "nervousness": [0.15, 0.18, -0.08, 0.25],
33
+ "disappointment": [-0.22, -0.20, -0.18, 0.28],
34
+ "disapproval": [-0.08, 0.05, 0.18, 0.10],
35
+ "embarrassment": [-0.18, -0.15, -0.22, 0.25],
36
+ "grief": [-0.42, -0.45, -0.40, 0.50],
37
+ "remorse": [-0.25, -0.22, -0.20, 0.32],
38
+ "sadness": [-0.32, -0.30, -0.28, 0.38],
39
+ "confusion": [0.12, -0.15, -0.08, 0.25],
40
+ "curiosity": [0.18, 0.10, 0.08, 0.05],
41
+ "desire": [0.12, -0.05, 0.10, -0.05],
42
+ "realization": [0.10, -0.08, 0.05, 0.15],
43
+ "surprise": [0.35, 0.12, 0.28, 0.08],
44
+ "neutral": [0.0, 0.0, 0.0, 0.0],
45
+ }
46
+
47
+
48
+ def build_emotion_matrix(labels: Sequence[str]) -> np.ndarray:
49
+ """Build the (num_labels x 4) weight matrix aligned to the model's label order.
50
+
51
+ Aligning by label *name* (not index) keeps the mapping correct regardless of
52
+ how the model orders its labels. Raises if a label has no defined mapping.
53
+ """
54
+ try:
55
+ rows = [EMOTION_WEIGHTS[label.lower()] for label in labels]
56
+ except KeyError as exc: # pragma: no cover - defensive
57
+ raise KeyError(f"No prosody weights defined for emotion label: {exc}") from exc
58
+ return np.array(rows, dtype=np.float64)
59
+
60
+
61
+ def compute_discrete_shift(probs: np.ndarray, weight_matrix: np.ndarray) -> np.ndarray:
62
+ """Discrete prosody shift: S_disc = probs @ W_emo."""
63
+ return probs @ weight_matrix
app/main.py CHANGED
@@ -1,35 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
 
 
2
  from fastapi.staticfiles import StaticFiles
3
- from app.services.engine import run_empathy_engine
4
- from app.tts.pause_injection import inject_pauses
5
 
6
- import uuid
7
- from pathlib import Path
8
- from app.tts.tts_engine import synthesize
 
 
 
 
9
 
10
- app = FastAPI()
11
 
12
- audio_dir = Path("audio_outputs")
13
- audio_dir.mkdir(exist_ok=True)
14
 
15
- @app.post("/speak")
16
- async def speak(text:str):
 
 
 
17
 
18
- pitch,rate,energy,pauses = run_empathy_engine(text)
19
- text_for_tts = inject_pauses(text, pauses)
 
 
 
20
 
21
- file=f"audio_outputs/{uuid.uuid4()}.mp3"
22
 
23
- await synthesize(
24
- text_for_tts,
25
- file,
26
- rate=f"{rate:+.0f}%",
27
- pitch=f"{pitch:+.0f}Hz",
28
- volume=f"{energy:+.0f}%"
 
 
 
 
 
 
 
 
 
 
 
 
29
  )
30
 
31
- return {"audio":file}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
- app.mount("/audio_outputs", StaticFiles(directory="audio_outputs"), name="audio_outputs")
35
- app.mount("/", StaticFiles(directory="app/static", html=True), name="static")
 
1
+ """Application factory and lifespan wiring.
2
+
3
+ Startup sequence (distinct, observable stages):
4
+ 1. process starts, logging configured, singletons created, audio dir purged;
5
+ 2. model loading kicked off in the background (server is already accepting
6
+ connections and /health is green);
7
+ 3. /ready flips to true once both models finish loading.
8
+
9
+ Shutdown performs a final audio cleanup.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import logging
15
+ from contextlib import asynccontextmanager
16
+ from pathlib import Path
17
+
18
  from fastapi import FastAPI
19
+ from fastapi.middleware.cors import CORSMiddleware
20
+ from fastapi.responses import RedirectResponse
21
  from fastapi.staticfiles import StaticFiles
 
 
22
 
23
+ from app.api import analyze, audio, health, synthesize, voices
24
+ from app.audio.store import AudioStore
25
+ from app.core.config import get_settings
26
+ from app.core.errors import register_exception_handlers
27
+ from app.core.logging import configure_logging
28
+ from app.ml.manager import ModelManager
29
+ from app.tts.synth import EdgeTTSSynthesizer
30
 
31
+ logger = logging.getLogger(__name__)
32
 
 
 
33
 
34
+ @asynccontextmanager
35
+ async def lifespan(app: FastAPI):
36
+ settings = get_settings()
37
+ configure_logging(settings.log_level)
38
+ logger.info("Starting EmpathVoice")
39
 
40
+ app.state.audio_store = AudioStore(settings.audio_dir, settings.audio_ttl_seconds)
41
+ app.state.audio_store.purge_all() # startup cleanup; nothing persists across runs
42
+ app.state.synthesizer = EdgeTTSSynthesizer()
43
+ app.state.manager = ModelManager(settings)
44
+ await app.state.manager.start_loading() # non-blocking; readiness flips later
45
 
46
+ yield
47
 
48
+ app.state.audio_store.purge_all()
49
+ logger.info("EmpathVoice stopped")
50
+
51
+
52
+ def create_app() -> FastAPI:
53
+ app = FastAPI(
54
+ title="EmpathVoice",
55
+ version="0.2.0",
56
+ summary="Emotion-aware text-to-speech with a transparent prosody pipeline.",
57
+ lifespan=lifespan,
58
+ )
59
+
60
+ # Dev convenience: allow the Vite dev server origin. Same-origin in prod.
61
+ app.add_middleware(
62
+ CORSMiddleware,
63
+ allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
64
+ allow_methods=["GET", "POST"],
65
+ allow_headers=["*"],
66
  )
67
 
68
+ register_exception_handlers(app)
69
+
70
+ # Liveness/readiness at root (conventional for probes); everything else under /api.
71
+ app.include_router(health.router)
72
+ for module in (analyze, synthesize, audio, voices):
73
+ app.include_router(module.router, prefix="/api")
74
+
75
+ # Serve the built React app when present (frontend/dist). API routes and
76
+ # /docs are registered first, so they take precedence over this catch-all
77
+ # mount. If the frontend isn't built, fall back to the API docs.
78
+ dist = Path(__file__).resolve().parent.parent / "frontend" / "dist"
79
+ if dist.is_dir():
80
+ app.mount("/", StaticFiles(directory=dist, html=True), name="frontend")
81
+ else:
82
+ @app.get("/", include_in_schema=False)
83
+ async def root() -> RedirectResponse:
84
+ return RedirectResponse(url="/docs")
85
+
86
+ return app
87
 
88
 
89
+ app = create_app()
 
app/ml/__init__.py ADDED
File without changes
app/ml/emotion_model.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """GoEmotions discrete-emotion model wrapper.
2
+
3
+ Heavy imports (torch/transformers) happen inside ``load`` so this module — and
4
+ anything that imports it — stays importable without the ML stack installed.
5
+ The model is placed on the target device and switched to eval mode (dropout
6
+ off) so inference is deterministic.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import logging
12
+
13
+ import numpy as np
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+
18
+ def _sigmoid(x: np.ndarray) -> np.ndarray:
19
+ return 1.0 / (1.0 + np.exp(-np.clip(x, -20.0, 20.0)))
20
+
21
+
22
+ class EmotionModel:
23
+ def __init__(self, model_name: str) -> None:
24
+ self.model_name = model_name
25
+ self._tokenizer = None
26
+ self._model = None
27
+ self._device = "cpu"
28
+ self.labels: list[str] = []
29
+
30
+ def load(self, device: str) -> None:
31
+ import torch
32
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
33
+
34
+ logger.info("Loading emotion model %s on %s", self.model_name, device)
35
+ self._tokenizer = AutoTokenizer.from_pretrained(self.model_name)
36
+ model = AutoModelForSequenceClassification.from_pretrained(self.model_name)
37
+ model.to(device)
38
+ model.eval() # disable dropout -> deterministic inference
39
+ self._model = model
40
+ self._device = device
41
+ self.labels = [model.config.id2label[i] for i in range(model.config.num_labels)]
42
+ self._torch = torch
43
+
44
+ def infer(self, text: str, temperature: float = 1.1) -> np.ndarray:
45
+ """Return per-label sigmoid scores (multi-label, not a distribution)."""
46
+ if self._model is None:
47
+ raise RuntimeError("EmotionModel.infer called before load()")
48
+ torch = self._torch
49
+ enc = self._tokenizer(
50
+ text, return_tensors="pt", truncation=True, max_length=512
51
+ ).to(self._device)
52
+ with torch.no_grad():
53
+ logits = self._model(**enc).logits.squeeze(0).cpu().numpy()
54
+ return _sigmoid(logits / temperature)
app/ml/manager.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Model lifecycle, device handling, and inference concurrency.
2
+
3
+ Responsibilities, deliberately in one place:
4
+ * detect the device once (auto: cuda if available else cpu; overridable);
5
+ * load both models exactly once per process, in eval mode, on that device;
6
+ * expose a three-state readiness (LOADING -> READY | FAILED) that is distinct
7
+ from "the process is up" (health) and from "a request succeeded";
8
+ * run blocking inference off the event loop, with a semaphore bounding how
9
+ many inferences touch the shared model instance at once.
10
+
11
+ Loading runs in a background thread kicked off at startup, so the server accepts
12
+ connections immediately and ``/ready`` flips to true when the models finish.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import asyncio
18
+ import enum
19
+ import logging
20
+
21
+ from starlette.concurrency import run_in_threadpool
22
+
23
+ from app.core.config import Settings
24
+ from app.emotion import pipeline
25
+ from app.emotion.weights import build_emotion_matrix
26
+ from app.ml.emotion_model import EmotionModel
27
+ from app.ml.vad_model import VADModel
28
+ from app.schemas import AnalyzeResponse
29
+
30
+ logger = logging.getLogger(__name__)
31
+
32
+
33
+ class ModelStatus(str, enum.Enum):
34
+ LOADING = "loading"
35
+ READY = "ready"
36
+ FAILED = "failed"
37
+
38
+
39
+ def _resolve_device(override: str | None) -> str:
40
+ if override:
41
+ return override
42
+ try:
43
+ import torch
44
+
45
+ return "cuda" if torch.cuda.is_available() else "cpu"
46
+ except Exception: # torch missing or broken -> degrade to cpu label
47
+ return "cpu"
48
+
49
+
50
+ class ModelManager:
51
+ def __init__(self, settings: Settings) -> None:
52
+ self._settings = settings
53
+ self.status = ModelStatus.LOADING
54
+ self.device: str | None = None
55
+ self.detail: str | None = None
56
+ self._emotion: EmotionModel | None = None
57
+ self._vad: VADModel | None = None
58
+ self._weight_matrix = None
59
+ self._semaphore = asyncio.Semaphore(settings.inference_concurrency)
60
+
61
+ @property
62
+ def ready(self) -> bool:
63
+ return self.status is ModelStatus.READY
64
+
65
+ async def start_loading(self) -> None:
66
+ """Kick off model loading in the background; returns immediately."""
67
+ asyncio.create_task(self._load_async())
68
+
69
+ async def _load_async(self) -> None:
70
+ try:
71
+ await run_in_threadpool(self._load_blocking)
72
+ self.status = ModelStatus.READY
73
+ logger.info("Models ready on %s", self.device)
74
+ except Exception as exc: # noqa: BLE001 - record, don't crash the app
75
+ self.status = ModelStatus.FAILED
76
+ self.detail = str(exc)
77
+ logger.exception("Model loading failed: %s", exc)
78
+
79
+ def _load_blocking(self) -> None:
80
+ self.device = _resolve_device(self._settings.device)
81
+ emotion = EmotionModel(self._settings.emotion_model)
82
+ emotion.load(self.device)
83
+ vad = VADModel(self._settings.vad_model)
84
+ vad.load(self.device)
85
+ self._weight_matrix = build_emotion_matrix(emotion.labels)
86
+ self._emotion = emotion
87
+ self._vad = vad
88
+
89
+ async def analyze(self, text: str, expressiveness: float = 1.0) -> AnalyzeResponse:
90
+ if not self.ready:
91
+ raise RuntimeError("Models are not ready")
92
+ async with self._semaphore:
93
+ return await run_in_threadpool(
94
+ pipeline.analyze,
95
+ text,
96
+ self._emotion,
97
+ self._vad,
98
+ self._weight_matrix,
99
+ expressiveness,
100
+ )
app/ml/vad_model.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """VAD-BERT continuous-affect model wrapper.
2
+
3
+ Fixes two correctness issues from the original:
4
+ * the model is now moved to the target device (the original left it on CPU
5
+ while sending inputs to the GPU — a crash waiting for any CUDA box);
6
+ * the model is switched to eval mode so dropout no longer randomizes output.
7
+
8
+ The output head is validated to be 3-dimensional (valence, arousal, dominance)
9
+ so a mismatched checkpoint fails loudly at load instead of deep inside NumPy.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import logging
15
+
16
+ import numpy as np
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ def _sigmoid(x: np.ndarray) -> np.ndarray:
22
+ return 1.0 / (1.0 + np.exp(-np.clip(x, -20.0, 20.0)))
23
+
24
+
25
+ class VADModel:
26
+ def __init__(self, model_name: str) -> None:
27
+ self.model_name = model_name
28
+ self._tokenizer = None
29
+ self._model = None
30
+ self._device = "cpu"
31
+
32
+ def load(self, device: str) -> None:
33
+ import torch
34
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
35
+
36
+ logger.info("Loading VAD model %s on %s", self.model_name, device)
37
+ self._tokenizer = AutoTokenizer.from_pretrained(self.model_name)
38
+ model = AutoModelForSequenceClassification.from_pretrained(self.model_name)
39
+ n_out = model.config.num_labels
40
+ if n_out != 3:
41
+ raise ValueError(
42
+ f"VAD model '{self.model_name}' has {n_out} outputs; expected 3 "
43
+ "(valence, arousal, dominance)."
44
+ )
45
+ model.to(device)
46
+ model.eval()
47
+ self._model = model
48
+ self._device = device
49
+ self._torch = torch
50
+
51
+ def infer(self, text: str, temperature: float = 1.2) -> np.ndarray:
52
+ """Return [valence, arousal, dominance], each in [0, 1]."""
53
+ if self._model is None:
54
+ raise RuntimeError("VADModel.infer called before load()")
55
+ torch = self._torch
56
+ enc = self._tokenizer(
57
+ text, return_tensors="pt", truncation=True, max_length=512
58
+ ).to(self._device)
59
+ with torch.no_grad():
60
+ raw = self._model(**enc).logits.squeeze(0).cpu().numpy()
61
+ return _sigmoid(raw / temperature)
app/models/__pycache__/emotion_model.cpython-312.pyc DELETED
Binary file (1.87 kB)
 
app/models/__pycache__/vad_model.cpython-312.pyc DELETED
Binary file (1.58 kB)
 
app/models/emotion_model.py DELETED
@@ -1,22 +0,0 @@
1
- import torch
2
- import numpy as np
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
-
5
- GO_MODEL = "SamLowe/roberta-base-go_emotions"
6
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
-
8
- tok_go = AutoTokenizer.from_pretrained(GO_MODEL)
9
- mdl_go = AutoModelForSequenceClassification.from_pretrained(GO_MODEL).to(device)
10
-
11
- LABELS_GO = [mdl_go.config.id2label[i] for i in range(mdl_go.config.num_labels)]
12
-
13
- def _sigmoid(x):
14
- return 1.0 / (1.0 + np.exp(-np.clip(x, -20.0, 20.0)))
15
-
16
- @torch.no_grad()
17
- def infer_emotions(text, temperature=1.1):
18
-
19
- enc = tok_go(text, return_tensors="pt", truncation=True, max_length=512).to(device)
20
- logits = mdl_go(**enc).logits.squeeze(0).cpu().numpy()
21
-
22
- return _sigmoid(logits / temperature)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/models/vad_model.py DELETED
@@ -1,24 +0,0 @@
1
- import torch
2
- import numpy as np
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
-
5
- VAD_MODEL = "RobroKools/vad-bert"
6
-
7
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
-
9
- tok_vad = AutoTokenizer.from_pretrained(VAD_MODEL)
10
- mdl_vad = AutoModelForSequenceClassification.from_pretrained(VAD_MODEL)
11
-
12
- def _sigmoid(x):
13
- return 1.0 / (1.0 + np.exp(-np.clip(x, -20.0, 20.0)))
14
-
15
- @torch.no_grad()
16
- def infer_vad(text, temperature=1.2):
17
-
18
- enc = tok_vad(text, return_tensors="pt", truncation=True, max_length=512).to(device)
19
- raw = mdl_vad(**enc).logits.squeeze(0).cpu().numpy()
20
-
21
- vad = _sigmoid(raw / temperature)
22
-
23
- return vad
24
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/schemas.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Request/response contracts for the public API.
2
+
3
+ Kept in one module because the surface is small. These models define the
4
+ OpenAPI schema, validate input, and give the frontend typed shapes to mirror.
5
+
6
+ Terminology note: emotion/VAD numbers are *model estimates*, not calibrated
7
+ probabilities or ground-truth assessments of anyone's feelings. Field names and
8
+ descriptions reflect that deliberately.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from pydantic import BaseModel, Field, field_validator
14
+
15
+ from app.core.config import get_settings
16
+
17
+
18
+ def _validate_text(value: str) -> str:
19
+ text = value.strip()
20
+ if not text:
21
+ raise ValueError("text must not be empty")
22
+ limit = get_settings().max_text_chars
23
+ if len(text) > limit:
24
+ raise ValueError(f"text must be at most {limit} characters")
25
+ return text
26
+
27
+
28
+ # --- Shared sub-models -------------------------------------------------------
29
+
30
+ class EmotionEstimate(BaseModel):
31
+ """One discrete emotion label and its (uncalibrated) model score."""
32
+
33
+ label: str
34
+ score: float = Field(description="Sigmoid score in [0, 1]; not a calibrated probability.")
35
+
36
+
37
+ class VADEstimate(BaseModel):
38
+ """Continuous affect estimate, each component in [0, 1]."""
39
+
40
+ valence: float
41
+ arousal: float
42
+ dominance: float
43
+
44
+
45
+ class ProsodyParameters(BaseModel):
46
+ """Final speech-delivery adjustments applied to the TTS voice."""
47
+
48
+ pitch_hz: float = Field(description="Pitch shift in Hz (signed).")
49
+ rate_pct: float = Field(description="Speaking-rate change in percent (signed).")
50
+ energy_pct: float = Field(description="Loudness/energy change in percent (signed).")
51
+ pause_level: float = Field(description="Pause strength scalar in [0, 1].")
52
+
53
+
54
+ # --- /analyze ----------------------------------------------------------------
55
+
56
+ class AnalyzeRequest(BaseModel):
57
+ text: str
58
+
59
+ _check_text = field_validator("text")(_validate_text)
60
+
61
+
62
+ class AnalyzeResponse(BaseModel):
63
+ text: str
64
+ top_emotions: list[EmotionEstimate]
65
+ vad: VADEstimate
66
+ prosody: ProsodyParameters
67
+ explanation: str = Field(
68
+ description="Plain-language summary of how the estimates shaped delivery."
69
+ )
70
+
71
+
72
+ # --- /synthesize -------------------------------------------------------------
73
+
74
+ class SynthesizeRequest(BaseModel):
75
+ text: str
76
+ voice: str | None = Field(default=None, description="Voice id; defaults to server default.")
77
+ expressiveness: float = Field(
78
+ default=1.0,
79
+ ge=0.0,
80
+ le=1.5,
81
+ description="Global scale on prosody deltas before clamping (0 = flat, 1 = normal).",
82
+ )
83
+
84
+ _check_text = field_validator("text")(_validate_text)
85
+
86
+
87
+ class SynthesizeResponse(BaseModel):
88
+ audio_id: str
89
+ audio_url: str
90
+ expires_in_seconds: int
91
+ analysis: AnalyzeResponse
92
+
93
+
94
+ # --- Health / readiness / voices --------------------------------------------
95
+
96
+ class HealthResponse(BaseModel):
97
+ status: str = "ok"
98
+
99
+
100
+ class ReadinessResponse(BaseModel):
101
+ ready: bool
102
+ model_status: str = Field(description="loading | ready | failed")
103
+ device: str | None = None
104
+ detail: str | None = None
105
+
106
+
107
+ class Voice(BaseModel):
108
+ id: str
109
+ label: str
110
+ locale: str
111
+ gender: str
112
+
113
+
114
+ class VoicesResponse(BaseModel):
115
+ default: str
116
+ voices: list[Voice]
app/services/__pycache__/engine.cpython-312.pyc DELETED
Binary file (964 Bytes)
 
app/services/engine.py DELETED
@@ -1,25 +0,0 @@
1
- from app.models.emotion_model import infer_emotions
2
- from app.models.vad_model import infer_vad
3
-
4
- from app.utils.emotion_weights import compute_S_disc
5
- from app.utils.vad_mapping import compute_S_cont
6
- from app.utils.fusion import fuse
7
-
8
- from app.utils.processing import process_prosody
9
-
10
-
11
- def run_empathy_engine(text):
12
-
13
- probs = infer_emotions(text)
14
-
15
- vad = infer_vad(text)
16
-
17
- S_disc = compute_S_disc(probs)
18
-
19
- S_cont = compute_S_cont(vad)
20
-
21
- S_final = fuse(S_disc,S_cont,probs)
22
-
23
- pitch,rate,energy,pauses = process_prosody(S_final)
24
-
25
- return pitch,rate,energy,pauses
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static/index.html DELETED
@@ -1,76 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
- <title>Empathy Engine</title>
7
- <link rel="stylesheet" href="style.css" />
8
- <link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&family=DM+Sans:wght@300;400;500&display=swap" rel="stylesheet"/>
9
- </head>
10
- <body>
11
-
12
- <div class="bg-orbs">
13
- <div class="orb orb-1"></div>
14
- <div class="orb orb-2"></div>
15
- <div class="orb orb-3"></div>
16
- </div>
17
-
18
- <main class="card">
19
-
20
- <header class="card-header">
21
- <div class="icon-wrap">
22
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
23
- <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"/>
24
- <path d="M8 12c0-1.1.4-2.1 1-2.8M16 12c0-1.1-.4-2.1-1-2.8M9 15.5c.8.8 1.9 1.3 3 1.3s2.2-.5 3-1.3"/>
25
- <circle cx="9" cy="10" r="0.8" fill="currentColor"/>
26
- <circle cx="15" cy="10" r="0.8" fill="currentColor"/>
27
- </svg>
28
- </div>
29
- <div>
30
- <h1>Empathy Engine</h1>
31
- <p class="subtitle">Emotion-aware text to speech</p>
32
- </div>
33
- </header>
34
-
35
- <div class="textarea-wrap">
36
- <textarea id="text" placeholder="Type something with feeling — a poem, a message, a thought…" rows="5"></textarea>
37
- <span class="char-count" id="charCount">0</span>
38
- </div>
39
-
40
- <button class="generate-btn" id="generateBtn" onclick="generate()">
41
- <span class="btn-text">Generate Voice</span>
42
- <span class="btn-spinner" id="spinner"></span>
43
- <svg class="btn-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
44
- <polygon points="5 3 19 12 5 21 5 3"/>
45
- </svg>
46
- </button>
47
-
48
- <div class="audio-wrap" id="audioWrap">
49
- <audio id="player" controls></audio>
50
- </div>
51
-
52
- </main>
53
-
54
- <script src="script.js"></script>
55
- <script>
56
- // char counter
57
- const textarea = document.getElementById('text');
58
- const charCount = document.getElementById('charCount');
59
- textarea.addEventListener('input', () => {
60
- charCount.textContent = textarea.value.length;
61
- });
62
-
63
- // loading state wrapper
64
- const _orig = window.generate;
65
- window.generate = async function() {
66
- const btn = document.getElementById('generateBtn');
67
- const aw = document.getElementById('audioWrap');
68
- btn.classList.add('loading');
69
- aw.classList.remove('visible');
70
- await _orig();
71
- btn.classList.remove('loading');
72
- aw.classList.add('visible');
73
- };
74
- </script>
75
- </body>
76
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static/script.js DELETED
@@ -1,36 +0,0 @@
1
- async function generate() {
2
- const textEl = document.getElementById("text");
3
- const player = document.getElementById("player");
4
-
5
- if (!textEl || !player) {
6
- return;
7
- }
8
-
9
- const text = textEl.value.trim();
10
- if (!text) {
11
- alert("Please enter text first.");
12
- return;
13
- }
14
-
15
- try {
16
- const url = `/speak?text=${encodeURIComponent(text)}`;
17
- const response = await fetch(url, { method: "POST" });
18
-
19
- if (!response.ok) {
20
- const err = await response.text();
21
- throw new Error(err || `Request failed with status ${response.status}`);
22
- }
23
-
24
- const payload = await response.json();
25
- if (!payload.audio) {
26
- throw new Error("Backend did not return an audio path.");
27
- }
28
-
29
- // Cache-bust so repeated generations are always fresh.
30
- player.src = `${payload.audio}?t=${Date.now()}`;
31
- await player.play();
32
- } catch (error) {
33
- console.error("Generate failed:", error);
34
- alert("Could not generate audio. Check backend logs for details.");
35
- }
36
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static/style.css DELETED
@@ -1,184 +0,0 @@
1
- *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
2
-
3
- :root {
4
- --bg: #0e0d12;
5
- --surface: #16141c;
6
- --surface2: #1e1b28;
7
- --border: rgba(255,255,255,0.07);
8
- --accent: #e8865a;
9
- --accent2: #c45ea0;
10
- --text: #f0ecff;
11
- --muted: #7a748f;
12
- --radius: 18px;
13
- }
14
-
15
- body {
16
- min-height: 100vh;
17
- background: var(--bg);
18
- display: flex;
19
- align-items: center;
20
- justify-content: center;
21
- font-family: 'DM Sans', sans-serif;
22
- color: var(--text);
23
- padding: 24px;
24
- overflow: hidden;
25
- }
26
-
27
- /* ── floating orbs ── */
28
- .bg-orbs { position: fixed; inset: 0; pointer-events: none; z-index: 0; }
29
- .orb {
30
- position: absolute;
31
- border-radius: 50%;
32
- filter: blur(90px);
33
- opacity: .35;
34
- animation: drift 12s ease-in-out infinite alternate;
35
- }
36
- .orb-1 { width: 340px; height: 340px; background: #c45ea0; top: -80px; left: -60px; animation-delay: 0s; }
37
- .orb-2 { width: 260px; height: 260px; background: #e8865a; bottom: -60px; right: -40px; animation-delay: -4s; }
38
- .orb-3 { width: 200px; height: 200px; background: #5e6ac4; top: 50%; left: 55%; animation-delay: -8s; }
39
- @keyframes drift {
40
- from { transform: translate(0, 0) scale(1); }
41
- to { transform: translate(30px, 20px) scale(1.1); }
42
- }
43
-
44
- /* ── card ── */
45
- .card {
46
- position: relative;
47
- z-index: 1;
48
- width: 100%;
49
- max-width: 560px;
50
- background: var(--surface);
51
- border: 1px solid var(--border);
52
- border-radius: 28px;
53
- padding: 36px;
54
- backdrop-filter: blur(20px);
55
- box-shadow: 0 32px 80px rgba(0,0,0,.55), 0 0 0 1px rgba(255,255,255,.04);
56
- }
57
-
58
- /* ── header ── */
59
- .card-header {
60
- display: flex;
61
- align-items: center;
62
- gap: 16px;
63
- margin-bottom: 28px;
64
- }
65
- .icon-wrap {
66
- width: 48px; height: 48px;
67
- flex-shrink: 0;
68
- background: linear-gradient(135deg, var(--accent2), var(--accent));
69
- border-radius: 14px;
70
- display: flex; align-items: center; justify-content: center;
71
- color: #fff;
72
- }
73
- .icon-wrap svg { width: 24px; height: 24px; }
74
- h1 {
75
- font-family: 'DM Serif Display', serif;
76
- font-size: 1.6rem;
77
- font-weight: 400;
78
- letter-spacing: -.01em;
79
- line-height: 1;
80
- }
81
- .subtitle {
82
- font-size: .78rem;
83
- color: var(--muted);
84
- margin-top: 4px;
85
- font-weight: 300;
86
- letter-spacing: .04em;
87
- text-transform: uppercase;
88
- }
89
-
90
- /* ── textarea ── */
91
- .textarea-wrap {
92
- position: relative;
93
- margin-bottom: 20px;
94
- }
95
- textarea {
96
- width: 100%;
97
- background: var(--surface2);
98
- border: 1px solid var(--border);
99
- border-radius: var(--radius);
100
- color: var(--text);
101
- font-family: 'DM Sans', sans-serif;
102
- font-size: .95rem;
103
- font-weight: 300;
104
- line-height: 1.7;
105
- padding: 18px 20px 32px;
106
- resize: none;
107
- outline: none;
108
- transition: border-color .2s;
109
- }
110
- textarea::placeholder { color: var(--muted); }
111
- textarea:focus { border-color: rgba(232,134,90,.4); }
112
- .char-count {
113
- position: absolute;
114
- bottom: 12px;
115
- right: 16px;
116
- font-size: .72rem;
117
- color: var(--muted);
118
- pointer-events: none;
119
- }
120
-
121
- /* ── button ── */
122
- .generate-btn {
123
- width: 100%;
124
- position: relative;
125
- display: flex;
126
- align-items: center;
127
- justify-content: center;
128
- gap: 10px;
129
- background: linear-gradient(135deg, var(--accent2) 0%, var(--accent) 100%);
130
- border: none;
131
- border-radius: var(--radius);
132
- color: #fff;
133
- font-family: 'DM Sans', sans-serif;
134
- font-size: .95rem;
135
- font-weight: 500;
136
- padding: 15px 24px;
137
- cursor: pointer;
138
- transition: opacity .2s, transform .15s;
139
- overflow: hidden;
140
- }
141
- .generate-btn::before {
142
- content: '';
143
- position: absolute;
144
- inset: 0;
145
- background: rgba(255,255,255,.12);
146
- opacity: 0;
147
- transition: opacity .2s;
148
- }
149
- .generate-btn:hover::before { opacity: 1; }
150
- .generate-btn:active { transform: scale(.98); }
151
- .generate-btn.loading .btn-text { opacity: .4; }
152
- .generate-btn.loading .btn-icon { display: none; }
153
- .generate-btn.loading .btn-spinner { display: block; }
154
-
155
- .btn-icon { width: 16px; height: 16px; }
156
- .btn-spinner {
157
- display: none;
158
- width: 18px; height: 18px;
159
- border: 2px solid rgba(255,255,255,.3);
160
- border-top-color: #fff;
161
- border-radius: 50%;
162
- animation: spin .7s linear infinite;
163
- }
164
- @keyframes spin { to { transform: rotate(360deg); } }
165
-
166
- /* ── audio player ── */
167
- .audio-wrap {
168
- opacity: 0;
169
- transform: translateY(8px);
170
- pointer-events: none;
171
- transition: opacity .35s ease, transform .35s ease;
172
- margin-top: 16px;
173
- }
174
- .audio-wrap.visible {
175
- opacity: 1;
176
- transform: translateY(0);
177
- pointer-events: auto;
178
- }
179
- audio {
180
- width: 100%;
181
- border-radius: 12px;
182
- accent-color: var(--accent);
183
- filter: invert(0);
184
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static1/static/index.html DELETED
@@ -1,115 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6
- <title>Empathy Engine</title>
7
- <link rel="stylesheet" href="style.css" />
8
- <link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&family=DM+Sans:wght@300;400;500&display=swap" rel="stylesheet"/>
9
- </head>
10
- <body>
11
-
12
- <div class="bg-orbs">
13
- <div class="orb orb-1"></div>
14
- <div class="orb orb-2"></div>
15
- <div class="orb orb-3"></div>
16
- </div>
17
-
18
- <main class="card">
19
-
20
- <header class="card-header">
21
- <div class="icon-wrap">
22
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
23
- <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2z"/>
24
- <path d="M8 12c0-1.1.4-2.1 1-2.8M16 12c0-1.1-.4-2.1-1-2.8M9 15.5c.8.8 1.9 1.3 3 1.3s2.2-.5 3-1.3"/>
25
- <circle cx="9" cy="10" r="0.8" fill="currentColor"/>
26
- <circle cx="15" cy="10" r="0.8" fill="currentColor"/>
27
- </svg>
28
- </div>
29
- <div>
30
- <h1>Empathy Engine</h1>
31
- <p class="subtitle">Emotion-aware text to speech</p>
32
- </div>
33
- </header>
34
-
35
- <div class="textarea-wrap">
36
- <textarea id="text" placeholder="Type something with feeling — a poem, a message, a thought…" rows="5"></textarea>
37
- <span class="char-count" id="charCount">0</span>
38
- </div>
39
-
40
- <div class="emotion-bar" id="emotionBar">
41
- <span class="emotion-label">detected tone</span>
42
- <div class="emotion-chips" id="emotionChips">
43
- <span class="chip" data-emotion="calm">calm</span>
44
- <span class="chip" data-emotion="joyful">joyful</span>
45
- <span class="chip" data-emotion="somber">somber</span>
46
- <span class="chip" data-emotion="urgent">urgent</span>
47
- <span class="chip" data-emotion="tender">tender</span>
48
- </div>
49
- </div>
50
-
51
- <button class="generate-btn" id="generateBtn" onclick="generate()">
52
- <span class="btn-text">Generate Voice</span>
53
- <span class="btn-spinner" id="spinner"></span>
54
- <svg class="btn-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
55
- <polygon points="5 3 19 12 5 21 5 3"/>
56
- </svg>
57
- </button>
58
-
59
- <div class="waveform-container" id="waveformContainer">
60
- <div class="waveform" id="waveform">
61
- <span></span><span></span><span></span><span></span><span></span>
62
- <span></span><span></span><span></span><span></span><span></span>
63
- <span></span><span></span><span></span><span></span><span></span>
64
- </div>
65
- </div>
66
-
67
- <div class="audio-wrap" id="audioWrap">
68
- <audio id="player" controls></audio>
69
- </div>
70
-
71
- </main>
72
-
73
- <script src="script.js"></script>
74
- <script>
75
- // char counter
76
- const textarea = document.getElementById('text');
77
- const charCount = document.getElementById('charCount');
78
- textarea.addEventListener('input', () => {
79
- charCount.textContent = textarea.value.length;
80
- detectEmotion(textarea.value);
81
- });
82
-
83
- // naive client-side emotion hint (visual only, doesn't affect /speak)
84
- function detectEmotion(text) {
85
- const t = text.toLowerCase();
86
- const scores = {
87
- joyful: (t.match(/happy|joy|laugh|smile|love|wonderful|great|yay|celebrate/g)||[]).length,
88
- somber: (t.match(/sad|cry|loss|miss|alone|dark|grief|pain|hurt|sorrow/g)||[]).length,
89
- urgent: (t.match(/now|fast|hurry|quick|stop|help|please|must|need|critical/g)||[]).length,
90
- tender: (t.match(/gentle|soft|care|hug|hold|warm|dear|heart|baby|sweet/g)||[]).length,
91
- calm: (t.match(/peace|quiet|still|breath|slow|rest|serene|calm|ease/g)||[]).length,
92
- };
93
- const max = Math.max(...Object.values(scores));
94
- document.querySelectorAll('.chip').forEach(chip => {
95
- chip.classList.remove('active');
96
- if (max > 0 && scores[chip.dataset.emotion] === max) chip.classList.add('active');
97
- });
98
- }
99
-
100
- // patch generate to show loading state & waveform
101
- const _orig = window.generate;
102
- window.generate = async function() {
103
- const btn = document.getElementById('generateBtn');
104
- const wc = document.getElementById('waveformContainer');
105
- const aw = document.getElementById('audioWrap');
106
- btn.classList.add('loading');
107
- wc.classList.add('active');
108
- aw.classList.remove('visible');
109
- await _orig();
110
- btn.classList.remove('loading');
111
- aw.classList.add('visible');
112
- };
113
- </script>
114
- </body>
115
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static1/static/script.js DELETED
@@ -1,36 +0,0 @@
1
- async function generate() {
2
- const textEl = document.getElementById("text");
3
- const player = document.getElementById("player");
4
-
5
- if (!textEl || !player) {
6
- return;
7
- }
8
-
9
- const text = textEl.value.trim();
10
- if (!text) {
11
- alert("Please enter text first.");
12
- return;
13
- }
14
-
15
- try {
16
- const url = `/speak?text=${encodeURIComponent(text)}`;
17
- const response = await fetch(url, { method: "POST" });
18
-
19
- if (!response.ok) {
20
- const err = await response.text();
21
- throw new Error(err || `Request failed with status ${response.status}`);
22
- }
23
-
24
- const payload = await response.json();
25
- if (!payload.audio) {
26
- throw new Error("Backend did not return an audio path.");
27
- }
28
-
29
- // Cache-bust so repeated generations are always fresh.
30
- player.src = `${payload.audio}?t=${Date.now()}`;
31
- await player.play();
32
- } catch (error) {
33
- console.error("Generate failed:", error);
34
- alert("Could not generate audio. Check backend logs for details.");
35
- }
36
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/static1/static/style.css DELETED
@@ -1,260 +0,0 @@
1
- *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
2
-
3
- :root {
4
- --bg: #0e0d12;
5
- --surface: #16141c;
6
- --surface2: #1e1b28;
7
- --border: rgba(255,255,255,0.07);
8
- --accent: #e8865a;
9
- --accent2: #c45ea0;
10
- --text: #f0ecff;
11
- --muted: #7a748f;
12
- --radius: 18px;
13
- }
14
-
15
- body {
16
- min-height: 100vh;
17
- background: var(--bg);
18
- display: flex;
19
- align-items: center;
20
- justify-content: center;
21
- font-family: 'DM Sans', sans-serif;
22
- color: var(--text);
23
- padding: 24px;
24
- overflow: hidden;
25
- }
26
-
27
- /* ── floating orbs ── */
28
- .bg-orbs { position: fixed; inset: 0; pointer-events: none; z-index: 0; }
29
- .orb {
30
- position: absolute;
31
- border-radius: 50%;
32
- filter: blur(90px);
33
- opacity: .35;
34
- animation: drift 12s ease-in-out infinite alternate;
35
- }
36
- .orb-1 { width: 340px; height: 340px; background: #c45ea0; top: -80px; left: -60px; animation-delay: 0s; }
37
- .orb-2 { width: 260px; height: 260px; background: #e8865a; bottom: -60px; right: -40px; animation-delay: -4s; }
38
- .orb-3 { width: 200px; height: 200px; background: #5e6ac4; top: 50%; left: 55%; animation-delay: -8s; }
39
- @keyframes drift {
40
- from { transform: translate(0, 0) scale(1); }
41
- to { transform: translate(30px, 20px) scale(1.1); }
42
- }
43
-
44
- /* ── card ── */
45
- .card {
46
- position: relative;
47
- z-index: 1;
48
- width: 100%;
49
- max-width: 560px;
50
- background: var(--surface);
51
- border: 1px solid var(--border);
52
- border-radius: 28px;
53
- padding: 36px;
54
- backdrop-filter: blur(20px);
55
- box-shadow: 0 32px 80px rgba(0,0,0,.55), 0 0 0 1px rgba(255,255,255,.04);
56
- }
57
-
58
- /* ── header ── */
59
- .card-header {
60
- display: flex;
61
- align-items: center;
62
- gap: 16px;
63
- margin-bottom: 28px;
64
- }
65
- .icon-wrap {
66
- width: 48px; height: 48px;
67
- flex-shrink: 0;
68
- background: linear-gradient(135deg, var(--accent2), var(--accent));
69
- border-radius: 14px;
70
- display: flex; align-items: center; justify-content: center;
71
- color: #fff;
72
- }
73
- .icon-wrap svg { width: 24px; height: 24px; }
74
- h1 {
75
- font-family: 'DM Serif Display', serif;
76
- font-size: 1.6rem;
77
- font-weight: 400;
78
- letter-spacing: -.01em;
79
- line-height: 1;
80
- }
81
- .subtitle {
82
- font-size: .78rem;
83
- color: var(--muted);
84
- margin-top: 4px;
85
- font-weight: 300;
86
- letter-spacing: .04em;
87
- text-transform: uppercase;
88
- }
89
-
90
- /* ── textarea ── */
91
- .textarea-wrap {
92
- position: relative;
93
- margin-bottom: 16px;
94
- }
95
- textarea {
96
- width: 100%;
97
- background: var(--surface2);
98
- border: 1px solid var(--border);
99
- border-radius: var(--radius);
100
- color: var(--text);
101
- font-family: 'DM Sans', sans-serif;
102
- font-size: .95rem;
103
- font-weight: 300;
104
- line-height: 1.7;
105
- padding: 18px 20px 32px;
106
- resize: none;
107
- outline: none;
108
- transition: border-color .2s;
109
- }
110
- textarea::placeholder { color: var(--muted); }
111
- textarea:focus { border-color: rgba(232,134,90,.4); }
112
- .char-count {
113
- position: absolute;
114
- bottom: 12px;
115
- right: 16px;
116
- font-size: .72rem;
117
- color: var(--muted);
118
- pointer-events: none;
119
- }
120
-
121
- /* ── emotion chips ── */
122
- .emotion-bar {
123
- display: flex;
124
- align-items: center;
125
- gap: 10px;
126
- margin-bottom: 20px;
127
- flex-wrap: wrap;
128
- }
129
- .emotion-label {
130
- font-size: .7rem;
131
- color: var(--muted);
132
- text-transform: uppercase;
133
- letter-spacing: .06em;
134
- flex-shrink: 0;
135
- }
136
- .emotion-chips { display: flex; gap: 6px; flex-wrap: wrap; }
137
- .chip {
138
- font-size: .72rem;
139
- padding: 4px 12px;
140
- border-radius: 100px;
141
- border: 1px solid var(--border);
142
- color: var(--muted);
143
- background: var(--surface2);
144
- transition: all .25s;
145
- }
146
- .chip.active {
147
- border-color: var(--accent);
148
- color: var(--accent);
149
- background: rgba(232,134,90,.1);
150
- }
151
-
152
- /* ── button ── */
153
- .generate-btn {
154
- width: 100%;
155
- position: relative;
156
- display: flex;
157
- align-items: center;
158
- justify-content: center;
159
- gap: 10px;
160
- background: linear-gradient(135deg, var(--accent2) 0%, var(--accent) 100%);
161
- border: none;
162
- border-radius: var(--radius);
163
- color: #fff;
164
- font-family: 'DM Sans', sans-serif;
165
- font-size: .95rem;
166
- font-weight: 500;
167
- padding: 15px 24px;
168
- cursor: pointer;
169
- transition: opacity .2s, transform .15s;
170
- overflow: hidden;
171
- }
172
- .generate-btn::before {
173
- content: '';
174
- position: absolute;
175
- inset: 0;
176
- background: rgba(255,255,255,.12);
177
- opacity: 0;
178
- transition: opacity .2s;
179
- }
180
- .generate-btn:hover::before { opacity: 1; }
181
- .generate-btn:active { transform: scale(.98); }
182
- .generate-btn.loading .btn-text { opacity: .4; }
183
- .generate-btn.loading .btn-icon { display: none; }
184
- .generate-btn.loading .btn-spinner { display: block; }
185
-
186
- .btn-icon { width: 16px; height: 16px; }
187
- .btn-spinner {
188
- display: none;
189
- width: 18px; height: 18px;
190
- border: 2px solid rgba(255,255,255,.3);
191
- border-top-color: #fff;
192
- border-radius: 50%;
193
- animation: spin .7s linear infinite;
194
- }
195
- @keyframes spin { to { transform: rotate(360deg); } }
196
-
197
- /* ── waveform ── */
198
- .waveform-container {
199
- height: 0;
200
- overflow: hidden;
201
- transition: height .4s ease;
202
- display: flex;
203
- align-items: center;
204
- justify-content: center;
205
- }
206
- .waveform-container.active { height: 56px; }
207
- .waveform {
208
- display: flex;
209
- align-items: center;
210
- gap: 4px;
211
- height: 40px;
212
- }
213
- .waveform span {
214
- display: block;
215
- width: 3px;
216
- border-radius: 99px;
217
- background: linear-gradient(to top, var(--accent2), var(--accent));
218
- animation: wave 1.2s ease-in-out infinite;
219
- height: 6px;
220
- }
221
- .waveform span:nth-child(1) { animation-delay: 0s; }
222
- .waveform span:nth-child(2) { animation-delay: .08s; }
223
- .waveform span:nth-child(3) { animation-delay: .16s; }
224
- .waveform span:nth-child(4) { animation-delay: .24s; }
225
- .waveform span:nth-child(5) { animation-delay: .32s; }
226
- .waveform span:nth-child(6) { animation-delay: .4s; }
227
- .waveform span:nth-child(7) { animation-delay: .48s; }
228
- .waveform span:nth-child(8) { animation-delay: .56s; }
229
- .waveform span:nth-child(9) { animation-delay: .48s; }
230
- .waveform span:nth-child(10) { animation-delay: .4s; }
231
- .waveform span:nth-child(11) { animation-delay: .32s; }
232
- .waveform span:nth-child(12) { animation-delay: .24s; }
233
- .waveform span:nth-child(13) { animation-delay: .16s; }
234
- .waveform span:nth-child(14) { animation-delay: .08s; }
235
- .waveform span:nth-child(15) { animation-delay: 0s; }
236
-
237
- @keyframes wave {
238
- 0%, 100% { height: 6px; opacity: .4; }
239
- 50% { height: 32px; opacity: 1; }
240
- }
241
-
242
- /* ── audio player ── */
243
- .audio-wrap {
244
- opacity: 0;
245
- transform: translateY(8px);
246
- pointer-events: none;
247
- transition: opacity .35s ease, transform .35s ease;
248
- margin-top: 16px;
249
- }
250
- .audio-wrap.visible {
251
- opacity: 1;
252
- transform: translateY(0);
253
- pointer-events: auto;
254
- }
255
- audio {
256
- width: 100%;
257
- border-radius: 12px;
258
- accent-color: var(--accent);
259
- filter: invert(0);
260
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/tts/__init__.py ADDED
File without changes
app/tts/__pycache__/pause_injection.cpython-312.pyc DELETED
Binary file (1.88 kB)