File size: 32,549 Bytes
3224bbc
9c97266
 
8090962
 
 
 
9c97266
8090962
 
 
9c97266
 
 
 
e2b07cd
9c97266
 
 
 
 
 
 
 
 
 
8090962
 
9c97266
 
 
e2b07cd
9c97266
e2b07cd
9c97266
 
e2b07cd
3224bbc
9c97266
 
 
 
 
 
 
 
 
 
 
 
 
 
e2b07cd
9c97266
e2b07cd
8090962
 
 
 
e2b07cd
3224bbc
 
 
 
 
9c97266
3224bbc
 
 
9c97266
e2b07cd
8090962
 
 
 
 
 
 
 
 
 
 
 
3224bbc
e2b07cd
9c97266
 
 
 
 
 
 
 
 
 
 
 
3224bbc
9c97266
 
 
 
 
 
3224bbc
9c97266
 
8a0b475
3224bbc
9c97266
3224bbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba8391f
8a0b475
ba8391f
 
 
3224bbc
9c97266
 
 
ba8391f
9c97266
ba8391f
 
3224bbc
ba8391f
 
3224bbc
 
 
 
 
 
 
9c97266
 
 
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c97266
3224bbc
 
8090962
 
 
 
 
 
3224bbc
 
9c97266
3224bbc
9c97266
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3224bbc
 
9c97266
 
 
 
3224bbc
9c97266
 
 
3224bbc
 
 
 
 
 
 
 
 
9c97266
 
3224bbc
9c97266
3224bbc
9c97266
 
 
 
 
 
3224bbc
9c97266
 
 
 
 
 
3224bbc
 
9c97266
8090962
9c97266
3224bbc
8090962
 
3224bbc
 
 
 
 
8090962
3224bbc
 
 
 
8090962
 
9c97266
8090962
 
9c97266
8090962
 
3224bbc
8090962
 
 
 
9c97266
 
3224bbc
9c97266
 
 
 
3224bbc
9c97266
8090962
3224bbc
 
 
 
 
 
 
 
8090962
3224bbc
 
 
 
 
9c97266
 
3224bbc
 
 
 
 
 
 
 
 
 
9c97266
 
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3224bbc
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3224bbc
9c97266
 
 
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2b07cd
 
8090962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c97266
8090962
 
9c97266
8090962
 
 
 
 
3224bbc
8090962
 
 
9c97266
8090962
 
 
 
 
 
 
 
 
9c97266
8090962
 
 
 
 
 
 
9c97266
8090962
 
9c97266
8090962
 
 
9c97266
8090962
e2b07cd
8090962
 
e2b07cd
 
8090962
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import spaces  # MUST BE THE VERY FIRST IMPORT FOR ZEROGPU
import os
import io
import re
import json
import uuid
import zipfile
import threading
import xml.etree.ElementTree as ET
from html.parser import HTMLParser
from datetime import datetime, timezone
from pathlib import Path

import requests
import torch
import gradio as gr
import sympy as sp
from sympy.parsing.sympy_parser import (
    parse_expr,
    standard_transformations,
    convert_xor,
)
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    StoppingCriteria,
    StoppingCriteriaList,
    TextIteratorStreamer,
)
from peft import PeftModel

# ----------------------------- CONFIG -----------------------------

GITHUB_REPO = "rickeysharma942-stack/english-math-tutor"
RELEASE_TAG = "chatbot"

# local file name -> asset name on the GitHub release
ASSETS = {
    "english_adapter": {
        "adapter_config.json": "english_adapter_config.json",
        "adapter_model.safetensors": "english_adapter_model.safetensors",
    },
    "math_adapter": {
        "adapter_config.json": "math_adapter_config.json",
        "adapter_model.safetensors": "math_adapter_model.safetensors",
    },
    "general_adapter": {
        "adapter_config.json": "general_adapter_config.json",
        "adapter_model.safetensors": "general_adapter_model.safetensors",
    },
}

ADAPTER_DIR = Path("adapters") / RELEASE_TAG

MAX_FILE_CHARS = 12000      # per attached file
MAX_HISTORY_CHARS = 40000   # prompt budget (oldest turns are dropped first)
MAX_CHATS = 40              # chats kept in the browser
STORAGE_KEY = "tutor_studio_chats_v1"

