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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MomsVoiceAI β Fix & Optimization Report
|
| 2 |
+
|
| 3 |
+
**Date:** 2026-06-14
|
| 4 |
+
**Branch:** `fix/voice-clone-card`, `fix/lfm-model-param`
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## 1. LFM2.5-Audio Inference (`inference_lfm.py`)
|
| 9 |
+
|
| 10 |
+
### 1.1 System Prompt Fix
|
| 11 |
+
**Before:** Story context was embedded in the system prompt as a custom "friendly storyteller" instruction.
|
| 12 |
+
**After:** System prompt simplified to the official interleaved speech-to-speech directive:
|
| 13 |
+
```
|
| 14 |
+
"Respond with interleaved text and audio."
|
| 15 |
+
```
|
| 16 |
+
Story context moved to the **user turn** as a text prefix, which is the correct placement for the LFM2.5-Audio interleaved model.
|
| 17 |
+
|
| 18 |
+
### 1.2 Audio Decode β Streaming β Post-generation
|
| 19 |
+
**Before:** Mimi streaming decode ran frame-by-frame during `generate_interleaved`, requiring `mimi.streaming(1)` context and manual state resets (`_reset_mimi_streaming_state`).
|
| 20 |
+
**After:** Audio tokens are collected during generation, then decoded in one call via `processor.decode(audio_codes)`. This is more stable and avoids Mimi internal buffer corruption between calls.
|
| 21 |
+
|
| 22 |
+
```python
|
| 23 |
+
# Before (streaming, fragile)
|
| 24 |
+
with mimi.streaming(1):
|
| 25 |
+
for t in model.generate_interleaved(...):
|
| 26 |
+
if t.numel() == 8:
|
| 27 |
+
wav_chunk = mimi.decode(codes)
|
| 28 |
+
|
| 29 |
+
# After (post-generation, stable)
|
| 30 |
+
for t in model.generate_interleaved(...):
|
| 31 |
+
if t.numel() == 8:
|
| 32 |
+
audio_out.append(t)
|
| 33 |
+
audio_codes = torch.stack(audio_out[:-1], dim=1).unsqueeze(0) # drop EOS frame
|
| 34 |
+
waveform = processor.decode(audio_codes).cpu().float()[0].numpy()
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
### 1.3 Removed `torch.compile`
|
| 38 |
+
`torch.compile(mode="reduce-overhead", dynamic=True)` was applied to the LFM model on CUDA. This caused silent audio output and Mimi state bugs in practice. Removed in favor of eager mode.
|
| 39 |
+
|
| 40 |
+
### 1.4 Mimi Warmup in `get_lfm_model()`
|
| 41 |
+
An eager Mimi decode warmup now runs immediately after model load on CUDA to force lazy construction and catch device errors early:
|
| 42 |
+
```python
|
| 43 |
+
with torch.no_grad(), mimi.streaming(1):
|
| 44 |
+
mimi.decode(torch.randint(0, 2048, (1, 8, 1), device=device))
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### 1.5 `max_new_tokens` 100 β 512 β 150
|
| 48 |
+
**Before (original):** Default was `100` tokens β too short for a complete spoken answer.
|
| 49 |
+
**After (first fix):** Raised to `512` to give the model room to finish. See Β§5 for why this was later revised.
|
| 50 |
+
**After (current):** Reduced to `150`. See Β§5 for full reasoning.
|
| 51 |
+
|
| 52 |
+
### 1.6 New Functions Added
|
| 53 |
+
| Function | Purpose |
|
| 54 |
+
|---|---|
|
| 55 |
+
| `get_model_status()` | Returns device, dtype, GPU memory usage for diagnostics |
|
| 56 |
+
| `text_to_audio_lfm()` | LFM-based TTS mode (read text aloud via interleaved generation) |
|
| 57 |
+
| `_assemble_waveform()` | Shared waveform assembly + peak normalization helper |
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## 2. Q&A Recording Flow (`app.py`)
|
| 62 |
+
|
| 63 |
+
### 2.1 Removed JS Auto-click on Ask Button
|
| 64 |
+
**Before:** Clicking "β Ask a Question" ran JavaScript to auto-click the Record button 100ms later. This fired before mic permission was granted, breaking browser microphone access.
|
| 65 |
+
**After:** JS removed. The user clicks Record manually (required by browser security model β mic access requires a direct user gesture on the Record button itself).
|
| 66 |
+
|
| 67 |
+
### 2.2 `stop_recording` Wired Directly to Python
|
| 68 |
+
**Before:** `stop_recording` used `fn=None` + a JS hack that clicked the "Get Answer" submit button after a 600ms delay.
|
| 69 |
+
**After:** `stop_recording` calls `auto_submit_on_stop` directly in Python, which immediately invokes LFM inference.
|
| 70 |
+
|
| 71 |
+
```
|
| 72 |
+
Recording stops β stop_recording β auto_submit_on_stop β build_qa_response
|
| 73 |
+
β answer_question_audio (LFM inference) β answer audio plays automatically
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### 2.3 Removed Double-trigger Race Condition
|
| 77 |
+
**Before:** Both `question_audio.change` and `stop_recording` (via JS) triggered submission independently, causing a race condition where LFM was called twice.
|
| 78 |
+
**After:** `question_audio.change` handler removed. `stop_recording` is the single auto-trigger. `claim_audio_submission` dedup guards against any remaining duplicate calls. The "Get Answer" button remains as a manual fallback for text-only questions.
|
| 79 |
+
|
| 80 |
+
---
|
| 81 |
+
|
| 82 |
+
## 3. Voice Clone Card Management (`app.py`)
|
| 83 |
+
|
| 84 |
+
### 3.1 Persistent Voice Cards Across Page Reloads
|
| 85 |
+
**Before:** `voices_state` was always initialized from `mock_voices` (hardcoded placeholders). Cloned voice profiles saved to `Voice_Profile/` never appeared as cards after a page reload.
|
| 86 |
+
**After:** On startup, `list_saved_profiles()` reads all saved `.pt` profiles from disk and builds `_initial_voices` to populate `voices_state`. Previously cloned voices appear as cards immediately on next launch.
|
| 87 |
+
|
| 88 |
+
### 3.2 Voice Resets to Vivian on Page Refresh
|
| 89 |
+
**Before:** `voice_profile_state` was initialized to `_default_profile_id` (most recently saved clone), so refreshing the browser silently loaded the clone.
|
| 90 |
+
**After:** `voice_profile_state = gr.State(None)` β always starts with the stock Vivian voice. The user explicitly selects a clone by clicking its card.
|
| 91 |
+
|
| 92 |
+
### 3.3 Active Narrator Indicator
|
| 93 |
+
Added `narrator_info` HTML component in the Library player panel displaying the currently active voice. Updates on:
|
| 94 |
+
- Voice card click
|
| 95 |
+
- Book selection
|
| 96 |
+
|
| 97 |
+
### 3.4 Removed Fake Placeholder Voice Cards
|
| 98 |
+
`Grandpa Joseph` and `Aunt Sarah` were placeholder cards with no real voice profile. Clicking them silently fell through to the stock Vivian voice, which was misleading. Removed. Only real profiles and `Mom's Voice` (Vivian stock) remain.
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## 4. Pipeline Comparison β Demo vs Production
|
| 103 |
+
|
| 104 |
+
The full speech-to-speech pipeline matches `app_qa_flow_demo.py` and `inference_lfm_fix.py`:
|
| 105 |
+
|
| 106 |
+
| Stage | Demo / Fix | Production (current) |
|
| 107 |
+
|---|---|---|
|
| 108 |
+
| Audio input SR | `librosa.load(sr=16000)` | Same β |
|
| 109 |
+
| System prompt | `"Respond with interleaved text and audio."` | Same β |
|
| 110 |
+
| Story context | User turn prefix | Same β |
|
| 111 |
+
| `max_new_tokens` | `512` | `150` (reduced β see Β§5) |
|
| 112 |
+
| `audio_temperature` | `1.0` | Same β |
|
| 113 |
+
| `audio_top_k` | `4` | Same β |
|
| 114 |
+
| Audio decode | `processor.decode(audio_codes)` | Same β |
|
| 115 |
+
| Normalization | `* (0.9 / peak)` | Same β |
|
| 116 |
+
| Return values | 4 `(text, wav, sr, gen_stats)` | 3 `(text, wav, sr)` β `gen_stats` dropped for `qa_flow.py` compatibility |
|
| 117 |
+
| `stop_recording` trigger | Direct Python call | Same β |
|
| 118 |
+
| Dedup | `claim_audio_submission` | Same β |
|
| 119 |
+
|
| 120 |
+
The only intentional difference: `inference_lfm_fix.py` returns a 4th `gen_stats` dict. Dropped in production because `qa_flow.py` unpacks exactly 3 values (`answer_text, waveform, sr = answer_fn(...)`). Stats are logged internally instead.
|
| 121 |
+
|
| 122 |
+
---
|
| 123 |
+
|
| 124 |
+
## 5. Q&A Answer Quality β Prompt Engineering & Token Budget
|
| 125 |
+
|
| 126 |
+
### 5.1 Problem: Long, Repetitive Answers
|
| 127 |
+
|
| 128 |
+
With `max_new_tokens=512` and a minimal system prompt, the model frequently:
|
| 129 |
+
- Repeated the question before answering
|
| 130 |
+
- Produced 10β20 second audio responses for simple factual questions
|
| 131 |
+
- Ran out of generation budget mid-sentence on longer story contexts due to KV cache pressure
|
| 132 |
+
|
| 133 |
+
### 5.2 How `generate_interleaved` Counts Tokens
|
| 134 |
+
|
| 135 |
+
`generate_interleaved` yields one logical step at a time:
|
| 136 |
+
- 1 text token β `t.numel() == 1`
|
| 137 |
+
- 1 audio frame (8 Mimi codec tokens) β `t.numel() == 8`
|
| 138 |
+
|
| 139 |
+
`max_new_tokens` counts **logical steps**, not raw codec tokens. A 1-2 sentence spoken answer requires approximately:
|
| 140 |
+
- ~30 text tokens
|
| 141 |
+
- ~50 audio frames (~4 seconds at 12.5 fps)
|
| 142 |
+
- **Total: ~80 steps**
|
| 143 |
+
|
| 144 |
+
`max_new_tokens=512` allowed up to ~41 seconds of audio β 6Γ more than needed. If the model missed the EOS it would generate until the cap, wasting GPU memory and time.
|
| 145 |
+
|
| 146 |
+
### 5.3 Fix: `max_new_tokens` 512 β 150
|
| 147 |
+
|
| 148 |
+
150 steps covers a 4β6 second spoken answer with headroom. `text_to_audio_lfm` (TTS) retains `max_new_tokens=1024` since reading longer text aloud legitimately requires more frames.
|
| 149 |
+
|
| 150 |
+
### 5.4 Fix: Three-layer Prompt Constraint
|
| 151 |
+
|
| 152 |
+
Brevity is now enforced at three prompt positions so the model cannot ignore any single one:
|
| 153 |
+
|
| 154 |
+
**Layer 1 β System prompt** (sets global behavior):
|
| 155 |
+
```
|
| 156 |
+
Before: "Respond with interleaved text and audio."
|
| 157 |
+
After: "Respond with interleaved text and audio. Give a short, direct answer in 1-2 sentences. Do not repeat the question."
|
| 158 |
+
```
|
| 159 |
+
`"Do not repeat the question"` targets the most common failure mode: the model echoing the question before answering, which consumes ~20 tokens and audio frames with no value.
|
| 160 |
+
|
| 161 |
+
**Layer 2 β User context prefix** (constrains to story content):
|
| 162 |
+
```
|
| 163 |
+
Before: f"Story context:\n{story_context[:3000]}\n\nAnswer the question in 1-2 short sentences based only on the story."
|
| 164 |
+
After: f"Story context:\n{story_context[:2000]}\n\nBased only on the story above, answer briefly."
|
| 165 |
+
```
|
| 166 |
+
Context truncation also tightened from 3000 β 2000 characters (~750 β 500 tokens), leaving more of the context window budget for generation.
|
| 167 |
+
|
| 168 |
+
**Layer 3 β User turn closing** (highest recency β last text model reads before generating):
|
| 169 |
+
```python
|
| 170 |
+
chat.add_text("Answer in 1-2 sentences only.")
|
| 171 |
+
chat.end_turn()
|
| 172 |
+
```
|
| 173 |
+
Placed after the question (audio or text) so it is the final input before generation begins. Recency gives this the most direct influence on output length.
|
| 174 |
+
|
| 175 |
+
### 5.5 Why Three Layers?
|
| 176 |
+
|
| 177 |
+
Interleaved audio+text models do not respond to a single brevity instruction as reliably as pure text LLMs. The system prompt sets the prior, the context prefix reinforces it mid-conversation, and the closing instruction overrides drift just before generation starts. All three are needed for consistent short answers across different question types.
|
| 178 |
+
|
| 179 |
+
### 5.6 Summary of Changes in `answer_question_audio`
|
| 180 |
+
|
| 181 |
+
| Parameter | Before | After |
|
| 182 |
+
|---|---|---|
|
| 183 |
+
| `max_new_tokens` default | `512` | `150` |
|
| 184 |
+
| System prompt | `"Respond with interleaved text and audio."` | + brevity + no-repeat instruction |
|
| 185 |
+
| Story context truncation | `[:3000]` chars | `[:2000]` chars |
|
| 186 |
+
| Context instruction | `"Answer the question in 1-2 short sentencesβ¦"` | `"β¦answer briefly."` |
|
| 187 |
+
| Closing instruction | None | `"Answer in 1-2 sentences only."` after question |
|