File size: 41,486 Bytes
b7c755d
 
b6c29b8
 
b7c755d
 
 
 
 
4102668
b6c29b8
b7c755d
 
b6c29b8
b7c755d
 
 
 
b6c29b8
b7c755d
b6c29b8
 
 
b7c755d
 
 
 
 
 
 
 
 
 
 
 
4102668
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4102668
b7c755d
 
4102668
b7c755d
4102668
 
b7c755d
b6c29b8
b7c755d
4102668
b7c755d
 
b6c29b8
 
 
 
b7c755d
 
b6c29b8
 
b7c755d
b6c29b8
 
 
 
 
 
b7c755d
b6c29b8
 
 
4102668
 
b6c29b8
 
 
 
 
 
 
 
 
 
 
b7c755d
 
 
 
 
 
 
b6c29b8
 
 
 
 
 
b7c755d
 
 
 
b6c29b8
 
 
 
 
 
 
 
 
 
b7c755d
b6c29b8
 
 
 
 
b7c755d
 
b6c29b8
 
 
 
 
 
 
 
 
 
b7c755d
b6c29b8
 
 
 
 
 
 
 
 
 
 
b7c755d
b6c29b8
 
b7c755d
 
 
 
 
b6c29b8
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4102668
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6c29b8
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6c29b8
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4102668
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6c29b8
 
 
b7c755d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4102668
b7c755d
 
 
 
 
 
4102668
 
 
 
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
import json
import re
import base64
import io
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Tuple

import numpy as np
from PIL import Image
import gradio as gr
from huggingface_hub import InferenceClient

# ============================================================
# 0.  Model + guidelines setup (Inference API version)
# ============================================================

MODEL_NAME = "maryzhang/qwen3vl-guideline-lora-model"

print(f"Using hosted model via Inference API: {MODEL_NAME}", flush=True)

# This uses the HF Inference API (no local weights, no GPU in the Space)
# If the model is private, set HF_TOKEN as an environment variable in the Space.
hf_client = InferenceClient(MODEL_NAME)

GUIDELINES_PATH = "guidelines_final.json"


def load_guidelines(path: str) -> List[Dict[str, Any]]:
    """
    Robust loader for guidelines_final.json.
    Accepts:
      - a big sequence of JSON objects (your current format)
      - or a single list
      - or {"guidelines": [...]}
    Returns flat list of dicts that contain "guideline_id".
    """
    with open(path, "r") as f:
        raw = f.read()

    raw = raw.strip()
    if not raw:
        raise ValueError("guidelines_final.json is empty.")

    decoder = json.JSONDecoder()
    pos = 0
    length = len(raw)
    objects: List[Any] = []

    # collect all JSON fragments
    while pos < length:
        while pos < length and raw[pos].isspace():
            pos += 1
        if pos >= length:
            break
        try:
            obj, end = decoder.raw_decode(raw, pos)
        except json.JSONDecodeError:
            pos += 1
            continue
        objects.append(obj)
        pos = end

    if not objects:
        raise ValueError("No JSON fragments found in guidelines_final.json")

    candidates: List[Any] = []
    for obj in objects:
        if isinstance(obj, list):
            candidates.extend(obj)
        elif isinstance(obj, dict) and isinstance(obj.get("guidelines"), list):
            candidates.extend(obj["guidelines"])
        elif isinstance(obj, dict):
            candidates.append(obj)

    guidelines: List[Dict[str, Any]] = []
    for c in candidates:
        if isinstance(c, dict) and "guideline_id" in c:
            guidelines.append(c)

    if not guidelines:
        raise ValueError("Found JSON but no objects with 'guideline_id' field.")
    return guidelines


ALL_GUIDELINES: List[Dict[str, Any]] = load_guidelines(GUIDELINES_PATH)
GUIDELINE_BY_ID: Dict[str, Dict[str, Any]] = {g["guideline_id"]: g for g in ALL_GUIDELINES}

print(f"Loaded {len(ALL_GUIDELINES)} guidelines", flush=True)


# ============================================================
# 1.  Core LLM helpers (text-only + vision via Inference API)
# ============================================================

def run_text_llm(system_prompt: str, user_prompt: str, max_new_tokens: int = 768) -> str:
    """
    Use the hosted Qwen3-VL model in text-only mode via chat_completion.

    We build a simple system+user messages list and ask for a deterministic
    response (temperature=0).
    """
    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt},
    ]

    response = hf_client.chat_completion(
        messages=messages,
        max_tokens=max_new_tokens,
        temperature=0.0,
        stream=False,
    )
    # HuggingFace InferenceClient returns a ChatCompletionOutput
    text = response.choices[0].message.content
    return (text or "").strip()


def _pil_to_data_url(img: Image.Image, fmt: str = "PNG") -> str:
    """
    Convert a PIL image to a data URL (base64-encoded), which matches the
    format expected by chat_completion with vision support:
    type: "image_url", image_url: {"url": "data:image/png;base64,..."}
    """
    buf = io.BytesIO()
    img.save(buf, format=fmt)
    b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
    mime = "image/png" if fmt.upper() == "PNG" else "image/jpeg"
    return f"data:{mime};base64,{b64}"


