File size: 25,694 Bytes
0808b67
 
2c510c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dfe4f9
 
 
29b94f1
 
2c510c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0808b67
 
9f4501c
 
0cf1760
 
 
 
 
2c510c1
 
 
 
 
0cf1760
 
2c510c1
0cf1760
 
2c510c1
0cf1760
2c510c1
 
 
 
0cf1760
2c510c1
0cf1760
2c510c1
 
 
0cf1760
 
2c510c1
0cf1760
2c510c1
 
 
0cf1760
 
 
 
 
 
 
2c510c1
0cf1760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c510c1
 
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
from fastapi import FastAPI

import re
import inspect
import numpy as np
import pandas as pd
import os

from datetime import datetime

from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
from sentence_transformers import CrossEncoder

# import mysql.connector
import psycopg2

db_user = os.environ["DB_NAME"] 

# mydb = mysql.connector.connect(
#   host=os.environ["DB_HOST"],
#   user=os.environ["DB_USER"],
#   port=os.environ["DB_PORT"],
#   password=os.environ["DB_PASSWORD"]
# )

mydb = psycopg2.connect(
  host=os.environ["DB_HOST"],
  user=os.environ["DB_USER"],
  port=os.environ["DB_PORT"],
  database=os.environ["DB_NAME"],
  password=os.environ["DB_PASSWORD"]
)


# df_sample = pd.read_sql(f"SELECT * FROM {db_user}.sample", mydb)
# df_bobot = pd.read_sql(f"SELECT * FROM {db_user}.bobot", mydb)

df_sample = pd.read_sql(f"SELECT * FROM sample", mydb)
df_bobot = pd.read_sql(f"SELECT * FROM bobot", mydb)

df_sample["tg_bayar"] = pd.to_datetime(df_sample['tg_bayar']).dt.date
df_sample["tg_akhir_pkb"] = pd.to_datetime(df_sample['tg_akhir_pkb']).dt.date
df_sample["tg_akhir_stnk"] = pd.to_datetime(df_sample['tg_akhir_stnk']).dt.date


df_bobot['BOBOT'] = [float(x.replace(",",".")) for x in df_bobot['BOBOT']]#.astype(float)
df_bobot["JENIS_KENDARAAN"] = [x[:-2] + x[-1] if x[-2] == "." else x for x in df_bobot["JENIS_KENDARAAN"]]

dict_swdkllj = {
    "A":3000,
    "B":23000,
    "C1":35000,
    "C2":83000,
    "DP":35000,
    "DU":73000,
    "EP":153000,
    "EU":90000,
    "F":163000
}

def find_swdkllj(row):
    cc = int(row["jumlah_cc"])
    nm = str(row["nm_jenis_kb"]).upper()
    
    if nm in ["AMBULANCE", "DAMKAR", "MOBIL JENAZAH"]:
        return "A"
    if nm in ["ALAT BERAT"]:
        return "B"
    if nm in ["SEDAN", "JEEP", "MINIBUS"]:
        return "DP"
    
    if "SPD. MOTOR" in nm:
        if cc <= 50:
            return "A"
        elif cc <= 250:
            return "C1"
        else:
            return "C2"
    
    if "PICK UP" in nm:
        if cc <= 2400:
            return "DP"

df_sample["gol"] = df_sample.apply(find_swdkllj, axis=1)


df_sample["pnbp_stnk"] = df_sample["nm_jenis_kb"].apply(
    lambda x: 100000 if x == "SPD. MOTOR R2" else 200000
)

df_sample["pnbp_tnkb"] = df_sample["nm_jenis_kb"].apply(
    lambda x: 60000 if x == "SPD. MOTOR R2" else 100000
)

