Spaces:
Sleeping
Sleeping
jonathanjordan21
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import re
|
|
|
3 |
|
4 |
from sentence_transformers import SentenceTransformer
|
5 |
from sentence_transformers.util import cos_sim
|
@@ -41,48 +42,123 @@ codes = """001 - Vehicle Registration (New)
|
|
41 |
undetected = "099 - Other/Undetected"
|
42 |
|
43 |
|
44 |
-
codes = """001 - Pendaftaran Kendaraan (Baru)
|
45 |
-
002 - Pembaruan Pendaftaran Kendaraan
|
46 |
-
003 - Alih Kepemilikan Kendaraan
|
47 |
-
004 - Pembatalan Pendaftaran Kendaraan
|
48 |
-
005 - Penggantian Sertifikat Pendaftaran Kendaraan yang Hilang
|
49 |
-
006 - Pembaruan Perubahan Alamat
|
50 |
-
007 - Koreksi Data Kendaraan
|
51 |
-
008 - Koreksi Nama Kepemilikan
|
52 |
-
009 - Pembayaran Pajak Kendaraan
|
53 |
-
010 - Proses Denda Keterlambatan Pembayaran
|
54 |
-
011 - Pembaruan Jenis/Spesifikasi Kendaraan
|
55 |
-
012 - Pembayaran Pajak Kendaraan Melalui E-Samsat
|
56 |
-
013 - Penerbitan STNK (Sertifikat Pendaftaran Kendaraan)
|
57 |
-
014 - Pembaruan STNK
|
58 |
-
015 - Pemeriksaan Kelayakan Jalan Kendaraan Bermotor
|
59 |
-
016 - Pembaruan Nomor Plat Kendaraan
|
60 |
-
017 - Penggantian Plat yang Hilang
|
61 |
-
018 - Pendaftaran Ekspor Kendaraan
|
62 |
-
019 - Pendaftaran Impor Kendaraan
|
63 |
-
020 - Pendaftaran Kendaraan Armada
|
64 |
-
021 - Pembaruan Pendaftaran Kendaraan Massal
|
65 |
-
022 - Bantuan Asuransi Kendaraan
|
66 |
-
023 - Pelaporan Kecelakaan Kendaraan
|
67 |
-
024 - Deklarasi Perubahan Penggunaan Kendaraan (misalnya, pribadi ke komersial)
|
68 |
-
025 - Verifikasi Dokumen Hukum
|
69 |
-
026 - Alih Kepemilikan Kendaraan Warisan
|
70 |
-
027 - Penangguhan Sementara STNK
|
71 |
-
028 - Pembaruan Dokumen Bukti Kepemilikan
|
72 |
-
029 - Pemeriksaan Riwayat Kepemilikan Kendaraan
|
73 |
-
030 - Permintaan Perhitungan Ulang Pajak Kendaraan
|
74 |
-
031 - Permohonan Pembebasan Pajak (untuk kasus khusus)
|
75 |
-
032 - Alih Kepemilikan Kendaraan Pemilik yang Meninggal""".split("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
undetected = "099 - Lainnya/Tidak Terdeteksi"
|
78 |
|
79 |
model_ids = [
|
80 |
-
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
|
81 |
"Alibaba-NLP/gte-multilingual-base",
|
82 |
"BAAI/bge-m3",
|
83 |
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
84 |
"intfloat/multilingual-e5-small",
|
85 |
-
"sentence-transformers/distiluse-base-multilingual-cased-v2"
|
|
|
86 |
]
|
87 |
# model_id = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
88 |
# model_id = "Alibaba-NLP/gte-multilingual-base"
|
@@ -95,6 +171,48 @@ model = SentenceTransformer(model_id, trust_remote_code=True)
|
|
95 |
|
96 |
codes_emb = model.encode([x[6:] for x in codes])
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def respond(
|
99 |
message,
|
100 |
history: list[tuple[str, str]],
|
@@ -121,7 +239,9 @@ def respond(
|
|
121 |
|
122 |
matches = re.findall(pattern, message)
|
123 |
|
124 |
-
|
|
|
|
|
125 |
|
126 |
text_emb = model.encode(message)
|
127 |
scores = cos_sim(codes_emb, text_emb)[:,0]
|
@@ -143,8 +263,25 @@ def respond(
|
|
143 |
request_numbers = "\n".join(request_numbers)
|
144 |
request_details = "\n".join(request_details)
|
145 |
request_scores = "\n".join(request_scores)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
return "Request code number:\n" + request_numbers + "\n\nRequest detail:\n" + request_details + f"\n\nConfidence score:\n{request_scores}" + "\n\nPlate numbers: " + plate_numbers
|
148 |
|
149 |
|
150 |
s_max = scores.argmax()
|
@@ -156,8 +293,23 @@ def respond(
|
|
156 |
request_code = codes[scores.argmax()]
|
157 |
# "{:.2f}".format(a)
|
158 |
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
# for val in history:
|
162 |
# if val[0]:
|
163 |
# messages.append({"role": "user", "content": val[0]})
|
@@ -209,6 +361,8 @@ with gr.Blocks() as demo:
|
|
209 |
gr.Markdown("<br>".join(codes) + "<br>" + undetected)
|
210 |
gr.Markdown("# Valid License Plate Number Criteria:")
|
211 |
gr.Markdown("(1-2 letters) (4 numbers) (1-3 letters)")
|
|
|
|
|
212 |
|
213 |
|
214 |
gr.Markdown("# Choose & Load Model:")
|
|
|
1 |
import gradio as gr
|
2 |
import re
|
3 |
+
import inspect
|
4 |
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
from sentence_transformers.util import cos_sim
|
|
|
42 |
undetected = "099 - Other/Undetected"
|
43 |
|
44 |
|
45 |
+
# codes = """001 - Pendaftaran Kendaraan (Baru)
|
46 |
+
# 002 - Pembaruan Pendaftaran Kendaraan
|
47 |
+
# 003 - Alih Kepemilikan Kendaraan
|
48 |
+
# 004 - Pembatalan Pendaftaran Kendaraan
|
49 |
+
# 005 - Penggantian Sertifikat Pendaftaran Kendaraan yang Hilang
|
50 |
+
# 006 - Pembaruan Perubahan Alamat
|
51 |
+
# 007 - Koreksi Data Kendaraan
|
52 |
+
# 008 - Koreksi Nama Kepemilikan
|
53 |
+
# 009 - Pembayaran Pajak Kendaraan
|
54 |
+
# 010 - Proses Denda Keterlambatan Pembayaran
|
55 |
+
# 011 - Pembaruan Jenis/Spesifikasi Kendaraan
|
56 |
+
# 012 - Pembayaran Pajak Kendaraan Melalui E-Samsat
|
57 |
+
# 013 - Penerbitan STNK (Sertifikat Pendaftaran Kendaraan)
|
58 |
+
# 014 - Pembaruan STNK
|
59 |
+
# 015 - Pemeriksaan Kelayakan Jalan Kendaraan Bermotor
|
60 |
+
# 016 - Pembaruan Nomor Plat Kendaraan
|
61 |
+
# 017 - Penggantian Plat yang Hilang
|
62 |
+
# 018 - Pendaftaran Ekspor Kendaraan
|
63 |
+
# 019 - Pendaftaran Impor Kendaraan
|
64 |
+
# 020 - Pendaftaran Kendaraan Armada
|
65 |
+
# 021 - Pembaruan Pendaftaran Kendaraan Massal
|
66 |
+
# 022 - Bantuan Asuransi Kendaraan
|
67 |
+
# 023 - Pelaporan Kecelakaan Kendaraan
|
68 |
+
# 024 - Deklarasi Perubahan Penggunaan Kendaraan (misalnya, pribadi ke komersial)
|
69 |
+
# 025 - Verifikasi Dokumen Hukum
|
70 |
+
# 026 - Alih Kepemilikan Kendaraan Warisan
|
71 |
+
# 027 - Penangguhan Sementara STNK
|
72 |
+
# 028 - Pembaruan Dokumen Bukti Kepemilikan
|
73 |
+
# 029 - Pemeriksaan Riwayat Kepemilikan Kendaraan
|
74 |
+
# 030 - Permintaan Perhitungan Ulang Pajak Kendaraan
|
75 |
+
# 031 - Permohonan Pembebasan Pajak (untuk kasus khusus)
|
76 |
+
# 032 - Alih Kepemilikan Kendaraan Pemilik yang Meninggal""".split("\n")
|
77 |
+
|
78 |
+
codes = """001 - Pendaftaran Kendaraan
|
79 |
+
002 - Pembaruan Data Kendaraan
|
80 |
+
003 - Alih atau Pembatalan Kepemilikan
|
81 |
+
004 - Penggantian Dokumen atau Plat yang Hilang
|
82 |
+
005 - Pembayaran dan Pengelolaan Pajak Kendaraan
|
83 |
+
006 - Pemeriksaan dan Verifikasi Kendaraan
|
84 |
+
007 - Pendaftaran Kendaraan Ekspor, Impor, atau Armada
|
85 |
+
008 - Pelaporan dan Bantuan Terkait Kendaraan
|
86 |
+
009 - Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan"""
|
87 |
+
|
88 |
+
|
89 |
+
vehicle_tax_info = {
|
90 |
+
"B 1234 BCA": {
|
91 |
+
"no_rangka": "1237191234",
|
92 |
+
"type": "SUV",
|
93 |
+
"tanggal": "23 Desember 2024",
|
94 |
+
"status": "Belum Bayar",
|
95 |
+
"harga_jual": 150000 # In Rupiah
|
96 |
+
},
|
97 |
+
"B 5678 XYZ": {
|
98 |
+
"no_rangka": "9876543210",
|
99 |
+
"type": "Sedan",
|
100 |
+
"tanggal": "15 Januari 2025",
|
101 |
+
"status": "Belum Bayar",
|
102 |
+
"harga_jual": 300000 # In Rupiah
|
103 |
+
},
|
104 |
+
"D 3456 DEF": {
|
105 |
+
"no_rangka": "4561237890",
|
106 |
+
"type": "MPV",
|
107 |
+
"tanggal": "10 Februari 2025",
|
108 |
+
"status": "Sudah Bayar",
|
109 |
+
"harga_jual": 250000 # In Rupiah
|
110 |
+
}
|
111 |
+
}
|
112 |
+
|
113 |
+
# Table for detail calculations (perhitungan)
|
114 |
+
detail_perhitungan = {
|
115 |
+
"001": {
|
116 |
+
"name": "Pendaftaran Kendaraan",
|
117 |
+
"formula": lambda harga_jual: harga_jual * 0.1 # Example formula: 10% of harga_jual
|
118 |
+
},
|
119 |
+
"002": {
|
120 |
+
"name": "Pembaruan Data Kendaraan",
|
121 |
+
"formula": lambda harga_jual: harga_jual * 0.05 # Example formula: 5% of harga_jual
|
122 |
+
},
|
123 |
+
"003": {
|
124 |
+
"name": "Alih Kepemilikan Kendaraan",
|
125 |
+
"formula": lambda harga_jual: harga_jual * 0.1 # Example formula: 10% of harga_jual
|
126 |
+
},
|
127 |
+
"004": {
|
128 |
+
"name": "Penggantian Dokumen atau Plat yang Hilang",
|
129 |
+
"formula": lambda harga_jual: harga_jual * 0.03 # Example formula: 3% of harga_jual
|
130 |
+
},
|
131 |
+
"005": {
|
132 |
+
"name": "Pembayaran dan Pengelolaan Pajak Kendaraan",
|
133 |
+
"formula": lambda harga_jual: harga_jual * 0.12 # Example formula: 12% of harga_jual
|
134 |
+
},
|
135 |
+
"006": {
|
136 |
+
"name": "Pemeriksaan dan Verifikasi Kendaraan",
|
137 |
+
"formula": lambda harga_jual: 100000 # Example formula: 2% of harga_jual
|
138 |
+
},
|
139 |
+
"007": {
|
140 |
+
"name": "Pendaftaran Kendaraan Ekspor, Impor, atau Armada",
|
141 |
+
"formula": lambda harga_jual: harga_jual * 0.15 # Example formula: 15% of harga_jual
|
142 |
+
},
|
143 |
+
"008": {
|
144 |
+
"name": "Pelaporan dan Bantuan Terkait Kendaraan",
|
145 |
+
"formula": lambda harga_jual: harga_jual * 0.04 # Example formula: 4% of harga_jual
|
146 |
+
},
|
147 |
+
"009": {
|
148 |
+
"name": "Penangguhan atau Deklarasi Perubahan Penggunaan Kendaraan",
|
149 |
+
"formula": lambda harga_jual: harga_jual * 0.06 # Example formula: 6% of harga_jual
|
150 |
+
}
|
151 |
+
}
|
152 |
|
153 |
undetected = "099 - Lainnya/Tidak Terdeteksi"
|
154 |
|
155 |
model_ids = [
|
|
|
156 |
"Alibaba-NLP/gte-multilingual-base",
|
157 |
"BAAI/bge-m3",
|
158 |
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
159 |
"intfloat/multilingual-e5-small",
|
160 |
+
"sentence-transformers/distiluse-base-multilingual-cased-v2",
|
161 |
+
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
|
162 |
]
|
163 |
# model_id = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
164 |
# model_id = "Alibaba-NLP/gte-multilingual-base"
|
|
|
171 |
|
172 |
codes_emb = model.encode([x[6:] for x in codes])
|
173 |
|
174 |
+
|
175 |
+
def get_calculation(request_code, plate_number):
|
176 |
+
calc = detail_perhitungan.get(request_code)
|
177 |
+
vechicle = vehicle_tax_info.get(plate_number)
|
178 |
+
|
179 |
+
if vehicle == None or calc == None:
|
180 |
+
return None, None, None
|
181 |
+
|
182 |
+
harga_jual = vechicle.get("harga_jual")
|
183 |
+
formula = calc.get("formula")
|
184 |
+
result = formula(harga_jual)
|
185 |
+
description = inspect.getsource(formula).split(":")[-1].strip()
|
186 |
+
result_detail = request_code + " - " + calc.get("name")
|
187 |
+
|
188 |
+
# out = "=============================================="
|
189 |
+
# out += "\nWajib Pajak ingin melakukan proses berikut:\n"
|
190 |
+
# out = ""
|
191 |
+
|
192 |
+
return result, description, result_detail
|
193 |
+
|
194 |
+
|
195 |
+
def build_output(result, description, result_detail, plate_number):
|
196 |
+
return build_outputs([result], [description], [result_detail], plate_number)
|
197 |
+
|
198 |
+
|
199 |
+
def build_outputs(results, descriptions, result_details, plate_number):
|
200 |
+
out = "=============================================="
|
201 |
+
out += f"\nWajib Pajak dengan NoPol {plate_number} ingin melakukan proses berikut:\n"
|
202 |
+
|
203 |
+
for i, (res,desc,detail) in enumerate(zip(results, descriptions, result_details)):
|
204 |
+
out += f"{i+1}. {detail}\nDetail perhitungan:\n{desc.replace("harga_jual", str(res))}\n"
|
205 |
+
|
206 |
+
out += "Estimasi biaya: "
|
207 |
+
|
208 |
+
if len(results) > 1:
|
209 |
+
out += " + ".join([f"Rp{x}" for x in results])
|
210 |
+
out += f" = {sum(results)}"
|
211 |
+
else:
|
212 |
+
out += str(results[0])
|
213 |
+
return out
|
214 |
+
|
215 |
+
|
216 |
def respond(
|
217 |
message,
|
218 |
history: list[tuple[str, str]],
|
|
|
239 |
|
240 |
matches = re.findall(pattern, message)
|
241 |
|
242 |
+
plates = [" ".join(x).upper() for i,x in enumerate(matches)]
|
243 |
+
|
244 |
+
plate_numbers = ", ".join(plates)
|
245 |
|
246 |
text_emb = model.encode(message)
|
247 |
scores = cos_sim(codes_emb, text_emb)[:,0]
|
|
|
263 |
request_numbers = "\n".join(request_numbers)
|
264 |
request_details = "\n".join(request_details)
|
265 |
request_scores = "\n".join(request_scores)
|
266 |
+
|
267 |
+
out = "Request code number:\n" + request_numbers + "\n\nRequest detail:\n" + request_details + f"\n\nConfidence score:\n{request_scores}" + "\n\nPlate numbers: " + plate_numbers
|
268 |
+
|
269 |
+
for plate in plates:
|
270 |
+
results = descriptions = result_details = []
|
271 |
+
for code in request_numbers:
|
272 |
+
result, description, result_detail = get_calculation(code, plate)
|
273 |
+
|
274 |
+
if result != None:
|
275 |
+
results.append(result)
|
276 |
+
descriptions.append(description)
|
277 |
+
result_details.append(result_detail)
|
278 |
+
|
279 |
+
out += "\n\n" + build_outputs(results, descriptions, result_details, plate)
|
280 |
+
|
281 |
+
return out
|
282 |
+
|
283 |
+
# result, description, result_detailget_calculation(request_code, plate_number)
|
284 |
|
|
|
285 |
|
286 |
|
287 |
s_max = scores.argmax()
|
|
|
293 |
request_code = codes[scores.argmax()]
|
294 |
# "{:.2f}".format(a)
|
295 |
|
296 |
+
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
|
297 |
+
|
298 |
+
for plate in plates:
|
299 |
+
results = descriptions = result_details = []
|
300 |
+
result, description, result_detail = get_calculation(code, plate)
|
301 |
+
|
302 |
+
if result != None:
|
303 |
+
results.append(result)
|
304 |
+
descriptions.append(description)
|
305 |
+
result_details.append(result_detail)
|
306 |
|
307 |
+
out += "\n\n" + build_outputs(results, descriptions, result_details, plate)
|
308 |
+
|
309 |
+
return out
|
310 |
+
|
311 |
+
# if vehicle_tax_info.get(request_detail)
|
312 |
+
|
313 |
# for val in history:
|
314 |
# if val[0]:
|
315 |
# messages.append({"role": "user", "content": val[0]})
|
|
|
361 |
gr.Markdown("<br>".join(codes) + "<br>" + undetected)
|
362 |
gr.Markdown("# Valid License Plate Number Criteria:")
|
363 |
gr.Markdown("(1-2 letters) (4 numbers) (1-3 letters)")
|
364 |
+
gr.Markdown("# Example Valid Plate Numbers")
|
365 |
+
gr.Markdown("<br>".join(vehicle_tax_info.keys()))
|
366 |
|
367 |
|
368 |
gr.Markdown("# Choose & Load Model:")
|