def vlm_generate_json_from_images(
    prompt: str,
    images: List[Image.Image],
) -> Dict[str, Any]:
    """
    Call the hosted Qwen3-VL model with images + text using chat_completion.
    We ask it to return STRICT JSON and then parse the JSON out of the reply.

    This assumes the model supports OpenAI-style multimodal messages where
    each content item can be {"type": "image_url", "image_url": {"url": ...}}
    plus a text chunk.
    """
    if not images:
        images = [Image.new("RGB", (64, 64), "white")]

    # Build message content with multiple images + prompt text
    content: List[Dict[str, Any]] = []
    for img in images:
        url = _pil_to_data_url(img)
        content.append(
            {
                "type": "image_url",
                "image_url": {"url": url},
            }
        )

    content.append(
        {
            "type": "text",
            "text": prompt,
        }
    )

    messages = [
        {
            "role": "system",
            "content": "You are a vision model that ONLY replies with strict JSON.",
        },
        {
            "role": "user",
            "content": content,
        },
    ]

    # Ask for a deterministic, non-streaming, JSON-like answer
    response = hf_client.chat_completion(
        messages=messages,
        max_tokens=512,
        temperature=0.0,
        stream=False,
        # If your model supports response_format, you can uncomment:
        # response_format={"type": "json_object"},
    )
    raw = response.choices[0].message.content or ""
    raw = raw.strip()

    # Try to extract JSON object from the raw string
    m = re.search(r"\{.*\}", raw, re.DOTALL)
    if m:
        try:
            return json.loads(m.group(0))
        except Exception:
            pass
    return {"parse_error": True, "raw": raw}


# ============================================================
# 2.  Feature extraction & guideline selection
# ============================================================

FEATURE_PROMPT = """
You are assisting with manufacturability and GD&T review.
Given these 1–3 CAD / drawing images, return a JSON object with:
{
  "image_type": "cad_model" | "dimensioned_drawing" | "photo" | "other",
  "has_gdt": bool,
  "has_dimensions": bool,
  "features": {
    "holes": int,
    "vertical_faces": bool,
    "possible_draft": bool,
    "ribs": int,
    "fillets": bool,
    "chamfers": bool,
    "datum_symbols": ["A", "B"],
    "gdt_frames_present": bool,
    "text_dimensions_present": bool
  },
  "raw_notes": "short human-readable notes about what you see",
  "generated_description": "one-sentence description of the part/drawing",
  "suggested_guidelines": []
}
Rules:
- Infer only what is visible or strongly implied.
- Keep numbers rough (e.g., count of holes), not exact metrology.
- Only output valid JSON. No explanation outside the JSON.
- Do NOT hard-code any specific guideline IDs.
"""


def extract_visual_features(images: List[Image.Image]) -> Dict[str, Any]:
    if not images:
        return {
            "image_type": "",
            "has_gdt": False,
            "has_dimensions": False,
            "features": {
                "holes": 0,
                "vertical_faces": False,
                "possible_draft": False,
                "ribs": 0,
                "fillets": False,
                "chamfers": False,
                "datum_symbols": [],
                "gdt_frames_present": False,
                "text_dimensions_present": False,
            },
            "raw_notes": "",
            "generated_description": "",
            "suggested_guidelines": [],
        }

    vlm_json = vlm_generate_json_from_images(FEATURE_PROMPT, images)

    return {
        "image_type": vlm_json.get("image_type", ""),
        "has_gdt": vlm_json.get("has_gdt", False),
        "has_dimensions": vlm_json.get("has_dimensions", False),
        "features": vlm_json.get("features", {}),
        "raw_notes": vlm_json.get("raw_notes", ""),
        "generated_description": vlm_json.get("generated_description", ""),
        "suggested_guidelines": vlm_json.get("suggested_guidelines", []),
    }


def rag_retrieve(query: str, top_k: int = 6) -> List[Dict[str, Any]]:
    """
    Tiny RAG over the 20 guidelines.
    Now also includes pass_fail_logic in the searchable blob so the
    evaluator can "see" the numeric rules.
    """
    q = (query or "").lower()
    if not q.strip():
        return []

    scored = []
    for g in ALL_GUIDELINES:
        pfl = g.get("pass_fail_logic") or {}
        pfl_text = " ".join(f"{k}: {v}" for k, v in pfl.items())
        blob = " ".join(
            [
                g.get("topic", ""),
                " ".join(g.get("evaluation_criteria", []) or []),
                " ".join(g.get("expected_answers", []) or []),
                pfl_text,
            ]
        ).lower()
        score = sum(token in blob for token in q.split())
        if score > 0:
            scored.append((score, g))

    scored.sort(key=lambda x: x[0], reverse=True)
    hits = []
    for score, g in scored[:top_k]:
        pfl = g.get("pass_fail_logic") or {}
        pfl_text = " ".join(f"{k}: {v}" for k, v in pfl.items())
        text = (
            " ".join(g.get("evaluation_criteria", []) or [])
            or " ".join(g.get("expected_answers", []) or [])
            or pfl_text
        )
        hits.append(
            {
                "source": "guideline",
                "text": text,
                "meta": {
                    "guideline_id": g["guideline_id"],
                    "topic": g.get("topic", ""),
                },
            }
        )
    return hits