codes = """001 - Vehicle Registration (New)
002 - Vehicle Registration Renewal
003 - Vehicle Ownership Transfer
004 - Vehicle De-registration
005 - Lost Registration Certificate Replacement
006 - Address Change Update
007 - Vehicle Data Correction
008 - Ownership Name Correction
009 - Vehicle Tax Payment
010 - Late Payment Fee Processing
011 - Vehicle Type/Specification Update
012 - BBNKB (Transfer Fee of Vehicle Ownership)
013 - STNK Issuance (Vehicle Registration Certificate)
014 - STNK Renewal
015 - Motor Vehicle Roadworthiness Inspection
016 - Plate Number Renewal
017 - Lost Plate Replacement
018 - Vehicle Export Registration
019 - Vehicle Import Registration
020 - Fleet Vehicle Registration
021 - Bulk Vehicle Registration Update
022 - Vehicle Insurance Assistance
023 - Vehicle Accident Reporting
024 - Vehicle Usage Change Declaration (e.g., personal to commercial)
025 - Legal Document Verification
026 - Ownership Transfer for Inherited Vehicle
027 - STNK Temporary Suspension
028 - Proof of Ownership Document Update
029 - Vehicle Ownership History Check
030 - Vehicle Tax Recalculation Request
031 - Tax Exemption Application (for special cases)
032 - Deceased Owner’s Vehicle Ownership Transfer""".split("\n")

undetected = "099 - Other/Undetected"



# codes = """001 - Pendaftaran Kendaraan (Baru)
# 002 - Pembaruan Pendaftaran Kendaraan
# 003 - Alih Kepemilikan Kendaraan
# 004 - Pembatalan Pendaftaran Kendaraan
# 005 - Penggantian Sertifikat Pendaftaran Kendaraan yang Hilang
# 006 - Pembaruan Perubahan Alamat
# 007 - Koreksi Data Kendaraan
# 008 - Koreksi Nama Kepemilikan
# 009 - Pembayaran Pajak Kendaraan
# 010 - Proses Denda Keterlambatan Pembayaran
# 011 - Pembaruan Jenis/Spesifikasi Kendaraan
# 012 - Pembayaran Pajak Kendaraan Melalui E-Samsat
# 013 - Penerbitan STNK (Sertifikat Pendaftaran Kendaraan)
# 014 - Pembaruan STNK
# 015 - Pemeriksaan Kelayakan Jalan Kendaraan Bermotor
# 016 - Pembaruan Nomor Plat Kendaraan
# 017 - Penggantian Plat yang Hilang
# 018 - Pendaftaran Ekspor Kendaraan
# 019 - Pendaftaran Impor Kendaraan
# 020 - Pendaftaran Kendaraan Armada
# 021 - Pembaruan Pendaftaran Kendaraan Massal
# 022 - Bantuan Asuransi Kendaraan
# 023 - Pelaporan Kecelakaan Kendaraan
# 024 - Deklarasi Perubahan Penggunaan Kendaraan (misalnya, pribadi ke komersial)
# 025 - Verifikasi Dokumen Hukum
# 026 - Alih Kepemilikan Kendaraan Warisan
# 027 - Penangguhan Sementara STNK
# 028 - Pembaruan Dokumen Bukti Kepemilikan
# 029 - Pemeriksaan Riwayat Kepemilikan Kendaraan
# 030 - Permintaan Perhitungan Ulang Pajak Kendaraan
# 031 - Permohonan Pembebasan Pajak (untuk kasus khusus)
# 032 - Alih Kepemilikan Kendaraan Pemilik yang Meninggal""".split("\n")

codes = """001 - Pendaftaran Kendaraan
002 - Pembaruan Data Kendaraan
003 - Alih Kepemilikan atau Balik Nama Kendaraan
004 - Pelaporan Dokumen atau Plat yang Hilang
005 - Pembayaran dan Pengelolaan Pajak Kendaraan
006 - Pemeriksaan dan Verifikasi Kendaraan
007 - Pendaftaran Kendaraan Ekspor, Impor, atau Armada
008 - Pelaporan dan Bantuan Terkait Kendaraan
009 - Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan""".split("\n")

codes = """003 - Alih Kepemilikan atau Balik Nama Kendaraan
005 - Pembayaran dan Pengelolaan Pajak Kendaraan""".split("\n")

# documents = """Vehicle Registration
# Vehicle Data Update
# Vehicle Ownership or Change of Name
# Reporting of Lost Documents or Plates
# Vehicle Tax Payment and Management
# Vehicle Inspection and Verification
# Registration of Export, Import, or Fleet Vehicles
# Reporting and Assistence of Vehicles Insurance
# Suspension or Declaration of Change of Vehicle Use""".split("\n")


