File size: 11,437 Bytes
b78eb26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a520f8
 
 
 
 
 
 
 
b78eb26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a520f8
 
 
b78eb26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Mathematical Sight-Reading Framework (MSRF): a Gradio drill app.

Learner reads a naked formula, answers in three parts, and a judge model (via
the learner's own OpenRouter key) scores it against a hidden reference sight-read.
Progress, key and model choice live in the browser (BrowserState); nothing is
stored on the server.
"""

import json
import os
import tempfile
import time

import gradio as gr

import bank
import engine
import judge

DEFAULT_MODEL = os.environ.get("JUDGE_MODEL", "anthropic/claude-sonnet-4.5")
DEFAULT_CHOICES = [
    m.strip()
    for m in os.environ.get(
        "JUDGE_MODELS",
        "anthropic/claude-sonnet-4.5,anthropic/claude-haiku-4.5,google/gemini-2.5-pro,openai/gpt-5-mini",
    ).split(",")
    if m.strip()
]
if DEFAULT_MODEL not in DEFAULT_CHOICES:
    DEFAULT_CHOICES.insert(0, DEFAULT_MODEL)

ON_SPACE = bool(os.environ.get("SPACE_ID"))

# BrowserState encrypts what it keeps in localStorage. Without a fixed secret Gradio
# invents a new random one every time the server starts, so after a Space restarts or
# sleeps the browser can no longer decrypt the saved key and progress and silently
# loses them. The secret is sent to the browser to do the encrypting, so this is
# obfuscation, not protection; the key is only as safe as your browser profile, as in
# any localStorage client. Override with the MSRF_STATE_SECRET Space variable if wanted.
STATE_SECRET = os.environ.get("MSRF_STATE_SECRET", "msrf-browser-state-v1")

LATEX = [
    {"left": "$$", "right": "$$", "display": True},
    {"left": "$", "right": "$", "display": False},
    {"left": "\\(", "right": "\\)", "display": False},
    {"left": "\\[", "right": "\\]", "display": True},
]

INTRO = """\
Read the formula like sheet music: decode the symbols, say what the machinery does, then ground it.
Write in LaTeX with `$...$` if you like. A judge model scores you on **Precision** (0-10),
**Concept** (Superficial / Competent / Exemplary) and **Transfer** (A / B / C).
"""

KEY_NOTE = """\
**About your key.** It is stored only in this browser (localStorage). To call OpenRouter it passes through
this Space's server in memory for the length of the request; it is not logged or saved. The source is public
in the Space's *Files* tab, so you can check. Use a key with a spend limit.
"""


# ----------------------------------------------------------------- drill ---
def _formula_md(f):
    return f"**{bank.TIER_NAMES[f['tier']]}**\n\n$$\n{f['latex']}\n$$"


def _tier_md(p):
    n = len(p["history"])
    return f"Your level: **{bank.TIER_NAMES[p['tier']]}** · drills done: {n}"


def serve_next(progress, last_id=None):
    p = engine.normalize(progress)
    f = engine.choose_next(p, bank.BANK, exclude=last_id)
    return _formula_md(f), _tier_md(p), f["id"], 0, "", "", "", "", ""


def hint(current, hints):
    if not current:
        return hints, gr.skip()
    level = min((hints or 0) + 1, 2)
    f = bank.BY_ID[current]
    lines = [f"**Hint 1 (types):** {f['hint_types']}"]
    if level >= 2:
        lines.append(f"**Hint 2 (mechanics):** {f['hint_mech']}")
    return level, "\n\n".join(lines) + f"\n\n*Each hint costs 0.1 of your composite score.*"


def _result_md(f, r, comp, tier_before, tier_after, hints):
    out = [
        f"### Scores\n\n| Precision | Concept | Transfer | Composite |\n|:-:|:-:|:-:|:-:|\n"
        f"| **{r['precision']}/10** | **{r['concept']}** | **{r['transfer']}** | {comp:.0%}"
        + (f" (−{hints * 10}% for hints)" if hints else "")
        + " |",
        f"**This was:** {f['name']}.",
    ]
    if tier_after > tier_before:
        out.append(f"**Level up** → {bank.TIER_NAMES[tier_after]}")
    elif tier_after < tier_before:
        out.append(f"**Stepping back** → {bank.TIER_NAMES[tier_after]}")
    out.append(f"#### Precision\n{r['precision_evidence']}")
    out.append(f"#### Concept\n{r['concept_evidence']}")
    dom = ", ".join(r["domains"]) if r["domains"] else "none credited"
    out.append(f"#### Transfer\nDomains credited: {dom}.  \n{r['transfer_evidence']}")
    if r["missed"]:
        out.append("#### Delta: what was missed\n" + "\n".join(f"- {m}" for m in r["missed"]))
    if r["exemplary"]:
        out.append("#### Exemplary sight-read\n" + r["exemplary"])
    return "\n\n".join(out)


def evaluate(p1, p2, p3, current, hints, progress, key, model):
    if not current:
        return "Press **New formula** to get a drill first.", current, progress
    if len((p1 or "").strip() + (p2 or "").strip() + (p3 or "").strip()) < 20:
        return "Write a little more in the three parts before evaluating.", current, progress
    if not (key or "").strip():
        return "Add your OpenRouter API key in **Settings** first.", current, progress
    f = bank.BY_ID[current]
    try:
        r = judge.call_judge(key.strip(), (model or DEFAULT_MODEL).strip(), f, p1, p2, p3)
    except judge.JudgeError as e:
        return f"**Judge error:** {e}\n\nNothing was recorded. Try again or pick another model.", current, progress
    p = engine.normalize(progress)
    tier_before = p["tier"]
    p = engine.record_attempt(p, f, r, hints or 0, now=int(time.time()))
    comp = p["history"][-1]["composite"]
    return _result_md(f, r, comp, tier_before, p["tier"], hints or 0), None, p


# -------------------------------------------------------------- progress ---
TAG_HEADERS = ["Notation family", "Attempts", "Mean precision", "Mean concept (0-2)", "Mean transfer (0-2)"]
RECENT_HEADERS = ["Formula", "Tier", "Precision", "Concept", "Transfer", "Hints", "Composite"]


def progress_views(progress):
    p = engine.normalize(progress)
    h = p["history"]
    if not h:
        return "No drills yet.", [], [], ""
    mean = sum(x["composite"] for x in h) / len(h)
    summary = f"Level **{p['tier']}** · {len(h)} drills · mean composite {mean:.0%}"
    recent = [
        [bank.BY_ID[x["id"]]["name"] if x["id"] in bank.BY_ID else x["id"], x["tier"], x["precision"], x["concept"], x["transfer"], x["hints"], f"{x['composite']:.0%}"]
        for x in reversed(h[-15:])
    ]
    due = engine.due_cards(p, bank.BANK)
    due_md = "**Due for review:** " + (", ".join(bank.BY_ID[c]["name"] for c in due) if due else "nothing yet")
    return summary, engine.stats_by_tag(p, bank.BANK), recent, due_md


def export_progress(progress):
    path = os.path.join(tempfile.mkdtemp(), "msrf-progress.json")
    with open(path, "w") as fh:
        json.dump(engine.normalize(progress), fh, indent=1)
    return path


def import_progress(file):
    if not file:
        return gr.skip(), "No file chosen."
    try:
        with open(file) as fh:
            raw = json.load(fh)
    except (OSError, ValueError):
        return gr.skip(), "That file isn't valid JSON."
    p = engine.normalize(raw)
    if not p["history"] and not p["cards"]:
        return gr.skip(), "No usable progress found in that file."
    return p, f"Imported {len(p['history'])} drills."


def refresh_models(current_model):
    ids = judge.list_models()
    if not ids:
        return gr.update(), "Couldn't reach OpenRouter's model list. Type a model id directly instead."
    choices = ids if current_model in ids else [current_model] + ids
    return gr.update(choices=choices, value=current_model), f"{len(ids)} models loaded."


def greet(profile: gr.OAuthProfile | None):
    return f"Signed in as **{profile.username}**" if profile else ""


# -------------------------------------------------------------------- ui ---
with gr.Blocks(title="Mathematical Sight-Reading") as demo:
    prog_state = gr.BrowserState(engine.new_progress(), storage_key="msrf_progress", secret=STATE_SECRET)
    key_state = gr.BrowserState("", storage_key="msrf_openrouter_key", secret=STATE_SECRET)
    model_state = gr.BrowserState(DEFAULT_MODEL, storage_key="msrf_judge_model", secret=STATE_SECRET)
    current = gr.State(None)
    hints = gr.State(0)

    with gr.Row():
        gr.Markdown("# 🎼 Mathematical Sight-Reading")
        with gr.Column(scale=0, min_width=220):
            if ON_SPACE:  # OAuth is only available on Spaces (locally it needs an HF token)
                gr.LoginButton(size="sm")
            greet_md = gr.Markdown()

    with gr.Tab("Drill"):
        gr.Markdown(INTRO)
        tier_md = gr.Markdown()
        formula_md = gr.Markdown(latex_delimiters=LATEX, elem_id="formula")
        with gr.Row():
            next_btn = gr.Button("New formula", variant="secondary")
            hint_btn = gr.Button("Hint", variant="secondary")
        hint_md = gr.Markdown(latex_delimiters=LATEX)
        p1 = gr.Textbox(label="1. Formal sight-reading (types, inputs/outputs, scopes, conditions)", lines=4)
        p2 = gr.Textbox(label="2. Concept & mechanics (what is it doing? what happens at limits?)", lines=4)
        p3 = gr.Textbox(label="3. Application & metaphor (aim for two or more fields)", lines=4)
        eval_btn = gr.Button("Evaluate", variant="primary")
        result_md = gr.Markdown(latex_delimiters=LATEX)

    with gr.Tab("Progress"):
        summary_md = gr.Markdown()
        due_md = gr.Markdown()
        gr.Markdown("#### Weakest notation families first")
        tag_df = gr.Dataframe(headers=TAG_HEADERS, interactive=False)
        gr.Markdown("#### Recent drills")
        recent_df = gr.Dataframe(headers=RECENT_HEADERS, interactive=False)
        with gr.Row():
            export_btn = gr.Button("Export progress")
            export_file = gr.File(label="Your progress file", interactive=False)
            import_btn = gr.UploadButton("Import progress", file_types=[".json"])
        import_msg = gr.Markdown()

    with gr.Tab("Settings"):
        gr.Markdown(KEY_NOTE)
        key_box = gr.Textbox(label="OpenRouter API key", type="password", placeholder="sk-or-...")
        model_dd = gr.Dropdown(
            label="Judge model (OpenRouter id)",
            choices=DEFAULT_CHOICES,
            value=DEFAULT_MODEL,
            allow_custom_value=True,
            info="A stronger judge grades Tier 3-4 index work more reliably; a cheaper one is fine for Tier 1-2.",
        )
        with gr.Row():
            refresh_btn = gr.Button("Load current OpenRouter model list")
            models_msg = gr.Markdown()

    drill_out = [formula_md, tier_md, current, hints, hint_md, result_md, p1, p2, p3]
    views = [summary_md, tag_df, recent_df, due_md]

    def init(progress, key, model):
        p = engine.normalize(progress)
        return (key or "", model or DEFAULT_MODEL, p) + serve_next(p) + progress_views(p)

    demo.load(init, [prog_state, key_state, model_state], [key_box, model_dd, prog_state] + drill_out + views)
    if ON_SPACE:
        demo.load(greet, None, greet_md)

    next_btn.click(serve_next, [prog_state, current], drill_out)
    hint_btn.click(hint, [current, hints], [hints, hint_md])
    eval_btn.click(
        evaluate, [p1, p2, p3, current, hints, prog_state, key_state, model_state], [result_md, current, prog_state]
    ).then(progress_views, prog_state, views)

    key_box.change(lambda k: k, key_box, key_state)
    model_dd.change(lambda m: m, model_dd, model_state)
    refresh_btn.click(refresh_models, model_dd, [model_dd, models_msg])
    export_btn.click(export_progress, prog_state, export_file)
    import_btn.upload(import_progress, import_btn, [prog_state, import_msg]).then(progress_views, prog_state, views)

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft())