-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
311 lines (261 loc) · 9.27 KB
/
utils.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import base64
import multiprocessing
import socket
import subprocess
import time
from io import BytesIO
import anthropic
import google.generativeai as genai
import torch
import tqdm
from PIL import Image
from openai import OpenAI
args = None
def gemini_infer(image, query):
try:
model = genai.GenerativeModel(model_name=args.model)
image_ = Image.open(BytesIO(base64.b64decode(image)))
response = model.generate_content([query, image_])
return response.text
except:
time.sleep(1)
try:
model = genai.GenerativeModel(model_name=args.model)
image_ = Image.open(BytesIO(base64.b64decode(image)))
response = model.generate_content([query, image_])
return response.text
except:
print("Warning! gemini infer does not work")
return "TODO"
def claude_func(image, query):
client = anthropic.Anthropic(api_key=args.api_key)
image_data = base64.b64decode(image)
with BytesIO(image_data) as img_buffer:
img = Image.open(img_buffer).convert("RGB")
with BytesIO() as output_buffer:
img.save(output_buffer, format='JPEG')
image_str = base64.b64encode(output_buffer.getvalue()).decode('utf8')
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_str,
},
},
{
"type": "text",
"text": query,
}
],
}
]
try:
completion = client.messages.create(
model=args.model,
max_tokens=512,
messages=messages,
)
except Exception as e:
print("Error")
print(e)
time.sleep(60)
completion = client.messages.create(
model=args.model,
max_tokens=512,
messages=messages,
)
return completion.content[0].text
def func(obj):
if len(obj) == 2:
image, query = obj
response2 = query2 = None
else:
assert len(obj) == 4
image, query, response2, query2 = obj
if args.model.startswith("gemini"):
return gemini_infer(image, query)
elif args.model.startswith("claude"):
return claude_func(image, query)
client = OpenAI(api_key=args.api_key, base_url=args.base_url)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": query},
],
},
]
if image is not None:
messages[0]['content'].append({
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image}",
},
})
if response2 is not None:
assert query2 is not None
messages += [{
"role": "assistant",
"content": [
{"type": "text", "text": response2},
],
}, {
"role": "user",
"content": [
{"type": "text", "text": query2},
],
}]
else:
assert query2 is None
if args.model == 'auto':
model = client.models.list().data[0].id
else:
model = args.model
try:
completion = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.7,
)
except:
time.sleep(1)
try:
completion = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.7,
)
except Exception as e:
print("Warning! infer does not work")
print("Error:")
print(e)
return "TODO"
return completion.choices[0].message.content
def get_pool(n_proc):
class DummyPool:
imap = map
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
if n_proc == 0:
return DummyPool()
else:
return multiprocessing.Pool(n_proc)
def infer(queries, images, given_args):
global args
args = given_args
if args.model.startswith("gemini"):
genai.configure(api_key=args.api_key)
responses = []
assert len(images) == len(queries)
with get_pool(args.n_proc) as p:
for response in tqdm.tqdm(p.imap(func, zip(images, queries)), total=len(images)):
responses.append(response)
if len(responses) <= 5:
print("\n--- Example output:", len(responses))
print(responses[-1])
return responses
def find_available_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('0.0.0.0', 0))
return s.getsockname()[1]
def launch_locally(backend, model):
port = find_available_port()
if backend == 'lmdeploy':
cmd = ['lmdeploy', 'serve', 'api_server', model, '--server-port', str(port),
'--tp', str(torch.cuda.device_count()), ]
if 'prometheus' in model:
cmd += ['--chat-template', 'llava-v1', ]
elif backend == 'vllm':
cmd = ['vllm', 'serve', model, '--port', str(port), '--dtype', 'auto', '--api-key', 'YOUR_API_KEY',
'--trust-remote-code', '--tensor-parallel-size', str(torch.cuda.device_count()), ]
if '3.2' in model or 'nvlm' in model.lower():
cmd += ['--enforce-eager', '--max-num-seqs', '32', '--max_model_len', '40000', ]
else:
assert backend == 'sglang'
cmd = ['python', '-m', 'sglang.launch_server', '--model-path', model, '--port', str(port),
'--chat-template=chatml-llava', ]
if '7b' in model.lower():
tp = 1
if 'llava-critic' in model:
cmd += ['--tokenizer-path', 'lmms-lab/llava-onevision-qwen2-7b-ov', ]
elif '11b' in model.lower() or '13b' in model.lower():
tp = 2
else:
assert '72b' in model.lower()
tp = 4
if 'llava-critic' in model:
cmd += ['--tokenizer-path', 'lmms-lab/llava-onevision-qwen2-72b-ov-sft', ]
assert torch.cuda.device_count() % tp == 0
dp = torch.cuda.device_count() // tp
cmd += ['--tp', str(tp), '--dp', str(dp), ]
process = subprocess.Popen(cmd, stdout=subprocess.DEVNULL)
while True:
try:
_ = OpenAI(api_key='YOUR_API_KEY', base_url=f'http://0.0.0.0:{port}/v1').models.list()
print("> launched. Proceed")
return process, port
except:
pass
time.sleep(5)
def get_answer_format(item):
DEFAULT_ANSWER = "a single number, word or phrase"
BINARY_ANSWER = "either \"Yes\" or \"No\""
MULTI_CHOICE_ANSWER = 'in letter form of the choice selected, e.g., "A", "B", "C", "D"'
INTEGER_ANSWER = "an integer number, e.g. 1, 2, 3"
dataset = item['meta_data']['src_dataset']
if dataset in ["VSR", "FigureQA", "POPE", "HallusionBench", ]:
# a few datasets with only yes or no
return BINARY_ANSWER
elif dataset in ["WeMath", "ScienceQA", "SceMQA", "EmbSpatial", ]:
# a few datasets completely multi-choice
return MULTI_CHOICE_ANSWER
elif dataset == "PlotQA":
if item['label'] in ['Yes', 'No', ]:
return BINARY_ANSWER
else:
return DEFAULT_ANSWER
elif dataset == "MathVista":
if item['meta_data']["question_type"] == "multi_choice":
return MULTI_CHOICE_ANSWER
elif item['meta_data']["answer_type"] == "integer":
return INTEGER_ANSWER
elif item['meta_data']["answer_type"] == "float":
return "a decimal number, e.g., 1.23, 1.34, 1.45"
else:
assert item['meta_data']["answer_type"] == "list"
return "a list, e.g., [1, 2, 3], [1.2, 1.3, 1.4]"
elif dataset == "MMVet":
return DEFAULT_ANSWER
elif dataset == "MathVision":
return "an integer number, e.g. -5, a decimal number, e.g. 3.5, or a coordinate, e.g. (1, 2)"
elif dataset == "MMMU":
if item["meta_data"]["question_type"] == "multiple-choice":
return MULTI_CHOICE_ANSWER
else:
return DEFAULT_ANSWER
elif dataset == "TallyQA":
return INTEGER_ANSWER
elif dataset in ["TextVQA", "DocVQA", ]:
return "a word, a phrase, or a short concise sentence"
elif dataset == "GQA":
return DEFAULT_ANSWER
elif dataset == "CLEVR":
try:
_ = int(item['label'])
return INTEGER_ANSWER
except:
if item['label'] in ['yes', 'no', ]:
return BINARY_ANSWER
else:
return DEFAULT_ANSWER
elif dataset == "ChartQA":
return DEFAULT_ANSWER
else:
raise ValueError(f"Dataset not found: {dataset}")