# Your adapters were trained on Qwen3-8B, so the base model MUST be Qwen3-8B.
BASE_MODEL = os.environ.get("BASE_MODEL") or "Qwen/Qwen3-8B"
# Set USE_4BIT=1 only on a normal GPU Space (T4/L4). Leave off for ZeroGPU.
USE_4BIT = os.environ.get("USE_4BIT", "0") == "1"

MODES = {
    "General": {"adapter": "general_adapter"},
    "English": {"adapter": "english_adapter"},
    "Math": {"adapter": "math_adapter"},
}

# ----------------------------- SUPPORTED FILE TYPES (30) -----------------------------

DOC_TYPES = [".pdf", ".docx", ".xlsx", ".pptx", ".odt", ".rtf", ".epub", ".html", ".ipynb"]
DATA_TYPES = [".txt", ".md", ".csv", ".tsv", ".json", ".xml", ".yaml", ".log", ".ini"]
CODE_TYPES = [".py", ".js", ".ts", ".java", ".c", ".cpp", ".cs", ".go", ".php", ".sql", ".sh", ".css"]
ALIAS_TYPES = [".htm", ".yml", ".toml", ".cfg", ".markdown", ".jsx", ".tsx", ".h", ".hpp", ".rb", ".rs"]

SUPPORTED_30 = DOC_TYPES + DATA_TYPES + CODE_TYPES
ACCEPTED_TYPES = SUPPORTED_30 + ALIAS_TYPES
SPECIAL_TYPES = set(DOC_TYPES) | {".htm"}
PLAIN_TEXT_TYPES = set(ACCEPTED_TYPES) - SPECIAL_TYPES

# ----------------------------- MODEL LOADING -----------------------------


def download_file(url: str, dest: Path):
    dest.parent.mkdir(parents=True, exist_ok=True)
    tmp = dest.with_suffix(dest.suffix + ".part")
    with requests.get(url, stream=True, timeout=60, allow_redirects=True) as r:
        r.raise_for_status()
        with open(tmp, "wb") as f:
            for chunk in r.iter_content(chunk_size=1024 * 1024):
                if chunk:
                    f.write(chunk)
    tmp.replace(dest)


def ensure_adapters():
    base_url = f"https://github.com/{GITHUB_REPO}/releases/download/{RELEASE_TAG}"
    for adapter_name, files in ASSETS.items():
        for local_name, asset_name in files.items():
            dest = ADAPTER_DIR / adapter_name / local_name
            if not dest.exists():
                print(f"Downloading {asset_name} ...")
                download_file(f"{base_url}/{asset_name}", dest)


def load_model():
    ensure_adapters()
    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)

    if USE_4BIT:
        bnb = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_compute_dtype=torch.float16,
            bnb_4bit_quant_type="nf4",
            bnb_4bit_use_double_quant=True,
        )
        base = AutoModelForCausalLM.from_pretrained(
            BASE_MODEL,
            quantization_config=bnb,
            device_map={"": 0},
            torch_dtype=torch.float16,
        )
    else:
        # bf16 (~16 GB) fits on a ZeroGPU slice; no bitsandbytes needed.
        base = AutoModelForCausalLM.from_pretrained(
            BASE_MODEL,
            torch_dtype=torch.bfloat16,
        )  # stays on CPU for now

    # ZeroGPU has no real GPU at startup, so read adapter files onto the CPU
    # (PEFT would otherwise try to load them straight onto "cuda" and crash).
    dev = None if USE_4BIT else "cpu"
    model = PeftModel.from_pretrained(
        base,
        str(ADAPTER_DIR / "english_adapter"),
        adapter_name="english_adapter",
        torch_device=dev,
    )
    model.load_adapter(str(ADAPTER_DIR / "math_adapter"), adapter_name="math_adapter", torch_device=dev)
    model.load_adapter(str(ADAPTER_DIR / "general_adapter"), adapter_name="general_adapter", torch_device=dev)
    model.eval()
    if not USE_4BIT:
        model.to("cuda")  # ZeroGPU defers the real transfer until a @spaces.GPU call
    return tokenizer, model


print(f"Loading {BASE_MODEL} and adapters...")
tokenizer, model = load_model()
generation_lock = threading.Lock()
print("Model ready.")

# ----------------------------- FILE PARSING -----------------------------