def classify_mode(description: str, feature_summary: Dict[str, Any]) -> str:
    desc_lower = (description or "").lower()
    feats = feature_summary.get("features", {})

    image_type = (feature_summary.get("image_type") or "").lower()
    has_gdt_flag = bool(feature_summary.get("has_gdt"))
    has_dims_flag = bool(feature_summary.get("has_dimensions"))

    has_datum = bool(feats.get("datum_symbols"))
    has_gdt_feat = feats.get("gdt_frames_present", False)

    cad_like_words = ["cad", "model", "solid", "surface", "bottle", "housing", "rib"]
    drawing_like_words = ["drawing", "dimension", "tolerance"]

    has_cad_words = any(w in desc_lower for w in cad_like_words)
    has_drawing_words = any(w in desc_lower for w in drawing_like_words)

    gd_signals = any(
        [
            image_type == "dimensioned_drawing",
            has_gdt_flag,
            has_gdt_feat,
            has_datum,
            has_dims_flag,
            has_drawing_words,
        ]
    )
    cad_signals = any(
        [
            image_type == "cad_model",
            has_cad_words,
        ]
    )

    if gd_signals and cad_signals:
        return "mixed"
    if gd_signals:
        return "gdt"
    if cad_signals:
        return "dfm"
    return "dfm"


def select_applicable_guidelines(
    feature_summary: Dict[str, Any],
    description: str,
    max_guidelines: int = 5,
) -> List[Dict[str, Any]]:
    """
    Choose a subset of guidelines out of all 20, based on dfm/gdt mode.
    Returns lightweight dicts (guideline_id + topic), but the evaluator
    will later look up the full objects from GUIDELINE_BY_ID.
    """
    mode = classify_mode(description, feature_summary)
    suggestions = feature_summary.get("suggested_guidelines") or []

    def category_of(g: Dict[str, Any]) -> str:
        cat = (g.get("category") or "").lower()
        if cat in ("dfm", "gdt"):
            return cat
        gid = (g.get("guideline_id") or "").upper()
        if gid.startswith("D"):
            return "dfm"
        if gid.startswith("G"):
            return "gdt"
        return ""

    picked: List[Dict[str, Any]] = []
    suggested_ids = set()

    # 1) honour any suggested_guidelines (if they match the mode)
    for s in suggestions:
        gid = s.get("guideline_id")
        if not gid:
            continue
        g = GUIDELINE_BY_ID.get(gid)
        if not g:
            continue
        cat = category_of(g)
        if mode == "gdt" and cat != "gdt":
            continue
        if mode == "dfm" and cat != "dfm":
            continue
        picked.append({"guideline_id": gid, "topic": g.get("topic", "")})
        suggested_ids.add(gid)

    # 2) fill in from ALL_GUIDELINES based on mode
    for g in ALL_GUIDELINES:
        gid = g["guideline_id"]
        if gid in suggested_ids:
            continue
        cat = category_of(g)
        if mode == "gdt" and cat == "gdt":
            picked.append({"guideline_id": gid, "topic": g["topic"]})
        elif mode == "dfm" and cat == "dfm":
            picked.append({"guideline_id": gid, "topic": g["topic"]})
        elif mode == "mixed" and cat in ("gdt", "dfm"):
            picked.append({"guideline_id": gid, "topic": g["topic"]})

    # 3) in mixed mode, bias GD&T first
    if mode == "mixed":
        def is_gdt(gid: str) -> bool:
            g = GUIDELINE_BY_ID.get(gid, {})
            return category_of(g) == "gdt"

        picked.sort(key=lambda x: 0 if is_gdt(x["guideline_id"]) else 1)

    return picked[:max_guidelines]


# ============================================================
# 3.  Evaluation utilities
# ============================================================

def extract_json_from_text(text: str) -> Dict[str, Any]:
    m = re.search(r"\{.*\}", text, re.DOTALL)
    if not m:
        return {"parse_error": True, "raw": text}
    try:
        return json.loads(m.group(0))
    except Exception:
        return {"parse_error": True, "raw": text}