# examples = [
#     {"code": "001", "examples": [
#         "Register a new vehicle",
#         "Vehicle registration application",
#         "Apply for vehicle registration",
#         "Submit new vehicle registration",
#         "New vehicle registration request"
#     ]},
#     {"code": "002", "examples": [
#         "Update vehicle information",
#         "Change details of a registered vehicle",
#         "Submit updated vehicle data",
#         "Request vehicle data update",
#         "Modify vehicle registration information"
#     ]},
#     {"code": "003", "examples": [
#         "Transfer vehicle ownership",
#         "Apply for vehicle name change",
#         "Request ownership transfer for a vehicle",
#         "Submit vehicle ownership change",
#         "Process vehicle title transfer"
#     ]},
#     {"code": "004", "examples": [
#         "Report lost vehicle documents (STNK, BPKB, etc)",
#         "Declare missing license plates",
#         "File a report for lost vehicle papers",
#         "Lost vehicle documents reporting",
#         "Inform authorities about missing vehicle documents"
#     ]},
#     {"code": "005", "examples": [
#         "Pay vehicle tax",
#         "Manage vehicle tax payments",
#         "Submit vehicle tax payment",
#         "Request vehicle tax management services",
#         "Complete annual vehicle tax"
#     ]},
#     {"code": "006", "examples": [
#         "Conduct vehicle inspection",
#         "Request vehicle verification check",
#         "Submit vehicle for verification",
#         "Schedule vehicle examination",
#         "Apply for vehicle inspection and verification"
#     ]},
#     {"code": "007", "examples": [
#         "Register an imported vehicle",
#         "Submit registration for export vehicle",
#         "Apply for fleet vehicle registration",
#         "Register new vehicle for export or import",
#         "Fleet vehicle registration application"
#     ]},
#     {"code": "008", "examples": [
#         "Report vehicle-related issues",
#         "Request assistance for a vehicle problem",
#         "Submit report concerning a vehicle",
#         "File for vehicle-related support",
#         "Seek help with vehicle concerns"
#     ]},
#     {"code": "009", "examples": [
#         "Suspend vehicle usage",
#         "Declare change of vehicle usage purpose",
#         "Request a hold on vehicle usage",
#         "Submit application for vehicle usage change",
#         "File for suspension of vehicle operations"
#     ]}
# ]

examples = [
    {"code": "001", "examples": [
        "Daftarkan kendaraan baru",
        "Pengajuan pendaftaran kendaraan",
        "Ajukan pendaftaran kendaraan",
        "Serahkan pendaftaran kendaraan baru",
        "Permintaan pendaftaran kendaraan baru"
    ]},
    {"code": "002", "examples": [
        "Perbarui informasi kendaraan",
        "Ubah detail kendaraan yang terdaftar",
        "Ajukan pembaruan data kendaraan",
        "Permintaan pembaruan data kendaraan",
        "Modifikasi informasi pendaftaran kendaraan"
    ]},
    {"code": "003", "examples": [
        "Alihkan kepemilikan kendaraan",
        "Ajukan perubahan nama kendaraan",
        "Permintaan pengalihan kepemilikan kendaraan",
        "Serahkan perubahan kepemilikan kendaraan",
        "Proses alih nama kendaraan"
    ]},
    {"code": "004", "examples": [
        "Laporkan dokumen kendaraan hilang (STNK, BPKB, dll)",
        "Nyatakan plat nomor hilang",
        "Ajukan laporan dokumen kendaraan hilang",
        "Pelaporan dokumen kendaraan yang hilang",
        "Informasikan pihak berwenang tentang dokumen kendaraan yang hilang"
    ]},
    {"code": "005", "examples": [
        "Bayar pajak kendaraan",
        "Kelola pembayaran pajak kendaraan",
        "Ajukan pembayaran pajak kendaraan",
        "Permintaan layanan manajemen pajak kendaraan",
        "Selesaikan pajak tahunan kendaraan"
    ]},
    {"code": "006", "examples": [
        "Lakukan pemeriksaan kendaraan",
        "Ajukan pemeriksaan verifikasi kendaraan",
        "Serahkan kendaraan untuk verifikasi",
        "Jadwalkan pemeriksaan kendaraan",
        "Ajukan inspeksi dan verifikasi kendaraan"
    ]},
    {"code": "007", "examples": [
        "Daftarkan kendaraan impor",
        "Ajukan pendaftaran kendaraan ekspor",
        "Ajukan pendaftaran kendaraan armada",
        "Daftarkan kendaraan baru untuk ekspor atau impor",
        "Pengajuan pendaftaran kendaraan armada"
    ]},
    {"code": "008", "examples": [
        "Laporkan masalah terkait kendaraan",
        "Permintaan bantuan untuk masalah kendaraan",
        "Ajukan laporan mengenai kendaraan",
        "Ajukan dukungan terkait kendaraan",
        "Cari bantuan terkait permasalahan kendaraan"
    ]},
    {"code": "009", "examples": [
        "Tangguhkan penggunaan kendaraan",
        "Nyatakan perubahan tujuan penggunaan kendaraan",
        "Ajukan penangguhan penggunaan kendaraan",
        "Ajukan perubahan penggunaan kendaraan",
        "Ajukan penangguhan operasional kendaraan"
    ]}
]



# examples = [
#     {"code": "001", "examples": [
#         codes[0][6:],
#         "Pendaftaran kendaraan",
#         "Daftar baru untuk plat kendaraan",
#         "Registrasi kendaraan"
#     ]},
#     {"code": "002", "examples": [
#         codes[1][6:],
#         "Perubahan alamat, warna, komponnen kendaraan",
#         "Update informasi kendaraan",
#         "Perubahan data pemilik",
#     ]},
#     {"code": "003", "examples": [
#         codes[2][6:],
#         "Alih kepemilikan kendaraan",
#         "Balik nama untuk kendaraan",
#         "Perubahan pemilik pada kendaraan"
#     ]},
#     {"code": "004", "examples": [
#         codes[3][6:],
#         "Laporan kehilangan dokumen kendaraan",
#         "Hilangnya plat nomor kendaraan",
#         "Dokumen kendaraan dilaporkan hilang"
#     ]},
#     {"code": "005", "examples": [
#         codes[4][6:],
#         "Pembayaran pajak kendaraan",
#         "Kelola pajak tahunan kendaraan",
#         "Pajak kendaraan"
#     ]},
#     {"code": "006", "examples": [
#         codes[5][6:],
#         "Pemeriksaan fisik kendaraan",
#         "Verifikasi kendaraan",
#         "Cek kondisi kendaraan"
#     ]},
#     {"code": "007", "examples": [
#         codes[6][6:],
#         "Pendaftaran kendaraan impor",
#         "Registrasi armada baru",
#         "Kendaraan ekspor didaftarkan"
#     ]},
#     {"code": "008", "examples": [
#         codes[7][6:],
#         "Pelaporan terkait kendaraan",
#         "Bantuan atau Asuransi atas kendaraan",
#         "Laporkan masalah kendaraan"
#     ]},
#     {"code": "009", "examples": [
#         codes[8][6:],
#         "Penangguhan penggunaan kendaraan",
#         "Deklarasi perubahan fungsi kendaraan",
#         "Penggunaan baru untuk kendaraan"
#     ]}
# ]


vehicle_tax_info = {
    "B 1234 BCA": {
        "no_rangka": "1237191234",
        "type": "SUV",
        "tanggal": "23 Desember 2024",
        "status": "Belum Bayar",
        "harga_jual": 500_000_000  # In Rupiah
    },
    "B 5678 XYZ": {
        "no_rangka": "9876543210",
        "type": "Sedan",
        "tanggal": "15 Januari 2025",
        "status": "Belum Bayar",
        "harga_jual": 375_800_000  # In Rupiah
    },
    "D 3456 DEF": {
        "no_rangka": "4561237890",
        "type": "MPV",
        "tanggal": "10 Februari 2025",
        "status": "Sudah Bayar",
        "harga_jual": 400_000_000  # In Rupiah
    }
}