class _HTMLText(HTMLParser):
    def __init__(self):
        super().__init__()
        self.parts, self._skip = [], 0

    def handle_starttag(self, tag, attrs):
        if tag in ("script", "style"):
            self._skip += 1

    def handle_endtag(self, tag):
        if tag in ("script", "style") and self._skip:
            self._skip -= 1

    def handle_data(self, data):
        if not self._skip and data.strip():
            self.parts.append(data.strip())


def html_to_text(raw: str) -> str:
    p = _HTMLText()
    p.feed(raw)
    return "\n".join(p.parts)


def _read_pdf(data: bytes) -> str:
    from pypdf import PdfReader
    reader = PdfReader(io.BytesIO(data))
    pages = []
    for i, page in enumerate(reader.pages, 1):
        t = (page.extract_text() or "").strip()
        if t:
            pages.append(f"[Page {i}]\n{t}")
    return "\n\n".join(pages) or "[No extractable text found - this PDF may be a scan.]"


def _read_docx(data: bytes) -> str:
    from docx import Document
    doc = Document(io.BytesIO(data))
    parts = [p.text for p in doc.paragraphs if p.text.strip()]
    for table in doc.tables:
        for row in table.rows:
            cells = [c.text.strip() for c in row.cells]
            if any(cells):
                parts.append(" | ".join(cells))
    return "\n".join(parts)


def _read_xlsx(data: bytes) -> str:
    from openpyxl import load_workbook
    wb = load_workbook(io.BytesIO(data), read_only=True, data_only=True)
    parts, size = [], 0
    for sheet in wb.worksheets:
        parts.append(f"## Sheet: {sheet.title}")
        for row in sheet.iter_rows(values_only=True):
            row_str = " | ".join(str(v) for v in row if v is not None)
            if row_str.strip():
                parts.append(row_str)
                size += len(row_str)
            if size > MAX_FILE_CHARS:
                return "\n".join(parts)
    return "\n".join(parts)


def _read_pptx(data: bytes) -> str:
    from pptx import Presentation
    prs = Presentation(io.BytesIO(data))
    parts = []
    for i, slide in enumerate(prs.slides, 1):
        parts.append(f"## Slide {i}")
        for shape in slide.shapes:
            if getattr(shape, "has_text_frame", False) and shape.text_frame.text.strip():
                parts.append(shape.text_frame.text.strip())
    return "\n".join(parts)


def _read_odt(data: bytes) -> str:
    with zipfile.ZipFile(io.BytesIO(data)) as z:
        root = ET.fromstring(z.read("content.xml"))
    parts = []
    for el in root.iter():
        if el.tag.endswith("}p") or el.tag.endswith("}h"):
            text = "".join(el.itertext()).strip()
            if text:
                parts.append(text)
    return "\n".join(parts)


def _read_epub(data: bytes) -> str:
    parts = []
    with zipfile.ZipFile(io.BytesIO(data)) as z:
        for name in sorted(z.namelist()):
            if name.lower().endswith((".xhtml", ".html", ".htm")):
                text = html_to_text(z.read(name).decode("utf-8", errors="replace"))
                if text.strip():
                    parts.append(text)
    return "\n\n".join(parts)


def _read_rtf(data: bytes) -> str:
    raw = data.decode("utf-8", errors="replace")
    try:
        from striprtf.striprtf import rtf_to_text
        return rtf_to_text(raw)
    except Exception:
        text = re.sub(r"\\[a-z]+-?\d* ?|[{}]", "", raw)
        return text.strip()


def _read_ipynb(data: bytes) -> str:
    nb = json.loads(data.decode("utf-8", errors="replace"))
    parts = []
    for cell in nb.get("cells", []):
        src = cell.get("source", "")
        src = "".join(src) if isinstance(src, list) else str(src)
        if not src.strip():
            continue
        if cell.get("cell_type") == "code":
            parts.append(f"```python\n{src}\n```")
        else:
            parts.append(src)
    return "\n\n".join(parts)


def file_path_of(file_obj) -> Path:
    """Gradio may hand us a str path, a dict with 'path', or an object with .name."""
    if isinstance(file_obj, str):
        return Path(file_obj)
    if isinstance(file_obj, dict):
        return Path(file_obj.get("path") or file_obj.get("name"))
    return Path(file_obj.name)