def downgrade_if_no_measurements(
    eval_json: Dict[str, Any],
    qa_text: str,
) -> Dict[str, Any]:
    q_lower = (qa_text or "").lower()
    no_data = any(
        phrase in q_lower
        for phrase in [
            "no measurement data",
            "no measured data",
            "assume 0 mm",
            "assume zero",
            "no cmm data",
        ]
    )
    if not no_data:
        return eval_json

    sensitive_topics = [
        "True Position",
        "Profile",
        "Flatness",
        "Concentricity",
        "Runout",
        "Cylindricity",
        "Circularity",
    ]

    for g in eval_json.get("guidelines", []):
        topic = g.get("topic", "")
        if any(t in topic for t in sensitive_topics):
            g["result"] = "NEEDS_INFO"
            g["reason"] = (
                "This guideline depends on measurement data, and you mentioned that "
                "measurements are not available yet. That's completely fine at the "
                "design stage, so this is marked as NEEDS_INFO rather than PASS/FAIL."
            )
            g["recommendation"] = (
                "Once you have inspection or simulation data, you can re-run this check "
                "to confirm the tolerance is still realistic."
            )

    return eval_json


def calibrate_eval_scores(eval_json: Dict[str, Any]) -> Dict[str, Any]:
    guidelines = eval_json.get("guidelines", [])
    eval_json.setdefault("overall", {})

    if not guidelines:
        eval_json["overall"].update(
            {
                "summary": "No guidelines were evaluated.",
                "verdict": "NEEDS_MORE_DATA",
                "manufacturability_score": 0.6,
            }
        )
        return eval_json

    weights = {"PASS": 1.0, "NEEDS_INFO": 0.7, "FAIL": 0.0}
    results = [g.get("result", "NEEDS_INFO") for g in guidelines]

    if all(r == "NEEDS_INFO" for r in results):
        eval_json["overall"].update(
            {
                "summary": (
                    "All guidelines are marked as NEEDS_INFO for now because some data "
                    "is missing. That's okay—this just means more information will make "
                    "the review stronger later."
                ),
                "verdict": "NEEDS_MORE_DATA",
                "manufacturability_score": 0.65,
            }
        )
        return eval_json

    scores = [weights.get(r, 0.7) for r in results]
    avg = sum(scores) / len(scores)

    if avg > 0.9:
        verdict = "GOOD"
    elif avg > 0.75:
        verdict = "ACCEPTABLE"
    elif avg > 0.6:
        verdict = "RISKY"
    else:
        verdict = "NEEDS_MORE_DATA"

    eval_json["overall"].update(
        {
            "summary": (
                "Automatic manufacturability summary based on the "
                "reviewed guidelines."
            ),
            "verdict": verdict,
            "manufacturability_score": round(float(avg), 2),
        }
    )
    return eval_json


def sanitize_eval_language(
    eval_json: Dict[str, Any],
    description: str,
    feature_summary: Dict[str, Any],
) -> Dict[str, Any]:
    desc_lower = (description or "").lower()
    feats = feature_summary.get("features", {})

    is_machined = any(
        w in desc_lower for w in ["machined", "cnc", "turned", "lathe", "ground"]
    )
    is_molded_like = feats.get("possible_draft", False) or any(
        w in desc_lower for w in ["mold", "mould", "injection", "cast", "die cast"]
    )

    guideline_explanations = {
        "True Position Tolerance": (
            "True position helps ensure that holes or pins line up correctly in "
            "assembly, so parts fit together without binding or excessive play."
        ),
        "Profile Tolerance": (
            "Profile controls how closely a surface matches its ideal CAD shape. "
            "This matters a lot for sealing, smooth airflow, and consistent contact."
        ),
        "Flatness": (
            "Flatness makes sure a surface does not bow or warp, which is important "
            "for good sealing and accurate mounting faces."
        ),
        "Concentricity": (
            "Concentricity ensures that different cylindrical features share the same "
            "axis. This is crucial for rotating parts, shafts, and precision fits."
        ),
    }

    encouraging_phrases = {
        "PASS": (
            "Nice work—this guideline looks solid. If you want to go further, you "
            "could explore tolerance stack-ups or measurement planning for production."
        ),
        "NEEDS_INFO": (
            "This isn’t a failure—it just means more information (like measurements "
            "or simulation results) would help finish the story."
        ),
        "FAIL": (
            "This might cause manufacturability or inspection challenges, but it's a "
            "great opportunity to iterate and improve the design early."
        ),
    }

    for g in eval_json.get("guidelines", []):
        topic = g.get("topic", "")
        result = g.get("result", "NEEDS_INFO")

        if topic in guideline_explanations:
            g["why_it_matters"] = guideline_explanations[topic]

        g.setdefault("recommendation", "")
        g["recommendation"] = (g["recommendation"] or "").strip()
        extra = encouraging_phrases.get(result)
        if extra:
            if g["recommendation"]:
                g["recommendation"] += " "
            g["recommendation"] += extra

        # clean out weird generic ranges / hole size hallucinations
        for key in ["reason", "recommendation"]:
            text = g.get(key, "")
            if not isinstance(text, str):
                continue

            sentences = re.split(r"(?<=[.!?])\s+", text)
            cleaned_sents = []
            for s in sentences:
                s_lower = s.lower()
                if (
                    "typical range" in s_lower
                    or "small holes" in s_lower
                    or "< 5 mm" in s_lower
                    or "less than 5 mm" in s_lower
                ):
                    continue
                cleaned_sents.append(s)

            new_text = " ".join(cleaned_sents).strip()

            if is_machined and not is_molded_like:
                new_text = (
                    new_text.replace(
                        "molding process capabilities",
                        "machining process capabilities",
                    )
                    .replace("molding process capability", "machining process capability")
                    .replace("molding process", "machining process")
                )

            g[key] = new_text

    overall = eval_json.get("overall", {})
    if overall.get("verdict") == "POOR":
        overall["verdict"] = "NEEDS_MORE_DATA"
        overall["summary"] = (
            "Some guidelines look challenging with the current information, but that "
            "just means there is room to refine the design and collect more data."
        )
    eval_json["overall"] = overall
    return eval_json


