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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torchvision.transforms as T
|
4 |
+
from decord import VideoReader, cpu
|
5 |
+
from PIL import Image
|
6 |
+
from torchvision.transforms.functional import InterpolationMode
|
7 |
+
from transformers import AutoModel, AutoTokenizer
|
8 |
+
|
9 |
+
IMAGENET_MEAN = (0.485, 0.456, 0.406)
|
10 |
+
IMAGENET_STD = (0.229, 0.224, 0.225)
|
11 |
+
|
12 |
+
def build_transform(input_size):
|
13 |
+
MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
|
14 |
+
transform = T.Compose([
|
15 |
+
T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
|
16 |
+
T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
|
17 |
+
T.ToTensor(),
|
18 |
+
T.Normalize(mean=MEAN, std=STD)
|
19 |
+
])
|
20 |
+
return transform
|
21 |
+
|
22 |
+
|
23 |
+
def load_image(image_file, input_size=800, max_num=12):
|
24 |
+
image = Image.open(image_file).convert('RGB')
|
25 |
+
transform = build_transform(input_size=input_size)
|
26 |
+
pixel_values = [transform(image) for image in images]
|
27 |
+
pixel_values = torch.stack(pixel_values)
|
28 |
+
return pixel_values
|
29 |
+
|
30 |
+
|
31 |
+
def main(image_path, model, tokenizer):
|
32 |
+
path = "OpenGVLab/InternVL2_5-4B"
|
33 |
+
model = AutoModel.from_pretrained(
|
34 |
+
path,
|
35 |
+
torch_dtype=torch.bfloat16,
|
36 |
+
load_in_8bit=True,
|
37 |
+
low_cpu_mem_usage=True,
|
38 |
+
use_flash_attn=True,
|
39 |
+
trust_remote_code=True).eval()
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
|
41 |
+
pixel_values = load_image(image_path, max_num=12).to(torch.bfloat16).cuda()
|
42 |
+
generation_config = dict(max_new_tokens=1024, do_sample=True)
|
43 |
+
|
44 |
+
question = """<image>\n**Instruction:**
|
45 |
+
Analyze the image to extract values for the specified keys. Use the detailed descriptions below to determine the correct value for each key. Handle missing or ambiguous data as instructed.
|
46 |
+
|
47 |
+
---
|
48 |
+
### Keys and Descriptions
|
49 |
+
|
50 |
+
1. **`Surat Ketetapan Kewajiban Pembayaran PKB/BBNKB,SWDKLLJ DAN PNBP`**
|
51 |
+
- **Extract**: The value of the field is"Surat Ketetapan Kewajiban Pembayaran PKB/BBNKB, SWDKLLJ DAN PNBP" and this is title.
|
52 |
+
- **If the Field is Present** : "present"
|
53 |
+
- **If the Field is Absent**: `"null"`
|
54 |
+
|
55 |
+
2. **`nomor_registrasi`**
|
56 |
+
- **Extract**: The "NOMOR REGISTRASI" field.
|
57 |
+
- **If the Field is Absent**: `"null"`
|
58 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
59 |
+
|
60 |
+
3. **`nama_pemilik`**
|
61 |
+
- **Extract**: The "NAMA PEMILIK" field.
|
62 |
+
- **If the Field is Absent**: `"null"`
|
63 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
64 |
+
|
65 |
+
|
66 |
+
4. **`alamat`**
|
67 |
+
- **Extract**: The "ALAMAT" field and this is address may contain 1 or more lines format : "BUNTET PESANTREN RT/RW/015/005 DESA MERTAPADA
|
68 |
+
KULON KEC. ASTANAJAPURA KAB.CIREBON ".
|
69 |
+
- **If the Field is Absent**: `"null"`
|
70 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
71 |
+
|
72 |
+
5. **`merk/type`**
|
73 |
+
- **Extract**: The "MERK/TYPE" field.
|
74 |
+
- **If the Field is Absent**: `"null"`
|
75 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
76 |
+
|
77 |
+
|
78 |
+
6. **`jenis/model`**
|
79 |
+
- **Extract**: The "JENIS/MODEL" field.
|
80 |
+
- **If the Field is Absent**: `"null"`
|
81 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
82 |
+
|
83 |
+
7. **`tahun_pembuatan`**
|
84 |
+
- **Extract**: The "TAHUN PEMBUATAN/PERAKITAN" field.
|
85 |
+
- **If the Field is Absent**: `"null"`
|
86 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
87 |
+
|
88 |
+
8. **`warna_kb`**
|
89 |
+
- **Extract**: The "WARNA KB" field.
|
90 |
+
- **If the Field is Absent**: `"null"`
|
91 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
92 |
+
|
93 |
+
9. **`isi_silinder`**
|
94 |
+
- **Extract**: The "ISI SILINDER/HP" field.
|
95 |
+
- **If the Field is Absent**: `"null"`
|
96 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
97 |
+
|
98 |
+
10. **`nomor_rangka`**
|
99 |
+
- **Extract**: The "NOMOR RANGKA/NIK" field.
|
100 |
+
- **If the Field is Absent**: `"null"`
|
101 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
102 |
+
|
103 |
+
11.**`nomor_mesin`**
|
104 |
+
- **Extract**: The "NOMOR MESIN" field.
|
105 |
+
- **If the Field is Absent**: `"null"`
|
106 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
107 |
+
|
108 |
+
12. **`no_bpkp`**
|
109 |
+
- **Extract**: The "NO BPKB" field.
|
110 |
+
- **If the Field is Absent**: `"null"`
|
111 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
112 |
+
|
113 |
+
|
114 |
+
13. **`berlaku s/d`**
|
115 |
+
- **Extract**: The "BERLAKU S/D" field.
|
116 |
+
- **If the Field is Absent**: `"null"`
|
117 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
118 |
+
|
119 |
+
|
120 |
+
14. **`bahan_bakar`**
|
121 |
+
- **Extract**: The "BAHAN BAKAR" field.
|
122 |
+
- **If the Field is Absent**: `"null"`
|
123 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
124 |
+
|
125 |
+
|
126 |
+
15. **`warna_tnkb`**
|
127 |
+
- **Extract**: The "WARNA TNKB" field.
|
128 |
+
- **If the Field is Absent**: `"null"`
|
129 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
130 |
+
|
131 |
+
|
132 |
+
16. **`kepemilikan_ke`**
|
133 |
+
- **Extract**: The "KEPEMILIKAN KE" field.
|
134 |
+
- **If the Field is Absent**: `"null"`
|
135 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
136 |
+
|
137 |
+
17. **`no_registrasi_lama`**
|
138 |
+
- **Extract**: The "NO REGISTRASI LAMA" field.
|
139 |
+
- **If the Field is Absent**: `"null"`
|
140 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
141 |
+
|
142 |
+
18. **`kode_njkb`**
|
143 |
+
- **Extract**: The "KODE NJKB" field.
|
144 |
+
- **If the Field is Absent**: `"null"`
|
145 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
146 |
+
|
147 |
+
19. **`no`**
|
148 |
+
- **Extract**: The "NO." field.
|
149 |
+
- **If the Field is Absent**: `"null"`
|
150 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
151 |
+
|
152 |
+
20.**`asal_daerah`**
|
153 |
+
- **Extract**: The "ASAL DAERAH" field.
|
154 |
+
- **If the Field is Absent**: `"null"`
|
155 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
156 |
+
|
157 |
+
21. **`no_urut`**
|
158 |
+
- **Extract**: The "NO. URUT" field.
|
159 |
+
- **If the Field is Absent**: `"null"`
|
160 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
161 |
+
|
162 |
+
22. **`no_kohir`**
|
163 |
+
- **Extract**: The "NO. KOHIR" field.
|
164 |
+
- **If the Field is Absent**: `"null"`
|
165 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
166 |
+
|
167 |
+
23. **`nik_no.hp`**
|
168 |
+
- **Extract**: The "NIK/NO.HP" field.
|
169 |
+
- **If the Field is Absent**: `"null"`
|
170 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
171 |
+
|
172 |
+
24. **`pkok_bbnkb`**
|
173 |
+
- **Extract**: The "PKOK BBNKB" field.
|
174 |
+
- **If the Field is Absent**: `"null"`
|
175 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
176 |
+
|
177 |
+
25. **`pkok_pkb`**
|
178 |
+
- **Extract**: The "POKOK PKB" field.
|
179 |
+
- **If the Field is Absent**: `"null"`
|
180 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
181 |
+
|
182 |
+
26. **`pkok_swdkljj`**
|
183 |
+
- **Extract**: The "POKOK SWDKLJJ" field.
|
184 |
+
- **If the Field is Absent**: `"null"`
|
185 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
186 |
+
|
187 |
+
27. **`pkok_penerbitan_stnk`**
|
188 |
+
- **Extract**: The "POKOK PENERBITAN STNK" field.
|
189 |
+
- **If the Field is Absent**: `"null"`
|
190 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
191 |
+
|
192 |
+
28. **`pkok_penerbitan_tnkb`**
|
193 |
+
- **Extract**: The "POKOK PENERBITAN TNKB/NRKB PILIHAN" field.
|
194 |
+
- **If the Field is Absent**: `"null"`
|
195 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
196 |
+
|
197 |
+
29. **`pkok_jumlah`**
|
198 |
+
- **Extract**: The "POKOK JUMLAH" field.
|
199 |
+
- **If the Field is Absent**: `"null"`
|
200 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
201 |
+
|
202 |
+
30. **`sanksi_adm_bbnkb`**
|
203 |
+
- **Extract**: The "SANKSI ADM BBNKB" field.
|
204 |
+
- **If the Field is Absent**: `"null"`
|
205 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
206 |
+
|
207 |
+
31. **`sanksi_adm_pkb`**
|
208 |
+
- **Extract**: The "SANKSI ADM PKB" field.
|
209 |
+
- **If the Field is Absent**: `"null"`
|
210 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
211 |
+
|
212 |
+
32. **`sanksi_adm_swdkljj`**
|
213 |
+
- **Extract**: The "SANKSI ADM SWDKLJJ" field.
|
214 |
+
- **If the Field is Absent**: `"null"`
|
215 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
216 |
+
|
217 |
+
33. **`sanksi_adm_penerbitan_stnk`**
|
218 |
+
- **Extract**: The "SANKSI ADM PENERBITAN STNK" field.
|
219 |
+
- **If the Field is Absent**: `"null"`
|
220 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
221 |
+
|
222 |
+
34. **`sanksi_adm_penerbitan_tnkb`**
|
223 |
+
- **Extract**: The "SANKSI ADM PENERBITAN TNKB/NRKB PILIHAN" field.
|
224 |
+
- **If the Field is Absent**: `"null"`
|
225 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
226 |
+
|
227 |
+
35. **`sanksi_adm_jumlah`**
|
228 |
+
- **Extract**: The "SANKSI ADM JUMLAH " field.
|
229 |
+
- **If the Field is Absent**: `"null"`
|
230 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
231 |
+
|
232 |
+
36. **`jumlah_bbnkb`**
|
233 |
+
- **Extract**: The "JUMLAH BBNKB" field.
|
234 |
+
- **If the Field is Absent**: `"null"`
|
235 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
236 |
+
|
237 |
+
37. **`jumlah_pkb`**
|
238 |
+
- **Extract**: The "JUMLAH PKB " field.
|
239 |
+
- **If the Field is Absent**: `"null"`
|
240 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
241 |
+
|
242 |
+
38. **`jumlah_swdkljj`**
|
243 |
+
- **Extract**: The "JUMLAH SWDKLJJ" field.
|
244 |
+
- **If the Field is Absent**: `"null"`
|
245 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
246 |
+
|
247 |
+
39. **`jumlah_penerbitan_stnk`**
|
248 |
+
- **Extract**: The "JUMLAH PENERBITAN STNK" field.
|
249 |
+
- **If the Field is Absent**: `"null"`
|
250 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
251 |
+
|
252 |
+
40. **`jumlah_penerbitan_tnkb`**
|
253 |
+
- **Extract**: The "JUMLAH PENERBITAN TNKB/NRKB PILIHAN" field.
|
254 |
+
- **If the Field is Absent**: `"null"`
|
255 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
256 |
+
|
257 |
+
41. **`jumlah_jumlah`**
|
258 |
+
- **Extract**: The "JUMLAH JUMLAH" field.
|
259 |
+
- **If the Field is Absent**: `"null"`
|
260 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
261 |
+
|
262 |
+
42. **`ditetapkan_tanggal`**
|
263 |
+
- **Extract**: The "DITETAPKAN TANGGAL" field.
|
264 |
+
- **If the Field is Absent**: `"null"`
|
265 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
266 |
+
|
267 |
+
43. **`penetapan`**
|
268 |
+
- **Extract**: The "PENETAPAN" field.
|
269 |
+
- **If the Field is Absent**: `"null"`
|
270 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
271 |
+
|
272 |
+
44. **`kasir`**
|
273 |
+
- **Extract**: The "KASIR" field.
|
274 |
+
- **If the Field is Absent**: `"null"`
|
275 |
+
- **If the Field is Present but No Value is Provided**: `"empty"`
|
276 |
+
|
277 |
+
---
|
278 |
+
### Reasoning Process
|
279 |
+
For each key, explain your reasoning:
|
280 |
+
Indicate whether the field was present.
|
281 |
+
Justify the extracted value or the use of "null" or "empty" as per the conditions.
|
282 |
+
|
283 |
+
### Output Format
|
284 |
+
|
285 |
+
```json
|
286 |
+
{
|
287 |
+
"Surat_Ketetapan_Kewajiban_Pembayaran_PKB_BBNKB_SWDKLLJ_DAN_PNBP": "present OR null",
|
288 |
+
"nomor_registrasi": "<value> OR empty OR null",
|
289 |
+
"nama_pemilik": "<value> OR empty OR null",
|
290 |
+
"alamat": "<value> OR empty OR null",
|
291 |
+
"merk_type": "<value> OR empty OR null",
|
292 |
+
"jenis_model": "<value> OR empty OR null",
|
293 |
+
"tahun_pembuatan": "<value> OR empty OR null",
|
294 |
+
"warna_kb": "<value> OR empty OR null",
|
295 |
+
"isi_silinder": "<value> OR empty OR null",
|
296 |
+
"nomor_rangka": "<value> OR empty OR null",
|
297 |
+
"nomor_mesin": "<value> OR empty OR null",
|
298 |
+
"no_bpkp": "<value> OR empty OR null",
|
299 |
+
"berlaku_s_d": "<value> OR empty OR null",
|
300 |
+
"bahan_bakar": "<value> OR empty OR null",
|
301 |
+
"warna_tnkb": "<value> OR empty OR null",
|
302 |
+
"kepemilikan_ke": "<value> OR empty OR null",
|
303 |
+
"no_registrasi_lama": "<value> OR empty OR null",
|
304 |
+
"kode_njkb": "<value> OR empty OR null",
|
305 |
+
"no": "<value> OR empty OR null",
|
306 |
+
"asal_daerah": "<value> OR empty OR null",
|
307 |
+
"no_urut": "<value> OR empty OR null",
|
308 |
+
"no_kohir": "<value> OR empty OR null",
|
309 |
+
"nik_no_hp": "<value> OR empty OR null",
|
310 |
+
"pkok_bbnkb": "<value> OR empty OR null",
|
311 |
+
"pkok_pkb": "<value> OR empty OR null",
|
312 |
+
"pkok_swdkljj": "<value> OR empty OR null",
|
313 |
+
"pkok_penerbitan_stnk": "<value> OR empty OR null",
|
314 |
+
"pkok_penerbitan_tnkb": "<value> OR empty OR null",
|
315 |
+
"pkok_jumlah": "<value> OR empty OR null",
|
316 |
+
"sanksi_adm_bbnkb": "<value> OR empty OR null",
|
317 |
+
"sanksi_adm_pkb": "<value> OR empty OR null",
|
318 |
+
"sanksi_adm_swdkljj": "<value> OR empty OR null",
|
319 |
+
"sanksi_adm_penerbitan_stnk": "<value> OR empty OR null",
|
320 |
+
"sanksi_adm_penerbitan_tnkb": "<value> OR empty OR null",
|
321 |
+
"sanksi_adm_jumlah": "<value> OR empty OR null",
|
322 |
+
"jumlah_bbnkb": "<value> OR empty OR null",
|
323 |
+
"jumlah_pkb": "<value> OR empty OR null",
|
324 |
+
"jumlah_swdkljj": "<value> OR empty OR null",
|
325 |
+
"jumlah_penerbitan_stnk": "<value> OR empty OR null",
|
326 |
+
"jumlah_penerbitan_tnkb": "<value> OR empty OR null",
|
327 |
+
"jumlah_jumlah": "<value> OR empty OR null",
|
328 |
+
"ditetapkan_tanggal": "<value> OR empty OR null",
|
329 |
+
"penetapan": "<value> OR empty OR null",
|
330 |
+
"kasir": "<value> OR empty OR null"
|
331 |
+
}
|
332 |
+
}
|
333 |
+
Return Output:
|
334 |
+
Generate a JSON object:
|
335 |
+
{
|
336 |
+
"reasoning": "reasoning for each key",
|
337 |
+
"output JSON": "key-value pairs"
|
338 |
+
}
|
339 |
+
—
|
340 |
+
|
341 |
+
"""
|
342 |
+
response = model.chat(tokenizer, pixel_values, question, generation_config)
|
343 |
+
return (f'User: {question}\nAssistant: {response}')
|