def extract_text_from_upload(file_obj) -> str:
    path = file_path_of(file_obj)
    ext = path.suffix.lower()
    if ext not in ACCEPTED_TYPES:
        return f"[Unsupported file type: {ext or 'no extension'}]"
    try:
        data = path.read_bytes()
        if ext in PLAIN_TEXT_TYPES:
            return data.decode("utf-8", errors="replace")
        if ext == ".pdf":
            return _read_pdf(data)
        if ext == ".docx":
            return _read_docx(data)
        if ext == ".xlsx":
            return _read_xlsx(data)
        if ext == ".pptx":
            return _read_pptx(data)
        if ext == ".odt":
            return _read_odt(data)
        if ext == ".epub":
            return _read_epub(data)
        if ext == ".rtf":
            return _read_rtf(data)
        if ext == ".ipynb":
            return _read_ipynb(data)
        if ext in (".html", ".htm"):
            return html_to_text(data.decode("utf-8", errors="replace"))
    except Exception as e:
        return f"[Could not read {path.name}: {e}]"
    return "[Unsupported file type]"


def build_file_context(files):
    """Returns (context_text, file_names, skipped_names)."""
    chunks, names, skipped = [], [], []
    for f in files or []:
        path = file_path_of(f)
        names.append(path.name)
        if path.suffix.lower() not in ACCEPTED_TYPES:
            skipped.append(path.name)
            continue
        text = extract_text_from_upload(f)
        total = len(text)
        text = text[:MAX_FILE_CHARS]
        if total > MAX_FILE_CHARS:
            text += f"\n...[truncated: showing {MAX_FILE_CHARS} of {total} characters]"
        chunks.append(f"--- File: {path.name} ---\n{text}")
    return "\n\n".join(chunks), names, skipped


# ----------------------------- MATH CHECKER -----------------------------

SYMPY_NAMES = [
    "diff", "integrate", "limit", "solve", "simplify", "expand", "factor",
    "sin", "cos", "tan", "exp", "log", "sqrt", "pi", "E",
]
SAFE_SYMPY = {n: getattr(sp, n) for n in SYMPY_NAMES if hasattr(sp, n)}
SAFE_SYMPY.update({letter: sp.Symbol(letter) for letter in "xyztnabc"})
# parse_expr's auto_number / auto_symbol transforms emit these names, and
# they must be resolvable because __builtins__ and globals are locked down.
SAFE_SYMPY.update({
    "Integer": sp.Integer,
    "Float": sp.Float,
    "Rational": sp.Rational,
    "Symbol": sp.Symbol,
    "Function": sp.Function,
})
SYMPY_TRANSFORMS = standard_transformations + (convert_xor,)


def run_sympy(expression: str) -> str:
    expression = (expression or "").strip()
    if not expression:
        return "Enter an expression first."
    try:
        result = sp.simplify(
            parse_expr(
                expression,
                local_dict=dict(SAFE_SYMPY),
                global_dict={"__builtins__": {}},
                transformations=SYMPY_TRANSFORMS,
            )
        )
        return str(result)
    except Exception as e:
        return f"SymPy Error: {e}"


# ----------------------------- GENERATION -----------------------------


def trim_messages(messages):
    """Drop the oldest turns until the prompt fits MAX_HISTORY_CHARS (always keep the last message)."""
    def total(ms):
        return sum(len(m["content"]) for m in ms)

    while len(messages) > 1 and total(messages) > MAX_HISTORY_CHARS:
        messages.pop(0)
    while len(messages) > 1 and messages[0]["role"] == "assistant":
        messages.pop(0)
    return messages


class StopOnFlag(StoppingCriteria):
    """Lets us stop generation (and free the GPU) when the user presses Stop."""

    def __init__(self, flag: threading.Event):
        self.flag = flag

    def __call__(self, input_ids, scores, **kwargs):
        return torch.full((input_ids.shape[0],), self.flag.is_set(), dtype=torch.bool, device=input_ids.device)


