-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefine.html
More file actions
2798 lines (2522 loc) · 138 KB
/
Copy pathrefine.html
File metadata and controls
2798 lines (2522 loc) · 138 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>REFINE — Clinical NLP Rule Refinement</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f8fafc; color: #1e293b; display: flex; flex-direction: column; min-height: 100vh; font-size: 14px; }
header { background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%); color: white; padding: 14px 24px; display: flex; align-items: center; gap: 16px; box-shadow: 0 2px 8px rgba(0,0,0,0.2); }
header .logo { font-size: 20px; font-weight: 800; letter-spacing: -0.5px; }
header .tagline { font-size: 12px; opacity: 0.75; margin-top: 1px; }
header .header-right { margin-left: auto; display: flex; align-items: center; gap: 16px; }
#llm-badge { font-size: 12px; padding: 4px 10px; border-radius: 20px; background: rgba(255,255,255,0.15); }
.app-body { display: flex; flex: 1; overflow: hidden; height: calc(100vh - 53px); }
/* SIDEBAR */
.sidebar { width: 268px; min-width: 268px; background: white; border-right: 1px solid #e2e8f0; padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 20px; }
.sidebar-section-title { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.8px; color: #94a3b8; margin-bottom: 10px; }
/* MAIN */
.main { flex: 1; overflow-y: auto; padding: 24px; }
/* TABS */
.tabs { display: flex; border-bottom: 2px solid #e2e8f0; margin-bottom: 24px; flex-wrap: wrap; }
.tab-btn { padding: 10px 16px; border: none; background: none; cursor: pointer; font-size: 13px; font-weight: 500; color: #64748b; border-bottom: 3px solid transparent; margin-bottom: -2px; transition: all 0.15s; white-space: nowrap; }
.tab-btn:hover { color: #374151; background: #f8fafc; }
.tab-btn.active { color: #1e40af; border-bottom-color: #1e40af; background: none; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }
/* CARDS */
.card { background: white; border: 1px solid #e2e8f0; border-radius: 10px; padding: 20px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }
.card-title { font-size: 14px; font-weight: 600; color: #0f172a; margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.card-subtitle { font-size: 12px; color: #64748b; margin-bottom: 14px; line-height: 1.6; }
/* FORM */
.form-group { margin-bottom: 12px; }
.form-group label { display: block; font-size: 12px; font-weight: 500; color: #374151; margin-bottom: 4px; }
.form-group input, .form-group select, .form-group textarea { width: 100%; padding: 7px 10px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 13px; color: #1e293b; outline: none; background: white; }
.form-group input:focus, .form-group select:focus { border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59,130,246,0.1); }
/* BUTTONS */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 6px; font-size: 12px; font-weight: 500; cursor: pointer; border: none; transition: all 0.15s; white-space: nowrap; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: #1e40af; color: white; }
.btn-primary:hover:not(:disabled) { background: #1d4ed8; }
.btn-secondary { background: #f1f5f9; color: #374151; border: 1px solid #e2e8f0; }
.btn-secondary:hover:not(:disabled) { background: #e2e8f0; }
.btn-success { background: #059669; color: white; }
.btn-success:hover:not(:disabled) { background: #047857; }
.btn-danger { background: #dc2626; color: white; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-warning { background: #d97706; color: white; }
.btn-warning:hover:not(:disabled) { background: #b45309; }
.btn-sm { padding: 5px 11px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }
/* PILL TOGGLES */
.pill-group { display: flex; gap: 6px; }
.pill { flex: 1; text-align: center; padding: 7px 10px; border: 2px solid #e2e8f0; border-radius: 8px; cursor: pointer; font-size: 12px; font-weight: 500; color: #64748b; transition: all 0.15s; }
.pill.active { border-color: #1e40af; background: #eff6ff; color: #1e40af; }
/* DROP ZONES */
.drop-zone { border: 2px dashed #cbd5e1; border-radius: 8px; padding: 20px 14px; text-align: center; cursor: pointer; transition: all 0.15s; }
.drop-zone:hover, .drop-zone.over { border-color: #3b82f6; background: #eff6ff; }
.drop-zone input[type=file] { display: none; }
.drop-zone .dz-icon { font-size: 24px; margin-bottom: 5px; }
.drop-zone .dz-label { font-size: 13px; color: #475569; font-weight: 500; }
.drop-zone .dz-sub { font-size: 11px; color: #94a3b8; margin-top: 3px; }
.drop-zone.loaded { border-color: #10b981; background: #f0fdf4; }
.drop-zone.loaded .dz-label { color: #059669; }
/* BADGES */
.badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.badge-fp { background: #fee2e2; color: #dc2626; }
.badge-fn { background: #fef3c7; color: #b45309; }
.badge-tp { background: #d1fae5; color: #059669; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-purple { background: #ede9fe; color: #7c3aed; }
.badge-gray { background: #f1f5f9; color: #64748b; }
/* FILE TAGS */
.file-tag { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; font-family: monospace; }
.ft-refall { background: #d1fae5; color: #065f46; }
.ft-remove { background: #fee2e2; color: #991b1b; }
.ft-context { background: #fef3c7; color: #92400e; }
.ft-match { background: #ede9fe; color: #5b21b6; }
/* TABLE */
.tbl-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
thead th { background: #f8fafc; padding: 9px 12px; text-align: left; border-bottom: 2px solid #e2e8f0; font-weight: 600; color: #374151; white-space: nowrap; position: sticky; top: 0; z-index: 1; }
tbody td { padding: 8px 12px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
tbody tr:hover td { background: #fafafa; }
.tbl-scroll { max-height: 400px; overflow-y: auto; }
/* METRIC BOXES */
.metric-row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.metric-box { background: white; border: 1px solid #e2e8f0; border-radius: 8px; padding: 14px 18px; min-width: 90px; text-align: center; flex: 1; }
.metric-box.highlight { border-color: #1e40af; background: #eff6ff; }
.metric-num { font-size: 28px; font-weight: 700; color: #1e40af; line-height: 1; }
.metric-lbl { font-size: 11px; color: #64748b; margin-top: 3px; }
.metric-delta { font-size: 11px; margin-top: 2px; font-weight: 600; }
.delta-up { color: #059669; }
.delta-down { color: #dc2626; }
.delta-same { color: #94a3b8; }
/* PROGRESS */
.prog-wrap { margin-bottom: 14px; }
.prog-label { font-size: 12px; color: #1e40af; margin-bottom: 4px; }
.prog-bg { background: #e2e8f0; border-radius: 4px; height: 5px; }
.prog-fill { background: #1e40af; height: 5px; border-radius: 4px; transition: width 0.3s; }
/* RULE ITEMS */
.rule-item { border: 1px solid #e2e8f0; border-radius: 8px; padding: 14px; margin-bottom: 10px; }
.rule-item.accepted { border-color: #10b981; background: #f0fdf4; }
.rule-item.rejected { border-color: #e2e8f0; background: #fafafa; opacity: 0.6; }
.rule-item-hdr { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.rule-actions { margin-left: auto; display: flex; gap: 6px; }
.rule-pattern { font-family: 'Courier New', monospace; font-size: 12px; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 5px; padding: 7px 10px; width: 100%; color: #0f172a; }
.rule-pattern:focus { border-color: #3b82f6; outline: none; background: white; }
.rule-expl { font-size: 12px; color: #475569; margin: 8px 0 6px; line-height: 1.5; }
.rule-src { font-size: 11px; color: #94a3b8; border-top: 1px solid #f1f5f9; padding-top: 7px; margin-top: 6px; font-style: italic; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* INFO BOX */
.info-box { background: #eff6ff; border: 1px solid #bfdbfe; border-radius: 7px; padding: 12px 14px; font-size: 12px; color: #1e40af; line-height: 1.6; margin-bottom: 16px; }
.info-box code { background: #dbeafe; padding: 1px 5px; border-radius: 3px; font-size: 11px; font-family: monospace; }
.warn-box { background: #fffbeb; border: 1px solid #fde68a; border-radius: 7px; padding: 12px 14px; font-size: 12px; color: #92400e; line-height: 1.6; margin-bottom: 16px; }
/* SPINNER */
.spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid rgba(255,255,255,0.4); border-top-color: white; border-radius: 50%; animation: spin 0.6s linear infinite; }
.spinner-dark { border-color: rgba(30,64,175,0.2); border-top-color: #1e40af; }
@keyframes spin { to { transform: rotate(360deg); } }
/* EMPTY STATE */
.empty-state { text-align: center; padding: 40px 24px; color: #94a3b8; }
.empty-icon { font-size: 36px; margin-bottom: 10px; }
.empty-text { font-size: 13px; }
/* ITERATION HISTORY TABLE */
.iter-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.iter-table th { background: #f8fafc; padding: 8px 12px; text-align: left; border-bottom: 2px solid #e2e8f0; font-weight: 600; }
.iter-table td { padding: 8px 12px; border-bottom: 1px solid #f1f5f9; }
.iter-table tr.best-row td { background: #f0fdf4; font-weight: 600; }
/* EXPORT FILE CARD */
.export-fc { border: 1px solid #e2e8f0; border-radius: 8px; overflow: hidden; margin-bottom: 12px; }
.export-fc-hdr { background: #f8fafc; padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #e2e8f0; }
.export-fc-body { padding: 12px 14px; font-family: monospace; font-size: 11px; background: #fafafa; max-height: 130px; overflow-y: auto; color: #374151; white-space: pre-wrap; word-break: break-all; }
/* SESSION STATS */
.sess-row { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 5px; color: #475569; }
.sess-row strong { color: #1e293b; }
/* PIPELINE STEPS */
.pipe-step { display:flex; align-items:flex-start; gap:14px; padding:14px 16px; border:1px solid #e2e8f0; border-radius:8px; margin-bottom:8px; background:white; transition:all 0.2s; }
.pipe-step[data-status="running"] { border-color:#3b82f6; background:#eff6ff; }
.pipe-step[data-status="done"] { border-color:#10b981; background:#f0fdf4; }
.pipe-step[data-status="error"] { border-color:#dc2626; background:#fef2f2; }
.pipe-step[data-status="skipped"] { border-color:#e2e8f0; background:#fafafa; opacity:0.6; }
.pipe-step[data-status="paused"] { border-color:#d97706; background:#fffbeb; }
.ps-icon { font-size:22px; width:32px; text-align:center; flex-shrink:0; margin-top:2px; }
.ps-body { flex:1; }
.ps-label { font-size:13px; font-weight:600; color:#0f172a; margin-bottom:3px; }
.ps-detail { font-size:11px; color:#64748b; }
.ps-metric { font-size:12px; font-weight:700; color:#1e40af; min-width:80px; text-align:right; white-space:nowrap; }
/* HIGHLIGHT SPAN IN TEXT */
.hi-tp { background: #bbf7d0; border-radius: 2px; padding: 0 2px; }
.hi-fp { background: #fecaca; border-radius: 2px; padding: 0 2px; }
.hi-fn { background: #fef08a; border-radius: 2px; padding: 0 2px; }
</style>
</head>
<body>
<header>
<div>
<div class="logo">🔬 REFINE</div>
<div class="tagline">Model Evaluation · Error Analysis · Model Refinement · Model Re-evaluation</div>
</div>
<div class="header-right">
<span id="llm-badge">⚪ LLM not configured</span>
<span id="iter-badge" style="font-size:12px;padding:4px 10px;border-radius:20px;background:rgba(255,255,255,0.12);">Round 0</span>
</div>
</header>
<div class="app-body">
<!-- ══ SIDEBAR ══ -->
<aside class="sidebar">
<div>
<div class="sidebar-section-title">Task / Concept</div>
<div class="form-group">
<label>Concept name <span style="font-weight:400;color:#94a3b8;">(e.g. Falls, Delirium, Medication)</span></label>
<input type="text" id="concept-name" value="Falls" placeholder="Falls" oninput="updateConceptUI()">
</div>
<div class="form-group">
<label>Concept norm / tag <span style="font-weight:400;color:#94a3b8;">(XML tag & default norm)</span></label>
<input type="text" id="concept-norm" value="fall" placeholder="fall" oninput="updateConceptUI()">
</div>
<div class="form-group">
<label>Concept description <span style="font-weight:400;color:#94a3b8;">(for LLM context)</span></label>
<input type="text" id="concept-desc" value="a patient fall event" placeholder="a patient fall event">
</div>
</div>
<div>
<div class="sidebar-section-title">LLM Configuration</div>
<div class="form-group">
<div class="pill-group">
<div class="pill active" id="pill-azure" onclick="selectProvider('azure')">Azure OpenAI</div>
<div class="pill" id="pill-ollama" onclick="selectProvider('ollama')">Ollama</div>
</div>
</div>
<div id="azure-cfg">
<div class="form-group"><label>Endpoint URL</label><input type="text" id="az-endpoint" placeholder="https://…openai.azure.com"></div>
<div class="form-group"><label>Deployment Name</label><input type="text" id="az-deployment" placeholder="gpt-4o"></div>
<div class="form-group"><label>API Key</label><input type="password" id="az-key" placeholder="••••••••"></div>
</div>
<div id="ollama-cfg" style="display:none">
<div class="form-group"><label>Base URL</label><input type="text" id="ol-url" value="http://localhost:11434"></div>
<div class="form-group"><label>Model</label><input type="text" id="ol-model" value="llama3.1"></div>
</div>
<button class="btn btn-primary" style="width:100%;justify-content:center;" onclick="testLLM()">
<span id="test-inner">Test Connection</span>
</button>
</div>
<div>
<div class="sidebar-section-title">Prompting Options</div>
<div class="form-group">
<label>Taxonomy level</label>
<div class="pill-group">
<div class="pill active" id="pill-fine" onclick="selectTaxonomyLevel('fine')">Fine-grained</div>
<div class="pill" id="pill-consolidated" onclick="selectTaxonomyLevel('consolidated')">Consolidated</div>
</div>
</div>
<!-- Fine-grained taxonomy tree -->
<div id="tax-fine" style="margin-bottom:10px;">
<div style="font-size:10px;color:#94a3b8;margin-bottom:4px;display:flex;justify-content:space-between;">
<span>Select classes to include in prompt</span>
<span>
<span style="cursor:pointer;color:#1e40af;" onclick="setAllTax(true)">all</span> ·
<span style="cursor:pointer;color:#1e40af;" onclick="setAllTax(false)">none</span>
</span>
</div>
<div id="tax-fine-tree" style="max-height:260px;overflow-y:auto;border:1px solid #e2e8f0;border-radius:6px;padding:6px 8px;"></div>
</div>
<!-- Consolidated taxonomy -->
<div id="tax-consolidated" style="display:none;margin-bottom:10px;">
<div style="font-size:10px;color:#94a3b8;margin-bottom:4px;">Select dimensions to include</div>
<div id="tax-cons-tree" style="border:1px solid #e2e8f0;border-radius:6px;padding:6px 8px;"></div>
</div>
<div class="form-group">
<label>Temperature</label>
<input type="text" id="temperature" value="0" style="width:70px;">
</div>
</div>
<div>
<div class="sidebar-section-title">Evaluation Matching</div>
<div class="form-group">
<label>Span match tolerance (chars)</label>
<input type="number" id="span-tol" value="0" min="0" max="5" style="width:70px;" title="0 = exact match, 1-5 = fuzzy">
</div>
<div class="form-group">
<label>Gold: include in positives</label>
<select id="gold-filter">
<option value="strict">exclusion=no, confirmed, present, patient</option>
<option value="noexcl">exclusion=no (any certainty/status)</option>
<option value="all">All annotations</option>
</select>
</div>
</div>
<div>
<div class="sidebar-section-title">Local Server</div>
<div class="form-group">
<label>Server URL</label>
<input type="text" id="server-url" value="http://localhost:5050" placeholder="http://localhost:5050">
</div>
<div style="display:flex;gap:6px;align-items:center;margin-bottom:8px;">
<button class="btn btn-secondary btn-sm" style="flex:1;justify-content:center;" onclick="connectServer()">Connect</button>
<span id="server-badge" style="font-size:11px;padding:3px 8px;border-radius:10px;background:#f1f5f9;color:#94a3b8;white-space:nowrap;">⚪ offline</span>
</div>
<div id="server-info" style="display:none;font-size:11px;color:#475569;background:#f8fafc;border:1px solid #e2e8f0;border-radius:6px;padding:8px;"></div>
</div>
<div>
<div class="sidebar-section-title">Session</div>
<div class="sess-row">Round: <strong id="ss-round">0</strong></div>
<div class="sess-row">Errors loaded: <strong id="ss-errors">0</strong></div>
<div class="sess-row">Classified: <strong id="ss-classified">0</strong></div>
<div class="sess-row">Rules accepted: <strong id="ss-rules">0</strong></div>
<div class="sess-row">Best F1: <strong id="ss-bestf1">—</strong></div>
<div style="margin-top:10px;display:flex;gap:6px;">
<button class="btn btn-secondary btn-sm" style="flex:1;justify-content:center;" onclick="clearSession()">Clear All</button>
<button class="btn btn-warning btn-sm" style="flex:1;justify-content:center;" onclick="switchTab('eval',document.querySelector('[data-tab=eval]'))">+ New Round</button>
</div>
</div>
</aside>
<!-- ══ MAIN ══ -->
<main class="main">
<div class="tabs">
<button class="tab-btn active" data-tab="configure" onclick="switchTab('configure',this)">⚙️ Configure</button>
<button class="tab-btn" data-tab="pipeline" onclick="switchTab('pipeline',this)">🚀 Pipeline</button>
<button class="tab-btn" data-tab="eval" onclick="switchTab('eval',this)">📊 Evaluate</button>
<button class="tab-btn" data-tab="classify" onclick="switchTab('classify',this)">🔍 Classify Errors</button>
<button class="tab-btn" data-tab="rules" onclick="switchTab('rules',this)">🔧 Generate Rules</button>
<button class="tab-btn" data-tab="history" onclick="switchTab('history',this)">📈 Iteration History</button>
<button class="tab-btn" data-tab="export" onclick="switchTab('export',this)">📤 Export</button>
</div>
<!-- ════ TAB: CONFIGURE ════ -->
<div id="tab-configure" class="tab-pane active">
<div class="info-box">
<strong>REFINE workflow:</strong>
<br>① Set your <strong>concept name + norm</strong> in the sidebar (e.g. Delirium / delirium, Medication / medication)
<br>② Upload gold XML + system .ann → evaluate F1/P/R → errors auto-populate
<br>③ LLM classifies each FP/FN → generates MedTagger regex rules
<br>④ Accept rules → download updated rule files → re-run MedTagger jar
<br>⑤ Upload new .ann output → evaluate again → track improvement across rounds
</div>
<div class="card">
<div class="card-title">Annotation Guideline <span style="font-size:11px;font-weight:400;color:#94a3b8;">optional</span></div>
<div class="card-subtitle">YAML defining what each concept norm means. Improves LLM rule generation.</div>
<div class="drop-zone" id="dz-guide" onclick="document.getElementById('fi-guide').click()">
<input type="file" id="fi-guide" accept=".yaml,.yml" onchange="loadGuideline(event)">
<div class="dz-icon">📋</div>
<div class="dz-label">annotation_guideline.yaml</div>
<div class="dz-sub">Click to upload</div>
</div>
<pre id="guide-preview" style="display:none;font-size:11px;background:#f8fafc;border:1px solid #e2e8f0;border-radius:6px;padding:10px;margin-top:10px;max-height:110px;overflow-y:auto;"></pre>
</div>
<div class="card">
<div class="card-title">Existing MedTagger Rule Files <span style="font-size:11px;font-weight:400;color:#94a3b8;">optional but recommended</span></div>
<div class="card-subtitle">Drop your <code>models/<Task>/</code> folder below — the app auto-detects all rule files. Or load files individually.</div>
<!-- Folder drop zone -->
<div class="drop-zone" id="dz-models-folder" onclick="document.getElementById('fi-models-folder').click()" style="margin-bottom:14px;">
<input type="file" id="fi-models-folder" webkitdirectory multiple onchange="loadModelsFolder(event)">
<div class="dz-icon">📁</div>
<div class="dz-label" id="folder-lbl">Drop models/<Task>/ folder here</div>
<div class="dz-sub">Scans regexp/, context/, and rules/ subfolders automatically</div>
</div>
<!-- Fixed slots: context + matchrules -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-bottom:8px;">
<div class="rule-slot" id="slot-context" style="padding:8px 10px;border:1px dashed #d1d5db;border-radius:6px;font-size:11px;color:#94a3b8;">📙 contextRule.txt — not loaded</div>
<div class="rule-slot" id="slot-matchrules" style="padding:8px 10px;border:1px dashed #d1d5db;border-radius:6px;font-size:11px;color:#94a3b8;">📓 matchrules.txt — not loaded</div>
</div>
<!-- Dynamic regexp file slots (one per regexp file discovered) -->
<div id="regexp-slots" style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:8px;margin-bottom:10px;"></div>
<!-- Individual overrides (collapsed) -->
<details style="margin-top:6px;">
<summary style="font-size:11px;color:#64748b;cursor:pointer;user-select:none;">Load context / matchrules individually</summary>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-top:10px;">
<div>
<div style="font-size:11px;font-weight:600;color:#92400e;margin-bottom:4px;">📙 contextRule.txt</div>
<div class="drop-zone" id="dz-context" onclick="document.getElementById('fi-context').click()" style="padding:10px;">
<input type="file" id="fi-context" accept=".txt" onchange="loadRuleFile(event,'context')">
<div class="dz-sub" id="context-lbl">Click to upload</div>
</div>
</div>
<div>
<div style="font-size:11px;font-weight:600;color:#5b21b6;margin-bottom:4px;">📓 matchrules.txt</div>
<div class="drop-zone" id="dz-matchrules" onclick="document.getElementById('fi-matchrules').click()" style="padding:10px;">
<input type="file" id="fi-matchrules" accept=".txt" onchange="loadRuleFile(event,'matchrules')">
<div class="dz-sub" id="matchrules-lbl">Click to upload</div>
</div>
</div>
</div>
<p style="font-size:11px;color:#94a3b8;margin:8px 0 0;">Regexp files (.txt) are loaded via the folder picker above or auto-loaded when server connects.</p>
</details>
</div>
<!-- Evaluation Inputs -->
<div class="card">
<div class="card-title">Evaluation Inputs</div>
<div class="card-subtitle">Choose what to evaluate against. The Pipeline reads these automatically — no need to re-upload between rounds.</div>
<!-- Mode toggle -->
<div style="display:flex;align-items:center;gap:10px;margin-bottom:16px;flex-wrap:wrap;">
<span style="font-size:12px;font-weight:600;color:#374151;">Mode:</span>
<div class="pill-group" style="flex:0 0 auto;">
<div class="pill active" id="pill-eval-note" onclick="selectEvalMode('note')" style="font-size:12px;padding:6px 14px;">📄 Note-level</div>
<div class="pill" id="pill-eval-csv" onclick="selectEvalMode('csv')" style="font-size:12px;padding:6px 14px;">📋 Sentence CSV</div>
</div>
<span id="eval-mode-hint" style="font-size:11px;color:#94a3b8;">Gold XML + .ann span matching</span>
</div>
<!-- Note-level uploads -->
<div id="cfg-note-inputs">
<div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
<div>
<div style="font-size:11px;font-weight:600;color:#065f46;margin-bottom:5px;">🏅 Gold standard XML <span style="font-weight:400;color:#94a3b8;">(gold/)</span></div>
<div class="drop-zone" id="dz-gold" onclick="document.getElementById('fi-gold').click()" style="padding:12px;">
<input type="file" id="fi-gold" accept=".xml" multiple onchange="loadGoldXML(event)">
<div class="dz-icon" style="font-size:18px;">📑</div>
<div class="dz-label" id="gold-lbl">note1.txt.xml …</div>
<div class="dz-sub">Multiple files OK · via server when connected</div>
</div>
</div>
<div>
<div style="font-size:11px;font-weight:600;color:#1e40af;margin-bottom:5px;">🤖 System .ann output <span style="font-weight:400;color:#94a3b8;">(output/)</span></div>
<div class="drop-zone" id="dz-sysann" onclick="document.getElementById('fi-sysann').click()" style="padding:12px;">
<input type="file" id="fi-sysann" accept=".ann" multiple onchange="loadSysANN(event)">
<div class="dz-icon" style="font-size:18px;">📄</div>
<div class="dz-label" id="sysann-lbl">note1.txt.ann …</div>
<div class="dz-sub">Multiple files OK · auto-loaded by Pipeline</div>
</div>
</div>
</div>
</div>
<!-- CSV uploads -->
<div id="cfg-csv-inputs" style="display:none;">
<div class="drop-zone" id="dz-csv" onclick="document.getElementById('fi-csv').click()">
<input type="file" id="fi-csv" accept=".csv" onchange="loadCSVFile(event)">
<div class="dz-icon">📋</div>
<div class="dz-label" id="csv-lbl">Drop CSV file here</div>
<div class="dz-sub">uid · sentence · gold_standard · original_prediction</div>
</div>
<div id="csv-preview" style="display:none;margin-top:12px;">
<div style="display:flex;gap:8px;margin-bottom:8px;flex-wrap:wrap;" id="csv-summary-pills"></div>
<div class="tbl-scroll tbl-wrap" style="max-height:180px;">
<table><thead><tr><th>uid</th><th>Type</th><th>Sentence</th><th>Gold / Prediction</th></tr></thead>
<tbody id="csv-preview-tbody"></tbody>
</table>
</div>
<div style="margin-top:12px;">
<label style="font-size:11px;font-weight:600;color:#374151;">MedTagger script</label>
<select id="csv-script" style="display:block;width:100%;max-width:360px;margin-top:5px;font-size:12px;padding:6px 8px;border:1px solid #d1d5db;border-radius:6px;">
<option value="">— select script (connect server first) —</option>
</select>
</div>
</div>
</div>
</div>
</div><!-- /tab-configure -->
<!-- ════ TAB: EVALUATE ════ -->
<div id="tab-eval" class="tab-pane">
<!-- Status bar -->
<div id="eval-status-bar" style="display:flex;align-items:center;gap:12px;padding:12px 16px;background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;margin-bottom:16px;flex-wrap:wrap;">
<span style="font-size:12px;font-weight:600;color:#374151;">Mode:</span>
<span id="eval-mode-tag" class="badge badge-blue" style="font-size:12px;padding:4px 10px;">📄 Note-level</span>
<span id="eval-inputs-status" style="font-size:12px;color:#94a3b8;">No inputs loaded — configure in ⚙️ Configure tab</span>
<div style="margin-left:auto;display:flex;gap:8px;">
<button class="btn btn-secondary btn-sm" onclick="switchTab('configure',document.querySelector('[data-tab=configure]'))">⚙️ Configure</button>
<button class="btn btn-secondary btn-sm" id="eval-manual-btn" onclick="S.evalMode==='csv'?runCSVEvaluation():runEvaluation()" title="Evaluate now (manual / offline)">▶ Evaluate</button>
<button class="btn btn-primary btn-sm" onclick="switchTab('pipeline',document.querySelector('[data-tab=pipeline]'))">🚀 Pipeline</button>
</div>
</div>
<!-- ── NOTE-LEVEL results ── -->
<div id="eval-note-panel">
<div id="eval-results" style="display:none;">
<div class="metric-row" id="eval-metrics"></div>
<div class="card">
<div class="card-title">
Extraction Details
<div style="display:flex;gap:8px;">
<select id="filter-eval-type" style="font-size:12px;padding:5px 8px;border:1px solid #e2e8f0;border-radius:5px;" onchange="renderEvalTable()">
<option value="">All</option><option value="TP">TP</option><option value="FP">FP</option><option value="FN">FN</option>
</select>
<button class="btn btn-warning btn-sm" onclick="sendErrorsToClassify()">→ Send FP+FN to Classify</button>
</div>
</div>
<div class="tbl-scroll tbl-wrap"><table>
<thead><tr><th>File</th><th>Type</th><th>Span</th><th>Text</th><th>Certainty</th><th>Status</th><th>Sentence</th></tr></thead>
<tbody id="eval-tbody"></tbody>
</table></div>
</div>
<div class="card">
<div class="card-title">Per-File Breakdown</div>
<div class="tbl-wrap"><table>
<thead><tr><th>File</th><th>TP</th><th>FP</th><th>FN</th><th>Precision</th><th>Recall</th><th>F1</th></tr></thead>
<tbody id="perfile-tbody"></tbody>
</table></div>
</div>
</div>
<div id="eval-empty" class="empty-state">
<div class="empty-icon">📊</div>
<div class="empty-text">Run the Pipeline to see evaluation results here</div>
</div>
</div>
<!-- ── CSV results ── -->
<div id="eval-csv-panel" style="display:none;">
<!-- CSV progress (shown during pipeline run) -->
<div id="csv-progress" style="display:none;" class="card">
<div class="card-title">Running…</div>
<div class="prog-wrap">
<div class="prog-label" id="csv-prog-label">Preparing…</div>
<div class="prog-bg"><div class="prog-fill" id="csv-prog-bar" style="width:0%"></div></div>
</div>
<div id="csv-prog-steps" style="font-size:12px;color:#475569;line-height:2;"></div>
</div>
<div id="csv-results" style="display:none;">
<div class="metric-row" id="csv-metrics"></div>
<div class="card">
<div class="card-title">
Sentence-Level Results
<div style="display:flex;gap:8px;">
<select id="filter-csv-type" style="font-size:12px;padding:5px 8px;border:1px solid #e2e8f0;border-radius:5px;" onchange="renderCSVResultTable()">
<option value="">All sentences</option>
<option value="FP_fixed">✅ FP fixed</option>
<option value="FP_remain">❌ FP remaining</option>
<option value="FN_fixed">✅ FN fixed</option>
<option value="FN_remain">❌ FN remaining</option>
</select>
<button class="btn btn-warning btn-sm" onclick="sendCSVErrorsToClassify()">→ Send remaining errors to Classify</button>
</div>
</div>
<div class="tbl-scroll tbl-wrap">
<table><thead><tr>
<th>uid</th><th>Type</th><th>Result</th><th>Concept</th><th>Model Output</th><th>Sentence</th>
</tr></thead>
<tbody id="csv-result-tbody"></tbody>
</table>
</div>
</div>
<div id="csv-empty" class="empty-state">
<div class="empty-icon">📋</div>
<div class="empty-text">Run the Pipeline to see sentence-level results here</div>
</div>
</div>
</div>
</div><!-- /tab-eval -->
<!-- ════ TAB: CLASSIFY ════ -->
<div id="tab-classify" class="tab-pane">
<div class="card">
<div class="card-title">
LLM Error Classification
<div style="display:flex;gap:8px;">
<button class="btn btn-secondary btn-sm" onclick="clearClassifications()">Clear</button>
<button class="btn btn-primary btn-sm" id="classify-all-btn" onclick="classifyAll()">🤖 Classify All</button>
</div>
</div>
<div class="card-subtitle">
LLM classifies each FP/FN using the MedError taxonomy. FP errors → figure out what false trigger to suppress; FN errors → what pattern is missing.
</div>
<div id="classify-prog" style="display:none;" class="prog-wrap">
<div class="prog-label" id="cls-prog-label">Classifying…</div>
<div class="prog-bg"><div class="prog-fill" id="cls-prog-bar" style="width:0%"></div></div>
</div>
<div id="classify-empty" class="empty-state">
<div class="empty-icon">📂</div>
<div class="empty-text">Run Evaluate first — FP/FN errors will appear here automatically</div>
</div>
<div id="classify-tbl-wrap" style="display:none;" class="tbl-scroll tbl-wrap">
<table>
<thead><tr>
<th style="width:36px;">#</th>
<th style="width:52px;">Type</th>
<th style="width:80px;">File</th>
<th style="width:60px;">Span</th>
<th>Sentence</th>
<th style="width:140px;">Error Class</th>
<th style="width:260px;">Reasoning</th>
<th style="width:58px;">Action</th>
</tr></thead>
<tbody id="classify-tbody"></tbody>
</table>
</div>
</div>
</div>
<!-- ════ TAB: RULES ════ -->
<div id="tab-rules" class="tab-pane">
<div class="card">
<div class="card-title">
Rule Generation
<div style="display:flex;gap:8px;flex-wrap:wrap;">
<button class="btn btn-secondary btn-sm" onclick="acceptAll()">✓ Accept All</button>
<button class="btn btn-secondary btn-sm" onclick="rejectAll()">✗ Reject All</button>
<button class="btn btn-secondary btn-sm" onclick="clearRules()">Clear</button>
<button class="btn btn-primary btn-sm" id="gen-btn" onclick="generateAllRules()">🔧 Generate Rules</button>
</div>
</div>
<div class="card-subtitle">
LLM generates candidate MedTagger patterns per classified error. FN → match pattern file. FP from bad context → <code>contextRule.txt</code>. FP from wrong keyword → removal pattern file. Edit before accepting, then export and re-run MedTagger.
</div>
<div id="rules-prog" style="display:none;" class="prog-wrap">
<div class="prog-label" id="rules-prog-label">Generating…</div>
<div class="prog-bg"><div class="prog-fill" id="rules-prog-bar" style="width:0%"></div></div>
</div>
<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:14px;align-items:center;">
<select id="filter-file" style="font-size:12px;padding:5px 8px;border:1px solid #e2e8f0;border-radius:5px;" onchange="renderRules()">
<option value="">All files</option>
<option id="opt-match-file" value="">Match patterns</option>
<option id="opt-remove-file" value="">Removal patterns</option>
<option value="contextRule.txt">contextRule.txt</option>
</select>
<select id="filter-status" style="font-size:12px;padding:5px 8px;border:1px solid #e2e8f0;border-radius:5px;" onchange="renderRules()">
<option value="">All status</option>
<option value="pending">Pending</option>
<option value="accepted">Accepted</option>
<option value="rejected">Rejected</option>
</select>
<div id="rules-file-summary" style="display:flex;gap:6px;flex-wrap:wrap;"></div>
</div>
<div id="rules-empty" class="empty-state"><div class="empty-icon">🔧</div><div class="empty-text">Classify errors first, then generate rules</div></div>
<div id="rules-list"></div>
</div>
</div>
<!-- ════ TAB: ITERATION HISTORY ════ -->
<div id="tab-history" class="tab-pane">
<div class="card">
<div class="card-title">
F1 / Precision / Recall Across Rounds
<button class="btn btn-secondary btn-sm" onclick="clearHistory()">Clear History</button>
</div>
<div id="history-chart-wrap" style="position:relative;height:260px;">
<canvas id="history-chart"></canvas>
</div>
<div id="history-chart-empty" class="empty-state" style="padding:30px;">
<div class="empty-icon">📈</div>
<div class="empty-text">Run at least one evaluation to start tracking</div>
</div>
</div>
<div class="card">
<div class="card-title">Round-by-Round Metrics</div>
<div class="tbl-wrap">
<table class="iter-table">
<thead><tr>
<th>#</th><th>Label</th><th>TP</th><th>FP</th><th>FN</th>
<th>Precision</th><th>Recall</th><th>F1</th><th>ΔF1</th><th>Date</th>
</tr></thead>
<tbody id="history-tbody"></tbody>
</table>
</div>
<div id="history-empty" class="empty-state"><div class="empty-icon">📋</div><div class="empty-text">No evaluation rounds yet</div></div>
</div>
<div class="card">
<div class="card-title">Error Class Distribution (Latest Round)</div>
<canvas id="cls-chart" style="max-height:220px;"></canvas>
<div id="cls-chart-empty" class="empty-state" style="padding:20px;"><div class="empty-icon">📊</div><div class="empty-text">No classified errors yet</div></div>
</div>
</div>
<!-- ════ TAB: PIPELINE ════ -->
<div id="tab-pipeline" class="tab-pane">
<div class="card">
<div class="card-title">
Full Refinement Pipeline
<div style="display:flex;gap:8px;">
<button class="btn btn-secondary btn-sm" id="pipe-stop-btn" onclick="stopPipeline()" style="display:none;">⏹ Stop</button>
<button class="btn btn-primary" id="pipe-start-btn" onclick="startPipeline()">🚀 Start Full Process</button>
</div>
</div>
<div class="card-subtitle">
Runs all six steps automatically: evaluate → classify errors → generate rules → write rules to disk → re-run MedTagger → re-evaluate and compare.
Requires the local bridge server (<code>python server.py</code>).
</div>
<!-- Server status inline -->
<div id="pipe-server-notice" style="display:flex;align-items:center;gap:10px;padding:8px 12px;background:#fefce8;border:1px solid #fde047;border-radius:6px;margin-bottom:12px;font-size:12px;color:#854d0e;">
⚠️ Local server not connected — script list will be empty.
<button class="btn btn-secondary btn-sm" onclick="connectServer()" style="margin-left:auto;">🔌 Connect Now</button>
</div>
<!-- Pipeline config -->
<div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:12px;margin-bottom:16px;">
<div class="form-group" style="margin:0;">
<label>MedTagger script</label>
<select id="pipe-script" style="width:100%;font-size:12px;padding:6px 8px;border:1px solid #d1d5db;border-radius:6px;" onchange="document.getElementById('pipe-script-manual').value=''">
<option value="">— select —</option>
</select>
<input type="text" id="pipe-script-manual" placeholder="or type script name manually" style="width:100%;margin-top:4px;font-size:11px;padding:4px 7px;border:1px solid #e2e8f0;border-radius:5px;color:#374151;" oninput="syncManualScript(this.value)">
</div>
<div class="form-group" style="margin:0;">
<label>Round label</label>
<input type="text" id="round-label" placeholder="e.g. Baseline" style="width:100%;">
</div>
<div class="form-group" style="margin:0;">
<label>Max errors to classify</label>
<input type="number" id="pipe-max-errors" value="20" min="1" max="200" style="width:100%;">
</div>
<div class="form-group" style="margin:0;">
<label>Rule acceptance</label>
<select id="pipe-accept-mode" style="width:100%;font-size:12px;padding:6px 8px;border:1px solid #d1d5db;border-radius:6px;">
<option value="auto">Auto-accept all generated rules</option>
<option value="pause">Pause for manual review</option>
</select>
</div>
</div>
<!-- Step progress -->
<div id="pipe-steps">
<div class="pipe-step" id="ps-1" data-status="idle">
<div class="ps-icon" id="psi-1">①</div>
<div class="ps-body">
<div class="ps-label">Baseline Evaluation</div>
<div class="ps-detail" id="psd-1">Load gold XML + current .ann → compute F1/P/R</div>
</div>
<div class="ps-metric" id="psm-1"></div>
</div>
<div class="pipe-step" id="ps-2" data-status="idle">
<div class="ps-icon" id="psi-2">②</div>
<div class="ps-body">
<div class="ps-label">Error Classification</div>
<div class="ps-detail" id="psd-2">LLM classifies each FP/FN using MedError taxonomy</div>
</div>
<div class="ps-metric" id="psm-2"></div>
</div>
<div class="pipe-step" id="ps-3" data-status="idle">
<div class="ps-icon" id="psi-3">③</div>
<div class="ps-body">
<div class="ps-label">Rule Generation</div>
<div class="ps-detail" id="psd-3">LLM generates MedTagger regex/context patterns per error</div>
</div>
<div class="ps-metric" id="psm-3"></div>
</div>
<div class="pipe-step" id="ps-4" data-status="idle">
<div class="ps-icon" id="psi-4">④</div>
<div class="ps-body">
<div class="ps-label">Write Rules to Disk</div>
<div class="ps-detail" id="psd-4">Append accepted patterns to rule files via local server</div>
</div>
<div class="ps-metric" id="psm-4"></div>
</div>
<div class="pipe-step" id="ps-5" data-status="idle">
<div class="ps-icon" id="psi-5">⑤</div>
<div class="ps-body">
<div class="ps-label">Re-run MedTagger</div>
<div class="ps-detail" id="psd-5">Execute shell script via local server</div>
</div>
<div class="ps-metric" id="psm-5"></div>
</div>
<div class="pipe-step" id="ps-6" data-status="idle">
<div class="ps-icon" id="psi-6">⑥</div>
<div class="ps-body">
<div class="ps-label">Re-evaluate + Compare</div>
<div class="ps-detail" id="psd-6">Load new .ann → compute F1/P/R → track in history</div>
</div>
<div class="ps-metric" id="psm-6"></div>
</div>
</div>
</div>
<!-- Before / After comparison -->
<div id="pipe-compare" style="display:none;">
<div class="card">
<div class="card-title">Before vs. After</div>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
<div>
<div style="font-size:12px;font-weight:600;color:#64748b;margin-bottom:10px;">Before refinement</div>
<div class="metric-row" id="cmp-before" style="flex-direction:column;gap:8px;"></div>
</div>
<div>
<div style="font-size:12px;font-weight:600;color:#059669;margin-bottom:10px;">After refinement</div>
<div class="metric-row" id="cmp-after" style="flex-direction:column;gap:8px;"></div>
</div>
</div>
</div>
<div class="card">
<div class="card-title">Improvement Breakdown</div>
<div class="tbl-wrap">
<table>
<thead><tr><th>Metric</th><th>Before</th><th>After</th><th>Change</th><th>Verdict</th></tr></thead>
<tbody id="cmp-tbody"></tbody>
</table>
</div>
<div style="margin-top:14px;display:flex;gap:8px;">
<button class="btn btn-primary btn-sm" onclick="startPipeline()">🔁 Run Another Round</button>
<button class="btn btn-secondary btn-sm" onclick="switchTab('history', document.querySelector('[data-tab=history]'))">📈 View History</button>
</div>
</div>
</div>
<!-- Review rules pause panel -->
<div id="pipe-review-panel" style="display:none;">
<div class="card" style="border-color:#d97706;">
<div class="card-title" style="color:#b45309;">
⏸ Review Generated Rules
<button class="btn btn-success btn-sm" onclick="resumePipelineAfterReview()">✓ Accept & Continue</button>
</div>
<div class="card-subtitle">Review the rules below before they are written to disk. Uncheck any you want to skip.</div>
<div id="pipe-review-list"></div>
</div>
</div>
</div>
<!-- ════ TAB: EXPORT ════ -->
<div id="tab-export" class="tab-pane">
<div class="warn-box" id="export-how-to">
<strong>How to apply generated rules:</strong><br>
1. Download the updated rule files below<br>
2. Copy them to <code id="export-model-path">models/<Task>/regexp/</code> and <code id="export-context-path">models/<Task>/context/</code> in your MedTagger folder<br>
3. Run: <code style="font-family:monospace;">java -jar MedTagger-fit-context.jar <input_dir> <output_dir> <rules_dir></code><br>
4. Go to <strong>Evaluate</strong> tab → upload the new <code>output/*.ann</code> → run evaluation → see F1 improvement
</div>
<div class="metric-row" id="export-stats"></div>
<div class="card">
<div class="card-title">
Generated Rule Files
<div style="display:flex;gap:8px;">
<button class="btn btn-secondary btn-sm" onclick="exportCSV()">⬇ Error Analysis CSV</button>
<button class="btn btn-primary btn-sm" onclick="exportAll()">⬇ Download All Rule Files</button>
</div>
</div>
<div id="export-files-list"></div>
<div id="export-empty" class="empty-state"><div class="empty-icon">📭</div><div class="empty-text">No accepted rules yet</div></div>
</div>
</div>
</main>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.4.1/papaparse.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.0/chart.umd.min.js"></script>
<script>
// ══════════════════════════════════════════════════════
// STATE
// ══════════════════════════════════════════════════════
const S = {
provider: 'azure',
guideline: null,
ruleFiles: { context: null, matchrules: null, regexp: {} },
// regexp: { [shortname]: { filename, norm, content, isRemove } }
// shortname = part after "resources_regexp_re" in filename, e.g. "fall", "fallRemove", "delirium"
// Taxonomy
taxonomyLevel: 'fine', // 'fine' | 'consolidated'
serverOnline: false,
// CSV sentence-level eval
evalMode: 'note', // 'note' | 'csv'
csvRows: [], // [{uid, sentence, gold_standard, original_prediction}]
csvResults: [], // [{uid, type, sentence, norm, modelExtracted, fixed, annLines}]
// Task identity (generic)
get conceptName() { return document.getElementById('concept-name').value.trim() || 'Concept'; },
get conceptNorm() { return document.getElementById('concept-norm').value.trim() || 'concept'; },
get conceptDesc() { return document.getElementById('concept-desc').value.trim() || 'a clinical concept'; },
// Evaluation data
goldDocs: {}, // filename → { text, annotations: [{start,end,text,certainty,status,experiencer,exclusion}] }
sysANNDocs: {}, // filename → [{start,end,text,sentence,norm,certainty,status,experiencer}]
// Current evaluation result
evalItems: [], // [{filename,type:'TP'|'FP'|'FN',start,end,text,certainty,status,sentence,goldStart,goldEnd}]
// Error queue for classify
errors: [], // {uid,filename,start,end,sentence,text,gold_standard,system_prediction,FP_FN,norm,error_class,error_dimension,reasoning,rules:[]}
// Generated rules
genRules: [], // {id,error_uid,norm,FP_FN,error_class,file,pattern,editable,explanation,error_sentence,status}
// Iteration history
history: [], // {round,label,TP,FP,FN,P,R,F1,timestamp}
// Charts
historyChart: null,
clsChart: null
};
let uid_counter = 1;
// ══════════════════════════════════════════════════════
// UI
// ══════════════════════════════════════════════════════
function switchTab(name, btn) {
document.querySelectorAll('.tab-pane').forEach(p => p.classList.remove('active'));
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
document.getElementById('tab-' + name).classList.add('active');
if (btn) btn.classList.add('active');
if (name === 'classify') renderClassifyTable();
if (name === 'rules') renderRules();
if (name === 'history') renderHistory();
if (name === 'export') renderExport();
}
function selectProvider(p) {
S.provider = p;
['azure','ollama'].forEach(x => document.getElementById('pill-'+x).classList.toggle('active', x===p));
document.getElementById('azure-cfg').style.display = p==='azure' ? '' : 'none';
document.getElementById('ollama-cfg').style.display = p==='ollama' ? '' : 'none';
}
function updateConceptUI() {
const name = S.conceptName, norm = S.conceptNorm;
// Header
const ht = document.getElementById('header-task');
if (ht) ht.textContent = name;
// (Rule file name hints removed — regexp files are now dynamic)
// Export path hints
const mp = document.getElementById('export-model-path');
const cp = document.getElementById('export-context-path');
if (mp) mp.textContent = `models/${name}/regexp/`;
if (cp) cp.textContent = `models/${name}/context/`;
// Rule filter dropdown options
const matchFile = `resources_regexp_ref${norm}.txt`;
const removeFile = `resources_regexp_ref${norm}Remove.txt`;
const om = document.getElementById('opt-match-file');
const or_ = document.getElementById('opt-remove-file');
if (om) { om.value = matchFile; om.textContent = matchFile; }
if (or_) { or_.value = removeFile; or_.textContent = removeFile; }
}
function updateSidebar() {
document.getElementById('ss-round').textContent = S.history.length;
document.getElementById('ss-errors').textContent = S.errors.length;
document.getElementById('ss-classified').textContent = S.errors.filter(e=>e.error_class).length;
document.getElementById('ss-rules').textContent = S.genRules.filter(r=>r.status==='accepted').length;
const bestF1 = S.history.length ? Math.max(...S.history.map(h=>h.F1)) : null;
document.getElementById('ss-bestf1').textContent = bestF1 !== null ? pct(bestF1) : '—';
document.getElementById('iter-badge').textContent = 'Round ' + S.history.length;
}
function clearSession() {
if (!confirm('Clear all data, classifications, rules, and history?')) return;
S.errors=[]; S.genRules=[]; S.history=[]; S.evalItems=[];
S.goldDocs={}; S.sysANNDocs={};
document.getElementById('eval-results').style.display='none';
document.getElementById('eval-empty').style.display='';
renderClassifyTable(); renderRules(); renderHistory(); updateSidebar();
}
// ══════════════════════════════════════════════════════
// LLM
// ══════════════════════════════════════════════════════
async function callLLM(messages) {
const temp = parseFloat(document.getElementById('temperature').value) || 0;
if (S.provider === 'azure') {
const ep = v('az-endpoint').replace(/\/$/, '');
const dep = v('az-deployment'), key = v('az-key');
if (!ep||!dep||!key) throw new Error('Azure config incomplete.');
const r = await fetch(`${ep}/openai/deployments/${dep}/chat/completions?api-version=2024-02-01`, {
method:'POST', headers:{'Content-Type':'application/json','api-key':key},
body: JSON.stringify({ messages, temperature:temp, max_tokens:1000 })
});
if (!r.ok) throw new Error(`Azure ${r.status}: ${(await r.text()).substring(0,200)}`);
return (await r.json()).choices[0].message.content;
} else {
const url = v('ol-url').replace(/\/$/, ''), model = v('ol-model');
if (!url||!model) throw new Error('Ollama config incomplete.');
const r = await fetch(`${url}/v1/chat/completions`, {
method:'POST', headers:{'Content-Type':'application/json'},
body: JSON.stringify({ model, messages, temperature:temp, stream:false })
});
if (!r.ok) throw new Error(`Ollama ${r.status}: ${await r.text()}`);
return (await r.json()).choices[0].message.content;
}
}
function exJSON(text) {
const m = text.match(/```json\s*([\s\S]*?)```/) || text.match(/```\s*([\s\S]*?)```/);
return JSON.parse((m ? m[1] : text).trim());
}
async function testLLM() {
const el = document.getElementById('test-inner');
el.innerHTML = '<span class="spinner"></span> Testing…';
try {
await callLLM([{role:'user',content:'Reply: {"ok":true}'}]);