File size: 24,008 Bytes
1f89a4a
 
e8aeffc
1f89a4a
 
e8aeffc
 
 
 
 
 
 
1f6aac5
 
e8aeffc
 
 
 
 
 
1f6aac5
e8aeffc
 
1f6aac5
 
 
 
1f89a4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f6aac5
 
1f89a4a
 
 
 
 
e8aeffc
 
 
 
 
1f89a4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ead3aa0
1f6aac5
 
ead3aa0
e8aeffc
 
1f89a4a
 
 
1f6aac5
1f89a4a
 
 
 
 
 
 
 
 
 
 
1f6aac5
1f89a4a
 
1f6aac5
1f89a4a
1f6aac5
1f89a4a
1f6aac5
1f89a4a
 
 
 
1f6aac5
 
 
 
 
 
 
 
1f89a4a
 
 
 
1f6aac5
 
1f89a4a
e8aeffc
1f6aac5
1f89a4a
e8aeffc
1f6aac5
 
 
1f89a4a
 
 
1f6aac5
 
 
1f89a4a
 
1f6aac5
 
1f89a4a
1f6aac5
 
1f89a4a
ead3aa0
1f89a4a
 
 
1f6aac5
 
1f89a4a
1f6aac5
 
1f89a4a
1f6aac5
1f89a4a
ead3aa0
1f89a4a
 
 
 
ead3aa0
 
1f89a4a
1f6aac5
ead3aa0
 
1f6aac5
1f89a4a
ead3aa0
1f89a4a
 
ead3aa0
1f6aac5
 
 
 
 
ead3aa0
1f6aac5
1f89a4a
1f6aac5
 
 
1f89a4a
1f6aac5
 
ead3aa0
1f89a4a
1f6aac5
 
1f89a4a
1f6aac5
e8aeffc
1f6aac5
 
 
 
1f89a4a
1f6aac5
 
ead3aa0
1f89a4a
e8aeffc
1f89a4a
1f6aac5
1f89a4a
 
ead3aa0
 
1f89a4a
 
 
 
 
 
 
 
1f6aac5
 
1f89a4a
 
 
 
1f6aac5
 
1f89a4a
 
e8aeffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f89a4a
e8aeffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ead3aa0
 
 
1f89a4a
ead3aa0
1f6aac5
1f89a4a
 
e8aeffc
 