@spaces.GPU(duration=120)
def generate_response(messages, mode, temperature, max_new_tokens):
    adapter_name = MODES.get(mode, MODES["General"])["adapter"]

    prompt = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
        enable_thinking=False,
    )
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
    stop_flag = threading.Event()
    errors = []

    temperature = float(temperature)
    gen_kwargs = dict(
        max_new_tokens=int(max_new_tokens),
        repetition_penalty=1.05,
        pad_token_id=tokenizer.pad_token_id,
        eos_token_id=tokenizer.convert_tokens_to_ids("<|im_end|>"),
        stopping_criteria=StoppingCriteriaList([StopOnFlag(stop_flag)]),
    )
    if temperature > 0.01:
        gen_kwargs.update(do_sample=True, temperature=temperature, top_p=0.8)
    else:
        gen_kwargs.update(do_sample=False)

    def worker():
        try:
            with generation_lock:
                model.set_adapter(adapter_name)
                with torch.inference_mode():
                    model.generate(**inputs, streamer=streamer, **gen_kwargs)
        except Exception as e:  # make sure the streaming loop never hangs
            errors.append(e)
            streamer.on_finalized_text("", stream_end=True)

    t = threading.Thread(target=worker, daemon=True)
    t.start()

    partial = ""
    try:
        for new_text in streamer:
            partial += new_text
            yield partial
        t.join()
        if errors:
            yield partial + f"\n\n⚠️ Generation error: {errors[0]}"
    finally:
        stop_flag.set()  # also fires when the user presses Stop
        t.join(timeout=10)


# ----------------------------- CHAT STORE (kept in the visitor's own browser) -----------------------------


def now_iso():
    return datetime.now(timezone.utc).isoformat()


def new_chat_obj():
    return {"id": uuid.uuid4().hex[:12], "title": "New chat", "updated": now_iso(), "turns": []}


def ensure_store(store):
    if not isinstance(store, dict) or not isinstance(store.get("chats"), dict):
        store = {"chats": {}, "current": None}
    if store.get("current") not in store["chats"]:
        if store["chats"]:
            store["current"] = max(store["chats"].values(), key=lambda c: c["updated"])["id"]
        else:
            chat = new_chat_obj()
            store["chats"][chat["id"]] = chat
            store["current"] = chat["id"]
    return store


def current_chat(store):
    return store["chats"][store["current"]]


def normalize_store(store):
    """Turns that never got an answer (stopped / interrupted) get a note instead of staying blank."""
    for cid, chat in store["chats"].items():
        if cid == store.get("busy"):
            continue
        for turn in chat["turns"]:
            if not turn.get("a"):
                turn["a"] = "*(no response)*"
    return store


def make_title(text, names):
    base = (text or "").strip().splitlines()[0] if (text or "").strip() else ""
    if not base and names:
        base = f"📎 {names[0]}"
    base = base or "New chat"
    return base if len(base) <= 38 else base[:37] + "…"


def history_update(store):
    chats = sorted(store["chats"].values(), key=lambda c: c["updated"], reverse=True)
    return gr.update(choices=[(c["title"], c["id"]) for c in chats], value=store["current"])


def user_display(turn):
    parts = []
    if turn.get("u"):
        parts.append(turn["u"])
    if turn.get("files"):
        parts.append("\n".join(f"📎 {n}" for n in turn["files"]))
    return "\n\n".join(parts)


def chat_display(chat):
    return [[user_display(t), (t.get("a") or "…")] for t in chat["turns"]]


def store_to_json(store):
    store = ensure_store(store)
    data = {"chats": store["chats"], "current": store["current"]}
    s = json.dumps(data)
    # localStorage is ~5 MB: drop the oldest chats if we are getting close
    while len(s) > 3_500_000 and len(data["chats"]) > 1:
        oldest = min(
            (c for cid, c in data["chats"].items() if cid != data["current"]),
            key=lambda c: c["updated"],
        )
        del data["chats"][oldest["id"]]
        s = json.dumps(data)
    return s


def prune_chats(store):
    while len(store["chats"]) > MAX_CHATS:
        oldest = min(
            (c for cid, c in store["chats"].items() if cid != store["current"]),
            key=lambda c: c["updated"],
        )
        del store["chats"][oldest["id"]]


def build_messages(chat):
    msgs = []
    for t in chat["turns"]:
        content = t.get("u") or ""
        if t.get("ctx"):
            content += "\n\nAttached files:\n" + t["ctx"]
        msgs.append({"role": "user", "content": content})
        if t.get("a"):
            msgs.append({"role": "assistant", "content": t["a"]})
    return trim_messages(msgs)


# ----------------------------- UI HANDLERS -----------------------------


def on_load(raw):
    try:
        parsed = json.loads(raw) if raw else None
    except Exception:
        parsed = None
    store = normalize_store(ensure_store(parsed))
    return store, history_update(store), chat_display(current_chat(store))