# Table for detail calculations (perhitungan)
detail_perhitungan = {
    "001": {
        "name": "Pendaftaran Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.1,
        # Example formula: 10% of harga_jual
    },
    "002": {
        "name": "Pembaruan Data Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.05,
        # Example formula: 5% of harga_jual
    },
    "003": {
        "name": "Alih Kepemilikan (Balik Nama) Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.1,  
        # Example formula: 10% of harga_jual
    },
    "004": {
        "name": "Penggantian Dokumen atau Plat yang Hilang",
        "formula": lambda harga_jual: harga_jual * 0.03,
        # Example formula: 3% of harga_jual
    },
    "005": {
        "name": "Pembayaran dan Pengelolaan Pajak Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.12,
        # Example formula: 12% of harga_jual
    },
    "006": {
        "name": "Pemeriksaan dan Verifikasi Kendaraan",
        "formula": lambda harga_jual: 100000,
        # Example formula: 2% of harga_jual
    },
    "007": {
        "name": "Pendaftaran Kendaraan Ekspor, Impor, atau Armada",
        "formula": lambda harga_jual: harga_jual * 0.15,
        # Example formula: 15% of harga_jual
    },
    "008": {
        "name": "Pelaporan dan Bantuan Terkait Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.04,
        # Example formula: 4% of harga_jual
    },
    "009": {
        "name": "Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan",
        "formula": lambda harga_jual: harga_jual * 0.06,
        # Example formula: 6% of harga_jual
    }
}

undetected = "099 - Lainnya/Tidak Terdeteksi"

model_ids = [
    "BAAI/bge-m3",
    "sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
    "intfloat/multilingual-e5-small",
    "sentence-transformers/distiluse-base-multilingual-cased-v2",
    "Alibaba-NLP/gte-multilingual-base",
    "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
    "jinaai/jina-reranker-v2-base-multilingual",
    "BAAI/bge-reranker-v2-m3",
]
# model_id = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
# model_id = "Alibaba-NLP/gte-multilingual-base"
# model_id = "BAAI/bge-m3"
# model_id = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
# model_id = "intfloat/multilingual-e5-small"
# model_id = "sentence-transformers/distiluse-base-multilingual-cased-v2"

model_id = model_ids[-1]
model = None
codes_emb = None

def load_model(model_id):
    # global model
    # global codes_emb
    if model_id in model_ids[-2:]:
        model = CrossEncoder(
            # "jinaai/jina-reranker-v2-base-multilingual",
            # "BAAI/bge-reranker-v2-m3",
            model_id,
            automodel_args={"torch_dtype": "auto"},
            trust_remote_code=True,
        )
        return model, None
    else:
        model = SentenceTransformer(model_id, trust_remote_code=True)
        codes_emb = model.encode([x[6:] for x in codes])
        # codes_emb = model.encode([x["examples"] for x in examples])#.mean(axis=1)
        # codes_emb = np.mean([model.encode(x["examples"]) for x in examples], axis=1)
        return model, codes_emb
        
model, codes_emb = load_model(model_id)

# for x in examples:
#   codes_emb.append(model.encode(x["examples"]))
# codes_emb = np.mean(codes_emb, axis=1)


def censor_middle(number, num_to_hide=4):
  number_str = str(number)
  if num_to_hide == -1:
    num_to_hide = int(len(number_str)*0.8)
  middle_index = len(number_str) // 2
  start_index = middle_index - num_to_hide // 2
  end_index = middle_index + num_to_hide // 2

  censored_part = "\*" * num_to_hide
  censored_number = number_str[:start_index] + censored_part + number_str[end_index:]

  return censored_number


def get_calculation(request_code, plate_number):
    print(request_code, plate_number, "GET CALC")
    calc = detail_perhitungan.get(request_code)
    vehicle = vehicle_tax_info.get(plate_number)
    
    if vehicle != None and calc != None:
        harga_jual = vehicle.get("harga_jual")
        formula = calc.get("formula")
        result = formula(harga_jual)
        description = inspect.getsource(formula).split(":", 2)[-1].strip()
        result_detail = request_code + " - " + calc.get("name")
        return result, str(description), result_detail

    elif calc != None:
        formula = calc.get("formula")
        description = inspect.getsource(formula).split(":", 2)[-1].strip()
        result_detail = request_code + " - " + calc.get("name")
        return None, str(description), result_detail

    else:
        return None, None, None


def build_output_formula(descriptions, result_details):
    out = "----------------------------------------------------\n\n"
    out = "Daftar Kode Permohonan:\n"
    for i, (desc,detail) in enumerate(zip(descriptions, result_details)):
        # harga_jual = str(vehicle.get("harga_jual"))
        out += f"{i+1}. {detail}\nRumus: {desc}\n"
    return out