1f6aac5
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""
Spelling Practice – Hugging Face Space
Multi-user safe via gr.State (JSON string per session).
"""

# ── Import gradio first, then patch the gradio_client 0.8 schema bug ──────────
# gradio_client 0.8 (bundled with gradio 4.40) crashes in get_api_info() when
# any component schema contains `additionalProperties: true` (a bool).
# The fix: wrap json_schema_to_python_type to catch the exception, patching
# both the gradio_client module AND the already-imported reference in gradio.blocks.
import gradio as gr

try:
    import gradio_client.utils as _gcu
    import gradio.blocks as _gb
    _orig_jspt = _gcu.json_schema_to_python_type
    def _safe_jspt(schema):
        try:
            return _orig_jspt(schema)
        except Exception:
            return "Any"
    _gcu.json_schema_to_python_type = _safe_jspt
    _gb.client_utils.json_schema_to_python_type = _safe_jspt
except Exception:
    pass
# ─────────────────────────────────────────────────────────────────────────────

import os
import glob
import random
import tempfile
import difflib
import json
import time
import threading

from gtts import gTTS

try:
    from llama_cpp import Llama
    _LLAMA_AVAILABLE = True
except ImportError:
    _LLAMA_AVAILABLE = False

_llm = None
_llm_lock = threading.Lock()

def get_llm():
    global _llm
    if _llm is not None:
        return _llm
    if not _LLAMA_AVAILABLE:
        return None
    with _llm_lock:
        if _llm is not None:
            return _llm
        gguf_files = glob.glob("/app/*.gguf") + glob.glob("*.gguf")
        if not gguf_files:
            print("[spelling-app] No .gguf file found – AI tips disabled.")
            return None
        model_path = gguf_files[0]
        print(f"[spelling-app] Loading model: {model_path}")
        _llm = Llama(model_path=model_path, n_ctx=512, n_threads=2,
                     n_gpu_layers=int(os.getenv("N_GPU_LAYERS", "-1")), verbose=False)
        print("[spelling-app] Model ready.")
        return _llm

SENTENCES = {
    "easy": [
        "The student read a book.", "I like to learn English.",
        "The bank has no money.", "She has a new job.",
        "He can read very fast.", "The map shows a river.",
        "We write notes in class.", "Put the book on the desk.",
        "I see a small farm.", "The goal is very clear.",
    ],
    "medium": [
        "The Southern colonies grew tobacco for Europe.",
        "The workers found jobs in the factory.",
        "He forgot to cite the original source.",
        "They experienced a major economic boom.",
        "My teacher helps me write better essays.",
        "The market was very weak in December.",
        "She received a new evening gown quickly.",
        "We need to manage our time properly.",
        "The group discussion went very well.",
        "The online platform closes at midnight.",
    ],
    "hard": [
        "The government announced policies to address the downturn.",
        "She has an extraordinary talent for analyzing economic literature.",
        "The archaeologist made a fascinating discovery of ancient artifacts.",
        "It is necessary to manage your time effectively.",
        "The corporation was highly profitable after the merger.",
        "He struggled to translate the complex economic metaphors.",
        "The instructor approved the research outline immediately.",
        "She demonstrated remarkable perseverance throughout her academic journey.",
        "Maintaining a regular study schedule is very important.",
        "The researcher examined the primary historical document.",
    ],
    "expert": [
        "The multinational corporation acknowledged the financial discrepancy.",
        "His idiosyncratic research methodologies bewildered his academic colleagues.",
        "The distinguished economist delivered a highly influential speech.",
        "Technological advancements fundamentally transformed nineteenth-century transportation.",
        "Maslow conceptualized self-actualization as the highest human need.",
        "Agricultural consolidation overwhelmed small family farming operations.",
        "Translating metaphorical expressions requires extraordinary cultural competence.",
        "Plagiarism often leads to severe academic consequences.",
        "The entrepreneur demonstrated extraordinary economic decision-making.",
        "The researcher meticulously catalogued primary historical sources.",
    ],
}

LEVELS       = ["easy", "medium", "hard", "expert"]
LEVEL_LABELS = ["🟒 Beginner", "πŸ”΅ Intermediate", "🟠 Advanced", "πŸ”΄ Expert"]
PASS_REQ     = 3

def fresh_state_str():
    return json.dumps({"level": 0, "streak": 0, "sentence": "",
                       "score": 0, "total": 0, "history": []})

def S(s): return json.loads(s)
def D(d): return json.dumps(d)

def _make_audio(text):
    try:
        tts = gTTS(text=text, lang="en", slow=True)
        path = tempfile.mktemp(suffix=".mp3")
        tts.save(path)
        return path
    except Exception as exc:
        print(f"[spelling-app] gTTS error: {exc}")
        return None

def _find_errors(correct, attempt):
    c_words = correct.rstrip(".").lower().split()
    a_words = attempt.rstrip(".").lower().split()
    errors  = []
    for tag, i1, i2, j1, j2 in difflib.SequenceMatcher(None, a_words, c_words).get_opcodes():
        if tag == "replace":
            for w, r in zip(a_words[i1:i2], c_words[j1:j2]):
                if w != r: errors.append((w, r))
        elif tag == "delete":
            for w in a_words[i1:i2]: errors.append((w, "[extra word]"))
        elif tag == "insert":
            for r in c_words[j1:j2]: errors.append(("[missing]", r))
    return errors

def _ask_gemma(correct, attempt, errors):
    llm = get_llm()
    if not llm or not errors: return ""
    error_list = "\n".join(f'- You wrote "{w}", correct is "{r}"' for w, r in errors)
    prompt = (f"You are a friendly English spelling teacher.\n"
              f"The student had to write: {correct}\n"
              f"The student wrote:        {attempt}\n"
              f"Spelling mistakes:\n{error_list}\n\n"
              "For each mistake, write one short sentence explaining the error "
              "and one memory tip. Be encouraging. Keep it brief.")
    out = llm(prompt, max_tokens=200, temperature=0.3, stop=["###"])
    return out["choices"][0]["text"].strip()

def _build_diff(correct, attempt):
    c_words, a_words = correct.split(), attempt.split()
    parts = []
    sm = difflib.SequenceMatcher(
        None, [w.rstrip(".,").lower() for w in a_words],
        [w.rstrip(".,").lower() for w in c_words])
    for tag, i1, i2, j1, j2 in sm.get_opcodes():
        if tag == "equal":     parts.append(" ".join(a_words[i1:i2]))
        elif tag == "replace": parts.append("~~"+" ".join(a_words[i1:i2])+"~~ **"+" ".join(c_words[j1:j2])+"**")
        elif tag == "delete":  parts.append("~~"+" ".join(a_words[i1:i2])+"~~")
        elif tag == "insert":  parts.append("**["+" ".join(c_words[j1:j2])+"]**")
    return " ".join(parts)

def _level_info(state):
    lvl = state["level"]
    extra = f"  Β·  streak {state['streak']}/{PASS_REQ} to level up" if lvl < len(LEVELS)-1 else "  Β·  MAX LEVEL πŸ†"
    return f"**{LEVEL_LABELS[lvl]}**{extra}"

def _score_info(state):
    t, s = state["total"], state["score"]
    return f"**Score: {s}/{t}** ({int(s/t*100) if t else 0}%)"

def new_sentence(ss):
    state = S(ss)
    sent  = random.choice(SENTENCES[LEVELS[state["level"]]])
    state["sentence"] = sent
    audio = _make_audio(sent)
    hint  = "🎧 Listen carefully, then type what you heard and press **Submit**."
    if audio is None:
        hint = f"⚠️ Audio unavailable. Sentence: **{sent}**"
    return audio, "", hint, _level_info(state), _score_info(state), D(state)

def submit_answer(attempt, ss):
    state = S(ss)
    if not state.get("sentence"):
        return "", "⚠️ Press **New Sentence** first!", _level_info(state), _score_info(state), ss
    correct = state["sentence"]
    state["total"] += 1
    errors  = _find_errors(correct, attempt)
    clean_a = attempt.strip().rstrip(".").lower()
    clean_c = correct.strip().rstrip(".").lower()
    if not errors and clean_a == clean_c:
        state["score"]  += 1
        state["streak"] += 1
        msg = "βœ… **Correct!** Every word spelled perfectly."
        if state["streak"] >= PASS_REQ and state["level"] < len(LEVELS)-1:
            state["level"]  += 1
            state["streak"]  = 0
            msg += f"\n\nπŸŽ‰ **Level up! You are now {LEVEL_LABELS[state['level']]}.**"
    else:
        state["streak"] = 0
        diff_str  = _build_diff(correct, attempt)
        gemma_tip = _ask_gemma(correct, attempt, errors)
        ai_block  = f"\n\n---\n\n**πŸ’‘ Tips:**\n\n{gemma_tip}" if gemma_tip else ""
        msg = ("❌ **Your answer with corrections:**\n\n" + diff_str +
               "\n\n*(~~strikethrough~~ = your error Β· **bold** = correct spelling)*" + ai_block)
    state["history"].append({"correct": correct, "attempt": attempt,
                              "ok": not bool(errors) and clean_a == clean_c,
                              "level": LEVELS[state["level"]], "ts": time.strftime("%H:%M:%S")})
    state["sentence"] = ""
    return attempt, msg, _level_info(state), _score_info(state), D(state)

def replay_audio(ss):
    state = S(ss)
    return _make_audio(state["sentence"]) if state.get("sentence") else None

def reset_game(_ss):
    state = S(fresh_state_str())
    return None, "", "Game reset. Press **New Sentence** to start!", _level_info(state), _score_info(state), fresh_state_str()

def export_history(ss):
    history = S(ss).get("history", [])
    if not history:
        return "No history yet."
    lines = []
    for h in history:
        ok = "βœ…" if h["ok"] else "❌"
        lines.append(f'{ok} [{h["level"]}] {h["ts"]}  |  {h["correct"]}  β†’  {h["attempt"]}')
    return "\n".join(lines)

CSS = "footer { display: none !important; }"
_init = S(fresh_state_str())

with gr.Blocks(title="Spelling Practice", css=CSS) as demo:
    session = gr.State(fresh_state_str())

    gr.Markdown("# πŸ“ Spelling Practice\nListen Β· Type Β· See errors Β· Get tips")

    with gr.Row():
        level_md = gr.Markdown(_level_info(_init))
        score_md = gr.Markdown(_score_info(_init))

    with gr.Row():
        new_btn    = gr.Button("🎲 New Sentence", variant="primary")
        replay_btn = gr.Button("πŸ” Replay Audio", variant="secondary")
        reset_btn  = gr.Button("πŸ”„ Reset Game",   variant="secondary")

    audio_out  = gr.Audio(label="Listen to the sentence", autoplay=True, interactive=False)
    hint_md    = gr.Markdown("Press **New Sentence** to begin.")
    answer_box = gr.Textbox(label="Type the sentence you heard",
                            placeholder="Write the full sentence here…", lines=2)
    submit_btn = gr.Button("βœ… Submit", variant="primary")
    feedback   = gr.Markdown("")

    with gr.Accordion("πŸ“Š Session History", open=False):
        export_btn  = gr.Button("πŸ“‹ Show history")
        history_box = gr.Textbox(label="Your session", lines=10, interactive=False)

    OUTS = [audio_out, answer_box, hint_md, level_md, score_md, session]
    new_btn.click(new_sentence,     [session],             OUTS)
    replay_btn.click(replay_audio,  [session],             [audio_out])
    submit_btn.click(submit_answer, [answer_box, session], [answer_box, feedback, level_md, score_md, session])
    answer_box.submit(submit_answer,[answer_box, session], [answer_box, feedback, level_md, score_md, session])
    reset_btn.click(reset_game,     [session],             OUTS)
    export_btn.click(export_history,[session],             [history_box])

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)
"""
Spelling Practice – Hugging Face Space
Multi-user safe via gr.State (JSON string per session).
"""

# ── Import gradio first, then patch the gradio_client 0.8 schema bug ──────────
# gradio_client 0.8 (bundled with gradio 4.40) crashes in get_api_info() when
# any component schema contains `additionalProperties: true` (a bool).
# The fix: wrap json_schema_to_python_type to catch the exception, patching
# both the gradio_client module AND the already-imported reference in gradio.blocks.
import gradio as gr

try:
    import gradio_client.utils as _gcu
    import gradio.blocks as _gb
    _orig_jspt = _gcu.json_schema_to_python_type
    def _safe_jspt(schema):
        try:
            return _orig_jspt(schema)
        except Exception:
            return "Any"
    _gcu.json_schema_to_python_type = _safe_jspt
    _gb.client_utils.json_schema_to_python_type = _safe_jspt
except Exception:
    pass
# ─────────────────────────────────────────────────────────────────────────────

import os
import glob
import random
import tempfile
import difflib
import json
import time
import threading

from gtts import gTTS

try:
    from llama_cpp import Llama
    _LLAMA_AVAILABLE = True
except ImportError:
    _LLAMA_AVAILABLE = False

_llm = None
_llm_lock = threading.Lock()

def get_llm():
    global _llm
    if _llm is not None:
        return _llm
    if not _LLAMA_AVAILABLE:
        return None
    with _llm_lock:
        if _llm is not None:
            return _llm
        gguf_files = glob.glob("/app/*.gguf") + glob.glob("*.gguf")
        if not gguf_files:
            print("[spelling-app] No .gguf file found – AI tips disabled.")
            return None
        model_path = gguf_files[0]
        print(f"[spelling-app] Loading model: {model_path}")
        _llm = Llama(model_path=model_path, n_ctx=512, n_threads=2,
                     n_gpu_layers=int(os.getenv("N_GPU_LAYERS", "-1")), verbose=False)
        print("[spelling-app] Model ready.")
        return _llm

SENTENCES = {
    "easy": [
        "The student read a book.", "I like to learn English.",
        "The bank has no money.", "She has a new job.",
        "He can read very fast.", "The map shows a river.",
        "We write notes in class.", "Put the book on the desk.",
        "I see a small farm.", "The goal is very clear.",
    ],
    "medium": [
        "The Southern colonies grew tobacco for Europe.",
        "The workers found jobs in the factory.",
        "He forgot to cite the original source.",
        "They experienced a major economic boom.",
        "My teacher helps me write better essays.",
        "The market was very weak in December.",
        "She received a new evening gown quickly.",
        "We need to manage our time properly.",
        "The group discussion went very well.",
        "The online platform closes at midnight.",
    ],
    "hard": [
        "The government announced policies to address the downturn.",
        "She has an extraordinary talent for analyzing economic literature.",
        "The archaeologist made a fascinating discovery of ancient artifacts.",
        "It is necessary to manage your time effectively.",
        "The corporation was highly profitable after the merger.",
        "He struggled to translate the complex economic metaphors.",
        "The instructor approved the research outline immediately.",
        "She demonstrated remarkable perseverance throughout her academic journey.",
        "Maintaining a regular study schedule is very important.",
        "The researcher examined the primary historical document.",
    ],
    "expert": [
        "The multinational corporation acknowledged the financial discrepancy.",
        "His idiosyncratic research methodologies bewildered his academic colleagues.",
        "The distinguished economist delivered a highly influential speech.",
        "Technological advancements fundamentally transformed nineteenth-century transportation.",
        "Maslow conceptualized self-actualization as the highest human need.",
        "Agricultural consolidation overwhelmed small family farming operations.",
        "Translating metaphorical expressions requires extraordinary cultural competence.",
        "Plagiarism often leads to severe academic consequences.",
        "The entrepreneur demonstrated extraordinary economic decision-making.",
        "The researcher meticulously catalogued primary historical sources.",
    ],
}

LEVELS       = ["easy", "medium", "hard", "expert"]
LEVEL_LABELS = ["🟒 Beginner", "πŸ”΅ Intermediate", "🟠 Advanced", "πŸ”΄ Expert"]
PASS_REQ     = 3

def fresh_state_str():
    return json.dumps({"level": 0, "streak": 0, "sentence": "",
                       "score": 0, "total": 0, "history": []})

def S(s): return json.loads(s)
def D(d): return json.dumps(d)

def _make_audio(text):
    try:
        tts = gTTS(text=text, lang="en", slow=True)
        path = tempfile.mktemp(suffix=".mp3")
        tts.save(path)
        return path
    except Exception as exc:
        print(f"[spelling-app] gTTS error: {exc}")
        return None

def _find_errors(correct, attempt):
    c_words = correct.rstrip(".").lower().split()
    a_words = attempt.rstrip(".").lower().split()
    errors  = []
    for tag, i1, i2, j1, j2 in difflib.SequenceMatcher(None, a_words, c_words).get_opcodes():
        if tag == "replace":
            for w, r in zip(a_words[i1:i2], c_words[j1:j2]):
                if w != r: errors.append((w, r))
        elif tag == "delete":
            for w in a_words[i1:i2]: errors.append((w, "[extra word]"))
        elif tag == "insert":
            for r in c_words[j1:j2]: errors.append(("[missing]", r))
    return errors

def _ask_gemma(correct, attempt, errors):
    llm = get_llm()
    if not llm or not errors: return ""
    error_list = "\n".join(f'- You wrote "{w}", correct is "{r}"' for w, r in errors)
    prompt = (f"You are a friendly English spelling teacher.\n"
              f"The student had to write: {correct}\n"
              f"The student wrote:        {attempt}\n"
              f"Spelling mistakes:\n{error_list}\n\n"
              "For each mistake, write one short sentence explaining the error "
              "and one memory tip. Be encouraging. Keep it brief.")
    out = llm(prompt, max_tokens=200, temperature=0.3, stop=["###"])
    return out["choices"][0]["text"].strip()

def _build_diff(correct, attempt):
    c_words, a_words = correct.split(), attempt.split()
    parts = []
    sm = difflib.SequenceMatcher(
        None, [w.rstrip(".,").lower() for w in a_words],
        [w.rstrip(".,").lower() for w in c_words])
    for tag, i1, i2, j1, j2 in sm.get_opcodes():
        if tag == "equal":     parts.append(" ".join(a_words[i1:i2]))
        elif tag == "replace": parts.append("~~"+" ".join(a_words[i1:i2])+"~~ **"+" ".join(c_words[j1:j2])+"**")
        elif tag == "delete":  parts.append("~~"+" ".join(a_words[i1:i2])+"~~")
        elif tag == "insert":  parts.append("**["+" ".join(c_words[j1:j2])+"]**")
    return " ".join(parts)

def _level_info(state):
    lvl = state["level"]
    extra = f"  Β·  streak {state['streak']}/{PASS_REQ} to level up" if lvl < len(LEVELS)-1 else "  Β·  MAX LEVEL πŸ†"
    return f"**{LEVEL_LABELS[lvl]}**{extra}"

def _score_info(state):
    t, s = state["total"], state["score"]
    return f"**Score: {s}/{t}** ({int(s/t*100) if t else 0}%)"

def new_sentence(ss):
    state = S(ss)
    sent  = random.choice(SENTENCES[LEVELS[state["level"]]])
    state["sentence"] = sent
    audio = _make_audio(sent)
    hint  = "🎧 Listen carefully, then type what you heard and press **Submit**."
    if audio is None:
        hint = f"⚠️ Audio unavailable. Sentence: **{sent}**"
    return audio, "", hint, _level_info(state), _score_info(state), D(state)

def submit_answer(attempt, ss):
    state = S(ss)
    if not state.get("sentence"):
        return "", "⚠️ Press **New Sentence** first!", _level_info(state), _score_info(state), ss
    correct = state["sentence"]
    state["total"] += 1
    errors  = _find_errors(correct, attempt)
    clean_a = attempt.strip().rstrip(".").lower()
    clean_c = correct.strip().rstrip(".").lower()
    if not errors and clean_a == clean_c:
        state["score"]  += 1
        state["streak"] += 1
        msg = "βœ… **Correct!** Every word spelled perfectly."
        if state["streak"] >= PASS_REQ and state["level"] < len(LEVELS)-1:
            state["level"]  += 1
            state["streak"]  = 0
            msg += f"\n\nπŸŽ‰ **Level up! You are now {LEVEL_LABELS[state['level']]}.**"
    else:
        state["streak"] = 0
        diff_str  = _build_diff(correct, attempt)
        gemma_tip = _ask_gemma(correct, attempt, errors)
        ai_block  = f"\n\n---\n\n**πŸ’‘ Tips:**\n\n{gemma_tip}" if gemma_tip else ""
        msg = ("❌ **Your answer with corrections:**\n\n" + diff_str +
               "\n\n*(~~strikethrough~~ = your error Β· **bold** = correct spelling)*" + ai_block)
    state["history"].append({"correct": correct, "attempt": attempt,
                              "ok": not bool(errors) and clean_a == clean_c,
                              "level": LEVELS[state["level"]], "ts": time.strftime("%H:%M:%S")})
    state["sentence"] = ""
    return attempt, msg, _level_info(state), _score_info(state), D(state)

def replay_audio(ss):
    state = S(ss)
    return _make_audio(state["sentence"]) if state.get("sentence") else None

def reset_game(_ss):
    state = S(fresh_state_str())
    return None, "", "Game reset. Press **New Sentence** to start!", _level_info(state), _score_info(state), fresh_state_str()

def export_history(ss):
    history = S(ss).get("history", [])
    if not history:
        return "No history yet."
    lines = []
    for h in history:
        ok = "βœ…" if h["ok"] else "❌"
        lines.append(f'{ok} [{h["level"]}] {h["ts"]}  |  {h["correct"]}  β†’  {h["attempt"]}')
    return "\n".join(lines)

CSS = "footer { display: none !important; }"
_init = S(fresh_state_str())

with gr.Blocks(title="Spelling Practice", css=CSS) as demo:
    session = gr.State(fresh_state_str())

    gr.Markdown("# πŸ“ Spelling Practice\nListen Β· Type Β· See errors Β· Get tips")

    with gr.Row():
        level_md = gr.Markdown(_level_info(_init))
        score_md = gr.Markdown(_score_info(_init))

    with gr.Row():
        new_btn    = gr.Button("🎲 New Sentence", variant="primary")
        replay_btn = gr.Button("πŸ” Replay Audio", variant="secondary")
        reset_btn  = gr.Button("πŸ”„ Reset Game",   variant="secondary")

    audio_out  = gr.Audio(label="Listen to the sentence", autoplay=True, interactive=False)
    hint_md    = gr.Markdown("Press **New Sentence** to begin.")
    answer_box = gr.Textbox(label="Type the sentence you heard",
                            placeholder="Write the full sentence here…", lines=2)
    submit_btn = gr.Button("βœ… Submit", variant="primary")
    feedback   = gr.Markdown("")

    with gr.Accordion("πŸ“Š Session History", open=False):
        export_btn  = gr.Button("πŸ“‹ Show history")
        history_box = gr.Textbox(label="Your session", lines=10, interactive=False)

    OUTS = [audio_out, answer_box, hint_md, level_md, score_md, session]
    new_btn.click(new_sentence,     [session],             OUTS)
    replay_btn.click(replay_audio,  [session],             [audio_out])
    submit_btn.click(submit_answer, [answer_box, session], [answer_box, feedback, level_md, score_md, session])
    answer_box.submit(submit_answer,[answer_box, session], [answer_box, feedback, level_md, score_md, session])
    reset_btn.click(reset_game,     [session],             OUTS)
    export_btn.click(export_history,[session],             [history_box])

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)