forked from mergeos-bounties/Loru
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
416 lines (350 loc) · 13.8 KB
/
Copy pathcli.py
File metadata and controls
416 lines (350 loc) · 13.8 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import annotations
import json
from pathlib import Path
from typing import Optional
import typer
from rich.console import Console
from rich.table import Table
from loru import __version__
from loru.config import OUT_DIR, RUNS_DIR, SAMPLES_DIR
from loru.data.loader import list_sample_files, sequence_summary
from loru.data.stats import compute_sequence_stats, detect_outliers, print_stats_table, print_outliers_table, print_summary
from loru.data.wlasl import load_wlasl_manifest, write_wlasl_manifest
from loru.infer.pipeline import sign_to_voice
from loru.infer.text import gloss_to_sentence, multi_gloss_to_sentence, sign_to_text
from loru.models.vocab import DEFAULT_GLOSS
from loru.train.toy_train import train_toy
app = typer.Typer(
help="Loru — sign-to-text and sign-to-voice (runnable offline demo).",
no_args_is_help=True,
)
data_app = typer.Typer(help="Dataset helpers")
samples_app = typer.Typer(help="Browse local landmark samples")
infer_app = typer.Typer(help="Inference (sign→text / sign→voice)")
train_app = typer.Typer(help="Training")
eval_app = typer.Typer(help="Evaluation")
gloss_app = typer.Typer(help="Inspect and compare gloss samples")
app.add_typer(data_app, name="data")
app.add_typer(samples_app, name="samples")
app.add_typer(infer_app, name="infer")
app.add_typer(train_app, name="train")
app.add_typer(eval_app, name="eval")
app.add_typer(gloss_app, name="gloss")
console = Console()
@app.command("version")
def version_cmd() -> None:
console.print(f"Loru {__version__}")
console.print(f"Demo gloss vocab ({len(DEFAULT_GLOSS)}): {', '.join(DEFAULT_GLOSS)}")
@data_app.command("stats")
def stats_cmd(
method: str = typer.Option("iqr", help="Outlier detection method (iqr or zscore)"),
threshold: float = typer.Option(1.5, help="Outlier threshold"),
directory: Optional[str] = typer.Option(None, help="Custom samples directory"),
) -> None:
"""Report frame counts per sample and flag outliers."""
from loru.config import SAMPLES_DIR
dir_path = Path(directory) if directory else SAMPLES_DIR
stats = compute_sequence_stats(dir_path)
outliers = detect_outliers(stats, method=method, threshold=threshold)
print_stats_table(stats)
print_outliers_table(outliers)
print_summary(stats, outliers)
@app.command("gui")
def gui_cmd() -> None:
"""Launch modern Qt desktop demo (pip install -e '.[gui]')."""
from loru.gui.app import main as gui_main
raise SystemExit(gui_main())
@app.command("demo")
def demo_cmd() -> None:
"""Run full offline demo: list samples, train, infer text+voice on hello."""
files = list_sample_files()
console.print(f"[cyan]samples[/cyan]={len(files)} dir={SAMPLES_DIR}")
report = train_toy(epochs=2)
console.print(f"[green]train accuracy[/green]={report['history'][-1]['accuracy']}")
hello = SAMPLES_DIR / "hello.json"
if not hello.exists() and files:
hello = files[0]
text = sign_to_text(hello)
console.print_json(data=text)
wav = OUT_DIR / "demo_hello.wav"
voice = sign_to_voice(hello, wav)
console.print(f"[green]voice[/green] {voice['audio_path']}")
console.print("[bold]Demo complete — offline sign→text→voice works.[/bold]")
@infer_app.command("stream")
def infer_stream(
glosses: str = typer.Option(
"hello thanks",
"--glosses",
"-g",
help="Space-separated gloss stream (continuous stub)",
),
) -> None:
"""Progressive multi-gloss sentence stream (scaffold for live recognition)."""
from loru.infer.stream import stream_glosses
parts = [g for g in glosses.replace(",", " ").split() if g.strip()]
console.print_json(data=stream_glosses(parts))
@infer_app.command("extract")
def infer_extract(
gloss: str = typer.Option("hello", "--gloss", "-g"),
out: Path | None = typer.Option(None, "--out", "-o"),
source: Path | None = typer.Option(None, "--source", "-s", exists=True, dir_okay=False),
frames: int = typer.Option(8, "--frames", "-n", min=1, max=60),
) -> None:
"""Extract/write landmark sample JSON (MediaPipe if available, else synthetic)."""
from loru.config import OUT_DIR
from loru.infer.extract import extract_landmarks, write_extract
payload = extract_landmarks(source, gloss=gloss, frames=frames)
path = out or (OUT_DIR / f"extract_{gloss}.json")
write_extract(payload, path)
console.print(f"[green]extract[/green] {path} frames={len(payload.get('frames') or [])} via={payload.get('extractor')}")
@data_app.command("vocab")
def data_vocab() -> None:
"""List demo gloss vocabulary with indices."""
table = Table(title=f"DEFAULT_GLOSS ({len(DEFAULT_GLOSS)})")
table.add_column("#", justify="right")
table.add_column("Gloss")
for i, g in enumerate(DEFAULT_GLOSS):
table.add_row(str(i), g)
console.print(table)
@data_app.command("sentence")
def data_sentence(
glosses: str = typer.Option(
"hello thanks",
"--glosses",
"-g",
help="Space-separated glosses → natural-language sentence",
),
) -> None:
"""Compose a sentence from multiple glosses (offline templates)."""
parts = [g for g in glosses.replace(",", " ").split() if g.strip()]
console.print_json(
data={
"glosses": parts,
"sentence": multi_gloss_to_sentence(parts),
"single": {g: gloss_to_sentence(g) for g in parts[:8]},
}
)
@data_app.command("coverage")
def data_coverage() -> None:
"""Which DEFAULT_GLOSS entries have bundled sample JSON."""
files = {p.stem for p in list_sample_files()}
table = Table(title="Gloss sample coverage")
table.add_column("Gloss")
table.add_column("Sample")
have = 0
for g in DEFAULT_GLOSS:
ok = g in files
if ok:
have += 1
table.add_row(g, "yes" if ok else "no")
console.print(table)
console.print(f"[dim]{have}/{len(DEFAULT_GLOSS)} glosses have samples[/dim]")
@data_app.command("wlasl-manifest")
def data_wlasl_manifest(
index: Path = typer.Option(
...,
"--index",
"-i",
exists=True,
dir_okay=False,
help="WLASL-style index JSON file.",
),
samples_dir: Path | None = typer.Option(
None,
"--samples-dir",
file_okay=False,
help="Directory of local Loru sample JSON files to match by normalized gloss.",
),
out: Path | None = typer.Option(
None,
"--out",
"-o",
help="Optional output path for the converted manifest JSON.",
),
) -> None:
"""Convert WLASL-style metadata into a Loru sequence manifest."""
if out:
manifest = write_wlasl_manifest(index, out, samples_dir=samples_dir)
console.print(f"[green]WLASL manifest[/green] {out} entries={len(manifest)}")
return
console.print_json(data=load_wlasl_manifest(index, samples_dir=samples_dir))
@data_app.command("export-csv")
def data_export_csv(
out: Path = typer.Option(None, "--out", "-o", help="Output CSV file path. Default: data/out/vocab_export.csv"),
) -> None:
"""Export gloss vocabulary as CSV with index and has_sample flag (for teachers)."""
import csv
from loru.config import OUT_DIR
files = {p.stem for p in list_sample_files()}
out_path = out or (OUT_DIR / "vocab_export.csv")
out_path.parent.mkdir(parents=True, exist_ok=True)
rows = []
for i, g in enumerate(DEFAULT_GLOSS):
has_sample = "true" if g in files else "false"
rows.append({"index": str(i), "gloss": g, "has_sample": has_sample})
with open(out_path, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["index", "gloss", "has_sample"])
writer.writeheader()
writer.writerows(rows)
console.print(f"[green]CSV written[/green] → {out_path} ({len(rows)} rows)")
@data_app.command("list")
def data_list() -> None:
files = list_sample_files()
if not files:
console.print(f"[yellow]No samples in {SAMPLES_DIR}[/yellow]")
raise typer.Exit()
table = Table(title=f"Samples ({len(files)})")
table.add_column("File")
table.add_column("Gloss")
table.add_column("Frames")
for path in files:
summary = sequence_summary(path)
table.add_row(path.name, summary["gloss"], str(summary["frames"]))
console.print(table)
@gloss_app.command("compare")
def gloss_compare(
sample_a: Path = typer.Option(..., "--a", exists=True, dir_okay=False),
sample_b: Path = typer.Option(..., "--b", exists=True, dir_okay=False),
) -> None:
"""Compare frame counts and a crude time-aligned landmark distance."""
from loru.data.compare import compare_gloss_samples
try:
console.print_json(data=compare_gloss_samples(sample_a, sample_b))
except ValueError as exc:
console.print(f"[red]{exc}[/red]")
raise typer.Exit(code=1) from exc
@samples_app.command("list")
def samples_list(
gloss: str | None = typer.Option(
None,
"--gloss",
"-g",
help="Case-insensitive gloss substring to include.",
),
directory: Path | None = typer.Option(
None,
"--directory",
"-d",
exists=True,
file_okay=False,
help="Custom sample directory. Defaults to data/samples.",
),
) -> None:
"""List samples with language and frame counts, optionally filtered by gloss."""
query = gloss.strip().lower() if gloss else None
rows = []
for path in list_sample_files(directory):
summary = sequence_summary(path)
if query and query not in summary["gloss"]:
continue
rows.append((path, summary))
root = directory or SAMPLES_DIR
if not rows:
detail = f" matching gloss '{gloss}'" if gloss else ""
console.print(f"[yellow]No samples{detail} in {root}[/yellow]")
raise typer.Exit()
table = Table(title=f"Samples ({len(rows)})")
table.add_column("File")
table.add_column("Gloss")
table.add_column("Language")
table.add_column("Frames", justify="right")
for path, summary in rows:
table.add_row(
path.name,
summary["gloss"],
summary["language"],
str(summary["frames"]),
)
console.print(table)
@infer_app.command("demo")
def infer_demo(sign: str = typer.Option("hello", "--sign", "-s")) -> None:
console.print(f"[cyan]gloss[/cyan]={sign}")
console.print(f"[green]text[/green]={gloss_to_sentence(sign)}")
@infer_app.command("text")
def infer_text(
sequence: Path = typer.Option(..., "--sequence", "-i", exists=True, dir_okay=False),
) -> None:
console.print_json(data=sign_to_text(sequence))
@infer_app.command("voice")
def infer_voice(
sequence: Path = typer.Option(..., "--sequence", "-i", exists=True, dir_okay=False),
out: Path = typer.Option(None, "--out", "-o"),
) -> None:
out_path = out or (OUT_DIR / f"{sequence.stem}.wav")
console.print_json(data=sign_to_voice(sequence, out_path))
@infer_app.command("sentence")
def infer_sentence(
gloss: list[str] = typer.Option(..., "--gloss", "-g", help="Repeat for multi-gloss"),
) -> None:
text = multi_gloss_to_sentence(gloss)
console.print_json(data={"glosses": gloss, "text": text})
@eval_app.command("toy")
def eval_toy() -> None:
files = list_sample_files()
if not files:
console.print("[yellow]No samples[/yellow]")
raise typer.Exit(1)
hits = 0
table = Table(title="Eval toy")
table.add_column("File")
table.add_column("True")
table.add_column("Pred")
table.add_column("Conf")
table.add_column("OK")
for path in files:
r = sign_to_text(path)
ok = r["predicted_gloss"] == r["true_gloss"]
if ok:
hits += 1
table.add_row(
path.name,
r["true_gloss"],
r["predicted_gloss"],
str(r["confidence"]),
"✓" if ok else "✗",
)
console.print(table)
acc = hits / len(files)
console.print(f"accuracy={acc:.3f} ({hits}/{len(files)})")
RUNS_DIR.mkdir(parents=True, exist_ok=True)
report = {"accuracy": round(acc, 4), "hits": hits, "n": len(files)}
(RUNS_DIR / "eval_toy.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
if acc < 0.8:
raise typer.Exit(1)
@train_app.command("toy")
def train_toy_cmd(epochs: int = typer.Option(3, "--epochs", "-e", min=1, max=50)) -> None:
report = train_toy(epochs=epochs)
console.print(f"[green]Training complete[/green] accuracy={report['history'][-1]['accuracy']}")
console.print(f"Report: {report['report_path']}")
@train_app.command("report")
def train_report() -> None:
path = Path("data/runs/toy_train_report.json")
if not path.exists():
console.print("[yellow]No report yet. Run: loru train toy[/yellow]")
raise typer.Exit(code=1)
console.print(path.read_text(encoding="utf-8"))
@app.command("serve")
def serve_cmd(
host: str = typer.Option("127.0.0.1", "--host"),
port: int = typer.Option(8766, "--port", min=1, max=65535),
) -> None:
"""Run FastAPI (pip install -e '.[api]')."""
try:
import uvicorn
except ImportError as exc:
console.print('[red]Install:[/red] pip install -e ".[api]"')
raise typer.Exit(1) from exc
console.print(f"Serving http://{host}:{port}/health")
uvicorn.run("loru.api.app:app", host=host, port=port, log_level="info")
if __name__ == "__main__":
app()
@eval_app.command("report")
def eval_report_cmd(
out: str = typer.Option(None, "--out", help="Output JSON path (default: data/runs/metrics.json)"),
top_k: int = typer.Option(5, "--top-k", help="Top-K for accuracy"),
) -> None:
"""Generate evaluation metrics report (top-k accuracy, confusion matrix, JSON export)."""
from loru.config import RUNS_DIR
from loru.eval.metrics import generate_report
output_path = Path(out) if out else RUNS_DIR / "metrics.json"
generate_report(output_path=output_path, top_k=top_k)