def build_output_vehicle(plate_number):
    vehicle = vehicle_tax_info.get(plate_number).copy()
    out = "----------------------------------------------------\n\n"
    out = "Nomor Polisi: " + plate_number + "\n"
    vehicle["no_rangka"] = censor_middle(vehicle["no_rangka"])
    vehicle["harga_jual"] = "Rp{:,}".format(vehicle["harga_jual"])
    out += "\n".join([k + " : " + str(v) for  k,v in vehicle.items()])
    return out

def build_output(result, description, result_detail, plate_number):
    return build_outputs([result], [description], [result_detail], plate_number)


def build_outputs(results, descriptions, result_details, plate_number):
    vehicle = vehicle_tax_info.get(plate_number).copy()
    vehicle["harga_jual"] = "Rp{:,}".format(vehicle["harga_jual"])

    out = "----------------------------------------------------\n\n"
    out = "Nomor Polisi: " + plate_number + "\n"
    out += "\n".join([k + " : " + str(v) if k != "no_rangka" else k + " : " + censor_middle(v) for  k,v in vehicle.items()])
    # out += "\n----------------------------------------------"
    
    # out += f"\nWajib Pajak dengan NoPol {plate_number} ingin melakukan proses berikut:\n"
    out += "\n\nDaftar Kode Permohonan:\n"

    for i, (res,desc,detail) in enumerate(zip(results, descriptions, result_details)):
        harga_jual = vehicle["harga_jual"]
        res_str = "{:,}".format(res)
        out += f"{i+1}. {detail}\nRumus: {desc}\nDetail perhitungan: {desc.replace('harga_jual', harga_jual)} = Rp{res_str}\n"

    # out += "----------------------------\nEstimasi biaya: "
    out += "\n\n\nEstimasi Biaya: "

    if len(results) > 1:
        out += " + ".join(["Rp{:,}".format(x) for x in results])
        out += " = Rp{:,}".format(sum(results))
    else:
        out += "Rp{:,}".format(results[0])

    out += "\n\n----------------------------"
    # out += "\n----------------------------\n\n--------------------------------"
    return out
    

app = FastAPI()