def new_chat(store):
    store = ensure_store(store)
    if store.get("busy"):
        return store, history_update(store), chat_display(current_chat(store))
    if not current_chat(store)["turns"]:  # already on an empty chat
        return store, history_update(store), chat_display(current_chat(store))
    chat = new_chat_obj()
    store["chats"][chat["id"]] = chat
    store["current"] = chat["id"]
    prune_chats(store)
    return store, history_update(store), []


def select_chat(chat_id, store):
    store = ensure_store(store)
    if not store.get("busy") and chat_id in store["chats"]:
        store["current"] = chat_id
        normalize_store(store)
    return store, history_update(store), chat_display(current_chat(store))


def delete_chat(store):
    store = ensure_store(store)
    if store.get("busy"):
        return store, history_update(store), chat_display(current_chat(store))
    del store["chats"][store["current"]]
    store["current"] = None
    store = normalize_store(ensure_store(store))
    return store, history_update(store), chat_display(current_chat(store))


def add_user(msg, store):
    store = ensure_store(store)
    chat = current_chat(store)
    keep = gr.skip()
    if store.get("busy"):
        gr.Warning("Please wait for the current answer to finish (or press Stop).")
        return store, chat_display(chat), keep, history_update(store)

    msg = msg or {}
    text = (msg.get("text") or "").strip()
    files = msg.get("files") or []
    if not text and not files:
        return store, chat_display(chat), keep, history_update(store)

    ctx, names, skipped = build_file_context(files)
    if skipped:
        gr.Warning("Unsupported file type skipped: " + ", ".join(skipped))
    if not text:
        text = "Please summarize the attached file(s)."

    chat["turns"].append({"u": text, "files": names, "ctx": ctx, "a": ""})
    if len(chat["turns"]) == 1:
        chat["title"] = make_title(text, names)
    chat["updated"] = now_iso()
    store["pending"] = True
    return store, chat_display(chat), {"text": "", "files": []}, history_update(store)


def respond(store, mode, temperature, max_new_tokens):
    store = ensure_store(store)
    chat = current_chat(store)
    if not store.get("pending") or not chat["turns"]:
        yield store, chat_display(chat), gr.skip()
        return

    store["pending"] = False
    store["busy"] = chat["id"]
    turn = chat["turns"][-1]
    messages = build_messages(chat)
    yield store, chat_display(chat), gr.skip()

    try:
        for partial in generate_response(messages, mode, temperature, max_new_tokens):
            turn["a"] = partial
            if store.get("current") == chat["id"]:
                yield store, chat_display(chat), gr.skip()
    except Exception as e:
        turn["a"] = (turn.get("a") or "") + f"\n\n⚠️ {e}"
    finally:
        store["busy"] = None

    if not turn.get("a"):
        turn["a"] = "*(no response)*"
    chat["updated"] = now_iso()
    yield store, chat_display(chat), history_update(store)


def stop_save(store):
    store = ensure_store(store)
    store["busy"] = None
    store["pending"] = False
    normalize_store(store)
    return store, chat_display(current_chat(store))


# ----------------------------- GRADIO UI -----------------------------