def evaluation_agent_txt(
    description: str,
    guidelines: List[Dict[str, Any]],
    qa_text: str,
    feature_summary: Dict[str, Any],
) -> Dict[str, Any]:
    """
    Core evaluator: this is where we now pass in:
    - evaluation_criteria
    - expected_answers
    - pass_fail_logic
    for EACH guideline, so the model can truly reason over your 20 rules.
    """
    # Enrich guideline objects from the global GUIDELINE_BY_ID
    enriched_guidelines = []
    for g in guidelines:
        gid = g.get("guideline_id")
        base = GUIDELINE_BY_ID.get(gid, {})
        enriched_guidelines.append(
            {
                "guideline_id": gid,
                "topic": base.get("topic", g.get("topic", "")),
                "category": base.get("category", ""),
                "evaluation_criteria": base.get("evaluation_criteria", []),
                "user_questions": base.get("user_questions", []),
                "expected_answers": base.get("expected_answers", []),
                "pass_fail_logic": base.get("pass_fail_logic", {}),
            }
        )

    rag_query_text = " ".join(
        [
            description or "",
            qa_text or "",
            json.dumps(feature_summary.get("features", {})),
        ]
    )
    rag_hits = rag_retrieve(rag_query_text, top_k=6)

    rag_context_lines = []
    for h in rag_hits:
        meta = h.get("meta", {})
        gid = meta.get("guideline_id", "UNKNOWN")
        topic = meta.get("topic", "")
        rag_context_lines.append(f"[GUIDELINE {gid} - {topic}]\n{h['text']}")
    rag_context = (
        "\n\n---\n\n".join(rag_context_lines)
        if rag_context_lines
        else "(no extra context)"
    )

    sys_prompt = (
        "You are a senior manufacturing / GD&T engineer and a patient instructor.\n"
        "You are given:\n"
        "- An optional short description of the part/product\n"
        "- A set of DFM/GD&T guidelines to apply (including evaluation_criteria,\n"
        "  expected_answers, and pass_fail_logic for each guideline)\n"
        "- A Q&A history where the student answered questions about each guideline\n"
        "- A feature summary extracted from CAD/drawing images\n"
        "- Additional reference passages from a guideline knowledge base (RAG)\n\n"
        "Your goals:\n"
        "1) For EACH guideline, use the student's numeric/text answers and the\n"
        "   'pass_fail_logic' rules to decide whether the guideline is PASS, FAIL,\n"
        "   or NEEDS_INFO.\n"
        "   • PASS = clearly satisfies the numeric / logical rules.\n"
        "   • FAIL = clearly violates at least one rule in pass_fail_logic.\n"
        "   • NEEDS_INFO = only if you truly cannot tell from the Q&A + features.\n"
        "2) Refer directly to the variables in pass_fail_logic (e.g., nominal_wall,\n"
        "   variation, rib_or_boss_thickness) and the numbers in the Q&A when\n"
        "   making decisions. Treat the rules as engineering check equations.\n"
        "3) Explain briefly WHY in clear engineering language.\n"
        "4) Offer encouraging, actionable recommendations—talk like a helpful TA.\n"
        "5) Comment qualitatively on tolerance feasibility in the 'overall' block.\n\n"
        "IMPORTANT:\n"
        "- You MUST try to produce PASS or FAIL when the numeric conditions are\n"
        "  clearly satisfied or violated. Do NOT default to NEEDS_INFO if the\n"
        "  student already provided the key numbers.\n"
        "- Only use NEEDS_INFO when the data is genuinely missing or ambiguous.\n\n"
        "Respond ONLY as a single JSON object with this schema:\n"
        "{\n"
        '  "guidelines": [\n'
        "    {\n"
        '      "guideline_id": str,\n'
        '      "topic": str,\n'
        '      "result": "PASS" | "FAIL" | "NEEDS_INFO",\n'
        '      "reason": str,\n'
        '      "recommendation": str\n'
        "    }\n"
        "  ],\n"
        '  "overall": {\n'
        '    "summary": str,\n'
        '    "verdict": "GOOD" | "ACCEPTABLE" | "RISKY" | "NEEDS_MORE_DATA",\n'
        '    "manufacturability_score": float\n'
        "  }\n"
        "}\n"
    )

    user_parts = [
        "DESCRIPTION:",
        description or "(none provided)",
        "\n\nGUIDELINES UNDER REVIEW (with criteria and logic):",
        json.dumps(enriched_guidelines, indent=2),
        "\n\nQ&A HISTORY (questions and answers as free text):",
        qa_text or "(no questions asked yet)",
        "\n\nFEATURE SUMMARY FROM IMAGE(S):",
        json.dumps(feature_summary, indent=2),
        "\n\nRETRIEVED REFERENCES (RAG):",
        rag_context,
        "\n\nProduce ONLY the JSON object.",
    ]
    user_prompt = "\n".join(user_parts)

    raw = run_text_llm(sys_prompt, user_prompt, max_new_tokens=1024)
    eval_json = extract_json_from_text(raw)

    if not eval_json.get("parse_error"):
        eval_json = downgrade_if_no_measurements(eval_json, qa_text)
        eval_json = calibrate_eval_scores(eval_json)
        eval_json = sanitize_eval_language(eval_json, description, feature_summary)
    return eval_json


