syedfaisalabrar commited on
Commit
2ed083e
·
verified ·
1 Parent(s): e491529

Create frontPrompt.py

Browse files
Files changed (1) hide show
  1. frontPrompt.py +216 -0
frontPrompt.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
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
+
49
+ ### Keys and Descriptions
50
+
51
+ 1. **`surat_tanda_nomor_kendaraan_bermotor`**
52
+ - **Extract**: The value of the field labeled as "Surat Tanda Nomor Kendaraan Bermotor" and this is titel.
53
+ - **If the Field is Absent**: `"null"`
54
+ - **If the Field is Present but No Value is Provided**: `"empty"`
55
+
56
+ 2. **`tempat_tanggal`**
57
+ - **Extract**: The location and date from the top right corner of the document.
58
+ - **Note**: This field does not have a title such as "Tempat - Tanggal."
59
+ - **Format**: `"CITY, DD MMM YYYY"` (e.g., `"JAKARTA, 07 DES 2018"`).
60
+ - **If the Field is Absent**: `"null"`
61
+ - **If the Field is Present but No Value is Provided**: `"empty"`
62
+
63
+ 3. **`no`**
64
+ - **Extract**: The value in the "NO" field.
65
+ - **If the Field is Absent**: `"null"`
66
+ - **If the Field is Present but No Value is Provided**: `"empty"`
67
+
68
+ 4. **`nomor_registrasi`**
69
+ - **Extract**: The "NOMOR REGISTRASI" field.
70
+ - **If the Field is Absent**: `"null"`
71
+ - **If the Field is Present but No Value is Provided**: `"empty"`
72
+
73
+ 5. **`nama_pemilik`**
74
+ - **Extract**: The "NAMA PEMILIK" field.
75
+ - **If the Field is Absent**: `"null"`
76
+ - **If the Field is Present but No Value is Provided**: `"empty"`
77
+
78
+ 6. **`alamat`**
79
+ - **Extract**: The "ALAMAT" field.
80
+ - **If the Field is Absent**: `"null"`
81
+ - **If the Field is Present but No Value is Provided**: `"empty"`
82
+
83
+ 7. **`merk`**
84
+ - **Extract**: The "MERK" field.
85
+ - **If the Field is Absent**: `"null"`
86
+ - **If the Field is Present but No Value is Provided**: `"empty"`
87
+
88
+ 8. **`type`**
89
+ - **Extract**: The "TYPE" field.
90
+ - **If the Field is Absent**: `"null"`
91
+ - **If the Field is Present but No Value is Provided**: `"empty"`
92
+
93
+ 9. **`jenis`**
94
+ - **Extract**: The "JENIS" field.
95
+ - **If the Field is Absent**: `"null"`
96
+ - **If the Field is Present but No Value is Provided**: `"empty"`
97
+
98
+ 10. **`model`**
99
+ - **Extract**: The "MODEL" field.
100
+ - **If the Field is Absent**: `"null"`
101
+ - **If the Field is Present but No Value is Provided**: `"empty"`
102
+
103
+ 11. **`tahun_pembuatan`**
104
+ - **Extract**: The "TAHUN PEMBUATAN" field.
105
+ - **If the Field is Absent**: `"null"`
106
+ - **If the Field is Present but No Value is Provided**: `"empty"`
107
+
108
+ 12. **`isi_silinder_daya_listrik`**
109
+ - **Extract**: The "ISI SILINDER / DAYA LISTRIK" field.
110
+ - **If the Field is Absent**: `"null"`
111
+ - **If the Field is Present but No Value is Provided**: `"empty"`
112
+
113
+ 13. **`nomor_rangka`**
114
+ - **Extract**: The "NOMOR RANGKA" field.
115
+ - **If the Field is Absent**: `"null"`
116
+ - **If the Field is Present but No Value is Provided**: `"empty"`
117
+
118
+ 14. **`nomor_mesin`**
119
+ - **Extract**: The "NOMOR MESIN" field.
120
+ - **If the Field is Absent**: `"null"`
121
+ - **If the Field is Present but No Value is Provided**: `"empty"`
122
+
123
+ 15. **`nik_tdp_nie_kitas_kitap`**
124
+ - **Extract**: The "NIK/TDP/NIE/KITAS/KITAP" field.
125
+ - **If the Field is Absent**: `"null"`
126
+ - **If the Field is Present but No Value is Provided**: `"empty"`
127
+
128
+ 16. **`warna`**
129
+ - **Extract**: The "WARNA" field.
130
+ - **If the Field is Absent**: `"null"`
131
+ - **If the Field is Present but No Value is Provided**: `"empty"`
132
+
133
+ 17. **`bahan_bakar`**
134
+ - **Extract**: The "BAHAN BAKAR" field.
135
+ - **If the Field is Absent**: `"null"`
136
+ - **If the Field is Present but No Value is Provided**: `"empty"`
137
+
138
+ 18. **`warna_tnkb`**
139
+ - **Extract**: The "WARNA TNKB" field.
140
+ - **If the Field is Absent**: `"null"`
141
+ - **If the Field is Present but No Value is Provided**: `"empty"`
142
+
143
+ 19. **`tahun_registrasi`**
144
+ - **Extract**: The "TAHUN REGISTRASI" field.
145
+ - **If the Field is Absent**: `"null"`
146
+ - **If the Field is Present but No Value is Provided**: `"empty"`
147
+
148
+ 20. **`nomor_bpkb`**
149
+ - **Extract**: The "NOMOR BPKB" field.
150
+ - **If the Field is Absent**: `"null"`
151
+ - **If the Field is Present but No Value is Provided**: `"empty"`
152
+
153
+ 21. **`kode_lokasi`**
154
+ - **Extract**: The "KODE LOKASI" field.
155
+ - **If the Field is Absent**: `"null"`
156
+ - **If the Field is Present but No Value is Provided**: `"empty"`
157
+
158
+ 22. **`no_urut_pendaftaran`**
159
+ - **Extract**: The "NO URUT PENDAFTARAN" field.
160
+ - **If the Field is Absent**: `"null"`
161
+ - **If the Field is Present but No Value is Provided**: `"empty"`
162
+
163
+ 23. **`berlaku_sampai`**
164
+ - **Extract**: The "BERLAKU SAMPAI" field.
165
+ - **If the Field is Absent**: `"null"`
166
+ - **If the Field is Present but No Value is Provided**: `"empty"`
167
+ 24. **`qr_code`**
168
+ - **Extract**: The value encoded in the QR code, if present.
169
+ - **If No QR Code is Found**: `"null"`
170
+ - **If a QR Code is Present but Contains No Data**: `"empty"`
171
+
172
+ ---
173
+
174
+ ### Output Format
175
+
176
+ ```json
177
+ {
178
+ "surat_tanda_nomor_kendaraan_bermotor": "<value> OR empty OR null",
179
+ "tempat_tanggal": "<value> OR empty OR null",
180
+ "no": "<value> OR empty OR null",
181
+ "nomor_registrasi": "<value> OR empty OR null",
182
+ "nama_pemilik": "<value> OR empty OR null",
183
+ "alamat": "<value> OR empty OR null",
184
+ "merk": "<value> OR empty OR null",
185
+ "type": "<value> OR empty OR null",
186
+ "jenis": "<value> OR empty OR null",
187
+ "model": "<value> OR empty OR null",
188
+ "tahun_pembuatan": "<value> OR empty OR null",
189
+ "isi_silinder_daya_listrik": "<value> OR empty OR null",
190
+ "nomor_rangka": "<value> OR empty OR null",
191
+ "nomor_mesin": "<value> OR empty OR null",
192
+ "nik_tdp_nie_kitas_kitap": "<value> OR empty OR null",
193
+ "warna": "<value> OR empty OR null",
194
+ "bahan_bakar": "<value> OR empty OR null",
195
+ "warna_tnkb": "<value> OR empty OR null",
196
+ "tahun_registrasi": "<value> OR empty OR null",
197
+ "nomor_bpkb": "<value> OR empty OR null",
198
+ "kode_lokasi": "<value> OR empty OR null",
199
+ "no_urut_pendaftaran": "<value> OR empty OR null",
200
+ "berlaku_sampai": "<value> OR empty OR null"
201
+ "qr_code" : "<value> OR empty OR null"
202
+ }
203
+ ### Reasoning Process
204
+ For each key, explain your reasoning:
205
+ Indicate whether the field was present.
206
+ Justify the extracted value or the use of "null" or "empty" as per the conditions.
207
+ Return Output:
208
+ Generate a JSON object:
209
+ {
210
+ "reasoning": "reasoning for each key",
211
+ "output JSON": "key-value pairs"
212
+ }
213
+ ---
214
+ """
215
+ response = model.chat(tokenizer, pixel_values, question, generation_config)
216
+ return (f'User: {question}\nAssistant: {response}')