File size: 12,081 Bytes
49525ce
 
 
 
 
 
 
b42769f
 
 
 
 
 
 
 
49525ce
f67144d
49525ce
 
f67144d
49525ce
 
 
 
 
 
 
 
 
 
b42769f
 
 
 
 
 
 
 
49525ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f67144d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42769f
49525ce
 
 
f67144d
49525ce
f67144d
 
49525ce
 
f67144d
 
 
49525ce
 
f67144d
 
49525ce
 
f67144d
49525ce
f67144d
 
 
49525ce
 
f67144d
 
49525ce
 
f67144d
 
b42769f
f67144d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49525ce
f67144d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b681a
f67144d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49525ce
f67144d
 
49525ce
 
 
f67144d
 
49525ce
 
 
b42769f
 
 
 
 
 
 
 
 
 
49525ce
53c4a2d
49525ce
53c4a2d
 
d2b681a
 
49525ce
 
 
 
 
 
 
d9154dc
49525ce
 
 
 
d9154dc
 
 
 
49525ce
d9154dc
 
 
 
49525ce
 
 
 
 
 
 
 
 
 
 
 
 
d9154dc
 
 
 
 
 
 
 
 
 
49525ce
d9154dc
49525ce
 
 
 
 
 
 
 
 
 
 
 
