File size: 2,875 Bytes
c6cfe5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""`hcc-meat` command-line interface.

    hcc-meat code <note.txt>           — run the pipeline on a single note, print JSON
    hcc-meat gallery                   — dump the precomputed demo gallery
    hcc-meat serve [--port 7860]       — run the FastAPI backend with uvicorn
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

import typer
from rich.console import Console
from rich.table import Table

from app.pipeline import Pipeline
from app.pipeline.types import Note

app = typer.Typer(help=__doc__, no_args_is_help=True)
console = Console()


@app.command()
def code(
    note_path: Path = typer.Argument(..., help="Path to a plain-text clinical note."),
    json_out: bool = typer.Option(False, "--json", help="Emit full JSON."),
) -> None:
    text = note_path.read_text()
    result = Pipeline().run(Note(note_id=note_path.stem, text=text))
    if json_out:
        sys.stdout.write(result.model_dump_json(indent=2))
        sys.stdout.write("\n")
        return

    table = Table(title=f"HCC suggestions for {note_path.name}")
    table.add_column("HCC")
    table.add_column("Label")
    table.add_column("RAF")
    table.add_column("Conf")
    table.add_column("Trump", justify="center")
    table.add_column("Risk")
    table.add_column("MEAT (MEAT)")
    for c in result.candidates:
        cov = c.meat_coverage()
        meat_str = "".join(
            cat.value if cov[cat] else "·" for cat in list(cov.keys())
        )
        table.add_row(
            c.hcc,
            c.hcc_label,
            f"{c.raf_weight:.3f}",
            f"{c.confidence_calibrated:.2f}",
            f"↓{c.hierarchy.trumped_by}" if c.hierarchy.trumped else "—",
            c.risk.level.upper() if c.risk else "?",
            meat_str,
        )
    console.print(table)
    console.print(f"[dim]verifier drops: {result.verifier_drops} | latency: {result.latency_ms:.1f} ms[/dim]")


@app.command()
def gallery(
    fixture: Path = typer.Option(
        Path("tests/fixtures/regression_suite.json"),
        help="Fixture file to run against.",
    )
) -> None:
    cases = json.loads(fixture.read_text())["cases"]
    pipe = Pipeline()
    out = []
    for c in cases:
        result = pipe.run(Note(note_id=c["case_id"], text=c["note_text"]))
        out.append({
            "case_id": c["case_id"],
            "result": result.model_dump(),
        })
    sys.stdout.write(json.dumps(out, indent=2))
    sys.stdout.write("\n")


@app.command()
def serve(
    host: str = typer.Option("0.0.0.0", help="Bind host."),
    port: int = typer.Option(7860, help="Bind port (7860 is HF Space default)."),
    reload: bool = typer.Option(False, help="uvicorn --reload for dev."),
) -> None:
    import uvicorn

    uvicorn.run("app.api.server:app", host=host, port=port, reload=reload)


if __name__ == "__main__":
    app()