def summarize_eval_for_student(eval_json: Dict[str, Any]) -> str:
    guidelines = eval_json.get("guidelines", [])
    overall = eval_json.get("overall", {})

    lines: List[str] = []
    lines.append(
        "Thanks, that’s all the questions I needed for now. "
        "Here’s your manufacturability snapshot based on those answers:"
    )
    lines.append("")

    score = overall.get("manufacturability_score")
    verdict = overall.get("verdict")
    summary = overall.get("summary", "")

    if score is not None or verdict:
        headline = "• Overall verdict: "
        if verdict:
            headline += str(verdict)
        if score is not None:
            headline += f" (score ≈ {score:.2f})"
        lines.append(headline)

    if summary:
        lines.append(f"• Summary: {summary}")
    lines.append("")

    if guidelines:
        lines.append("Guideline-by-guideline notes:")
        for g in guidelines:
            topic = g.get("topic", "Unnamed guideline")
            result = g.get("result", "NEEDS_INFO")
            reason = g.get("reason", "")
            rec = g.get("recommendation", "")
            lines.append(f"- {topic}{result}")
            if reason:
                lines.append(f"    • Why: {reason}")
            if rec:
                lines.append(f"    • Suggestion: {rec}")
    else:
        lines.append(
            "I wasn’t able to evaluate any specific guidelines, likely because "
            "we didn’t get enough structured answers."
        )

    lines.append("")
    lines.append(
        "If you’d like to see the raw JSON data for debugging or research, "
        "you can ask: “show me the JSON summary.”"
    )
    return "\n".join(lines)


# ============================================================
# 4.  Conversation state & router
# ============================================================

@dataclass
class GuidelineConversationState:
    selected_guidelines: List[Dict[str, Any]] = field(default_factory=list)
    current_guideline_idx: int = 0
    qa_log: List[Tuple[str, str]] = field(default_factory=list)
    max_questions: int = 8
    questions_asked: int = 0
    feature_summary: Dict[str, Any] = field(default_factory=dict)
    description: str = ""


def current_guideline(
    state: GuidelineConversationState,
) -> Optional[Dict[str, Any]]:
    if 0 <= state.current_guideline_idx < len(state.selected_guidelines):
        return state.selected_guidelines[state.current_guideline_idx]
    return None


def build_intro_message(
    description: str,
    feature_summary: Dict[str, Any],
    selected_guidelines: List[Dict[str, Any]],
    max_questions: int,
) -> str:
    gen_desc = feature_summary.get("generated_description") or ""
    raw_notes = feature_summary.get("raw_notes") or ""

    desc_bits = []
    if gen_desc:
        desc_bits.append(gen_desc)
    if description:
        desc_bits.append(description)
    if raw_notes:
        desc_bits.append(raw_notes)

    combined_desc = (
        " ".join(desc_bits)
        if desc_bits
        else "I’ll infer as much as I can directly from your image."
    )

    guideline_topics = [g["topic"] for g in selected_guidelines]
    guideline_list_str = (
        ", ".join(guideline_topics)
        if guideline_topics
        else "a small set of relevant DFM/GD&T rules"
    )

    intro = (
        f"{combined_desc}\n\n"
        "Based on this, I’ll walk you through a short manufacturability review.\n"
        f"We’ll look at these guidelines: {guideline_list_str}.\n"
        "I’ll ask at most ~"
        f"{max_questions} focused questions, and then summarize how "
        "manufacturable this design looks and where you could improve it.\n\n"
        "Let’s start with the first guideline."
    )
    return intro


def get_guideline_questions(gid: str) -> List[str]:
    g = GUIDELINE_BY_ID.get(gid)
    if not g:
        return []
    qs = g.get("user_questions") or g.get("questions") or []
    out = []
    for q in qs:
        if isinstance(q, str):
            out.append(q)
        elif isinstance(q, dict) and "question" in q:
            out.append(q["question"])
    return out