d9154dc
49525ce
 
 
 
 
 
 
f67144d
49525ce
 
 
 
 
 
 
f67144d
49525ce
 
 
dd7a14a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
app.py - High-Performance Hugging Face Space (Gradio + ZeroGPU)
===============================================================
Deploys Anvaya Speech Pathology & Articulation Diagnostics on Hugging Face Spaces
with support for Free ZeroGPU / 16 GB RAM CPU execution.
"""
from __future__ import annotations

# CRITICAL FOR HUGGING FACE ZEROGPU: 'import spaces' MUST occur before torch / transformers
try:
    import spaces
    HAS_SPACES = True
except ImportError:
    HAS_SPACES = False

import gc
import json
import os
import time
import traceback
from pathlib import Path
from typing import Optional, Dict, Any, List, Tuple

import gradio as gr
import numpy as np
import soundfile as sf
import torch

from ml.model.engine import SpeechDiagnosticEngine

# Lazy-loaded / Singleton Engine
_engine_instance: Optional[SpeechDiagnosticEngine] = None

def get_engine() -> SpeechDiagnosticEngine:
    global _engine_instance
    if _engine_instance is None:
        _engine_instance = SpeechDiagnosticEngine.get_instance()
    return _engine_instance

# Presets
WORD_PRESETS = [
    "rabbit",
    "red",
    "sun",
    "sweet",
    "three",
    "water",
    "kitten",
    "spot",
]

SENTENCE_PRESETS = {
    "The Red Rabbit (Rhotacism & Rhotic 'R' Evaluation)": "the red rabbit ran around the green yard",
    "The Sweet Sun (Sigmatism & Sibilant 'S' Evaluation)": "the sweet sun shines softly in the sky",
    "The Blue Spot (Phonetic Balance & Plosives)": "the blue spot is on the key",
    "The Rainbow Passage (Standard Clinical Protocol)": "the rainbow is a division of white light into many beautiful colors",
}


def _to_json_safe(obj: Any) -> Any:
    """Recursively convert numpy/torch structures to JSON-serializable primitives."""
    if isinstance(obj, dict):
        return {str(k): _to_json_safe(v) for k, v in obj.items()}
    elif isinstance(obj, (list, tuple)):
        return [_to_json_safe(x) for x in obj]
    elif isinstance(obj, (np.floating, float)):
        return float(obj)
    elif isinstance(obj, (np.integer, int)):
        return int(obj)
    elif isinstance(obj, np.ndarray):
        return _to_json_safe(obj.tolist())
    else:
        return obj


def _diagnose_speech_core(
    audio_path: Optional[str],
    target_phrase: str,
    healthy_baseline_path: Optional[str] = None,
) -> Tuple[str, str, str, str, str, str, dict]:
    """Run full diagnostic pipeline and return formatted clinical report for Gradio."""
    empty_dict = {"status": "waiting_for_input"}
    
    if not audio_path:
        return (
            "**Notice**: Please record speech or upload an audio file to evaluate.",
            "NO AUDIO",
            "N/A",
            "N/A",
            "",
            "",
            empty_dict,
        )

    if not target_phrase or not target_phrase.strip():
        return (
            "**Notice**: Please enter or select an expected Target Phrase.",
            "NO TARGET",
            "N/A",
            "N/A",
            "",
            "",
            empty_dict,
        )

    try:
        engine = get_engine()

        # Execute Diagnostic Engine
        diag_res = engine.diagnose_audio(
            audio_input=audio_path,
            target_phrase=target_phrase,
            normal_calibration_audio=healthy_baseline_path,
        )

        if diag_res.get("is_silent") or diag_res["decision"].get("is_silent"):
            return (
                "**No Speech Detected**: The audio is silent or below acoustic energy thresholds. Please speak clearly into your microphone.",
                "SILENT",
                "0 / 100",
                "0.0%",
                "",
                "- **Silence Guard Active**: No vocal signal detected in audio stream.",
                _to_json_safe(diag_res),
            )

        result = diag_res["decision"]
        pron = diag_res["pronunciation"]
        flaws = diag_res["flaws"]
        artic = diag_res["articulation"]
        p_stut = float(diag_res["stutter_probs"][1]) if (diag_res.get("stutter_probs") and len(diag_res["stutter_probs"]) > 1) else 0.0

        overall_bucket = result["buckets"]["overall"].upper()
        fluency_score = f"{int(result.get('fluency_100', 100))} / 100"
        pron_acc = f"{max(0.0, min(100.0, (1.0 - pron.get('wer', 0.0)) * 100.0)):.1f}%"

        # Build Word Alignment Chips HTML
        alignment = pron.get("alignment", [])
        chips_html = "<div style='display:flex; flex-wrap:wrap; gap:8px; padding:12px; background:rgba(15,23,42,0.6); border-radius:8px; margin:10px 0;'>"
        for item in alignment:
            status = item["status"]
            exp = item["expected"]
            spk = item["spoken"]
            if status == "correct":
                chips_html += f"<span style='padding:6px 12px; background:rgba(16,185,129,0.15); color:#34D399; border:1px solid rgba(16,185,129,0.3); border-radius:6px; font-weight:600;'>[MATCH] {exp}</span>"
            elif status == "substitution":
                chips_html += f"<span style='padding:6px 12px; background:rgba(239,68,68,0.15); color:#F87171; border:1px solid rgba(239,68,68,0.3); border-radius:6px; font-weight:600;'>[DIFF] {exp} (heard: \"{spk}\")</span>"
            elif status == "omission":
                chips_html += f"<span style='padding:6px 12px; background:rgba(245,158,11,0.15); color:#FBBF24; border:1px solid rgba(245,158,11,0.3); border-radius:6px; font-weight:600;'>[UNSPOKEN] {exp}</span>"
            elif status == "insertion":
                chips_html += f"<span style='padding:6px 12px; background:rgba(168,85,247,0.15); color:#C084FC; border:1px solid rgba(168,85,247,0.3); border-radius:6px; font-weight:600;'>[EXTRA] {spk}</span>"
        chips_html += "</div>"

        # Build Flaw Report Summary Markdown
        flaws_md = "### Specific Speech Pathology Findings:\n\n"
        if flaws["has_r_flaw"]:
            for r_err in flaws["r_sound_issues"]:
                flaws_md += f"- **Rhotacism Flaw**: {r_err['message']}\n"
        else:
            flaws_md += "- **'R' Sound Articulation**: Accurate (No R->W/L substitution detected).\n"

        if flaws["has_s_flaw"]:
            for s_err in flaws["s_sound_issues"]:
                flaws_md += f"- **Sigmatism Flaw**: {s_err['message']}\n"
        else:
            flaws_md += "- **'S' Sound Articulation**: Accurate (No sibilant lisp detected).\n"

        if p_stut >= 0.78:
            flaws_md += f"- **Disfluency Detected**: Elevated probability of repetition/block ({p_stut*100:.1f}%)\n"
        elif p_stut >= 0.60:
            flaws_md += f"- **Mild Hesitation**: Minor syllable repetition observed ({p_stut*100:.1f}%)\n"
        else:
            flaws_md += "- **Fluency Flow**: Continuous cadence (No disfluent events detected).\n"

        flaws_md += f"- **Voice Phonation Correlates**: Pitch F0={artic.get('f0_median_hz',0):.1f}Hz, HNR={artic.get('hnr_db',0):.1f}dB, Jitter={artic.get('jitter',0)*100:.2f}%\n"

        confidence_val = result.get("confidence", "high")
        heard_summary = f"**Decoded Transcription**: *\"{pron.get('asr_hypothesis','')}\"*\n\n**Confidence Rating**: `{confidence_val}` | **Latency**: `{diag_res['latency_ms']} ms`"

        return (
            heard_summary,
            overall_bucket,
            fluency_score,
            pron_acc,
            chips_html,
            flaws_md,
            _to_json_safe(diag_res),
        )
    except Exception as ex:
        err_msg = f"**Execution Error**: {ex}\n\n```\n{traceback.format_exc()}\n```"
        return (
            err_msg,
            "ERROR",
            "0 / 100",
            "0.0%",
            "",
            f"- **Internal Error**: {ex}",
            {"error": str(ex), "traceback": traceback.format_exc()},
        )


# Apply ZeroGPU acceleration decorator if running on Hugging Face ZeroGPU
if HAS_SPACES:
    @spaces.GPU
    def diagnose_speech_hf(*args, **kwargs):
        return _diagnose_speech_core(*args, **kwargs)
else:
    def diagnose_speech_hf(*args, **kwargs):
        return _diagnose_speech_core(*args, **kwargs)


# Construct Gradio Modern Interface
with gr.Blocks(title="Anvaya | Speech Screening & Phonetics") as demo:
    gr.Markdown("""
    # ANVAYA · Speech Screening & Phonetic Analysis Assistant
    ### Multi-Modal Screening: Neural Disfluency · Rhotacism ('r') · Sigmatism ('s' Lisp) · Voice Acoustics
    
    > **CLINICAL PRACTICE & RESEARCH DISCLAIMER**: Anvaya is an exploratory engineering prototype for speech screening and practice. It is **not** an FDA-cleared medical device, nor a substitute for professional clinical evaluation by a licensed Speech-Language Pathologist (SLP).
    """)

    with gr.Row():
        with gr.Column(scale=1):
            audio_input = gr.Audio(
                sources=["microphone", "upload"],
                type="filepath",
                label="Audio Ingestion (Record Microphone or Upload Audio)",
            )

            gr.Markdown("#### Single-Word Practice Presets:")
            with gr.Row():
                btn_w1 = gr.Button("rabbit", size="sm")
                btn_w2 = gr.Button("red", size="sm")
                btn_w3 = gr.Button("sun", size="sm")
                btn_w4 = gr.Button("sweet", size="sm")
            with gr.Row():
                btn_w5 = gr.Button("three", size="sm")
                btn_w6 = gr.Button("water", size="sm")
                btn_w7 = gr.Button("kitten", size="sm")
                btn_w8 = gr.Button("spot", size="sm")

            target_preset = gr.Dropdown(
                choices=list(SENTENCE_PRESETS.keys()),
                label="Standardized Clinical Protocols:",
                value="The Red Rabbit (Rhotacism & Rhotic 'R' Evaluation)",
            )

            target_text = gr.Textbox(
                label="Target Phrase (Expected Spoken Text):",
                value=SENTENCE_PRESETS["The Red Rabbit (Rhotacism & Rhotic 'R' Evaluation)"],
                lines=2,
            )

            # Bind Word Preset Buttons to set target phrase text
            btn_w1.click(fn=lambda: "rabbit", outputs=[target_text])
            btn_w2.click(fn=lambda: "red", outputs=[target_text])
            btn_w3.click(fn=lambda: "sun", outputs=[target_text])
            btn_w4.click(fn=lambda: "sweet", outputs=[target_text])
            btn_w5.click(fn=lambda: "three", outputs=[target_text])
            btn_w6.click(fn=lambda: "water", outputs=[target_text])
            btn_w7.click(fn=lambda: "kitten", outputs=[target_text])
            btn_w8.click(fn=lambda: "spot", outputs=[target_text])

            target_preset.change(
                fn=lambda k: SENTENCE_PRESETS.get(k, ""),
                inputs=[target_preset],
                outputs=[target_text],
            )

            healthy_baseline = gr.Audio(
                sources=["upload"],
                type="filepath",
                label="Healthy Baseline Sample (Optional 'My Normal' Calibration):",
            )

            diagnose_btn = gr.Button("Run Diagnostic Analysis", variant="primary", size="lg")

        with gr.Column(scale=2):
            with gr.Row():
                kpi_strat = gr.Textbox(label="Clinical Stratification", interactive=False)
                kpi_fluency = gr.Textbox(label="Fluency Index", interactive=False)
                kpi_acc = gr.Textbox(label="Pronunciation Accuracy", interactive=False)

            summary_box = gr.Markdown("### Clinical Assessment Summary\n*Results will appear here after analysis.*")
            alignment_html = gr.HTML(label="Word-Level Alignment")
            flaws_box = gr.Markdown("### Specific Speech Pathology Findings\n*Sound checks will appear here.*")

            with gr.Accordion("Auditable Telemetry & Acoustic Evidence Trace", open=False):
                raw_json = gr.JSON()

    diagnose_btn.click(
        fn=diagnose_speech_hf,
        inputs=[audio_input, target_text, healthy_baseline],
        outputs=[summary_box, kpi_strat, kpi_fluency, kpi_acc, alignment_html, flaws_box, raw_json],
    )

if __name__ == "__main__":
    demo.launch()