File size: 2,957 Bytes
f8768df
 
 
 
 
 
 
 
c4aee46
f8768df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2444ad5
f8768df
2444ad5
 
f8768df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Local Gradio demo for one timestamped economy or financial-news article."""

from __future__ import annotations

from functools import lru_cache

import gradio as gr

from range_curate.examples.general_economy import RelevanceEngine


@lru_cache(maxsize=1)
def _engine() -> RelevanceEngine:
    return RelevanceEngine()


def predict_article(text: str, timestamp: str):
    """Return a compact decision, explanation, and Mermaid diagram source."""

    if not text or not text.strip():
        raise gr.Error("Enter article text before running a prediction.")
    if not timestamp or not timestamp.strip():
        raise gr.Error("Enter an ISO 8601 publication timestamp.")

    try:
        prediction = _engine().predict(
            text,
            timestamp=timestamp,
            article_id="interactive-demo",
            source="gradio-local",
        )
    except (TypeError, ValueError) as exc:
        raise gr.Error(f"The article or timestamp is invalid: {exc}") from exc

    explanation = _engine().explain(prediction, format="text", detail="summary")
    diagram = _engine().explain(prediction, format="mermaid", detail="summary")
    return prediction.summary(), explanation, diagram


def build_demo() -> gr.Blocks:
    with gr.Blocks(title="RANGE") as demo:
        gr.Markdown(
            "# RANGE\n"
            "**Rapid Annotation, Normalization, and Gating Engine — General Economy example**\n\n"
            "Screen timestamped economic and financial news against the bundled "
            "general-economy taxonomy. Everything runs in this Python process."
        )
        with gr.Row():
            with gr.Column(scale=3):
                article = gr.Textbox(
                    label="Article text",
                    lines=12,
                    value=(
                        "CPI inflation cooled after the Federal Reserve cut the "
                        "federal funds rate. The S&P 500 advanced."
                    ),
                )
                timestamp = gr.Textbox(
                    label="Publication timestamp",
                    value="2025-01-15T10:30:00-05:00",
                    info="Use ISO 8601 with a UTC offset when possible.",
                )
                run = gr.Button("Predict relevance", variant="primary")
            with gr.Column(scale=2):
                decision = gr.JSON(label="Decision")
                explanation = gr.Markdown(label="Evidence explanation")

        diagram = gr.Code(
            language="markdown",
            label="Mermaid evidence diagram",
            lines=16,
        )
        run.click(
            fn=predict_article,
            inputs=[article, timestamp],
            outputs=[decision, explanation, diagram],
            api_name="predict",
        )
    return demo


demo = build_demo()


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