Spaces:
Configuration error
Configuration error
Upload ml/cli.py with huggingface_hub
Browse files
ml/cli.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
ml/cli.py - Single entry point for the whole diagnostic pipeline
|
| 3 |
================================================================
|
| 4 |
Glues: data build -> synthetic lattice -> stutter model -> pronunciation/articulation ->
|
| 5 |
-
fusion/self-calibration -> evaluation into one `
|
| 6 |
|
| 7 |
Commands:
|
| 8 |
download fetch real corpora from HF Hub
|
|
@@ -10,12 +10,13 @@ Commands:
|
|
| 10 |
synth-data generate high-quality semi-synthetic disfluency lattice dataset
|
| 11 |
train fine-tune wav2vec2 + LoRA stutter classifier with Focal Loss
|
| 12 |
eval out-of-speaker accuracy/precision/recall/F1 + evidence
|
| 13 |
-
|
| 14 |
diagnose run the full multi-modal diagnosis on an audio file
|
| 15 |
-
self-check run
|
| 16 |
|
| 17 |
Example:
|
| 18 |
python -m ml.cli diagnose --input my_speech.wav --prompt "The weather is nice"
|
|
|
|
| 19 |
"""
|
| 20 |
from __future__ import annotations
|
| 21 |
import argparse
|
|
@@ -60,92 +61,99 @@ def cmd_synth_data(args):
|
|
| 60 |
|
| 61 |
def cmd_train(args):
|
| 62 |
from ml.model.stutter_trainer import train
|
| 63 |
-
train(
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def cmd_fusion_fit(args):
|
| 69 |
from ml.model.fusion_fit import fit_fusion
|
| 70 |
-
fit_fusion(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def cmd_eval(args):
|
| 74 |
from ml.model.evaluate import evaluate
|
| 75 |
-
evaluate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
def cmd_diagnose(args):
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
pron = (mispronunciation_gop(args.input, args.prompt)
|
| 87 |
-
if args.prompt else
|
| 88 |
-
{"pron_score": None, "note": "no reference prompt supplied"})
|
| 89 |
-
|
| 90 |
-
# -- stutter probs from the REAL trained checkpoint --
|
| 91 |
-
sm = load_model(args.ckpt) if args.ckpt else None
|
| 92 |
-
probs = stutter_probs(args.input, sm) if sm else None
|
| 93 |
-
|
| 94 |
-
# -- self-calibration optional --
|
| 95 |
-
cal = None
|
| 96 |
-
if args.calibrate and sm is not None:
|
| 97 |
-
from ml.model.fusion import calibrate_from_normal
|
| 98 |
-
norm = stutter_probs(args.calibrate, sm)
|
| 99 |
-
if norm is not None:
|
| 100 |
-
cal = calibrate_from_normal(norm)
|
| 101 |
-
|
| 102 |
-
result = diag_statistics(probs, pron, artic, cal)
|
| 103 |
if args.json:
|
| 104 |
-
print(json.dumps(
|
| 105 |
else:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
def cmd_selfcheck(args):
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
print(
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
def build_parser():
|
| 140 |
-
ap = argparse.ArgumentParser(prog="anvaya", description="multi-modal speech diagnostic")
|
| 141 |
sub = ap.add_subparsers(dest="cmd", required=True)
|
| 142 |
|
| 143 |
-
p = sub.add_parser("download", help="
|
| 144 |
-
p.add_argument("--only", nargs="*")
|
| 145 |
p.add_argument("--dry-run", action="store_true")
|
| 146 |
p.set_defaults(func=cmd_download)
|
| 147 |
|
| 148 |
-
p = sub.add_parser("build-dataset")
|
| 149 |
p.add_argument("--corpora", nargs="*")
|
| 150 |
p.add_argument("--seed", type=int, default=42)
|
| 151 |
p.set_defaults(func=cmd_build_dataset)
|
|
@@ -158,7 +166,7 @@ def build_parser():
|
|
| 158 |
p.add_argument("--merge-real", action="store_true")
|
| 159 |
p.set_defaults(func=cmd_synth_data)
|
| 160 |
|
| 161 |
-
p = sub.add_parser("train")
|
| 162 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 163 |
p.add_argument("--out", default="ml/models/stutter")
|
| 164 |
p.add_argument("--epochs", type=int, default=5)
|
|
@@ -172,14 +180,14 @@ def build_parser():
|
|
| 172 |
p.add_argument("--focal-gamma", type=float, default=2.0)
|
| 173 |
p.set_defaults(func=cmd_train)
|
| 174 |
|
| 175 |
-
p = sub.add_parser("fusion-fit", help="fit
|
| 176 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 177 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
| 178 |
p.add_argument("--out", default="reports/ev")
|
| 179 |
p.add_argument("--device", default=None)
|
| 180 |
p.set_defaults(func=cmd_fusion_fit)
|
| 181 |
|
| 182 |
-
p = sub.add_parser("eval")
|
| 183 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 184 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
| 185 |
p.add_argument("--out", default="reports/ev")
|
|
@@ -187,7 +195,7 @@ def build_parser():
|
|
| 187 |
p.add_argument("--device", default=None)
|
| 188 |
p.set_defaults(func=cmd_eval)
|
| 189 |
|
| 190 |
-
p = sub.add_parser("diagnose")
|
| 191 |
p.add_argument("--input", required=True)
|
| 192 |
p.add_argument("--prompt", default="")
|
| 193 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
|
@@ -195,7 +203,7 @@ def build_parser():
|
|
| 195 |
p.add_argument("--json", action="store_true")
|
| 196 |
p.set_defaults(func=cmd_diagnose)
|
| 197 |
|
| 198 |
-
p = sub.add_parser("self-check")
|
| 199 |
p.set_defaults(func=cmd_selfcheck)
|
| 200 |
return ap
|
| 201 |
|
|
|
|
| 2 |
ml/cli.py - Single entry point for the whole diagnostic pipeline
|
| 3 |
================================================================
|
| 4 |
Glues: data build -> synthetic lattice -> stutter model -> pronunciation/articulation ->
|
| 5 |
+
fusion/self-calibration -> evaluation into one `ml.cli` command.
|
| 6 |
|
| 7 |
Commands:
|
| 8 |
download fetch real corpora from HF Hub
|
|
|
|
| 10 |
synth-data generate high-quality semi-synthetic disfluency lattice dataset
|
| 11 |
train fine-tune wav2vec2 + LoRA stutter classifier with Focal Loss
|
| 12 |
eval out-of-speaker accuracy/precision/recall/F1 + evidence
|
| 13 |
+
fusion-fit fit and evaluate offline logistic regression vs heuristic fusion
|
| 14 |
diagnose run the full multi-modal diagnosis on an audio file
|
| 15 |
+
self-check run strict pipeline self-checks (including explicit rabbit->wabbit)
|
| 16 |
|
| 17 |
Example:
|
| 18 |
python -m ml.cli diagnose --input my_speech.wav --prompt "The weather is nice"
|
| 19 |
+
python -m ml.cli self-check
|
| 20 |
"""
|
| 21 |
from __future__ import annotations
|
| 22 |
import argparse
|
|
|
|
| 61 |
|
| 62 |
def cmd_train(args):
|
| 63 |
from ml.model.stutter_trainer import train
|
| 64 |
+
return train(
|
| 65 |
+
data_dir=args.data,
|
| 66 |
+
out_dir=args.out,
|
| 67 |
+
epochs=args.epochs,
|
| 68 |
+
lr=args.lr,
|
| 69 |
+
batch=args.batch,
|
| 70 |
+
seed=args.seed,
|
| 71 |
+
fp16=not args.no_fp16,
|
| 72 |
+
binary=args.binary,
|
| 73 |
+
balance_train=args.balance,
|
| 74 |
+
focal_gamma=args.focal_gamma,
|
| 75 |
+
)
|
| 76 |
|
| 77 |
|
| 78 |
def cmd_fusion_fit(args):
|
| 79 |
from ml.model.fusion_fit import fit_fusion
|
| 80 |
+
fit_fusion(
|
| 81 |
+
data_dir=args.data,
|
| 82 |
+
ckpt_dir=args.ckpt,
|
| 83 |
+
out=args.out,
|
| 84 |
+
device=args.device,
|
| 85 |
+
)
|
| 86 |
|
| 87 |
|
| 88 |
def cmd_eval(args):
|
| 89 |
from ml.model.evaluate import evaluate
|
| 90 |
+
evaluate(
|
| 91 |
+
data_dir=args.data,
|
| 92 |
+
ckpt_dir=args.ckpt,
|
| 93 |
+
out=args.out,
|
| 94 |
+
threshold=args.threshold,
|
| 95 |
+
device=args.device,
|
| 96 |
+
)
|
| 97 |
|
| 98 |
|
| 99 |
def cmd_diagnose(args):
|
| 100 |
+
from ml.model.engine import SpeechDiagnosticEngine
|
| 101 |
+
engine = SpeechDiagnosticEngine.get_instance(ckpt_dir=args.ckpt)
|
| 102 |
+
res = engine.diagnose_audio(
|
| 103 |
+
audio_input=args.input,
|
| 104 |
+
target_phrase=args.prompt,
|
| 105 |
+
normal_calibration_audio=args.calibrate,
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
if args.json:
|
| 108 |
+
print(json.dumps(res, indent=2, default=str))
|
| 109 |
else:
|
| 110 |
+
dec = res["decision"]
|
| 111 |
+
print(f"Overall Classification: {dec['buckets']['overall'].upper()}")
|
| 112 |
+
print(f"Fluency Index: {dec['fluency_100']} / 100")
|
| 113 |
+
print(f"Confidence Level: {dec.get('confidence', 'N/A')}")
|
| 114 |
+
print(f"ASR Hypothesis: \"{res['pronunciation'].get('asr_hypothesis', '')}\"")
|
| 115 |
+
print(f"Pronunciation Score: {res['pronunciation'].get('pron_score', 0)*100:.1f}%")
|
| 116 |
+
print(f"Total Flaws Detected: {res['flaws']['total_flaws_count']}")
|
| 117 |
+
print(f"Inference Latency: {res['latency_ms']} ms")
|
| 118 |
|
| 119 |
|
| 120 |
def cmd_selfcheck(args):
|
| 121 |
+
from ml.model import pron_eval
|
| 122 |
+
print("[1/3] Checking DSP signal conditioning & VAD...")
|
| 123 |
+
test_wave = np.random.randn(16000).astype(np.float32) * 0.1
|
| 124 |
+
cond = pron_eval._apply_acoustic_conditioning(test_wave, 16000)
|
| 125 |
+
assert len(cond) == 16000, "Length mismatch in conditioning"
|
| 126 |
+
|
| 127 |
+
print("[2/3] Checking dynamic programming alignment & explicit rabbit->wabbit rhotacism...")
|
| 128 |
+
align = pron_eval.align_words("the red rabbit", "the wed wabbit")
|
| 129 |
+
# Verify exact word-level substitution status on rabbit vs wabbit
|
| 130 |
+
rabbit_item = next((item for item in align if item["expected"] == "rabbit"), None)
|
| 131 |
+
assert rabbit_item is not None, "Rabbit was omitted from alignment"
|
| 132 |
+
assert rabbit_item["status"] == "substitution", f"rabbit->wabbit was marked {rabbit_item['status']}, expected substitution"
|
| 133 |
+
|
| 134 |
+
flaws = pron_eval.analyze_speech_flaws("the red rabbit", "the wed wabbit", align, {})
|
| 135 |
+
assert any(e["expected"] == "rabbit" for e in flaws["r_sound_issues"]), "rabbit->wabbit substitution was not captured in r_sound_issues!"
|
| 136 |
+
assert any(e["expected"] == "red" for e in flaws["r_sound_issues"]), "red->wed substitution was not captured in r_sound_issues!"
|
| 137 |
+
|
| 138 |
+
print("[3/3] Checking sigmatism detection on sun->thun and sweet->thweet...")
|
| 139 |
+
align_s = pron_eval.align_words("the sweet sun", "the thweet thun")
|
| 140 |
+
flaws_s = pron_eval.analyze_speech_flaws("the sweet sun", "the thweet thun", align_s, {})
|
| 141 |
+
assert any(e["expected"] == "sun" for e in flaws_s["s_sound_issues"]), "sun->thun was not captured in s_sound_issues!"
|
| 142 |
+
assert any(e["expected"] == "sweet" for e in flaws_s["s_sound_issues"]), "sweet->thweet was not captured in s_sound_issues!"
|
| 143 |
+
|
| 144 |
+
print("All strict pipeline self-checks PASSED!")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def build_parser() -> argparse.ArgumentParser:
|
| 148 |
+
ap = argparse.ArgumentParser(prog="ml.cli", description="Anvaya Speech Pathology Diagnostic CLI")
|
|
|
|
|
|
|
| 149 |
sub = ap.add_subparsers(dest="cmd", required=True)
|
| 150 |
|
| 151 |
+
p = sub.add_parser("download", help="download external corpora from HuggingFace Hub")
|
| 152 |
+
p.add_argument("--only", nargs="*", help="corpus keys to download")
|
| 153 |
p.add_argument("--dry-run", action="store_true")
|
| 154 |
p.set_defaults(func=cmd_download)
|
| 155 |
|
| 156 |
+
p = sub.add_parser("build-dataset", help="assemble unified HF dataset")
|
| 157 |
p.add_argument("--corpora", nargs="*")
|
| 158 |
p.add_argument("--seed", type=int, default=42)
|
| 159 |
p.set_defaults(func=cmd_build_dataset)
|
|
|
|
| 166 |
p.add_argument("--merge-real", action="store_true")
|
| 167 |
p.set_defaults(func=cmd_synth_data)
|
| 168 |
|
| 169 |
+
p = sub.add_parser("train", help="train stutter LoRA model")
|
| 170 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 171 |
p.add_argument("--out", default="ml/models/stutter")
|
| 172 |
p.add_argument("--epochs", type=int, default=5)
|
|
|
|
| 180 |
p.add_argument("--focal-gamma", type=float, default=2.0)
|
| 181 |
p.set_defaults(func=cmd_train)
|
| 182 |
|
| 183 |
+
p = sub.add_parser("fusion-fit", help="fit and evaluate offline logistic fusion vs heuristic")
|
| 184 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 185 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
| 186 |
p.add_argument("--out", default="reports/ev")
|
| 187 |
p.add_argument("--device", default=None)
|
| 188 |
p.set_defaults(func=cmd_fusion_fit)
|
| 189 |
|
| 190 |
+
p = sub.add_parser("eval", help="evaluate out-of-speaker test split")
|
| 191 |
p.add_argument("--data", default="data/synthetic_lattice/dataset")
|
| 192 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
| 193 |
p.add_argument("--out", default="reports/ev")
|
|
|
|
| 195 |
p.add_argument("--device", default=None)
|
| 196 |
p.set_defaults(func=cmd_eval)
|
| 197 |
|
| 198 |
+
p = sub.add_parser("diagnose", help="run multi-modal diagnosis on audio file")
|
| 199 |
p.add_argument("--input", required=True)
|
| 200 |
p.add_argument("--prompt", default="")
|
| 201 |
p.add_argument("--ckpt", default="ml/models/stutter/stutter_lora")
|
|
|
|
| 203 |
p.add_argument("--json", action="store_true")
|
| 204 |
p.set_defaults(func=cmd_diagnose)
|
| 205 |
|
| 206 |
+
p = sub.add_parser("self-check", help="run strict self-checks")
|
| 207 |
p.set_defaults(func=cmd_selfcheck)
|
| 208 |
return ap
|
| 209 |
|