CSS = """
:root { --bg:#faf9f5; --side:#f0eee6; --line:#e5e2d8; --ink:#1f1e1d; --muted:#6b6a65; --accent:#c96442; --accent-d:#b5573a; }
body, .gradio-container { background: var(--bg) !important; color: var(--ink) !important; }
.gradio-container { max-width: 100% !important; padding: 0 !important; font-family: 'Inter', system-ui, sans-serif !important; }
footer { display: none !important; }

/* ---- sidebar ---- */
#sidebar { background: var(--side); border-right: 1px solid var(--line); padding: 18px 14px !important;
           min-height: 100vh; position: sticky; top: 0; align-self: flex-start; max-height: 100vh; overflow-y: auto; gap: 10px !important; }
#brand { font-family: Georgia, 'Times New Roman', serif; font-size: 1.35rem; color: var(--ink); margin: 2px 4px 6px; }
#brand p { margin: 0 !important; }
#new-chat { background: var(--accent) !important; color: #fff !important; border: none !important; border-radius: 10px !important; font-weight: 600; }
#new-chat:hover { background: var(--accent-d) !important; }
#recents-label { color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; margin: 8px 6px 0; }
#recents-label p { margin: 0 !important; }
#history, #history .wrap { background: transparent !important; border: none !important; padding: 0 !important; box-shadow: none !important; }
#history .wrap { display: flex; flex-direction: column; gap: 2px; }
#history label { background: transparent !important; border: none !important; border-radius: 8px !important; padding: 8px 10px !important;
                 width: 100%; cursor: pointer; color: var(--ink) !important; box-shadow: none !important; }
#history label:hover { background: #e8e5da !important; }
#history label.selected { background: #e3dfd2 !important; font-weight: 600; }
#history label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; max-width: 210px; }
#history input { display: none !important; }
#delete-chat { background: transparent !important; border: 1px solid var(--line) !important; color: var(--muted) !important; border-radius: 10px !important; }
#delete-chat:hover { color: #b3261e !important; border-color: #b3261e !important; }
#sidebar .block, #sidebar .form { background: transparent !important; border: none !important; box-shadow: none !important; }
#sidebar .accordion, #sidebar .label-wrap { background: transparent !important; color: var(--ink) !important; }

/* ---- main column ---- */
#main { max-width: 860px; margin: 0 auto; padding: 18px 20px 10px !important; }
#chatbot { background: transparent !important; border: none !important; box-shadow: none !important; }
#chatbot .message-row .message { border: none !important; box-shadow: none !important; font-size: 1rem; line-height: 1.65; }
#chatbot .message.bot, #chatbot .bot.message { background: transparent !important; color: var(--ink) !important; padding-left: 4px !important; }
#chatbot .message.user, #chatbot .user.message { background: var(--side) !important; color: var(--ink) !important; border-radius: 16px !important; }
#chatbot .placeholder-content, #chatbot .placeholder { text-align: center; font-family: Georgia, serif; color: var(--ink); }
#chatbot .placeholder h2 { font-weight: 400; font-size: 2rem; }
#chatbot .placeholder p { font-family: 'Inter', sans-serif; color: var(--muted); }
#mode-pills, #mode-pills .wrap { background: transparent !important; border: none !important; box-shadow: none !important; padding: 0 !important; justify-content: center; }
#mode-pills label { border-radius: 999px !important; border: 1px solid var(--line) !important; background: #fff !important; padding: 6px 16px !important;
                    color: var(--ink) !important; cursor: pointer; box-shadow: none !important; }
#mode-pills label.selected { background: var(--accent) !important; border-color: var(--accent) !important; color: #fff !important; }
#mode-pills input { display: none !important; }
#composer { background: #fff !important; border: 1px solid var(--line) !important; border-radius: 22px !important;
            box-shadow: 0 2px 10px rgba(0,0,0,.05) !important; }
#composer textarea { background: #fff !important; color: var(--ink) !important; }
#composer button.submit-button, #composer .submit-button { background: var(--accent) !important; color: #fff !important; border-radius: 10px !important; }
#stop-btn { background: transparent !important; border: 1px solid var(--line) !important; color: var(--muted) !important; border-radius: 10px !important; max-width: 160px; margin: 0 auto; }
#hint { color: var(--muted); font-size: .8rem; text-align: center; }
#hint p { margin: 0 !important; }
@media (max-width: 800px) { #sidebar { min-height: auto; position: static; max-height: none; } }
"""

FORCE_LIGHT_JS = "() => { document.body.classList.remove('dark'); document.documentElement.classList.remove('dark'); }"

LOAD_JS = f"""(v) => {{
  try {{ return [localStorage.getItem('{STORAGE_KEY}') || '']; }} catch (e) {{ return ['']; }}
}}"""

SAVE_JS = f"""(v) => {{
  try {{ localStorage.setItem('{STORAGE_KEY}', v); }} catch (e) {{ console.warn('Could not save chats', e); }}
}}"""

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

WELCOME = "## ✨ How can I help you today?\n\nPick a mode, ask a question, or attach a file with the 📎 button."

SUPPORTED_MD = (
    "**Documents:** " + ", ".join(DOC_TYPES) + "  \n"
    "**Data & text:** " + ", ".join(DATA_TYPES) + "  \n"
    "**Code:** " + ", ".join(CODE_TYPES) + "  \n"
    "**Also accepted:** " + ", ".join(ALIAS_TYPES) + "  \n\n"
    f"Long files are cut to {MAX_FILE_CHARS:,} characters each. Images, audio and legacy "
    ".doc/.xls/.ppt files are not supported (save them as .docx/.xlsx/.pptx first)."
)

