-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.py
296 lines (236 loc) · 9.81 KB
/
generate.py
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
import os
import re
import threading
import time
from typing import List, Tuple
from PIL import Image
import google.generativeai as genai
import tqdm
from utils import load_from_jsonl, append_to_jsonl
DATA_DIR = './'
API_KEY = 'your_api_key'
def generate_docvqa(model: genai.GenerativeModel, image: Image.Image, QAs: List[Tuple[str, str]]):
prompt = \
"""You are a document processing specialist. Based on the document scan image and question-answer pairs, help me finish the following jobs:
1. Write a brief description about the topic and key points of the document.
2. From the example question, get the thinking process of the example answer.
3. Imitate the example question-thought-answer tuples and generate 10 more question-thought-answer tuples. Keep the generated answer concise, i.e., using a single word or phrase.
Your output should follow this format, ${...} means the filling content:
Caption: ${brief_description}
"""
for idx, (question, answer) in enumerate(QAs):
prompt += \
f"""
Example {idx + 1}:
Q: {question}
T: ${{thought}}
A: {answer}
"""
prompt += \
"""
Generated 1:
Q: ${question}
T: ${thought}
A: ${answer}
Generated ..."""
response = model.generate_content([image, prompt])
return response.text
def generate_chartqa(model: genai.GenerativeModel, image: Image.Image, QAs: List[Tuple[str, str]]):
prompt = \
"""You are a data analyst. Based on the chart image and question-answer pairs, help me finish the following jobs:
1. Write a brief description about the topic and key points of the chart.
2. Restore the data table corresponding to the chart.
3. From the example question, get the thinking process of the example answer.
4. Imitate the example question-thought-answer tuples and generate 5 more question-thought-answer tuples. Keep the generated answer concise, i.e., using a single word or phrase.
Your output should follow this format, ${...} means the filling content:
Caption: ${brief_description}
Data: ${data_table}
"""
for idx, (question, answer) in enumerate(QAs):
prompt += \
f"""
Example {idx + 1}:
Q: {question}
T: ${{thought}}
A: {answer}
"""
prompt += \
"""
Generated 1:
Q: ${question}
T: ${thought}
A: ${answer}
Generated ..."""
response = model.generate_content([image, prompt])
return response.text
def generate_infovqa(model: genai.GenerativeModel, image: Image.Image, QAs: List[Tuple[str, str]]):
w, h = image.size
if w > 3072:
w = 3072
h = int(h / w * 3072)
image = image.resize((3072, h), resample=Image.Resampling.BICUBIC)
crops = []
crop_hint = ''
if h > 3072:
for y in range(0, h, 3072):
if h - y >= 3072:
crop = image.crop((0, y, w, y + 3072))
else:
crop = Image.new('RGB', (w, 3072))
crop.paste(image.crop((0, y, w, h)))
crops.append(crop)
crop_hint = f' (the whole image is vertically splitted into {len(crops)} sub-images)'
prompt = \
f"""You are a professional researcher. Based on the infographic image{crop_hint} and question-answer pairs, help me finish the following jobs:
1. Write a brief description about the topic and key points of the infographic.
2. From the example question, get the thinking process of the example answer.
3. Imitate the example question-thought-answer tuples and generate 10 more question-thought-answer tuples. Keep the generated answer concise, i.e., using a single word or phrase.
Your output should follow this format, ${{...}} means the filling content:
Caption: ${{brief_description}}
"""
for idx, (question, answer) in enumerate(QAs):
prompt += \
f"""
Example {idx + 1}:
Q: {question}
T: ${{thought}}
A: {answer}
"""
prompt += \
"""
Generated 1:
Q: ${question}
T: ${thought}
A: ${answer}
Generated ..."""
if crops:
response = model.generate_content([*crops, prompt])
else:
response = model.generate_content([image, prompt])
return response.text
def get_structured_extension(text):
structured_data = {}
caption_match = re.search(r'Caption:(.+?)\n\n', text, re.DOTALL)
structured_data['caption'] = caption_match.group(1).strip()
data_match = re.search(r'Data:\n\n(.+?)\n\n', text, re.DOTALL)
if data_match:
structured_data['data'] = data_match.group(1).strip()
example_matches = re.findall(r'Example \d:\nQ:(.+?)\nT:(.+?)\nA:(.+?)\n\n', text, re.DOTALL)
structured_data['example'] = []
for m in example_matches:
structured_data['example'].append({
'question': m[0].strip(),
'thought': m[1].strip(),
'answer': m[2].strip(),
})
assert len(structured_data['example']) > 0
generated_matches = re.findall(r'Generated \d:\nQ:(.+?)\nT:(.+?)\nA:(.+?)(?=\n\n|\Z)', text, re.DOTALL)
structured_data['generated'] = []
for m in generated_matches:
structured_data['generated'].append({
'question': m[0].strip(),
'thought': m[1].strip(),
'answer': m[2].strip(),
})
assert len(structured_data['generated']) > 0
return structured_data
def generate_conversation(model: genai.GenerativeModel, image: Image.Image):
w, h = image.size
if w > 3072:
w = 3072
h = int(h / w * 3072)
image = image.resize((3072, h), resample=Image.Resampling.BICUBIC)
crops = []
crop_hint = ''
if h > 3072:
for y in range(0, h, 3072):
if h - y >= 3072:
crop = image.crop((0, y, w, y + 3072))
else:
crop = Image.new('RGB', (w, 3072))
crop.paste(image.crop((0, y, w, h)))
crops.append(crop)
crop_hint = f' (the whole image is vertically splitted into {len(crops)} sub-images)'
prompt = \
f"""You are a senior researcher, imagine you are talking with a junior researcher about the content in the image{crop_hint}. Think loudly and show me the conversation.
For example, the junior researcher may ask diverse questions and you will give corresponding answers. Questions that have definite answers are preferred. The junior researcher may also ask complex questions, for example, asking you share knowledge about the topic or background stories related to the content in the image. Again, do not ask about uncertain details. Provide detailed answers when answering complex questions, for example, give detailed examples or reasoning steps to make the content more convincing and well-organized. You can include multiple paragraphs if necessary.
Your output should follow this format, ${{...}} means the filling content:
Junior: ${{...}}
Senior: ${{...}}
..."""
if crops:
response = model.generate_content([*crops, prompt])
else:
response = model.generate_content([image, prompt])
return response.text
def get_structured_conversation(text):
structured_data = []
turns = text.strip().split('Junior:')[1:]
for i, turn in enumerate(turns):
turn = turn.strip().split('Senior:')
if len(turn) == 2:
structured_data.append({
'user': turn[0].strip(),
'asst': turn[1].strip(),
})
else:
assert i == len(turns) - 1, '{}'.format(turn)
assert len(structured_data) > 0, 'Got empty conversation'
return structured_data
def generate_and_save(model, dataset, mode, annotation):
if mode == 'extension':
json_path = '{}.jsonl'.format(dataset)
else:
json_path = '{}_conv.jsonl'.format(dataset)
image_path = annotation['image']
QAs = annotation['QAs']
try:
image = Image.open(os.path.join(DATA_DIR, image_path))
if mode == 'extension':
if dataset == 'docvqa':
text = generate_docvqa(model, image, QAs)
elif dataset == 'chartqa':
text = generate_chartqa(model, image, QAs)
elif dataset == 'infovqa':
text = generate_infovqa(model, image, QAs)
structured = get_structured_extension(text)
else:
text = generate_conversation(model, image)
structured = get_structured_conversation(text)
append_to_jsonl({'image': image_path, 'gemini': structured}, json_path)
except Exception as e:
import traceback
traceback.print_exc()
print(f'WARNING: {e.__class__.__name__} - {image_path}')
if __name__ == '__main__':
genai.configure(api_key=API_KEY, transport='rest')
model = genai.GenerativeModel('gemini-pro-vision')
for dataset in ['docvqa', 'chartqa', 'infovqa']:
for mode in ['extension', 'conversation']:
src_json = os.path.join(DATA_DIR, 'ureader_json/train_{}.jsonl'.format(dataset))
if mode == 'extension':
dst_json = os.path.join(DATA_DIR, '{}.jsonl'.format(dataset))
else:
dst_json = os.path.join(DATA_DIR, '{}_conv.jsonl'.format(dataset))
data = load_from_jsonl(src_json)
if os.path.exists(dst_json):
done = set(_['image'] for _ in load_from_jsonl(dst_json))
else:
done = []
threads = []
last_time = time.time()
pbar = tqdm.tqdm(data)
for i, annotation in enumerate(pbar):
if annotation['image'] in done:
continue
thread = threading.Thread(target=generate_and_save, args=(model, dataset, mode, annotation))
time.sleep(max(1.0 - (time.time() - last_time), 0))
thread.start()
last_time = time.time()
threads.append(thread)
pbar.set_description(f'Active threads: {threading.active_count() - 2}')
while threading.active_count() - 2 > 0:
print(f'Active threads: {threading.active_count() - 2}')
time.sleep(1)
for thread in threads:
thread.join()