File size: 5,889 Bytes
b3112c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio app for the Fraud Pattern Co-Pilot demo.

Mirror of the Dispute/Collections apps. Cast strip → context →
Analyze → timeline + two-distribution scoreboard + streaming reasoning.

CLI:
    python -m encoder.src.demo.copilot_app_fraud_pattern \\
        --checkpoint encoder/experiments/fraud_pattern_v1/demo_checkpoint.pt \\
        --port 7864
"""

from __future__ import annotations

import argparse
from pathlib import Path

import gradio as gr
import torch

from encoder.src.demo.copilot_inference_fraud_pattern import (
    FraudPatternCastMember,
    FraudPatternCopilotModel,
)
from encoder.src.demo.copilot_render_fraud_pattern import (
    render_cast_strip,
    render_context,
    render_header,
    render_reasoning,
    render_timeline,
    render_two_dist_scoreboard,
)


_CONTAINER_WIDTH_PX = 1200


def _build_tab(model: FraudPatternCopilotModel) -> None:
    """Build the Fraud surface into the current Gradio context."""
    cast = model.cast

    selected_idx = gr.State(value=0)
    cast_html = gr.HTML(render_cast_strip(cast, 0))

    with gr.Row():
        cast_buttons: list[gr.Button] = []
        for i, m in enumerate(cast):
            short = " ".join(m.display_name.split(" ")[:2])
            btn = gr.Button(
                value=short,
                variant="primary" if i == 0 else "secondary",
                scale=1,
            )
            cast_buttons.append(btn)

    context_html = gr.HTML(render_context(cast[0]))

    with gr.Row():
        gr.HTML("<div style='flex: 1'></div>")
        analyze_btn = gr.Button(
            value="Analyze",
            variant="primary",
            size="lg",
            scale=0,
            min_width=180,
        )
        gr.HTML("<div style='flex: 1'></div>")

    timeline_html = gr.HTML(render_timeline(flagged_idx=cast[0].flagged_idx))

    with gr.Row(equal_height=True):
        with gr.Column(scale=3):
            scoreboard_html = gr.HTML(render_two_dist_scoreboard(None))
        with gr.Column(scale=2):
            reasoning_html = gr.HTML(render_reasoning(None))

    def _select(idx: int) -> tuple:
        member = cast[idx]
        button_updates = tuple(
            gr.update(variant="primary" if i == idx else "secondary")
            for i in range(len(cast))
        )
        return (
            idx,
            render_cast_strip(cast, idx),
            render_context(member),
            render_timeline(flagged_idx=member.flagged_idx),
            render_two_dist_scoreboard(None),
            render_reasoning(None),
        ) + button_updates

    for i, btn in enumerate(cast_buttons):
        btn.click(
            fn=lambda i=i: _select(i),
            inputs=None,
            outputs=[
                selected_idx, cast_html, context_html,
                timeline_html, scoreboard_html, reasoning_html,
                *cast_buttons,
            ],
        )

    def _analyze(idx: int):
        member: FraudPatternCastMember = cast[idx]
        result = model.predict(member, top_k=5)
        timeline = render_timeline(
            flagged_idx=member.flagged_idx,
            top_k_positions=result.top_k_positions,
            attribution_probs=result.attribution_probs,
        )
        scoreboard = render_two_dist_scoreboard(result)
        yield timeline, scoreboard, render_reasoning("")
        for partial in model.stream_reasoning(member, result, chunk_chars=6):
            yield timeline, scoreboard, render_reasoning(partial)

    analyze_btn.click(
        fn=_analyze,
        inputs=[selected_idx],
        outputs=[timeline_html, scoreboard_html, reasoning_html],
    )


def _build_ui(model: FraudPatternCopilotModel) -> gr.Blocks:
    """Standalone Blocks UI: Fraud-specific header + tab content."""
    with gr.Blocks(title="Fraud Pattern Co-Pilot — Liquid AI") as demo:
        gr.HTML(render_header())
        _build_tab(model)
    return demo


def main() -> None:
    parser = argparse.ArgumentParser(
        description="Fraud Pattern Co-Pilot Gradio demo",
    )
    parser.add_argument(
        "--checkpoint",
        type=Path,
        default=Path("encoder/experiments/fraud_pattern_v1/demo_checkpoint.pt"),
    )
    parser.add_argument(
        "--model-config",
        type=Path,
        default=Path("encoder/configs/model_fraud_pattern.yaml"),
    )
    parser.add_argument("--schema", type=Path, default=Path("data/schema.yaml"))
    parser.add_argument(
        "--histories",
        type=Path,
        default=Path("data/synthetic/token_ids.npy"),
    )
    parser.add_argument(
        "--cast",
        type=Path,
        default=Path("encoder/data/fraud_pattern_cast.json"),
    )
    parser.add_argument(
        "--device",
        type=str,
        default="cpu",
        choices=["cpu", "cuda", "mps"],
    )
    parser.add_argument("--port", type=int, default=7864)
    parser.add_argument("--share", action="store_true")
    args = parser.parse_args()

    device = torch.device(args.device)
    print(f"Loading FraudPatternCopilotModel on {device} ...")
    model = FraudPatternCopilotModel.from_paths(
        checkpoint_path=args.checkpoint,
        model_config_path=args.model_config,
        schema_path=args.schema,
        histories_path=args.histories,
        cast_path=args.cast,
        device=device,
    )
    print(f"  cast size: {len(model.cast)}")

    demo = _build_ui(model)
    demo.queue().launch(
        server_name="0.0.0.0",
        server_port=args.port,
        share=args.share,
        theme=gr.themes.Default(
            font=["Inter", "system-ui", "sans-serif"],
            font_mono=["JetBrains Mono", "ui-monospace", "monospace"],
        ),
        css=f"""
        .gradio-container {{
            max-width: {_CONTAINER_WIDTH_PX}px !important;
            background: #fafafa !important;
        }}
        """,
    )


if __name__ == "__main__":
    main()