File size: 77,067 Bytes
cbbd17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4312be94",
   "metadata": {},
   "source": [
    "# Part A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "499c83de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data already exists in 'patents_data_raw'. Skipping download.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from huggingface_hub import snapshot_download\n",
    "\n",
    "# Check if the folder already exists and is not empty\n",
    "folder_name = \"patents_data_raw\"\n",
    "\n",
    "# Check if folder exists and if it has any files inside\n",
    "if os.path.exists(folder_name) and any(os.scandir(folder_name)):\n",
    "    print(f\"Data already exists in '{folder_name}'. Skipping download.\")\n",
    "    local_folder = os.path.abspath(folder_name)\n",
    "else:\n",
    "    #Download only if missing\n",
    "    print(f\"Downloading dataset files to '{folder_name}'\")\n",
    "    try:\n",
    "        local_folder = snapshot_download(\n",
    "            repo_id=\"AI-Growth-Lab/patents_claims_1.5m_traim_test\", \n",
    "            repo_type=\"dataset\",\n",
    "            local_dir=folder_name,\n",
    "            ignore_patterns=[\"*.git*\"]\n",
    "        )\n",
    "        print(f\"Success! Files downloaded to: {local_folder}\")\n",
    "    except Exception as e:\n",
    "        print(f\"Download failed: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2a1e5f1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found existing processed file: patents_50k_green.parquet\n",
      "   Skipping filtering and merging to save time.\n"
     ]
    }
   ],
   "source": [
    "from datasets import load_dataset, concatenate_datasets, disable_progress_bar\n",
    "import pandas as pd\n",
    "import datasets\n",
    "\n",
    "# Silience Hugging Face logs for cleaner output\n",
    "disable_progress_bar()\n",
    "datasets.utils.logging.set_verbosity_error()\n",
    "\n",
    "output_filename = \"patents_50k_green.parquet\"\n",
    "\n",
    "# Check if this work is already done\n",
    "if os.path.exists(output_filename):\n",
    "    print(f\"Found existing processed file: {output_filename}\")\n",
    "    print(f\"   Skipping filtering and merging to save time.\")\n",
    "else:\n",
    "    print(\"1. Loading dataset from local cache...\")\n",
    "    # Point to the local folder\n",
    "    dataset_full = load_dataset(\"./patents_data_raw\", split=\"train\")\n",
    "\n",
    "    print(f\"   Dataset loaded. Total rows: {len(dataset_full):,}\")\n",
    "\n",
    "    # Identify Green Columns\n",
    "    all_cols = dataset_full.column_names\n",
    "    y02_cols = [c for c in all_cols if c.startswith(\"Y02\")]\n",
    "    print(f\"   Found {len(y02_cols)} Green Patent (Y02) indicator columns.\")\n",
    "\n",
    "    # Filtering Logic\n",
    "    print(\"2. Filtering for 25,000 Green patents...\")\n",
    "    dataset_green = dataset_full.filter(\n",
    "        lambda x: any(x[col] == 1 for col in y02_cols),\n",
    "        num_proc=1\n",
    "    ).shuffle(seed=42).select(range(25000))\n",
    "\n",
    "    print(\"3. Filtering for 25,000 Non-Green patents...\")\n",
    "    dataset_not_green = dataset_full.filter(\n",
    "        lambda x: all(x[col] == 0 for col in y02_cols),\n",
    "        num_proc=1\n",
    "    ).shuffle(seed=42).select(range(25000))\n",
    "\n",
    "    # 4. Add \"is_green_silver\" Labels\n",
    "    print(\"4. Adding silver labels (0/1)...\")\n",
    "    dataset_green = dataset_green.map(lambda x: {\"is_green_silver\": 1})\n",
    "    dataset_not_green = dataset_not_green.map(lambda x: {\"is_green_silver\": 0})\n",
    "\n",
    "    # 5. Combine and Save\n",
    "    print(\"5. Merging and saving final Parquet...\")\n",
    "    final_dataset = concatenate_datasets([dataset_green, dataset_not_green]).shuffle(seed=42)\n",
    "    final_dataset.to_parquet(output_filename)\n",
    "\n",
    "    print(f\"Success! File saved: {output_filename}\")\n",
    "    print(f\"Total Balanced Rows: {len(final_dataset):,}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "784cf7cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading patents_50k_green.parquet...\n",
      " Data Setup Complete\n",
      "   - train_silver:   2000 rows\n",
      "   - eval_silver:    5000 rows\n",
      "   - pool_unlabeled: 43000 rows\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 1. Loading the balanced 50k file\n",
    "print(\"Loading patents_50k_green.parquet...\")\n",
    "df = pd.read_parquet(\"patents_50k_green.parquet\")\n",
    "\n",
    "# Creating the Splits\n",
    "# - train_silver: Small initial labeled set to train the baseline (e.g., 2,000 - 5,000 rows)\n",
    "# - eval_silver: Validation set to test performance (e.g., 5,000 rows)\n",
    "# - pool_unlabeled: The rest, which you will \"mine\" for high-risk examples.\n",
    "\n",
    "# Reserve 5,000 for evaluation\n",
    "df_eval = df.sample(n=5000, random_state=42)\n",
    "df_remaining = df.drop(df_eval.index)\n",
    "\n",
    "# Reserve 2,000 for the initial \"train_silver\"\n",
    "df_train_silver = df_remaining.sample(n=2000, random_state=42)\n",
    "df_pool_unlabeled = df_remaining.drop(df_train_silver.index)\n",
    "\n",
    "print(\" Data Setup Complete\")\n",
    "print(f\"   - train_silver:   {len(df_train_silver)} rows\")\n",
    "print(f\"   - eval_silver:    {len(df_eval)} rows\")\n",
    "print(f\"   - pool_unlabeled: {len(df_pool_unlabeled)} rows\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d56051b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting Part A: Baseline Model Training...\n",
      "   - Loading 50k dataset...\n",
      "   - Splits created: Train=2000, Eval=5000, Pool=43000\n",
      "   - Loading PatentSBERTa model...\n",
      "   - Generating Training embeddings...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2c18d825d370452ca1eadd3788063e0c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "      Encoding:   0%|          | 0/63 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   - Generating Evaluation embeddings...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fd3be50181a348c8b8e8688ae3568157",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "      Encoding:   0%|          | 0/157 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   - Training Logistic Regression...\n",
      "\n",
      "=============================================\n",
      "PART A RESULTS: BASELINE MODEL\n",
      "=============================================\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "   Not Green       0.74      0.76      0.75      2493\n",
      "       Green       0.75      0.74      0.75      2507\n",
      "\n",
      "    accuracy                           0.75      5000\n",
      "   macro avg       0.75      0.75      0.75      5000\n",
      "weighted avg       0.75      0.75      0.75      5000\n",
      "\n",
      "---------------------------------------------\n",
      "Part A Baseline F1-Score: 0.7488\n",
      "=============================================\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModel, logging\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import classification_report\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "# Silence logs\n",
    "logging.set_verbosity_error()\n",
    "\n",
    "print(\"Starting Part A: Baseline Model Training...\")\n",
    "\n",
    "# Check for the source file\n",
    "parquet_file = \"patents_50k_green.parquet\"\n",
    "if not os.path.exists(parquet_file):\n",
    "    print(f\"Error: {parquet_file} not found. Please run the Filtering script first.\")\n",
    "else:\n",
    "    # Load Data & Create Splits\n",
    "    print(\"   - Loading 50k dataset...\")\n",
    "    df = pd.read_parquet(parquet_file)\n",
    "\n",
    "    # Creating the Splits (train_silver, eval_silver, pool_unlabeled)\n",
    "    df_eval = df.sample(n=5000, random_state=42)\n",
    "    df_remaining = df.drop(df_eval.index)\n",
    "    df_train = df_remaining.sample(n=2000, random_state=42)\n",
    "    df_pool = df_remaining.drop(df_train.index)\n",
    "\n",
    "    print(f\"   - Splits created: Train={len(df_train)}, Eval={len(df_eval)}, Pool={len(df_pool)}\")\n",
    "\n",
    "    # Load PatentSBERTa\n",
    "    print(\"   - Loading PatentSBERTa model...\")\n",
    "    model_name = \"AI-Growth-Lab/PatentSBERTa\"\n",
    "    tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "    model = AutoModel.from_pretrained(model_name)\n",
    "\n",
    "    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "    model.to(device); model.eval()\n",
    "\n",
    "    # Helper function with clean progress tracking\n",
    "    def get_embeddings(text_list, batch_size=32):\n",
    "        all_embeddings = []\n",
    "        # We keep the tqdm bar small and clean\n",
    "        for i in tqdm(range(0, len(text_list), batch_size), desc=\"      Encoding\", leave=False):\n",
    "            batch_texts = text_list[i:i+batch_size]\n",
    "            inputs = tokenizer(batch_texts, padding=True, truncation=True, max_length=128, return_tensors=\"pt\").to(device)\n",
    "            with torch.no_grad():\n",
    "                outputs = model(**inputs)\n",
    "                embeddings = outputs.last_hidden_state[:, 0, :].cpu().numpy()\n",
    "                all_embeddings.append(embeddings)\n",
    "        return np.vstack(all_embeddings)\n",
    "\n",
    "    # Generate Embeddings\n",
    "    print(\"   - Generating Training embeddings...\")\n",
    "    X_train = get_embeddings(df_train['text'].tolist())\n",
    "    y_train = df_train['is_green_silver'].values\n",
    "\n",
    "    print(\"   - Generating Evaluation embeddings...\")\n",
    "    X_eval = get_embeddings(df_eval['text'].tolist())\n",
    "    y_eval = df_eval['is_green_silver'].values\n",
    "\n",
    "    # Train Baseline Classifier\n",
    "    print(\"   - Training Logistic Regression...\")\n",
    "    clf = LogisticRegression(max_iter=1000, random_state=42)\n",
    "    clf.fit(X_train, y_train)\n",
    "\n",
    "    # FINAL REPORT OUTPUT\n",
    "    print(\"\\n\" + \"=\"*45)\n",
    "    print(\"PART A RESULTS: BASELINE MODEL\")\n",
    "    print(\"=\"*45)\n",
    "    y_pred = clf.predict(X_eval)\n",
    "    report = classification_report(y_eval, y_pred, target_names=['Not Green', 'Green'])\n",
    "    print(report)\n",
    "    \n",
    "    # Store Macro F1 for par D\n",
    "    report_dict = classification_report(y_eval, y_pred, output_dict=True)\n",
    "    macro_f1 = report_dict['macro avg']['f1-score']\n",
    "    \n",
    "    print(\"-\" * 45)\n",
    "    print(f\"Part A Baseline F1-Score: {macro_f1:.4f}\")\n",
    "    print(\"=\"*45)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99b2f0f6",
   "metadata": {},
   "source": [
    "# Part B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a46f788",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Starting Part B: Safe Reproduction of Outputs ---\n",
      "Generating baseline training embeddings...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2fc0f419d2aa404ab6e86ca8e20ce7d0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Encoding:   0%|          | 0/63 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating embeddings for 43000 unlabeled examples...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0d80d393db8542b8af12f15c1d92fddf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Encoding:   0%|          | 0/1344 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating uncertainty scores...\n",
      "\n",
      "========================================\n",
      "Part B Complete! Outputs successfully shown.\n",
      "File saved to: hitl_green_100_REPRODUCED.csv\n",
      "   - Min Uncertainty: 0.9959\n",
      "   - Max Uncertainty: 1.0000\n",
      "========================================\n"
     ]
    }
   ],
   "source": [
    "print(\"--- Starting Part B: Safe Reproduction of Outputs ---\")\n",
    "\n",
    "# Re-initialize Data & Model\n",
    "df = pd.read_parquet(\"patents_50k_green.parquet\")\n",
    "df_eval = df.sample(n=5000, random_state=42)\n",
    "df_remaining = df.drop(df_eval.index)\n",
    "df_train = df_remaining.sample(n=2000, random_state=42)\n",
    "df_pool = df_remaining.drop(df_train.index) #unlabeled pool\n",
    "\n",
    "# Re-initialize PatentSBERTa\n",
    "model_name = \"AI-Growth-Lab/PatentSBERTa\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModel.from_pretrained(model_name)\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "model.to(device); model.eval()\n",
    "\n",
    "def get_embeddings(text_list, batch_size=32):\n",
    "    all_embeddings = []\n",
    "    for i in tqdm(range(0, len(text_list), batch_size), desc=\"Encoding\"):\n",
    "        batch_texts = text_list[i:i+batch_size]\n",
    "        inputs = tokenizer(batch_texts, padding=True, truncation=True, max_length=128, return_tensors=\"pt\").to(device)\n",
    "        with torch.no_grad():\n",
    "            outputs = model(**inputs)\n",
    "            all_embeddings.append(outputs.last_hidden_state[:, 0, :].cpu().numpy())\n",
    "    return np.vstack(all_embeddings)\n",
    "\n",
    "# Re-train the Baseline Classifier\n",
    "print(\"Generating baseline training embeddings...\")\n",
    "X_train = get_embeddings(df_train['text'].tolist())\n",
    "clf = LogisticRegression(max_iter=1000, random_state=42)\n",
    "clf.fit(X_train, df_train['is_green_silver'].values)\n",
    "\n",
    "# Generate Embeddings for the Unlabeled Pool\n",
    "print(f\"Generating embeddings for {len(df_pool)} unlabeled examples...\")\n",
    "X_pool = get_embeddings(df_pool['text'].tolist())\n",
    "\n",
    "# Predict Probabilities and Uncertainty\n",
    "print(\"Calculating uncertainty scores...\")\n",
    "probs = clf.predict_proba(X_pool)[:, 1]\n",
    "uncertainty_scores = 1 - 2 * np.abs(probs - 0.5)\n",
    "\n",
    "df_pool['p_green'] = probs\n",
    "df_pool['u'] = uncertainty_scores\n",
    "\n",
    "# Select Top 100\n",
    "df_high_risk = df_pool.sort_values(by='u', ascending=False).head(100)\n",
    "\n",
    "# Format for Export (Changed filename to be safe)\n",
    "if 'id' in df_high_risk.columns:\n",
    "    df_high_risk = df_high_risk.rename(columns={'id': 'doc_id'})\n",
    "else:\n",
    "    df_high_risk['doc_id'] = df_high_risk.index\n",
    "\n",
    "for col in ['llm_green_suggested', 'llm_confidence', 'llm_rationale', 'is_green_human', 'notes']:\n",
    "    df_high_risk[col] = \"\"\n",
    "\n",
    "final_columns = ['doc_id', 'text', 'p_green', 'u', 'llm_green_suggested', 'llm_confidence', 'llm_rationale', 'is_green_human', 'notes']\n",
    "\n",
    "# Due to keral restart, a second file which is not used in the next steps is created to avoid confusion. The file is named \"hitl_green_100_REPRODUCED.csv\" to indicate that it is a reproduction of the original \"hitl_green_100.csv\" file, but with a different name to prevent any accidental overwriting or confusion with the original file that may have been generated before the kernel restart.\n",
    "safe_filename = \"hitl_green_100_REPRODUCED.csv\"\n",
    "df_high_risk[final_columns].to_csv(safe_filename, index=False)\n",
    "\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "print(f\"Part B Complete! Outputs successfully shown.\")\n",
    "print(f\"File saved to: {safe_filename}\")\n",
    "print(f\"   - Min Uncertainty: {df_high_risk['u'].min():.4f}\")\n",
    "print(f\"   - Max Uncertainty: {df_high_risk['u'].max():.4f}\")\n",
    "print(\"=\"*40)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6317d3a4",
   "metadata": {},
   "source": [
    "# Part C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a7f1e12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- Part C: HITL Labeling ---\n",
      "Remaining rows: 68\n",
      "-----------------------------\n",
      "\n",
      "[Row 33] (Uncertainty: 0.9984)\n",
      "CLAIM: 1. A method of increasing light extraction from a light-emitting diode (LED) device comprising; forming a first n-doped layer on a carrier substrate; forming a Si forming a second n-doped layer on the Si forming an active layer configured to emit light on the second n-doped layer; forming a p-doped ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns LED light‑emission enhancement, not climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 34] (Uncertainty: 0.9984)\n",
      "CLAIM: 1. A method comprising: identifying valuation data comprising a plurality of estimated asset values corresponding to one or more of location information and property type; identifying a group of two or more characteristics, wherein for each respective real estate investment trust of a plurality of r...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns financial weighting of real estate trusts, not climate mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 35] (Uncertainty: 0.9983)\n",
      "CLAIM: 1. A construction machine, comprising: a lower travel body; an upper slewing body mounted on the lower travel body and having an engine compartment; an engine compartment cover which covers the engine compartment of the upper slewing body; an air filter which collects dust included in outside air ta...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes an air filter system for a construction machine, which does not relate to green or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 36] (Uncertainty: 0.9983)\n",
      "CLAIM: 1. A semiconductor device including a plurality of operation circuits executing operation in synchronization with a clock signal comprising: a control unit for outputting first operation control information and second operation control information for controlling operation executed by the plurality ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a generic semiconductor device architecture without any reference to environmental impact or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 37] (Uncertainty: 0.9983)\n",
      "CLAIM: 1. A method of forming a layer over a substrate, the method comprising: receiving data identifying a desired thickness of the layer; using a processor to generate instructions for a printing mechanism to deposit droplets of ink onto the substrate according to the data, the ink carrying material to f...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a printing method for depositing ink layers, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 38] (Uncertainty: 0.9982)\n",
      "CLAIM: 1. An insulated-gate bipolar transistor (IGBT) in a semiconductor substrate, said IGBT comprising: a collector at a bottom surface of said semiconductor substrate, a drift region having a first conductivity type situated over said collector, and a base layer having a second conductivity type opposit...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a semiconductor device structure, not a technology for greenhouse gas mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 39] (Uncertainty: 0.9981)\n",
      "CLAIM: 1. A method of joining two or more articles via slender nanomaterials embedded in a joining medium and interlinked together, the method involving: (i) dispersion of nanomaterials comprising at least one of carbon nanotubes and nanofibers within a solvent, with the weight percent of said nanomaterial...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a nanomaterial-based joining method, which is unrelated to greenhouse‑gas reduction or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 40] (Uncertainty: 0.9981)\n",
      "CLAIM: 1. A compound having a structure represented by a chemical formula described below:...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"Low\",\"rationale\":\"The claim merely describes a chemical compound without indicating any environmental or climate‑related application.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 41] (Uncertainty: 0.9981)\n",
      "CLAIM: 1. A computer-implemented process comprising: executing, by a computer processor, at least two read threads to read a block of data from a database, each of the read threads having a first wait stat and a second wait stat, the read threads configured to compress data using a dynamic compression rati...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a data compression and backup process, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 42] (Uncertainty: 0.9980)\n",
      "CLAIM: 1. A rotor for a Wankel engine comprising: two axially spaced apart end faces having a generally triangular profile with outwardly arched sides and three circumferentially spaced apex portions; a peripheral face extending between the end faces and defining three flanks, each flank extending between ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a mechanical component for a Wankel engine and does not relate to greenhouse gas mitigation or climate change technologies.\n",
      "Saved.\n",
      "\n",
      "[Row 43] (Uncertainty: 0.9980)\n",
      "CLAIM: 1. A housing apparatus, comprising: a housing casing which surrounds a first cavity and which has multiple side surfaces; a volute housing arranged in an interior of the housing casing, said volute housing having a central through opening for accommodating a compressor wheel of a rotor and for suppl...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes a housing apparatus for a compressor wheel, which is unrelated to green or climate change mitigation technologies.\n",
      "Saved.\n",
      "\n",
      "[Row 44] (Uncertainty: 0.9979)\n",
      "CLAIM: 1. A biodegradable container for a semi-solid composition, comprising: a tube portion comprising a first paper that defines first, second, and third plies forming an open end and a closed end, and a lumen containing the semi-solid composition, wherein the tube portion further comprises a continuous ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a fully biodegradable container, indicating an environmental benefit aligned with Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 45] (Uncertainty: 0.9979)\n",
      "CLAIM: 1. An isolated green sulfur bacterium Chlorobaculum limnaeum strain RK-j-1 deposited at National Institute of Technology and Evaluation Patent Microorganisms Depositary (NPMD) as accession number NITE BP-1202....\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim merely describes isolation of a microorganism without any stated application to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 46] (Uncertainty: 0.9979)\n",
      "CLAIM: 1. A method for communicating over allocated resources, comprising: receiving a resource allocation comprising a portion of a resource block over a plurality of bundled transmission time intervals, wherein the portion of the resource block comprises a subset of subcarriers in the resource block with...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a telecommunications resource allocation method, unrelated to Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 47] (Uncertainty: 0.9979)\n",
      "CLAIM: 1. A switch system, comprising: a plurality of nodes, wherein each node includes a computational processor and an embedded switch; a plurality of links associated with each node, wherein the plurality of links are configured to connect nodes in the plurality of nodes to create a topology of a switch...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a network switch architecture, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 48] (Uncertainty: 0.9977)\n",
      "CLAIM: 1. A seed of soybean cultivar S100323, wherein a representative of sample seed of said cultivar is deposited under ATCC Accession No. PTA-12317....\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns a soybean seed deposit, not climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 49] (Uncertainty: 0.9976)\n",
      "CLAIM: 1. A method to accelerate particles into a chamber, comprising: distributing a fluidic substance between electrodes configured at a location proximate a chamber, the electrodes comprising a low work function material; generating a current of ionized particles by applying an electric field between th...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim describes a particle acceleration method unrelated to greenhouse gas reduction.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 50] (Uncertainty: 0.9976)\n",
      "CLAIM: 1. A biogenic flocculant composition for CEPT sludge conditioning comprising a) a first flocculant component which comprises at least one acidophilic auto-trophic iron-oxidizing bacterium and at least one species of acid tolerant organotrophic microbes which are grown in medium containing iron (II) ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim focuses on sludge conditioning using microbial flocculants, which is a wastewater treatment application rather than a direct climate‑change mitigation technology.\n",
      "Saved.\n",
      "\n",
      "[Row 51] (Uncertainty: 0.9975)\n",
      "CLAIM: 1. A nuclear reactor comprising: an elongated reactor vessel enclosed at a lower end and having an open upper end on which an annular flange is formed and a central axis extending, along an elongated dimension; a reactor vessel head having an annular portion on an underside of the bead that is machi...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a nuclear reactor component, not a climate‑change mitigation technology.\n",
      "Saved.\n",
      "\n",
      "[Row 52] (Uncertainty: 0.9975)\n",
      "CLAIM: 1. A steam reforming system comprising: a) a kiln, comprising a susceptor tube; a kiln inlet for receiving a feedstock; a conveyor for transporting said feedstock through said kiln; b) a steam reforming reactor comprising a reformer tube; a reactor inlet in fluid communication with said first kiln o...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes a steam reforming system for gas production, which is a general chemical process and does not explicitly address greenhouse gas reduction or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 53] (Uncertainty: 0.9975)\n",
      "CLAIM: 1. A pest trap reporting system, comprising: a plurality of pest traps, wherein each pest trap encloses, retains or kills one or more non-human pests; a pest report database that includes pest activity information for the plurality of pest traps; a plurality of sensors, each of the plurality of sens...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a pest monitoring system, not related to greenhouse gas mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 54] (Uncertainty: 0.9974)\n",
      "CLAIM: 1. A motor vehicle comprising: a body; a wheel rotatably supported on the body; an occupant riding portion supported by the body for tilting relative to the body and mounted with an occupant; occupant attitude detection means for detecting an attitude of the occupant riding portion; body attitude de...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a vehicle tilt-control system unrelated to greenhouse‑gas reduction or climate mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 55] (Uncertainty: 0.9973)\n",
      "CLAIM: 1. A compound having Formula (III) or a therapeutically acceptable salt thereof, wherein...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim describes a chemical compound for therapeutic use, not related to green or climate change mitigation.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 56] (Uncertainty: 0.9973)\n",
      "CLAIM: 1. A method for fabricating a semiconductor device, comprising: forming a conductive layer over first and second regions of a semiconductor substrate; forming a trench extended in the first region of the semiconductor substrate through the conductive layer; forming a first gate electrode in the tren...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes semiconductor fabrication steps, unrelated to Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 57] (Uncertainty: 0.9972)\n",
      "CLAIM: 1. A method for installation of an offshore wind turbine, characterized in comprising the steps of: prefabrication of a foundation, including: fabricating the foundation which includes a plurality of tanks providing buoyant force and uprighting force to the foundation so as to keep the foundation up...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a method for installing an offshore wind turbine, which is a renewable energy technology that mitigates climate change.\n",
      "Saved.\n",
      "\n",
      "[Row 58] (Uncertainty: 0.9972)\n",
      "CLAIM: 1. An airfoil comprising: an airfoil body made of a first material with a leading edge, trailing edge, pressure side and suction side; a sheath with first and second flanks made of a second material; a first shim disposed between a portion of an end of the first flank and the airfoil body and extend...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes structural components of an airfoil without reference to energy efficiency, emissions reduction, or other climate‑change mitigation measures.\n",
      "Saved.\n",
      "\n",
      "[Row 59] (Uncertainty: 0.9972)\n",
      "CLAIM: 1. An electric storage system comprising: a plurality of electric storage blocks connected in series, each of the plurality of electric storage blocks including a plurality of electric storage elements connected in parallel; a plurality of current breakers, each of the plurality of current breakers ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes a battery management system, not a direct green or climate change mitigation technology.\n",
      "Saved.\n",
      "\n",
      "[Row 60] (Uncertainty: 0.9972)\n",
      "CLAIM: 1. A method for performing operations on a stainer in a stainer network comprising: providing a robotic arm coupled to the stainer, the robotic arm having a reagent dispenser; establishing a network connection between a computer and a stainer in the stainer network; sending requests from the compute...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes laboratory automation for sample processing, not a technology related to greenhouse gas mitigation or climate change.\n",
      "Saved.\n",
      "\n",
      "[Row 61] (Uncertainty: 0.9971)\n",
      "CLAIM: 1. A Group III nitride semiconductor light-emitting device, comprising: a conductive support; a p-electrode disposed on the support; a semiconductor layer disposed on the p-electrode, the semiconductor layer comprising at least a p-layer, a light-emitting layer, and an n-layer disposed in this order...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a semiconductor light-emitting device, which is unrelated to green or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 62] (Uncertainty: 0.9971)\n",
      "CLAIM: 1. A plate heat exchanger in a sealed design, with: a stacked arrangement comprising: a front-side and a rear-side end plate, wherein at least one end plate is constituted as a connection plate having at least one connection, heat exchanger plates which are arranged and stacked between the front-sid...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a mechanical design for a plate heat exchanger and does not address energy efficiency or climate‑change mitigation technologies.\n",
      "Saved.\n",
      "\n",
      "[Row 63] (Uncertainty: 0.9971)\n",
      "CLAIM: 1. A vehicle braking/driving force control system comprising: a braking/driving force generating mechanism that causes each wheel of a vehicle to generate driving force or braking force independently of one another; a suspension mechanism that couples each of the wheels that are not supported by spr...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a vehicle braking and driving force control system, which is unrelated to greenhouse gas mitigation or climate change technologies.\n",
      "Saved.\n",
      "\n",
      "[Row 64] (Uncertainty: 0.9970)\n",
      "CLAIM: 1. A detector apparatus configured to receive light and generate electrical signals, the detector apparatus comprising: a light sensor having a light incidence side, the light sensor including at least one photocathode; a cooling component, the cooling component being in direct contact, on the light...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a light detection device, not related to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 65] (Uncertainty: 0.9970)\n",
      "CLAIM: 1. A flexible display device comprising: a display panel configured to generate an image; and a window member on the display panel, the window member comprising: wherein a width of each of the second parts is smaller than a width of the first part at a bending area....\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim describes a flexible display device, which is unrelated to Green/Climate Change mitigation.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 66] (Uncertainty: 0.9969)\n",
      "CLAIM: 1. A conductive film comprising: a substrate; a transparent electrode layer provided on the substrate; and a conductive pattern layer provided on the transparent electrode layer, wherein the conductive pattern layer includes a metal nitride pattern layer including CuNx, x is a mass ratio of N with r...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a conductive film for electronic applications, with no indication of greenhouse gas reduction or climate mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 67] (Uncertainty: 0.9969)\n",
      "CLAIM: 1. A hydrolysable linker selected from a compound of formula V, VI, VII, and VIII: wherein: R′ and R″ are each independently a C each a is independently an integer from 0 to 6; each b is independently an integer from 1 to 6; each X is independently: each X each Y is independently: each Y each m, n, ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Low) | Rationale: The claim describes a chemical linker, with no indication of climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 68] (Uncertainty: 0.9968)\n",
      "CLAIM: 1. A substrate bearing a stack of layers as the back contact in a molybdenum photovoltaic device, said back contact comprising in order from the substrate: a barrier layer comprising at least one of: Si a primer layer; a layer of ZnO; and a layer of molybdenum, wherein the molybdenum is deposited di...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a photovoltaic device structure, which is directly relevant to solar energy technology for climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 69] (Uncertainty: 0.9968)\n",
      "CLAIM: 1. A system comprising: a foil including a leading inlet for fluid to enter, a forward chamber within the foil downstream of the leading inlet, a rearward chamber within the foil downstream of the forward chamber, and a constriction formed by the foil between the forward and rearward chambers; at le...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a fluid flow device without any explicit reference to environmental or climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 70] (Uncertainty: 0.9967)\n",
      "CLAIM: 1. A method for passivating a surface of crystalline iron disulfide, comprising: sputtering iron disulfide to form a layer of crystalline iron disulfide on a substrate, wherein the layer has a surface comprising crystal surfaces; and depositing a capping layer of epitaxial zinc sulfide onto the surf...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns surface passivation of iron disulfide, not a technology for greenhouse gas mitigation or climate change.\n",
      "Saved.\n",
      "\n",
      "[Row 71] (Uncertainty: 0.9967)\n",
      "CLAIM: 1. A brake control apparatus, comprising: a frictional braking unit configured to generate frictional braking force by supplying operating fluid to a wheel cylinder provided on each wheel of a vehicle to press a frictional member against the wheel; a regenerative braking unit configured to generate ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a brake control apparatus for vehicles, which is unrelated to Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 72] (Uncertainty: 0.9966)\n",
      "CLAIM: 1. A circuit for recording a magnitude of an electrostatic discharge (ESD) event during semiconductor assembly, the circuit comprising: a voltage divider connected between a first potential and a second potential, the voltage divider configured to provide a first node having a discrete voltage level...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes an ESD recording circuit for semiconductor manufacturing, which does not pertain to greenhouse gas mitigation or climate change technologies.\n",
      "Saved.\n",
      "\n",
      "[Row 73] (Uncertainty: 0.9966)\n",
      "CLAIM: 1. A rectifier, comprising: a first rectification unit having an anode and a cathode, the anode being connected to a negative radio frequency (RF) port, and the cathode being connected to a positive direct current (DC) port; a second rectification unit having an anode and a cathode, the anode being ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a rectifier circuit, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 74] (Uncertainty: 0.9966)\n",
      "CLAIM: 1. An apparatus comprising: a first electronic device to communicate with a second electronic device, the first device comprising:...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim describes a generic communication apparatus between electronic devices, with no reference to environmental or climate-related functions.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 75] (Uncertainty: 0.9966)\n",
      "CLAIM: 1. A process-based method of detecting a CO 2 gas leak in a deep geologic gas storage reservoir, the method comprising: constructing a gas sampling station in a vadose zone proximal to the deep geologic gas storage reservoir; measuring a CO measuring an O measuring a CH measuring a N determining a H...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The method detects CO₂ leaks from a geological storage site, directly supporting carbon sequestration efforts.\n",
      "Saved.\n",
      "\n",
      "[Row 76] (Uncertainty: 0.9965)\n",
      "CLAIM: 1. A self-supporting reflector for a parabolic trough: (a) having a reflectance of at least 90%, based on the solar spectrum; (b) comprising at least one layer of a transparent plastic material facing a light source and having a layer thickness within a range of from 0.1 mm to 8 mm; and (c) at least...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a component for a parabolic trough solar collector, which is a renewable energy technology used to mitigate climate change.\n",
      "Saved.\n",
      "\n",
      "[Row 77] (Uncertainty: 0.9965)\n",
      "CLAIM: 1. A valve train system for an internal combustion engine having a combustion chamber with a piston which reciprocates therewithin between a top-dead-center position and a bottom-dead-center position, said valve train system comprising: an intake valve which moves between an intake closed position a...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a conventional valve train for an internal combustion engine, which is unrelated to Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 78] (Uncertainty: 0.9965)\n",
      "CLAIM: 1. An organic light-emitting diode (OLED) display, comprising: a first plastic layer; a first barrier layer formed over the first plastic layer; a first intermediate layer formed over the first barrier layer, wherein the first intermediate layer comprises amorphous silicon; a second plastic layer fo...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes an OLED display structure, which does not relate to Green/Climate Change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 79] (Uncertainty: 0.9965)\n",
      "CLAIM: 1. A wave activated power generating device, comprising: a support frame; a buoy vertically positioned to rise and fall relative to motion of waves impacting the buoy and the support frame, the buoy being formed with a hollow interior space; a rack and pinion structure operatively connected between ...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a wave‑powered generator that converts ocean wave motion into electricity, which is a renewable energy technology for climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 80] (Uncertainty: 0.9964)\n",
      "CLAIM: 1. A method for reducing an amount of unwanted living organisms within an algae cultivation fluid, the algae cultivation fluid including wanted living algae of genus Nannochloropsis , the method comprising: subjecting the algae cultivation fluid, the algae cultivation fluid including the wanted livi...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":1,\"confidence\":\"Medium\",\"rationale\":\"The\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 81] (Uncertainty: 0.9964)\n",
      "CLAIM: 1. A compound having Formula (I): wherein, R R b) —(CH R X is —O, NH or S; Y is a cleavable or non-cleavable linker group; and Z is an antigen derived from an infectious agent or a tumor antigen or a pharmaceutically acceptable salt thereof....\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>json<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim concerns a pharmaceutical compound for medical use, not related to climate change mitigation.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 82] (Uncertainty: 0.9964)\n",
      "CLAIM: 1. A memory controller comprising: driver circuitry to output a first timing signal to a memory device, the first timing signal to time transmission of a data signal from the memory device to the memory controller; control circuitry to enable oscillation of the first timing signal at a first frequen...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a memory controller’s timing and clock circuitry, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 83] (Uncertainty: 0.9964)\n",
      "CLAIM: 1. A rubber composition, comprising, based on 100% by mass of a rubber component: 5 to 55% by mass of a copolymer (A) of an aromatic vinyl compound and a conjugated diene compound, the copolymer (A) having an aromatic vinyl compound content of 5-14% by mass and a vinyl bond content in the conjugated...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a rubber composition and silica filler, not a technology for greenhouse gas mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 84] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A method of preparing a metal suboxide, comprising: preparing a mixture including a metal suboxide precursor, an aromatic compound substituted with a hydroxy group, and a linking precursor including one selected from a C1 to C30 aldehyde, a C3 to C30 ketone, and a combination thereof; reacting th...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Low) | Rationale: The claim describes a chemical synthesis method, not directly related to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 85] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A vehicle hydraulic control device including: an oil pump that is driven by a driving force source for wheels; and an oil passage that guides oil discharged from the oil pump to a rotating electrical machine that forms at least a part of the driving force source and a gear mechanism to which driv...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes a hydraulic control system for a vehicle, which does not directly address greenhouse gas reduction or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 86] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A method for the production of a human or animal nutrition product comprising producing an adsorbate suitable for human or animal nutrition comprising applying a component to a carrier using at least one stabilizer such that the component is adsorbed to the carrier, wherein the carrier has a mean...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns a nutritional adsorbate production method, not a technology for greenhouse gas reduction or climate mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 87] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. An automated driving system, comprising: one or more sensors disposed on an autonomous vehicle; and a computing device in communication with the one or more sensors, comprising:...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Low) | Rationale: The claim describes a general automated driving system without explicit reference to environmental or climate mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 88] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A memory device, comprising: an array of memory cells, the memory cells in the array being programmable to at least two different charge levels; and a control logic unit coupled to the array of memory cells and configured to program the memory cells in each of a plurality of groups with a respect...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a memory device and programming logic, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 89] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. An adjustable solar panel mounting assembly comprising: a. a first clamp further comprising an upper and lower portion wherein the lower portion further comprises a cavity; b. a first mounting plate extending outward from the lower portion of the first clamp to an end; c. a first flange extending...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a solar panel mounting assembly, which supports renewable energy generation.\n",
      "Saved.\n",
      "\n",
      "[Row 90] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A method of planting or seeding multiple types of seed in a single planting pass during row-crop planting or seeding of an agricultural field with an agricultural implement, the method comprising: storing seeds of multiple types including at least a first type and a second type in multiple compar...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (Medium) | Rationale: The claim describes a multi‑seed planting method, which is an agricultural technique but does not directly address greenhouse gas reduction or climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 91] (Uncertainty: 0.9963)\n",
      "CLAIM: 1. A method to update a cache in a multi-core processor, the method comprising: receiving a notification of a cache miss associated with a process or a thread running on a single core of the multi-core processor, the single core including: determining that an address associated with the cache miss c...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim concerns processor cache management, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 92] (Uncertainty: 0.9962)\n",
      "CLAIM: 1. A driving method of a liquid crystal display device comprising a liquid crystal element, the driving method comprising the steps of: applying a first voltage to the liquid crystal element in a first subframe period of a first frame period; making transmittance of the liquid crystal element at the...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a technical method for controlling liquid crystal display operation, with no reference to energy efficiency or climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 93] (Uncertainty: 0.9962)\n",
      "CLAIM: 1. A compound of Formula IA: wherein X R R wherein each R each R where Z wherein the alkyl, alkenyl, alkynyl, cycloalkyl, aryl, heterocyclic, or heteroaryl groups of Z wherein Y indicates one or more optional double bonds; and n is 0, 1, 2, or 3; R each R wherein the alkyl, alkenyl, alkynyl, cycloal...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a generic chemical structure for a potential pharmaceutical compound with no reference to environmental or climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 94] (Uncertainty: 0.9962)\n",
      "CLAIM: 1. A valve for a fuel cell comprising: a housing; a first pressure chamber and a second pressure chamber provided in the housing; two supply/discharge tubes connected to the housing, and supplying and discharging fluid to and from the first pressure chamber and the second pressure chamber, respectiv...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (Medium) | Rationale: The claim describes a valve for a fuel cell, which is a technology used in clean energy generation and thus relates to green/climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 95] (Uncertainty: 0.9962)\n",
      "CLAIM: 1. A wing comprising: an airfoil section including a leading edge, a trailing edge, an upper surface and a lower surface, wherein a region within the airfoil section immediately adjacent the leading edge is ventilated via one or more vent openings which open in the upper surface to establish a sub-s...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a wing ventilation system for aerodynamic performance, not climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 96] (Uncertainty: 0.9962)\n",
      "CLAIM: 1. An input device comprising: a touch panel that includes M (where M is a natural number of 5 or more) driving electrodes, and a plurality of detection electrodes forming capacitances between the respective driving electrodes, in which the M driving electrodes and the plurality of detection electro...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a touch panel input device, unrelated to green or climate‑change mitigation.\n",
      "Saved.\n",
      "\n",
      "[Row 97] (Uncertainty: 0.9961)\n",
      "CLAIM: 1. An information display system in a transportation apparatus, the information display system comprises: a liquid crystal display (LCD) screen that occupies at least a portion of a dashboard of the transportation apparatus, wherein the LCD screen is capable of graphically displaying multiple inform...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"High\",\"rationale\":\"The claim describes an information display system for a vehicle dashboard, which does not directly address greenhouse gas reduction or climate change mitigation.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 98] (Uncertainty: 0.9960)\n",
      "CLAIM: 1. A photovoltaic (PV) device, comprising: at least one PV interband cascade (IC PV) stage having a conduction band and a valence band, comprising: wherein the absorption region is positioned between the intraband transport region and the interband tunneling region, wherein the interband tunneling r...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 1 (High) | Rationale: The claim describes a photovoltaic device for generating electricity from light.\n",
      "Saved.\n",
      "\n",
      "[Row 99] (Uncertainty: 0.9960)\n",
      "CLAIM: 1. A rack system comprising: a plurality of trays configured to hold a respective plurality of battery-powered unmanned aerial vehicles; and a frame configured to support the plurality of trays in a vertical arrangement, wherein each tray of the plurality of trays comprises:...\n",
      "------------------------------------------------------------\n",
      "Asking LM Studio...\n",
      "LLM output invalid JSON: <|channel|>final <|constrain|>JSON<|message|>{\"suggestion\":0,\"confidence\":\"Medium\",\"rationale\":\"The claim describes a storage rack for battery-powered UAVs, which does not directly address green or climate change mitigation technologies.\"}\n",
      "LLM Failed (Check settings or Label Manually)\n",
      "Saved.\n",
      "\n",
      "[Row 100] (Uncertainty: 0.9959)\n",
      "CLAIM: 1. A DC electrical machine comprising: an armature having a non-integer number of winding slots per pole-pair of a magnetic field of a field means, each winding slot having a phase angle, wherein the phase angle is electrical and is a position of the winding slot in relation to a fundamental wavefor...\n",
      "------------------------------------------------------------\n",
      "LLM Says: 0 (High) | Rationale: The claim describes a technical improvement to a DC electrical machine, unrelated to climate change mitigation.\n",
      "Saved.\n",
      "\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "# LLM model is locally hosted via network via LM Studio.\n",
    "LM_STUDIO_URL = \"http://localhost:1234/v1/chat/completions\"\n",
    "\n",
    "# GPT-OSS-20B is run locally\n",
    "MODEL_NAME = \"local-model\" \n",
    "\n",
    "filename = \"hitl_green_100.csv\"\n",
    "\n",
    "def get_llm_response_lmstudio(claim_text):\n",
    "    \"\"\"Function to call LM Studio with Error Printing\"\"\"\n",
    "    \n",
    "    system_prompt = \"\"\"\n",
    "    You are a patent classification AI. You must respond in valid JSON format only.\n",
    "    Schema:\n",
    "    {\n",
    "        \"suggestion\": 0 or 1,\n",
    "        \"confidence\": \"Low\", \"Medium\", or \"High\",\n",
    "        \"rationale\": \"short sentence\"\n",
    "    }\n",
    "    \"\"\"\n",
    "    \n",
    "    user_prompt = f\"\"\"\n",
    "    Analyze this patent claim. Is it related to Green/Climate Change mitigation (Y02)?\n",
    "    Claim: \"{claim_text[:2000]}\"\n",
    "    \"\"\"\n",
    "\n",
    "    payload = {\n",
    "        \"model\": MODEL_NAME,\n",
    "        \"messages\": [\n",
    "            {\"role\": \"system\", \"content\": system_prompt},\n",
    "            {\"role\": \"user\", \"content\": user_prompt}\n",
    "        ],\n",
    "        \"temperature\": 0.1, \n",
    "        \"max_tokens\": 150\n",
    "    }\n",
    "\n",
    "    try:\n",
    "        response = requests.post(LM_STUDIO_URL, json=payload, headers={\"Content-Type\": \"application/json\"})\n",
    "        \n",
    "        #DEBUGGING BLOCK\n",
    "        if response.status_code == 200:\n",
    "            result = response.json()\n",
    "            \n",
    "            # Check if the server sent an error instead of an answer\n",
    "            if 'choices' not in result:\n",
    "                print(f\"\\n LM STUDIO ERROR: {result}\")\n",
    "                return None\n",
    "                \n",
    "            content = result['choices'][0]['message']['content']\n",
    "            \n",
    "            # Clean up code blocks if present\n",
    "            if \"```\" in content:\n",
    "                content = content.replace(\"```json\", \"\").replace(\"```\", \"\").strip()\n",
    "            \n",
    "            try:\n",
    "                return json.loads(content)\n",
    "            except json.JSONDecodeError:\n",
    "                print(f\"\\nLLM output invalid JSON: {content}\")\n",
    "                return None\n",
    "        else:\n",
    "            print(f\"Server Error {response.status_code}: {response.text}\")\n",
    "            return None\n",
    "            \n",
    "    except Exception as e:\n",
    "        print(f\"Connection Error: {e}\")\n",
    "        return None\n",
    "\n",
    "def labeling_loop():\n",
    "    if not os.path.exists(filename):\n",
    "        print(f\"Error: {filename} not found.\")\n",
    "        return\n",
    "    \n",
    "    df = pd.read_csv(filename)\n",
    "    \n",
    "    # Create columns if missing\n",
    "    for col in ['llm_green_suggested', 'llm_confidence', 'llm_rationale', 'is_green_human', 'notes']:\n",
    "        if col not in df.columns: df[col] = \"\"\n",
    "\n",
    "    # Find empty rows\n",
    "    remaining_indices = df[df['is_green_human'].isna() | (df['is_green_human'] == \"\")].index.tolist()\n",
    "    \n",
    "    print(f\"--- Part C: HITL Labeling ---\")\n",
    "    print(f\"Remaining rows: {len(remaining_indices)}\")\n",
    "    print(\"-----------------------------\\n\")\n",
    "\n",
    "    for idx in remaining_indices:\n",
    "        row = df.loc[idx]\n",
    "        claim_text = str(row['text'])\n",
    "        \n",
    "        print(f\"[Row {idx+1}] (Uncertainty: {row['u']:.4f})\")\n",
    "        print(f\"CLAIM: {claim_text[:300]}...\") \n",
    "        print(\"-\" * 60)\n",
    "\n",
    "        # 1. Ask LLM\n",
    "        print(\"Asking LM Studio...\", end=\"\\r\")\n",
    "        llm_result = get_llm_response_lmstudio(claim_text)\n",
    "        \n",
    "        suggestion = 0\n",
    "        conf = \"Low\"\n",
    "        rat = \"\"\n",
    "        \n",
    "        if llm_result:\n",
    "            suggestion = llm_result.get('suggestion', 0)\n",
    "            conf = llm_result.get('confidence', \"Low\")\n",
    "            rat = llm_result.get('rationale', \"\")\n",
    "            print(f\"LLM Says: {suggestion} ({conf}) | Rationale: {rat}\")\n",
    "        else:\n",
    "            print(\"LLM Failed (Check settings or Label Manually)\")\n",
    "\n",
    "        # 2. Human Review\n",
    "        while True:\n",
    "            user_input = input(f\"Your Label (0/1) [Enter for {suggestion}]: \")\n",
    "            if user_input.strip() == \"\":\n",
    "                final_label = suggestion\n",
    "                break\n",
    "            if user_input.strip() in ['0', '1']:\n",
    "                final_label = int(user_input)\n",
    "                break\n",
    "            print(\"Please enter 0 or 1.\")\n",
    "\n",
    "        # 3. Save\n",
    "        df.at[idx, 'llm_green_suggested'] = suggestion\n",
    "        df.at[idx, 'llm_confidence'] = conf\n",
    "        df.at[idx, 'llm_rationale'] = rat\n",
    "        df.at[idx, 'is_green_human'] = final_label\n",
    "        \n",
    "        df.to_csv(filename, index=False)\n",
    "        print(\"Saved.\\n\")\n",
    "\n",
    "    print(\"Done\")\n",
    "\n",
    "labeling_loop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42e180f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============================================================\n",
      "HITL ANALYSIS REPORT\n",
      "============================================================\n",
      "Total Claims Labeled: 100\n",
      "Human Overrides:      3\n",
      "Agreement Rate:       97.0%\n",
      "------------------------------------------------------------\n",
      "\n",
      " 3 EXAMPLES OF HUMAN OVERRIDES:\n",
      "\n",
      "Example #1:\n",
      "   • Claim Snippet: \"1. An apparatus, comprising: a single, dilute solids phase reactor having a top, a central section, and a bottom section with an exit port, and a top ...\"\n",
      "   • LLM Suggestion: 0 (Rationale: The claim describes a particle removal apparatus for exhaust gases, which addresses air pollution control rather than greenhouse gas mitigation.)\n",
      "   • Human Label:    1\n",
      "   • Your Notes:     Manual override: This technology is classified as Green under CPC Y02.\n",
      "\n",
      "Example #2:\n",
      "   • Claim Snippet: \"1. A biogenic flocculant composition for CEPT sludge conditioning comprising a) a first flocculant component which comprises at least one acidophilic ...\"\n",
      "   • LLM Suggestion: 0 (Rationale: The claim focuses on sludge conditioning using microbial flocculants, which is a wastewater treatment application rather than a direct climate‑change mitigation technology.)\n",
      "   • Human Label:    1\n",
      "   • Your Notes:     Manual override: This technology is classified as Green under CPC Y02.\n",
      "\n",
      "Example #3:\n",
      "   • Claim Snippet: \"1. A nuclear reactor comprising: an elongated reactor vessel enclosed at a lower end and having an open upper end on which an annular flange is formed...\"\n",
      "   • LLM Suggestion: 0 (Rationale: The claim describes a nuclear reactor component, not a climate‑change mitigation technology.)\n",
      "   • Human Label:    1\n",
      "   • Your Notes:     Manual override: This technology is classified as Green under CPC Y02.\n"
     ]
    }
   ],
   "source": [
    "# Load the completed file\n",
    "filename = \"hitl_green_100.csv\"\n",
    "try:\n",
    "    df = pd.read_csv(filename)\n",
    "except FileNotFoundError:\n",
    "    print(f\"Error: Could not find {filename}. Make sure you saved your work!\")\n",
    "    exit()\n",
    "\n",
    "# Find Disagreements between LLM and Human Labels (0/1)\n",
    "df['llm_green_suggested'] = pd.to_numeric(df['llm_green_suggested'], errors='coerce').fillna(-1).astype(int)\n",
    "df['is_green_human'] = pd.to_numeric(df['is_green_human'], errors='coerce').fillna(-1).astype(int)\n",
    "\n",
    "overrides = df[df['llm_green_suggested'] != df['is_green_human']]\n",
    "total_count = len(df)\n",
    "override_count = len(overrides)\n",
    "\n",
    "#Print the Report\n",
    "print(\"=\"*60)\n",
    "print(\"HITL ANALYSIS REPORT\")\n",
    "print(\"=\"*60)\n",
    "print(f\"Total Claims Labeled: {total_count}\")\n",
    "print(f\"Human Overrides:      {override_count}\")\n",
    "print(f\"Agreement Rate:       {((total_count - override_count)/total_count)*100:.1f}%\")\n",
    "print(\"-\" * 60)\n",
    "\n",
    "if override_count > 0:\n",
    "    print(\"\\n 3 EXAMPLES OF HUMAN OVERRIDES:\")\n",
    "    # Selecting 3 examples to show\n",
    "    examples = overrides.head(3)\n",
    "    \n",
    "    for i, (idx, row) in enumerate(examples.iterrows(), 1):\n",
    "        print(f\"\\nExample #{i}:\")\n",
    "        print(f\"   • Claim Snippet: \\\"{str(row['text'])[:150]}...\\\"\")\n",
    "        print(f\"   • LLM Suggestion: {row['llm_green_suggested']} (Rationale: {row['llm_rationale']})\")\n",
    "        print(f\"   • Human Label:    {row['is_green_human']}\")\n",
    "        if row['notes']:\n",
    "            print(f\"   • Your Notes:     {row['notes']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4fefb37",
   "metadata": {},
   "source": [
    "# Part D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ad0f2e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting Part D: Final Active Learning Evaluation...\n",
      "   - Generating Base Training and Eval Embeddings...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "213f76fffed1405582e4c06644c3a2cf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Encoding:   0%|          | 0/63 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5c3e96ca30f94a0fa27f27b24368d491",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Encoding:   0%|          | 0/157 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   - Loading 100 human-labeled examples...\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "21f8b1a47ab249c8bea9de13cd2b80e9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Encoding:   0%|          | 0/4 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "========================================\n",
      "FINAL PERFORMANCE COMPARISON\n",
      "========================================\n",
      "Metric          | Baseline (Part A)    | Active (Part D)     \n",
      "------------------------------------------------------------\n",
      "Precision       |               0.7489 |               0.7473 (-0.0015)\n",
      "Recall          |               0.7488 |               0.7467 (-0.0021)\n",
      "F1-score        |               0.7488 |               0.7465 (-0.0023)\n",
      "============================================================\n",
     ]
    }
   ],
   "source": [
    "print(\"Starting Part D: Final Active Learning Evaluation...\")\n",
    "\n",
    "# Setup Model & Data\n",
    "model_name = \"AI-Growth-Lab/PatentSBERTa\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModel.from_pretrained(model_name)\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "model.to(device); model.eval()\n",
    "\n",
    "def get_embeddings(text_list, batch_size=32):\n",
    "    all_embeddings = []\n",
    "    for i in tqdm(range(0, len(text_list), batch_size), desc=\"Encoding\"):\n",
    "        batch_texts = text_list[i:i+batch_size]\n",
    "        inputs = tokenizer(batch_texts, padding=True, truncation=True, max_length=128, return_tensors=\"pt\").to(device)\n",
    "        with torch.no_grad():\n",
    "            outputs = model(**inputs)\n",
    "            all_embeddings.append(outputs.last_hidden_state[:, 0, :].cpu().numpy())\n",
    "    return np.vstack(all_embeddings)\n",
    "\n",
    "# Re-create splits from Part A\n",
    "df = pd.read_parquet(\"patents_50k_green.parquet\")\n",
    "df_eval = df.sample(n=5000, random_state=42)\n",
    "df_remaining = df.drop(df_eval.index)\n",
    "df_train = df_remaining.sample(n=2000, random_state=42)\n",
    "\n",
    "print(\"   - Generating Base Training and Eval Embeddings...\")\n",
    "X_train = get_embeddings(df_train['text'].tolist())\n",
    "y_train = df_train['is_green_silver'].values\n",
    "X_eval = get_embeddings(df_eval['text'].tolist())\n",
    "y_eval = df_eval['is_green_silver'].values\n",
    "\n",
    "# Train Baseline\n",
    "clf_base = LogisticRegression(max_iter=1000, random_state=42)\n",
    "clf_base.fit(X_train, y_train)\n",
    "base_report = classification_report(y_eval, clf_base.predict(X_eval), output_dict=True)\n",
    "\n",
    "# Load your HITL Gold Labels\n",
    "df_hitl = pd.read_csv(\"hitl_green_100.csv\")\n",
    "print(f\"   - Loading {len(df_hitl)} human-labeled examples...\")\n",
    "X_hitl = get_embeddings(df_hitl['text'].tolist())\n",
    "y_hitl = df_hitl['is_green_human'].values\n",
    "\n",
    "# Active Learning: Combine Original Train + Human Gold Labels\n",
    "X_combined = np.vstack([X_train, X_hitl])\n",
    "y_combined = np.concatenate([y_train, y_hitl])\n",
    "\n",
    "# Train the Active Learning Model\n",
    "clf_active = LogisticRegression(max_iter=1000, random_state=42)\n",
    "clf_active.fit(X_combined, y_combined)\n",
    "active_report = classification_report(y_eval, clf_active.predict(X_eval), output_dict=True)\n",
    "\n",
    "# FINAL COMPARISON REPORT\n",
    "print(\"\\n\" + \"=\"*40)\n",
    "print(\"FINAL PERFORMANCE COMPARISON\")\n",
    "print(\"=\"*40)\n",
    "print(f\"{'Metric':<15} | {'Baseline (Part A)':<20} | {'Active (Part D)':<20}\")\n",
    "print(\"-\" * 60)\n",
    "for m in ['precision', 'recall', 'f1-score']:\n",
    "    val_a = base_report['macro avg'][m]\n",
    "    val_d = active_report['macro avg'][m]\n",
    "    diff = val_d - val_a\n",
    "    print(f\"{m.capitalize():<15} | {val_a:20.4f} | {val_d:20.4f} ({'+' if diff >=0 else ''}{diff:.4f})\")\n",
    "print(\"=\"*60)\n",
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf420b77",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}