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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
|
3 |
+
import re
|
4 |
+
import inspect
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import os
|
8 |
+
|
9 |
+
from datetime import datetime
|
10 |
+
|
11 |
+
from sentence_transformers import SentenceTransformer
|
12 |
+
from sentence_transformers.util import cos_sim
|
13 |
+
from sentence_transformers import CrossEncoder
|
14 |
+
|
15 |
+
# import mysql.connector
|
16 |
+
import psycopg2
|
17 |
+
|
18 |
+
db_user = os.environ["DB_NAME"]
|
19 |
+
|
20 |
+
# mydb = mysql.connector.connect(
|
21 |
+
# host=os.environ["DB_HOST"],
|
22 |
+
# user=os.environ["DB_USER"],
|
23 |
+
# port=os.environ["DB_PORT"],
|
24 |
+
# password=os.environ["DB_PASSWORD"]
|
25 |
+
# )
|
26 |
+
|
27 |
+
mydb = psycopg2.connect(
|
28 |
+
host=os.environ["DB_HOST"],
|
29 |
+
user=os.environ["DB_USER"],
|
30 |
+
port=os.environ["DB_PORT"],
|
31 |
+
database=os.environ["DB_NAME"],
|
32 |
+
password=os.environ["DB_PASSWORD"]
|
33 |
+
)
|
34 |
+
|
35 |
+
|
36 |
+
# df_sample = pd.read_sql(f"SELECT * FROM {db_user}.sample", mydb)
|
37 |
+
# df_bobot = pd.read_sql(f"SELECT * FROM {db_user}.bobot", mydb)
|
38 |
+
|
39 |
+
df_sample = pd.read_sql(f"SELECT * FROM sample", mydb)
|
40 |
+
df_bobot = pd.read_sql(f"SELECT * FROM bobot", mydb)
|
41 |
+
|
42 |
+
df_bobot['BOBOT'] = [float(x.replace(",",".")) for x in df_bobot['BOBOT']]#.astype(float)
|
43 |
+
df_bobot["JENIS_KENDARAAN"] = [x[:-2] + x[-1] if x[-2] == "." else x for x in df_bobot["JENIS_KENDARAAN"]]
|
44 |
+
|
45 |
+
dict_swdkllj = {
|
46 |
+
"A":3000,
|
47 |
+
"B":23000,
|
48 |
+
"C1":35000,
|
49 |
+
"C2":83000,
|
50 |
+
"DP":35000,
|
51 |
+
"DU":73000,
|
52 |
+
"EP":153000,
|
53 |
+
"EU":90000,
|
54 |
+
"F":163000
|
55 |
+
}
|
56 |
+
|
57 |
+
def find_swdkllj(row):
|
58 |
+
cc = int(row["jumlah_cc"])
|
59 |
+
nm = str(row["nm_jenis_kb"]).upper()
|
60 |
+
|
61 |
+
if nm in ["AMBULANCE", "DAMKAR", "MOBIL JENAZAH"]:
|
62 |
+
return "A"
|
63 |
+
if nm in ["ALAT BERAT"]:
|
64 |
+
return "B"
|
65 |
+
if nm in ["SEDAN", "JEEP", "MINIBUS"]:
|
66 |
+
return "DP"
|
67 |
+
|
68 |
+
if "SPD. MOTOR" in nm:
|
69 |
+
if cc <= 50:
|
70 |
+
return "A"
|
71 |
+
elif cc <= 250:
|
72 |
+
return "C1"
|
73 |
+
else:
|
74 |
+
return "C2"
|
75 |
+
|
76 |
+
if "PICK UP" in nm:
|
77 |
+
if cc <= 2400:
|
78 |
+
return "DP"
|
79 |
+
|
80 |
+
df_sample["gol"] = df_sample.apply(find_swdkllj, axis=1)
|
81 |
+
|
82 |
+
|
83 |
+
df_sample["pnbp_stnk"] = df_sample["nm_jenis_kb"].apply(
|
84 |
+
lambda x: 100000 if x == "SPD. MOTOR R2" else 200000
|
85 |
+
)
|
86 |
+
|
87 |
+
df_sample["pnbp_tnkb"] = df_sample["nm_jenis_kb"].apply(
|
88 |
+
lambda x: 60000 if x == "SPD. MOTOR R2" else 100000
|
89 |
+
)
|
90 |
+
|
91 |
+
codes = """001 - Vehicle Registration (New)
|
92 |
+
002 - Vehicle Registration Renewal
|
93 |
+
003 - Vehicle Ownership Transfer
|
94 |
+
004 - Vehicle De-registration
|
95 |
+
005 - Lost Registration Certificate Replacement
|
96 |
+
006 - Address Change Update
|
97 |
+
007 - Vehicle Data Correction
|
98 |
+
008 - Ownership Name Correction
|
99 |
+
009 - Vehicle Tax Payment
|
100 |
+
010 - Late Payment Fee Processing
|
101 |
+
011 - Vehicle Type/Specification Update
|
102 |
+
012 - BBNKB (Transfer Fee of Vehicle Ownership)
|
103 |
+
013 - STNK Issuance (Vehicle Registration Certificate)
|
104 |
+
014 - STNK Renewal
|
105 |
+
015 - Motor Vehicle Roadworthiness Inspection
|
106 |
+
016 - Plate Number Renewal
|
107 |
+
017 - Lost Plate Replacement
|
108 |
+
018 - Vehicle Export Registration
|
109 |
+
019 - Vehicle Import Registration
|
110 |
+
020 - Fleet Vehicle Registration
|
111 |
+
021 - Bulk Vehicle Registration Update
|
112 |
+
022 - Vehicle Insurance Assistance
|
113 |
+
023 - Vehicle Accident Reporting
|
114 |
+
024 - Vehicle Usage Change Declaration (e.g., personal to commercial)
|
115 |
+
025 - Legal Document Verification
|
116 |
+
026 - Ownership Transfer for Inherited Vehicle
|
117 |
+
027 - STNK Temporary Suspension
|
118 |
+
028 - Proof of Ownership Document Update
|
119 |
+
029 - Vehicle Ownership History Check
|
120 |
+
030 - Vehicle Tax Recalculation Request
|
121 |
+
031 - Tax Exemption Application (for special cases)
|
122 |
+
032 - Deceased Owner’s Vehicle Ownership Transfer""".split("\n")
|
123 |
+
|
124 |
+
undetected = "099 - Other/Undetected"
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
# codes = """001 - Pendaftaran Kendaraan (Baru)
|
129 |
+
# 002 - Pembaruan Pendaftaran Kendaraan
|
130 |
+
# 003 - Alih Kepemilikan Kendaraan
|
131 |
+
# 004 - Pembatalan Pendaftaran Kendaraan
|
132 |
+
# 005 - Penggantian Sertifikat Pendaftaran Kendaraan yang Hilang
|
133 |
+
# 006 - Pembaruan Perubahan Alamat
|
134 |
+
# 007 - Koreksi Data Kendaraan
|
135 |
+
# 008 - Koreksi Nama Kepemilikan
|
136 |
+
# 009 - Pembayaran Pajak Kendaraan
|
137 |
+
# 010 - Proses Denda Keterlambatan Pembayaran
|
138 |
+
# 011 - Pembaruan Jenis/Spesifikasi Kendaraan
|
139 |
+
# 012 - Pembayaran Pajak Kendaraan Melalui E-Samsat
|
140 |
+
# 013 - Penerbitan STNK (Sertifikat Pendaftaran Kendaraan)
|
141 |
+
# 014 - Pembaruan STNK
|
142 |
+
# 015 - Pemeriksaan Kelayakan Jalan Kendaraan Bermotor
|
143 |
+
# 016 - Pembaruan Nomor Plat Kendaraan
|
144 |
+
# 017 - Penggantian Plat yang Hilang
|
145 |
+
# 018 - Pendaftaran Ekspor Kendaraan
|
146 |
+
# 019 - Pendaftaran Impor Kendaraan
|
147 |
+
# 020 - Pendaftaran Kendaraan Armada
|
148 |
+
# 021 - Pembaruan Pendaftaran Kendaraan Massal
|
149 |
+
# 022 - Bantuan Asuransi Kendaraan
|
150 |
+
# 023 - Pelaporan Kecelakaan Kendaraan
|
151 |
+
# 024 - Deklarasi Perubahan Penggunaan Kendaraan (misalnya, pribadi ke komersial)
|
152 |
+
# 025 - Verifikasi Dokumen Hukum
|
153 |
+
# 026 - Alih Kepemilikan Kendaraan Warisan
|
154 |
+
# 027 - Penangguhan Sementara STNK
|
155 |
+
# 028 - Pembaruan Dokumen Bukti Kepemilikan
|
156 |
+
# 029 - Pemeriksaan Riwayat Kepemilikan Kendaraan
|
157 |
+
# 030 - Permintaan Perhitungan Ulang Pajak Kendaraan
|
158 |
+
# 031 - Permohonan Pembebasan Pajak (untuk kasus khusus)
|
159 |
+
# 032 - Alih Kepemilikan Kendaraan Pemilik yang Meninggal""".split("\n")
|
160 |
+
|
161 |
+
codes = """001 - Pendaftaran Kendaraan
|
162 |
+
002 - Pembaruan Data Kendaraan
|
163 |
+
003 - Alih Kepemilikan atau Balik Nama Kendaraan
|
164 |
+
004 - Pelaporan Dokumen atau Plat yang Hilang
|
165 |
+
005 - Pembayaran dan Pengelolaan Pajak Kendaraan
|
166 |
+
006 - Pemeriksaan dan Verifikasi Kendaraan
|
167 |
+
007 - Pendaftaran Kendaraan Ekspor, Impor, atau Armada
|
168 |
+
008 - Pelaporan dan Bantuan Terkait Kendaraan
|
169 |
+
009 - Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan""".split("\n")
|
170 |
+
|
171 |
+
codes = """003 - Alih Kepemilikan atau Balik Nama Kendaraan
|
172 |
+
005 - Pembayaran dan Pengelolaan Pajak Kendaraan""".split("\n")
|
173 |
+
|
174 |
+
# documents = """Vehicle Registration
|
175 |
+
# Vehicle Data Update
|
176 |
+
# Vehicle Ownership or Change of Name
|
177 |
+
# Reporting of Lost Documents or Plates
|
178 |
+
# Vehicle Tax Payment and Management
|
179 |
+
# Vehicle Inspection and Verification
|
180 |
+
# Registration of Export, Import, or Fleet Vehicles
|
181 |
+
# Reporting and Assistence of Vehicles Insurance
|
182 |
+
# Suspension or Declaration of Change of Vehicle Use""".split("\n")
|
183 |
+
|
184 |
+
|
185 |
+
# examples = [
|
186 |
+
# {"code": "001", "examples": [
|
187 |
+
# "Register a new vehicle",
|
188 |
+
# "Vehicle registration application",
|
189 |
+
# "Apply for vehicle registration",
|
190 |
+
# "Submit new vehicle registration",
|
191 |
+
# "New vehicle registration request"
|
192 |
+
# ]},
|
193 |
+
# {"code": "002", "examples": [
|
194 |
+
# "Update vehicle information",
|
195 |
+
# "Change details of a registered vehicle",
|
196 |
+
# "Submit updated vehicle data",
|
197 |
+
# "Request vehicle data update",
|
198 |
+
# "Modify vehicle registration information"
|
199 |
+
# ]},
|
200 |
+
# {"code": "003", "examples": [
|
201 |
+
# "Transfer vehicle ownership",
|
202 |
+
# "Apply for vehicle name change",
|
203 |
+
# "Request ownership transfer for a vehicle",
|
204 |
+
# "Submit vehicle ownership change",
|
205 |
+
# "Process vehicle title transfer"
|
206 |
+
# ]},
|
207 |
+
# {"code": "004", "examples": [
|
208 |
+
# "Report lost vehicle documents (STNK, BPKB, etc)",
|
209 |
+
# "Declare missing license plates",
|
210 |
+
# "File a report for lost vehicle papers",
|
211 |
+
# "Lost vehicle documents reporting",
|
212 |
+
# "Inform authorities about missing vehicle documents"
|
213 |
+
# ]},
|
214 |
+
# {"code": "005", "examples": [
|
215 |
+
# "Pay vehicle tax",
|
216 |
+
# "Manage vehicle tax payments",
|
217 |
+
# "Submit vehicle tax payment",
|
218 |
+
# "Request vehicle tax management services",
|
219 |
+
# "Complete annual vehicle tax"
|
220 |
+
# ]},
|
221 |
+
# {"code": "006", "examples": [
|
222 |
+
# "Conduct vehicle inspection",
|
223 |
+
# "Request vehicle verification check",
|
224 |
+
# "Submit vehicle for verification",
|
225 |
+
# "Schedule vehicle examination",
|
226 |
+
# "Apply for vehicle inspection and verification"
|
227 |
+
# ]},
|
228 |
+
# {"code": "007", "examples": [
|
229 |
+
# "Register an imported vehicle",
|
230 |
+
# "Submit registration for export vehicle",
|
231 |
+
# "Apply for fleet vehicle registration",
|
232 |
+
# "Register new vehicle for export or import",
|
233 |
+
# "Fleet vehicle registration application"
|
234 |
+
# ]},
|
235 |
+
# {"code": "008", "examples": [
|
236 |
+
# "Report vehicle-related issues",
|
237 |
+
# "Request assistance for a vehicle problem",
|
238 |
+
# "Submit report concerning a vehicle",
|
239 |
+
# "File for vehicle-related support",
|
240 |
+
# "Seek help with vehicle concerns"
|
241 |
+
# ]},
|
242 |
+
# {"code": "009", "examples": [
|
243 |
+
# "Suspend vehicle usage",
|
244 |
+
# "Declare change of vehicle usage purpose",
|
245 |
+
# "Request a hold on vehicle usage",
|
246 |
+
# "Submit application for vehicle usage change",
|
247 |
+
# "File for suspension of vehicle operations"
|
248 |
+
# ]}
|
249 |
+
# ]
|
250 |
+
|
251 |
+
examples = [
|
252 |
+
{"code": "001", "examples": [
|
253 |
+
"Daftarkan kendaraan baru",
|
254 |
+
"Pengajuan pendaftaran kendaraan",
|
255 |
+
"Ajukan pendaftaran kendaraan",
|
256 |
+
"Serahkan pendaftaran kendaraan baru",
|
257 |
+
"Permintaan pendaftaran kendaraan baru"
|
258 |
+
]},
|
259 |
+
{"code": "002", "examples": [
|
260 |
+
"Perbarui informasi kendaraan",
|
261 |
+
"Ubah detail kendaraan yang terdaftar",
|
262 |
+
"Ajukan pembaruan data kendaraan",
|
263 |
+
"Permintaan pembaruan data kendaraan",
|
264 |
+
"Modifikasi informasi pendaftaran kendaraan"
|
265 |
+
]},
|
266 |
+
{"code": "003", "examples": [
|
267 |
+
"Alihkan kepemilikan kendaraan",
|
268 |
+
"Ajukan perubahan nama kendaraan",
|
269 |
+
"Permintaan pengalihan kepemilikan kendaraan",
|
270 |
+
"Serahkan perubahan kepemilikan kendaraan",
|
271 |
+
"Proses alih nama kendaraan"
|
272 |
+
]},
|
273 |
+
{"code": "004", "examples": [
|
274 |
+
"Laporkan dokumen kendaraan hilang (STNK, BPKB, dll)",
|
275 |
+
"Nyatakan plat nomor hilang",
|
276 |
+
"Ajukan laporan dokumen kendaraan hilang",
|
277 |
+
"Pelaporan dokumen kendaraan yang hilang",
|
278 |
+
"Informasikan pihak berwenang tentang dokumen kendaraan yang hilang"
|
279 |
+
]},
|
280 |
+
{"code": "005", "examples": [
|
281 |
+
"Bayar pajak kendaraan",
|
282 |
+
"Kelola pembayaran pajak kendaraan",
|
283 |
+
"Ajukan pembayaran pajak kendaraan",
|
284 |
+
"Permintaan layanan manajemen pajak kendaraan",
|
285 |
+
"Selesaikan pajak tahunan kendaraan"
|
286 |
+
]},
|
287 |
+
{"code": "006", "examples": [
|
288 |
+
"Lakukan pemeriksaan kendaraan",
|
289 |
+
"Ajukan pemeriksaan verifikasi kendaraan",
|
290 |
+
"Serahkan kendaraan untuk verifikasi",
|
291 |
+
"Jadwalkan pemeriksaan kendaraan",
|
292 |
+
"Ajukan inspeksi dan verifikasi kendaraan"
|
293 |
+
]},
|
294 |
+
{"code": "007", "examples": [
|
295 |
+
"Daftarkan kendaraan impor",
|
296 |
+
"Ajukan pendaftaran kendaraan ekspor",
|
297 |
+
"Ajukan pendaftaran kendaraan armada",
|
298 |
+
"Daftarkan kendaraan baru untuk ekspor atau impor",
|
299 |
+
"Pengajuan pendaftaran kendaraan armada"
|
300 |
+
]},
|
301 |
+
{"code": "008", "examples": [
|
302 |
+
"Laporkan masalah terkait kendaraan",
|
303 |
+
"Permintaan bantuan untuk masalah kendaraan",
|
304 |
+
"Ajukan laporan mengenai kendaraan",
|
305 |
+
"Ajukan dukungan terkait kendaraan",
|
306 |
+
"Cari bantuan terkait permasalahan kendaraan"
|
307 |
+
]},
|
308 |
+
{"code": "009", "examples": [
|
309 |
+
"Tangguhkan penggunaan kendaraan",
|
310 |
+
"Nyatakan perubahan tujuan penggunaan kendaraan",
|
311 |
+
"Ajukan penangguhan penggunaan kendaraan",
|
312 |
+
"Ajukan perubahan penggunaan kendaraan",
|
313 |
+
"Ajukan penangguhan operasional kendaraan"
|
314 |
+
]}
|
315 |
+
]
|
316 |
+
|
317 |
+
|
318 |
+
|
319 |
+
# examples = [
|
320 |
+
# {"code": "001", "examples": [
|
321 |
+
# codes[0][6:],
|
322 |
+
# "Pendaftaran kendaraan",
|
323 |
+
# "Daftar baru untuk plat kendaraan",
|
324 |
+
# "Registrasi kendaraan"
|
325 |
+
# ]},
|
326 |
+
# {"code": "002", "examples": [
|
327 |
+
# codes[1][6:],
|
328 |
+
# "Perubahan alamat, warna, komponnen kendaraan",
|
329 |
+
# "Update informasi kendaraan",
|
330 |
+
# "Perubahan data pemilik",
|
331 |
+
# ]},
|
332 |
+
# {"code": "003", "examples": [
|
333 |
+
# codes[2][6:],
|
334 |
+
# "Alih kepemilikan kendaraan",
|
335 |
+
# "Balik nama untuk kendaraan",
|
336 |
+
# "Perubahan pemilik pada kendaraan"
|
337 |
+
# ]},
|
338 |
+
# {"code": "004", "examples": [
|
339 |
+
# codes[3][6:],
|
340 |
+
# "Laporan kehilangan dokumen kendaraan",
|
341 |
+
# "Hilangnya plat nomor kendaraan",
|
342 |
+
# "Dokumen kendaraan dilaporkan hilang"
|
343 |
+
# ]},
|
344 |
+
# {"code": "005", "examples": [
|
345 |
+
# codes[4][6:],
|
346 |
+
# "Pembayaran pajak kendaraan",
|
347 |
+
# "Kelola pajak tahunan kendaraan",
|
348 |
+
# "Pajak kendaraan"
|
349 |
+
# ]},
|
350 |
+
# {"code": "006", "examples": [
|
351 |
+
# codes[5][6:],
|
352 |
+
# "Pemeriksaan fisik kendaraan",
|
353 |
+
# "Verifikasi kendaraan",
|
354 |
+
# "Cek kondisi kendaraan"
|
355 |
+
# ]},
|
356 |
+
# {"code": "007", "examples": [
|
357 |
+
# codes[6][6:],
|
358 |
+
# "Pendaftaran kendaraan impor",
|
359 |
+
# "Registrasi armada baru",
|
360 |
+
# "Kendaraan ekspor didaftarkan"
|
361 |
+
# ]},
|
362 |
+
# {"code": "008", "examples": [
|
363 |
+
# codes[7][6:],
|
364 |
+
# "Pelaporan terkait kendaraan",
|
365 |
+
# "Bantuan atau Asuransi atas kendaraan",
|
366 |
+
# "Laporkan masalah kendaraan"
|
367 |
+
# ]},
|
368 |
+
# {"code": "009", "examples": [
|
369 |
+
# codes[8][6:],
|
370 |
+
# "Penangguhan penggunaan kendaraan",
|
371 |
+
# "Deklarasi perubahan fungsi kendaraan",
|
372 |
+
# "Penggunaan baru untuk kendaraan"
|
373 |
+
# ]}
|
374 |
+
# ]
|
375 |
+
|
376 |
+
|
377 |
+
vehicle_tax_info = {
|
378 |
+
"B 1234 BCA": {
|
379 |
+
"no_rangka": "1237191234",
|
380 |
+
"type": "SUV",
|
381 |
+
"tanggal": "23 Desember 2024",
|
382 |
+
"status": "Belum Bayar",
|
383 |
+
"harga_jual": 500_000_000 # In Rupiah
|
384 |
+
},
|
385 |
+
"B 5678 XYZ": {
|
386 |
+
"no_rangka": "9876543210",
|
387 |
+
"type": "Sedan",
|
388 |
+
"tanggal": "15 Januari 2025",
|
389 |
+
"status": "Belum Bayar",
|
390 |
+
"harga_jual": 375_800_000 # In Rupiah
|
391 |
+
},
|
392 |
+
"D 3456 DEF": {
|
393 |
+
"no_rangka": "4561237890",
|
394 |
+
"type": "MPV",
|
395 |
+
"tanggal": "10 Februari 2025",
|
396 |
+
"status": "Sudah Bayar",
|
397 |
+
"harga_jual": 400_000_000 # In Rupiah
|
398 |
+
}
|
399 |
+
}
|
400 |
+
|
401 |
+
# Table for detail calculations (perhitungan)
|
402 |
+
detail_perhitungan = {
|
403 |
+
"001": {
|
404 |
+
"name": "Pendaftaran Kendaraan",
|
405 |
+
"formula": lambda harga_jual: harga_jual * 0.1,
|
406 |
+
# Example formula: 10% of harga_jual
|
407 |
+
},
|
408 |
+
"002": {
|
409 |
+
"name": "Pembaruan Data Kendaraan",
|
410 |
+
"formula": lambda harga_jual: harga_jual * 0.05,
|
411 |
+
# Example formula: 5% of harga_jual
|
412 |
+
},
|
413 |
+
"003": {
|
414 |
+
"name": "Alih Kepemilikan (Balik Nama) Kendaraan",
|
415 |
+
"formula": lambda harga_jual: harga_jual * 0.1,
|
416 |
+
# Example formula: 10% of harga_jual
|
417 |
+
},
|
418 |
+
"004": {
|
419 |
+
"name": "Penggantian Dokumen atau Plat yang Hilang",
|
420 |
+
"formula": lambda harga_jual: harga_jual * 0.03,
|
421 |
+
# Example formula: 3% of harga_jual
|
422 |
+
},
|
423 |
+
"005": {
|
424 |
+
"name": "Pembayaran dan Pengelolaan Pajak Kendaraan",
|
425 |
+
"formula": lambda harga_jual: harga_jual * 0.12,
|
426 |
+
# Example formula: 12% of harga_jual
|
427 |
+
},
|
428 |
+
"006": {
|
429 |
+
"name": "Pemeriksaan dan Verifikasi Kendaraan",
|
430 |
+
"formula": lambda harga_jual: 100000,
|
431 |
+
# Example formula: 2% of harga_jual
|
432 |
+
},
|
433 |
+
"007": {
|
434 |
+
"name": "Pendaftaran Kendaraan Ekspor, Impor, atau Armada",
|
435 |
+
"formula": lambda harga_jual: harga_jual * 0.15,
|
436 |
+
# Example formula: 15% of harga_jual
|
437 |
+
},
|
438 |
+
"008": {
|
439 |
+
"name": "Pelaporan dan Bantuan Terkait Kendaraan",
|
440 |
+
"formula": lambda harga_jual: harga_jual * 0.04,
|
441 |
+
# Example formula: 4% of harga_jual
|
442 |
+
},
|
443 |
+
"009": {
|
444 |
+
"name": "Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan",
|
445 |
+
"formula": lambda harga_jual: harga_jual * 0.06,
|
446 |
+
# Example formula: 6% of harga_jual
|
447 |
+
}
|
448 |
+
}
|
449 |
+
|
450 |
+
undetected = "099 - Lainnya/Tidak Terdeteksi"
|
451 |
+
|
452 |
+
model_ids = [
|
453 |
+
"BAAI/bge-m3",
|
454 |
+
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
455 |
+
"intfloat/multilingual-e5-small",
|
456 |
+
"sentence-transformers/distiluse-base-multilingual-cased-v2",
|
457 |
+
"Alibaba-NLP/gte-multilingual-base",
|
458 |
+
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
|
459 |
+
"jinaai/jina-reranker-v2-base-multilingual",
|
460 |
+
"BAAI/bge-reranker-v2-m3",
|
461 |
+
]
|
462 |
+
# model_id = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
463 |
+
# model_id = "Alibaba-NLP/gte-multilingual-base"
|
464 |
+
# model_id = "BAAI/bge-m3"
|
465 |
+
# model_id = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
466 |
+
# model_id = "intfloat/multilingual-e5-small"
|
467 |
+
# model_id = "sentence-transformers/distiluse-base-multilingual-cased-v2"
|
468 |
+
|
469 |
+
model_id = model_ids[-1]
|
470 |
+
model = None
|
471 |
+
codes_emb = None
|
472 |
+
|
473 |
+
def load_model(model_id):
|
474 |
+
# global model
|
475 |
+
# global codes_emb
|
476 |
+
if model_id in model_ids[-2:]:
|
477 |
+
model = CrossEncoder(
|
478 |
+
# "jinaai/jina-reranker-v2-base-multilingual",
|
479 |
+
# "BAAI/bge-reranker-v2-m3",
|
480 |
+
model_id,
|
481 |
+
automodel_args={"torch_dtype": "auto"},
|
482 |
+
trust_remote_code=True,
|
483 |
+
)
|
484 |
+
return model, None
|
485 |
+
else:
|
486 |
+
model = SentenceTransformer(model_id, trust_remote_code=True)
|
487 |
+
codes_emb = model.encode([x[6:] for x in codes])
|
488 |
+
# codes_emb = model.encode([x["examples"] for x in examples])#.mean(axis=1)
|
489 |
+
# codes_emb = np.mean([model.encode(x["examples"]) for x in examples], axis=1)
|
490 |
+
return model, codes_emb
|
491 |
+
|
492 |
+
model, codes_emb = load_model(model_id)
|
493 |
+
|
494 |
+
# for x in examples:
|
495 |
+
# codes_emb.append(model.encode(x["examples"]))
|
496 |
+
# codes_emb = np.mean(codes_emb, axis=1)
|
497 |
+
|
498 |
+
|
499 |
+
def censor_middle(number, num_to_hide=4):
|
500 |
+
number_str = str(number)
|
501 |
+
if num_to_hide == -1:
|
502 |
+
num_to_hide = int(len(number_str)*0.8)
|
503 |
+
middle_index = len(number_str) // 2
|
504 |
+
start_index = middle_index - num_to_hide // 2
|
505 |
+
end_index = middle_index + num_to_hide // 2
|
506 |
+
|
507 |
+
censored_part = "\*" * num_to_hide
|
508 |
+
censored_number = number_str[:start_index] + censored_part + number_str[end_index:]
|
509 |
+
|
510 |
+
return censored_number
|
511 |
+
|
512 |
+
|
513 |
+
def get_calculation(request_code, plate_number):
|
514 |
+
print(request_code, plate_number, "GET CALC")
|
515 |
+
calc = detail_perhitungan.get(request_code)
|
516 |
+
vehicle = vehicle_tax_info.get(plate_number)
|
517 |
+
|
518 |
+
if vehicle != None and calc != None:
|
519 |
+
harga_jual = vehicle.get("harga_jual")
|
520 |
+
formula = calc.get("formula")
|
521 |
+
result = formula(harga_jual)
|
522 |
+
description = inspect.getsource(formula).split(":", 2)[-1].strip()
|
523 |
+
result_detail = request_code + " - " + calc.get("name")
|
524 |
+
return result, str(description), result_detail
|
525 |
+
|
526 |
+
elif calc != None:
|
527 |
+
formula = calc.get("formula")
|
528 |
+
description = inspect.getsource(formula).split(":", 2)[-1].strip()
|
529 |
+
result_detail = request_code + " - " + calc.get("name")
|
530 |
+
return None, str(description), result_detail
|
531 |
+
|
532 |
+
else:
|
533 |
+
return None, None, None
|
534 |
+
|
535 |
+
|
536 |
+
def build_output_formula(descriptions, result_details):
|
537 |
+
out = "----------------------------------------------------\n\n"
|
538 |
+
out = "Daftar Kode Permohonan:\n"
|
539 |
+
for i, (desc,detail) in enumerate(zip(descriptions, result_details)):
|
540 |
+
# harga_jual = str(vehicle.get("harga_jual"))
|
541 |
+
out += f"{i+1}. {detail}\nRumus: {desc}\n"
|
542 |
+
return out
|
543 |
+
|
544 |
+
|
545 |
+
def build_output_vehicle(plate_number):
|
546 |
+
vehicle = vehicle_tax_info.get(plate_number).copy()
|
547 |
+
out = "----------------------------------------------------\n\n"
|
548 |
+
out = "Nomor Polisi: " + plate_number + "\n"
|
549 |
+
vehicle["no_rangka"] = censor_middle(vehicle["no_rangka"])
|
550 |
+
vehicle["harga_jual"] = "Rp{:,}".format(vehicle["harga_jual"])
|
551 |
+
out += "\n".join([k + " : " + str(v) for k,v in vehicle.items()])
|
552 |
+
return out
|
553 |
+
|
554 |
+
def build_output(result, description, result_detail, plate_number):
|
555 |
+
return build_outputs([result], [description], [result_detail], plate_number)
|
556 |
+
|
557 |
+
|
558 |
+
def build_outputs(results, descriptions, result_details, plate_number):
|
559 |
+
vehicle = vehicle_tax_info.get(plate_number).copy()
|
560 |
+
vehicle["harga_jual"] = "Rp{:,}".format(vehicle["harga_jual"])
|
561 |
+
|
562 |
+
out = "----------------------------------------------------\n\n"
|
563 |
+
out = "Nomor Polisi: " + plate_number + "\n"
|
564 |
+
out += "\n".join([k + " : " + str(v) if k != "no_rangka" else k + " : " + censor_middle(v) for k,v in vehicle.items()])
|
565 |
+
# out += "\n----------------------------------------------"
|
566 |
+
|
567 |
+
# out += f"\nWajib Pajak dengan NoPol {plate_number} ingin melakukan proses berikut:\n"
|
568 |
+
out += "\n\nDaftar Kode Permohonan:\n"
|
569 |
+
|
570 |
+
for i, (res,desc,detail) in enumerate(zip(results, descriptions, result_details)):
|
571 |
+
harga_jual = vehicle["harga_jual"]
|
572 |
+
res_str = "{:,}".format(res)
|
573 |
+
out += f"{i+1}. {detail}\nRumus: {desc}\nDetail perhitungan: {desc.replace('harga_jual', harga_jual)} = Rp{res_str}\n"
|
574 |
+
|
575 |
+
# out += "----------------------------\nEstimasi biaya: "
|
576 |
+
out += "\n\n\nEstimasi Biaya: "
|
577 |
+
|
578 |
+
if len(results) > 1:
|
579 |
+
out += " + ".join(["Rp{:,}".format(x) for x in results])
|
580 |
+
out += " = Rp{:,}".format(sum(results))
|
581 |
+
else:
|
582 |
+
out += "Rp{:,}".format(results[0])
|
583 |
+
|
584 |
+
out += "\n\n----------------------------"
|
585 |
+
# out += "\n----------------------------\n\n--------------------------------"
|
586 |
+
return out
|
587 |
+
|
588 |
+
|
589 |
app = FastAPI()
|
590 |
|
591 |
@app.get("/")
|
592 |
def greet_json():
|
593 |
+
global codes_emb
|
594 |
+
global undetected
|
595 |
+
|
596 |
+
undetected_code = undetected[:3]
|
597 |
+
|
598 |
+
# if history and history[-1][-1][21:24] == undetected_code:
|
599 |
+
# list_his = ""
|
600 |
+
# for his in history[::-1]:
|
601 |
+
# if his[-1][21:24] != undetected_code:
|
602 |
+
# break
|
603 |
+
# list_his = his[0] + "\n" + list_his
|
604 |
+
|
605 |
+
# message += "\n" + list_his
|
606 |
+
|
607 |
+
# pattern = r'\b([A-Z]{1,2})\s?(\d{4})\s?([A-Z]{3})\b'
|
608 |
+
# pattern = r'\b([A-Z]{1,2})\s?(\d{4})\s?([A-Z]{1,3})\b'
|
609 |
+
pattern = r'\b([A-Za-z]{1,2})\s?(\d{4})\s?([A-Za-z]{1,3})\b'
|
610 |
+
|
611 |
+
matches = re.findall(pattern, message)
|
612 |
+
|
613 |
+
plates = [" ".join(x).upper() for i,x in enumerate(matches)]
|
614 |
+
|
615 |
+
plate_numbers = ", ".join(plates)
|
616 |
+
|
617 |
+
# if model.config._name_or_path in model_ids[-2:]:
|
618 |
+
if type(model) == CrossEncoder:
|
619 |
+
# documents = [v["name"] for v in detail_perhitungan.values()]
|
620 |
+
sentence_pairs = [[message, v["name"]] for v in detail_perhitungan.values()]
|
621 |
+
# sentence_pairs = [[message, doc] for doc in documents]
|
622 |
+
scores = model.predict(sentence_pairs, convert_to_tensor=True)
|
623 |
+
weights = [1,1,1,1,1,1,1,1,1]
|
624 |
+
# scores = [x["score"] for x in model.rank(message, documents)]
|
625 |
+
else:
|
626 |
+
text_emb = model.encode(message)
|
627 |
+
scores = cos_sim(codes_emb, text_emb).mean(axis=-1)#[:,0]
|
628 |
+
weights = [19,8,7,6,5,4,3,2,1]
|
629 |
+
|
630 |
+
scores_argsort = scores.argsort(descending=True)
|
631 |
+
|
632 |
+
|
633 |
+
# if n_num == 0:
|
634 |
+
# std = scores.std()
|
635 |
+
# else:
|
636 |
+
# std = 0
|
637 |
+
if n_num == 0:
|
638 |
+
# w_avg = np.average(scores[scores_argsort].numpy(), weights=range(len(scores),0,-1))
|
639 |
+
w_avg = np.average(scores[scores_argsort].numpy(), weights=weights)
|
640 |
+
else:
|
641 |
+
w_avg = 9999
|
642 |
+
#[::-1]
|
643 |
+
|
644 |
+
if is_multiple:
|
645 |
+
request_details = []
|
646 |
+
request_numbers = []
|
647 |
+
request_scores = []
|
648 |
+
# request_undetected = False
|
649 |
+
# for i,score in enumerate(scores):
|
650 |
+
if scores[scores_argsort[0]] < threshold:
|
651 |
+
request_details.append(undetected[6:])
|
652 |
+
request_numbers.append(undetected_code)
|
653 |
+
else:
|
654 |
+
for i in scores_argsort:
|
655 |
+
# if scores[scores_argsort[0]] - std <= scores[i]:
|
656 |
+
if scores[i] > w_avg:
|
657 |
+
request_details.append(codes[i][6:])
|
658 |
+
request_numbers.append(codes[i][:3])
|
659 |
+
request_scores.append(str( round(scores[i].tolist(), 3) ) )
|
660 |
+
else:
|
661 |
+
if len(request_scores) >= n_num:
|
662 |
+
break
|
663 |
+
if scores[i] > threshold:
|
664 |
+
request_details.append(codes[i][6:])
|
665 |
+
request_numbers.append(codes[i][:3])
|
666 |
+
request_scores.append(str( round(scores[i].tolist(), 3) ) )
|
667 |
+
|
668 |
+
|
669 |
+
request_numbers_copy = request_numbers
|
670 |
+
request_details_copy = request_details
|
671 |
+
|
672 |
+
request_numbers = "\n".join(request_numbers)
|
673 |
+
request_details = "\n".join(request_details)
|
674 |
+
request_scores = "\n".join(request_scores)
|
675 |
+
|
676 |
+
# if len(request_numbers_copy) > 0:
|
677 |
+
# for code, detail in zip(request_numbers_copy, request_details_copy):
|
678 |
+
# kode_mohon = detail_perhitungan.get(code)
|
679 |
+
# formula = kode_mohon.get("formula")
|
680 |
+
# description = inspect.getsource(formula).split(":", 2)[-1].strip()
|
681 |
+
|
682 |
+
# if request_undetected and len(plates) == 0:
|
683 |
+
# + f"\n\nConfidence score:\n{request_scores}"
|
684 |
+
# return "Request code number:\n" + request_numbers + "\n\nRequest detail:\n" + request_details + "\n\nPlate numbers: " + plate_numbers
|
685 |
+
print(request_scores)
|
686 |
+
out = ""
|
687 |
+
for plate in plates:
|
688 |
+
results, descriptions, result_details = [], [], []
|
689 |
+
for code in request_numbers_copy:
|
690 |
+
result, description, result_detail = get_calculation(code, plate)
|
691 |
+
|
692 |
+
if result != None:
|
693 |
+
results.append(result)
|
694 |
+
if descriptions != None:
|
695 |
+
descriptions.append(description)
|
696 |
+
result_details.append(result_detail)
|
697 |
+
|
698 |
+
if results:
|
699 |
+
out += "\n\n" + build_outputs(results, descriptions, result_details, plate)
|
700 |
+
elif vehicle_tax_info.get(plate):
|
701 |
+
out += "\n\n" + build_output_vehicle(plate)
|
702 |
+
|
703 |
+
if out == "":
|
704 |
+
descriptions, result_details = [], []
|
705 |
+
for code in request_numbers_copy:
|
706 |
+
result, description, result_detail = get_calculation(code, "")
|
707 |
+
if description != None:
|
708 |
+
descriptions.append(description)
|
709 |
+
result_details.append(result_detail)
|
710 |
+
if descriptions:
|
711 |
+
out += "\n\n" + build_output_formula(descriptions, result_details)
|
712 |
+
else:
|
713 |
+
return "Request code number: " + request_numbers + "\nRequest detail: " + request_details + "\nPlate numbers: " + plate_numbers
|
714 |
+
|
715 |
+
return {"results":out}
|
716 |
+
|
717 |
+
# result, description, result_detailget_calculation(request_code, plate_number)
|
718 |
+
|
719 |
+
|
720 |
+
|
721 |
+
s_max = scores.argmax()
|
722 |
+
|
723 |
+
if scores[s_max] < threshold:
|
724 |
+
# request_code = "033 - Other/Undetected"
|
725 |
+
request_code = undetected
|
726 |
+
else:
|
727 |
+
request_code = codes[scores.argmax()]
|
728 |
+
# "{:.2f}".format(a)
|
729 |
+
|
730 |
+
# out = "Request code number: " + request_code[:3] + "\nRequest detail: " + request_code[6:] + f"\nConfidence score: {round(scores[s_max].tolist(),3)}" + "\nPlate numbers: " + plate_numbers
|
731 |
+
|
732 |
+
out = ""
|
733 |
+
for plate in plates:
|
734 |
+
results, descriptions, result_details = [], [], []
|
735 |
+
result, description, result_detail = get_calculation(request_code[:3], plate)
|
736 |
+
|
737 |
+
if result != None:
|
738 |
+
results.append(result)
|
739 |
+
descriptions.append(description)
|
740 |
+
result_details.append(result_detail)
|
741 |
+
out += "\n\n" + build_outputs(results, descriptions, result_details, plate)
|
742 |
+
elif vehicle_tax_info.get(plate):
|
743 |
+
out += "\n\n" + build_output_vehicle(plate)
|
744 |
+
elif description != None:
|
745 |
+
descriptions.append(description)
|
746 |
+
result_details.append(result_detail)
|
747 |
+
out += "\n\n" + build_output_formula(descriptions, result_details)
|
748 |
+
# else:
|
749 |
+
# # + f"\nConfidence score: {round(scores[s_max].tolist(),3)}"
|
750 |
+
# out += "Request code number: " + request_code[:3] + "\nRequest detail: " + "\nPlate numbers: " + plate_numbers
|
751 |
+
|
752 |
+
if out == "":
|
753 |
+
if request_code[:3] == undetected_code:
|
754 |
+
return "Request code number: " + request_code[:3] + "\nRequest detail: " + request_code[6:] + "\nPlate numbers: " + plate_numbers
|
755 |
+
else:
|
756 |
+
result, description, result_detail = get_calculation(request_code[:3], "")
|
757 |
+
out += build_output_formula([description], [result_detail])
|
758 |
+
|
759 |
+
return {"results":out}
|