@app.post("/pred")
async def greet_json(
    message : str, 
    threshold : float = 0.0005,
    tarif_pkb : float = 0.015,
    tarif_bbnkb : float = 0.1,
):
    global codes_emb
    global undetected

    undetected_code = undetected[:3]
    
    # tarif_pkb = 1.5/100
    # tarif_bbnkb = 0.1

    pattern = r'\b([A-Za-z]{1,2})\s?(\d{2,4})\s?([A-Za-z]{1,3})\b'
    
    matches = re.findall(pattern, message)
    
    plates = [" ".join(x).upper() for i,x in enumerate(matches)]


    if type(model) == CrossEncoder:
        sentence_pairs = [[message, v[6:]] for v in codes]
        scores = model.predict(sentence_pairs, convert_to_tensor=True)
        weights = [9,8]
    else:
        text_emb = model.encode(message)
        scores = cos_sim(codes_emb, text_emb).mean(axis=-1)#[:,0]
        weights = [11,9]
        
    scores_argsort = scores.argsort(descending=True)
    w_avg = np.average(scores[scores_argsort].numpy(), weights=weights)


    out = ""
    
    for inp in plates:
    
        vehicle = df_sample[df_sample["no_polisi"] == inp.strip()].copy()
        
        if vehicle.shape[0] == 0:
          out += f"\n\n---\nKendaraan {inp} Tidak Ditemukan\n"
        else:
          vehicle["nm_pemilik"] = censor_middle(vehicle["nm_pemilik"].values[0], -1)
          vehicle["al_pemilik"] = censor_middle(vehicle["al_pemilik"].values[0], -1)
            
          v_type = vehicle["nm_jenis_kb"].values[0]
          nilai_jual = vehicle["nilai_jual"].values[0]
          pnbp_stnk = vehicle["pnbp_stnk"].values[0]
          pnbp_tnkb = vehicle["pnbp_tnkb"].values[0]
        
          bobot = df_bobot[df_bobot["JENIS_KENDARAAN"]==v_type]["BOBOT"].values[0]
        
          bbnkb = tarif_bbnkb * nilai_jual * 1 # pengenaan
          pkb = tarif_pkb * bobot * nilai_jual * 1 # pengenaan
        
          vehicle["nilai_jual"] = f"Rp{int(nilai_jual):,}"
          out += "\n\n---\nDetail Kendaraan:"
          for k,v in vehicle.iloc[0].items():
            out += f"\n{k} \t\t: {v}"
          # out += "\n=================================================================="
            
          out += "\n--"

          if scores[scores_argsort[0]] < threshold:
              continue

          header_rincian = "| POKOK | DENDA | TOTAL ||\n|-:|-:|-:|:-|\n"
          rincian = "\n\n### RINCIAN:\n\n" + header_rincian

          is_rincian = False

          out_k = ""

          if scores[0] > w_avg:
              out += f"\nBBNKB \t\t: {int(bbnkb):,}"
              
              out_k += "\nRumus Bea Balik Nama Kendaraan Bermotor (BBNKB) : TARIF x NJKB x PENGENAAN"
              out_k += f"\nKalkulasi : {tarif_bbnkb*100}% x Rp{int(nilai_jual):,} x 100%"
              out_k += f"\nTotal Pembayaran : Rp{int(bbnkb):,}\n"
              rincian += f"|{int(bbnkb):,}|0|{int(bbnkb):,}|BBNKB|\n"
              is_rincian = True
          else:
              bbnkb = 0
              
          if scores[1] > w_avg:
              d = datetime.now().date() - vehicle["tg_akhir_pkb"].values[0]
              d = d.days // 365
              if d < 1:
                  d = 1
              # swdkllj = 35000
              swdkllj = dict_swdkllj.get(vehicle["gol"].values[0])

              out += f"\nPKB \t\t: {int(pkb*d):,}"
              out += f"\nSWDKLLJ \t\t: {int(swdkllj*d):,}"
              out += f"\nPNBP STNK \t\t:{int(pnbp_stnk):,}"
              out += f"\nPNBP TNKB \t\t:{int(pnbp_tnkb):,}"
              out += f"\nTOTAL \t\t: {int(pkb*d + swdkllj*d + pnbp_stnk + pnbp_tnkb + bbnkb):,}"
              
              out_k += "\nRumus Pokok Pajak Kendaraan Bermotor (PKB) : TARIF * NJKB * BOBOT * PENGENAAN * TAHUN BAYAR"
              out_k += f"\nKalkulasi : {tarif_pkb*100}% * Rp{int(nilai_jual):,} * {bobot} * 100% * {d}"
              out_k += f"\nTotal Pembayaran : Rp{int(pkb*d):,}\n"
              out_k += "\nRumus Total PKB: PKB + SWDKLLJ + PNBP STNK + PNBP TNKB"
              out_k += f"\nKalkulasi : Rp{int(pkb*d):,} + Rp{int(swdkllj*d):,} + Rp{int(pnbp_stnk):,} + Rp{int(pnbp_tnkb):,}"
              out_k += f"\nTotal Pembayaran : Rp{int(pkb*d + swdkllj*d + pnbp_stnk + pnbp_tnkb):,}\n"
              
              rincian += f"|{int(pkb*d):,}|0|{int(pkb*d):,}|PKB|\n" + f"|{int(swdkllj*d):,}|0|{int(swdkllj*d):,}|SWDKLLJ|\n\n"
              rincian_pkb = "### RINCIAN PKB:\n\n| POKOK | DENDA | TOTAL |\n|-:|-:|-:|\n"
              rincian_swdkllj = "### RINCIAN SWDKLLJ:\n\n| POKOK | DENDA | TOTAL |\n|-:|-:|-:|\n"
              for i in range(d):
                  rincian_pkb += f"|{int(pkb):,}|0|{int(pkb):,}|PKB|\n"
                  rincian_swdkllj += f"|{int(swdkllj):,}|0|{int(swdkllj):,}|PKB|\n"
              rincian += rincian_pkb + "\n" + rincian_swdkllj
              is_rincian = True
          out += out_k
          if is_rincian:
              out += rincian
    if not out and scores[scores_argsort[0]] >= threshold:
        if scores[0] > w_avg:
            out += "\nRumus Bea Balik Nama Kendaraan Bermotor (BBNKB) : TARIF x NJKB x PENGENAAN"
        if scores[1] > w_avg:
            out += "\nRumus Pokok Pajak Kendaraan Bermotor (PKB) : TARIF * NJKB * BOBOT * PENGENAAN"

    return {"results":out}