theme = gr.themes.Base(
    primary_hue="orange",
    neutral_hue="stone",
    font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
).set(
    body_background_fill="#faf9f5",
    body_text_color="#1f1e1d",
    block_background_fill="transparent",
    block_border_width="0px",
    input_background_fill="#ffffff",
    button_primary_background_fill="#c96442",
    button_primary_background_fill_hover="#b5573a",
    button_primary_text_color="#ffffff",
)

with gr.Blocks(theme=theme, css=CSS, title="Tutor Studio", js=FORCE_LIGHT_JS) as demo:
    session = gr.State(None)                     # the chat store for this visitor (server-side, per session)
    persist_in = gr.Textbox(visible=False)       # receives localStorage content on page load
    persist_out = gr.Textbox(visible=False)      # carries JSON to the browser for saving

    with gr.Row(equal_height=False):
        # ---------------- sidebar ----------------
        with gr.Column(scale=1, min_width=270, elem_id="sidebar"):
            gr.Markdown("✨ Tutor Studio", elem_id="brand")
            new_btn = gr.Button("+  New chat", elem_id="new-chat")
            gr.Markdown("Recents", elem_id="recents-label")
            history_list = gr.Radio(choices=[], value=None, show_label=False, container=False, elem_id="history")
            delete_btn = gr.Button("🗑  Delete this chat", elem_id="delete-chat", size="sm")

            with gr.Accordion("Generation settings", open=False):
                temp_slider = gr.Slider(0.0, 1.0, value=0.35, step=0.05, label="Creativity (temperature)")
                tokens_slider = gr.Slider(256, 4096, value=1600, step=128, label="Max response tokens")

            with gr.Accordion("🧮 SymPy math checker", open=False):
                sympy_input = gr.Textbox(label="Expression", value="diff(x**3 + 5*x, x)")
                sympy_output = gr.Textbox(label="Result", interactive=False)
                sympy_btn = gr.Button("Calculate")
                sympy_btn.click(run_sympy, inputs=sympy_input, outputs=sympy_output)

            with gr.Accordion("📎 Supported file types (30)", open=False):
                gr.Markdown(SUPPORTED_MD)

        # ---------------- main chat ----------------
        with gr.Column(scale=4, elem_id="main"):
            chatbot = gr.Chatbot(
                elem_id="chatbot",
                show_label=False,
                height="62vh",
                bubble_full_width=False,
                show_copy_button=True,
                latex_delimiters=LATEX,
                placeholder=WELCOME,
            )
            mode_radio = gr.Radio(
                choices=list(MODES.keys()), value="General",
                show_label=False, container=False, elem_id="mode-pills",
            )
            composer = gr.MultimodalTextbox(
                elem_id="composer",
                show_label=False,
                container=False,
                file_types=ACCEPTED_TYPES,
                file_count="multiple",
                placeholder="Message Tutor Studio…  (use 📎 to attach files)",
                lines=1,
                max_lines=8,
            )
            stop_btn = gr.Button("■  Stop generating", elem_id="stop-btn", size="sm")
            gr.Markdown("Chats are saved only in this browser. Tutor Studio can make mistakes - check important answers.", elem_id="hint")

    # ---------------- events ----------------
    def persist(event):
        """After any change: serialize the store and save it to the browser's localStorage."""
        return event.then(store_to_json, [session], [persist_out], show_progress="hidden").then(
            None, [persist_out], None, js=SAVE_JS
        )

    sidebar_outputs = [session, history_list, chatbot]

    demo.load(on_load, [persist_in], sidebar_outputs, js=LOAD_JS)

    persist(new_btn.click(new_chat, [session], sidebar_outputs, show_progress="hidden"))
    persist(history_list.input(select_chat, [history_list, session], sidebar_outputs, show_progress="hidden"))
    persist(delete_btn.click(delete_chat, [session], sidebar_outputs, show_progress="hidden"))

    send = composer.submit(
        add_user, [composer, session], [session, chatbot, composer, history_list], show_progress="hidden"
    )
    generate = send.then(
        respond, [session, mode_radio, temp_slider, tokens_slider], [session, chatbot, history_list],
        show_progress="hidden",
    )
    persist(generate)

    stop_event = stop_btn.click(stop_save, [session], [session, chatbot], cancels=[generate], show_progress="hidden")
    persist(stop_event)

if __name__ == "__main__":
    demo.queue(max_size=20).launch()