def classify_user_turn(user_text: str, last_question: str) -> str:
    """
    Tiny router: is the user answering the guideline question,
    or asking their own side question?
    Returns "answer" or "student_question".
    """
    sys_prompt = (
        "You are a routing model for a tutoring chat about DFM/GD&T.\n"
        "Given the last question asked by the tutor and the student's reply,\n"
        "decide if the student is primarily ANSWERING the question, or asking a new\n"
        "QUESTION of their own (e.g., 'can I add a fillet here?').\n\n"
        "Reply ONLY in JSON like {\"label\": \"answer\"} or "
        "{\"label\": \"student_question\"}."
    )
    user_prompt = (
        f"Tutor_question: {last_question}\n"
        f"Student_message: {user_text}\n"
        "Label:"
    )
    raw = run_text_llm(sys_prompt, user_prompt, max_new_tokens=64)
    m = re.search(r"\{.*\}", raw, re.DOTALL)
    if not m:
        return "answer"
    try:
        obj = json.loads(m.group(0))
        label = (obj.get("label") or "").lower()
        if label in {"answer", "student_question"}:
            return label
    except Exception:
        pass
    return "answer"


def answer_student_question(
    user_text: str,
    state: GuidelineConversationState,
    chat_history: List[Tuple[str, str]],
) -> str:
    """
    Use the same model to answer a side-question in a friendly way.
    This does NOT advance the guideline review.
    """
    last_q = chat_history[-1][0] if chat_history else ""
    qa_snippets = []
    for q, a in state.qa_log[-3:]:
        qa_snippets.append(f"Q: {q}\nA: {a}")
    qa_str = "\n---\n".join(qa_snippets) if qa_snippets else "(no prior Q&A)"

    sys_prompt = (
        "You are a friendly manufacturing / GD&T teaching assistant inside a small app.\n"
        "The student may ask meta-questions like 'can I add a fillet here?', "
        "'is this draft enough?', or 'what tolerance should I use?'.\n"
        "Use the selected DFM/GD&T guidelines, the feature summary, and their answers\n"
        "to give concrete, practical advice.\n\n"
        "Prefer to reference guidelines by topic (e.g., Wall Thickness, Draft Angle).\n"
        "Talk about trade-offs (manufacturability, cost, risk).\n"
        "Keep answers short (2–6 sentences).\n"
        "Do NOT output JSON; just respond as normal helpful text."
    )
    user_parts = [
        "Part description:",
        state.description or "(none)",
        "\nFeature summary:",
        json.dumps(state.feature_summary, indent=2),
        "\nSelected guidelines:",
        json.dumps(state.selected_guidelines, indent=2),
        "\nRecent Q&A:",
        qa_str,
        "\nLast tutor question:",
        last_q or "(none)",
        "\nStudent question:",
        user_text,
    ]
    user_prompt = "\n".join(user_parts)
    reply = run_text_llm(sys_prompt, user_prompt, max_new_tokens=256)
    return reply


def step_conversation(
    chat_history: List[Tuple[str, str]],
    user_message: str,
    state: GuidelineConversationState,
) -> Tuple[List[Tuple[str, str]], GuidelineConversationState]:
    """
    One conversation step for an ANSWER (router already decided).
    """
    # Log student's answer into QA log
    if chat_history and user_message.strip():
        last_assistant, _ = chat_history[-1]
        state.qa_log.append((last_assistant, user_message))
        state.questions_asked += 1

    # Stopping condition
    if state.questions_asked >= state.max_questions or not current_guideline(state):
        qas_text = "\n".join([f"Q: {q}\nA: {a}" for q, a in state.qa_log])
        eval_json = evaluation_agent_txt(
            state.description,
            state.selected_guidelines,
            qas_text,
            state.feature_summary,
        )
        friendly_summary = summarize_eval_for_student(eval_json)
        chat_history.append((friendly_summary, ""))
        return chat_history, state

    # Otherwise, determine next question
    current = current_guideline(state)
    gid = current["guideline_id"]
    topic = current["topic"]
    questions = get_guideline_questions(gid)

    asked_for_this_topic = [q for q, _ in state.qa_log if topic in q]
    idx = len(asked_for_this_topic)

    if idx >= len(questions):
        # move to next guideline
        state.current_guideline_idx += 1
        if not current_guideline(state):
            return step_conversation(chat_history, user_message, state)
        current = current_guideline(state)
        gid = current["guideline_id"]
        topic = current["topic"]
        questions = get_guideline_questions(gid)
        idx = 0
        if not questions:
            return step_conversation(chat_history, user_message, state)

    q_text = questions[idx]
    header = (
        f"Now let’s look at {topic}.\n\n"
        "For this guideline, we’re checking a few key points from your DFM/GD&T rules. "
        "I’ll ask a quick question to see whether your design satisfies it.\n\n"
    )
    full_q = header + q_text
    chat_history.append((full_q, ""))
    return chat_history, state


# --------- helper to convert internal tuples -> Chatbot messages ----------

def tuples_to_messages(history: List[Tuple[str, str]]) -> List[Dict[str, Any]]:
    """
    Convert [(assistant, user), ...] to Chatbot 'messages' format:
    [{"role": "assistant", "content": "..."},
     {"role": "user", "content": "..."}, ...]
    """
    messages: List[Dict[str, Any]] = []
    for assistant_text, user_text in history:
        if assistant_text:
            messages.append({"role": "assistant", "content": assistant_text})
        if user_text:
            messages.append({"role": "user", "content": user_text})
    return messages


# ============================================================
# 5.  Gradio UI
# ============================================================

with gr.Blocks(title="DFM / GD&T Manufacturability Tutor") as demo:
    gr.Markdown(
        """
        # 📐 DFM / GD&T Manufacturability Tutor
        1. Upload **1–3 CAD screenshots or drawings**  
        2. *(Optional)* Add a short description of the part  
        3. Click **Start review**  
        4. Answer a few focused questions → get a guideline-by-guideline summary  

        This tool is powered by a hosted multimodal model via the Hugging Face Inference API,
        so it runs on free CPU hardware without loading big weights in this Space.
        """
    )

    state = gr.State(GuidelineConversationState())
    chat_state = gr.State([])  # internal: list[Tuple[str, str]]

    with gr.Row():
        with gr.Column(scale=3):
            chat = gr.Chatbot(
                label="Conversation",
                height=480,
            )
            user_box = gr.Textbox(
                label="Your answer or question",
                placeholder=(
                    "Answer the current question, or ask something like "
                    "'can I 3D print this?'"
                ),
            )
            start_btn = gr.Button("▶️ Start review (or restart)")
        with gr.Column(scale=2):
            image_input = gr.Image(
                type="numpy",
                label="Upload 1–3 CAD/drawing screenshots",
            )
            description_box = gr.Textbox(
                label="(Optional) Short description of the part",
                placeholder="e.g., 'Machined plunger for a relief valve with 60° cone'",
            )
            max_q_slider = gr.Slider(
                label="Max questions",
                minimum=3,
                maximum=12,
                value=8,
                step=1,
            )
            feature_debug = gr.JSON(
                label="Feature Summary (debug)",
                visible=False,
            )
            guideline_debug = gr.JSON(
                label="Selected Guidelines (debug)",
                visible=False,
            )

    # ---------- Event wiring ----------
    def _start(images, desc, max_q):
        """
        Gradio callback for 'Start review (or restart)'.
        Normalize images, run feature extractor, pick guidelines,
        compose intro + first question.
        """
        if images is None:
            image_list: List[np.ndarray] = []
        elif isinstance(images, list):
            image_list = images
        else:
            image_list = [images]

        pil_images = [Image.fromarray(img) for img in image_list] if image_list else []
        feature_summary = extract_visual_features(pil_images)
        selected = select_applicable_guidelines(
            feature_summary,
            desc or "",
            max_guidelines=5,
        )

        state_obj = GuidelineConversationState(
            selected_guidelines=selected,
            current_guideline_idx=0,
            qa_log=[],
            max_questions=int(max_q),
            questions_asked=0,
            feature_summary=feature_summary,
            description=desc or "",
        )

        chat_tuples: List[Tuple[str, str]] = []
        intro_msg = build_intro_message(
            desc or "",
            feature_summary,
            selected,
            int(max_q),
        )
        chat_tuples.append((intro_msg, ""))

        # Ask first guideline question
        chat_tuples, state_obj = step_conversation(chat_tuples, "", state_obj)

        chat_messages = tuples_to_messages(chat_tuples)
        return chat_messages, "", feature_summary, selected, state_obj, chat_tuples

    def _answer(user_text, tuple_history, state_obj: GuidelineConversationState):
        """
        Gradio callback for the textbox submit.
        - Route the user turn to 'answer' vs 'student_question'
        - If answer → advance guideline flow
        - If student_question → chatty side-answer, no state advancement
        """
        chat_history: List[Tuple[str, str]] = tuple_history or []
        user_text = (user_text or "").strip()
        if not user_text:
            chat_messages = tuples_to_messages(chat_history)
            return chat_messages, "", state_obj, chat_history

        last_question = chat_history[-1][0] if chat_history else ""
        label = classify_user_turn(user_text, last_question)

        if label == "student_question":
            reply = answer_student_question(user_text, state_obj, chat_history)
            chat_history.append((reply, ""))
            chat_messages = tuples_to_messages(chat_history)
            return chat_messages, "", state_obj, chat_history

        # label == "answer": attach answer to last question and advance
        if chat_history:
            last_q, _ = chat_history[-1]
            chat_history[-1] = (last_q, user_text)

        chat_history, new_state = step_conversation(
            chat_history,
            user_text,
            state_obj,
        )
        chat_messages = tuples_to_messages(chat_history)
        return chat_messages, "", new_state, chat_history

    # Button → start/restart the review
    start_btn.click(
        _start,
        inputs=[image_input, description_box, max_q_slider],
        outputs=[chat, user_box, feature_debug, guideline_debug, state, chat_state],
    )

    # Textbox submit → route + respond
    user_box.submit(
        _answer,
        inputs=[user_box, chat_state, state],
        outputs=[chat, user_box, state, chat_state],
    )


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