forked from traveler-framework/TraveLER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
432 lines (369 loc) · 13.2 KB
/
modules.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
Modules for interfacing with models in TraveLER.
"""
import abc
import asyncio
import base64
import io
import multiprocessing as mp
import os
import pickle
from io import BytesIO
from typing import List, Union
import aiohttp
import numpy as np
import openai
import requests
import torch
import torch.nn.functional as F
import yaml
from dotenv import load_dotenv
from openai import OpenAI
from transformers import AutoTokenizer
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
try:
config_path = os.path.join(os.environ["EXP_PATH"], "config.yaml")
with open(config_path) as f:
config = yaml.safe_load(f)
except Exception:
print("NO CONFIG FOUND")
config = {}
device = "cuda" if torch.cuda.is_available() else "cpu"
# ========================== Base abstract model ========================== #
class BaseModel(abc.ABC):
def __init__(self, port_number=None):
self.port_number = port_number
@abc.abstractmethod
def forward(self, *args, **kwargs):
pass
# ========================== Specific Models ========================== #
class GPT(BaseModel):
def __init__(self, max_tries=1):
super().__init__()
self.temperature = config["llm"]["temperature"]
self.model = config["llm"]["model"]
self.max_tries = max_tries
@staticmethod
def generate(
prompt,
model,
frequency_penalty=0,
presence_penalty=0,
max_tokens=4096,
n=1,
temperature=1,
max_tries=3,
):
for _ in range(max_tries):
try:
completion = openai.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": prompt},
],
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
max_tokens=max_tokens,
n=n,
temperature=temperature,
)
output_message = completion.choices[0].message.content
return output_message
except Exception as e:
print("===")
print("ERROR:", e)
print("=== TRYING AGAIN ===")
continue
print("Unable to get response from LLM")
return None
@staticmethod
def batch_generate(x):
return GPT.generate(*x)
def forward(self, prompts: Union[List[str], str], process_name=None):
# batched
if isinstance(prompts, list):
with mp.Pool(processes=mp.cpu_count()) as pool:
response = pool.map(
self.batch_generate, [(prompt, self.model) for prompt in prompts]
)
else:
# single
response = GPT.generate(prompts, self.model)
return response
class GPT_4V(BaseModel):
def __init__(self):
super().__init__()
@staticmethod
def encode_image(image_file):
image_bytes = io.BytesIO()
image_file.save(image_bytes, format="PNG")
image_byte_array = image_bytes.getvalue()
return base64.b64encode(image_byte_array).decode("utf-8")
def construct_payload(self, image, prompt):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai.api_key}",
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image}",
"detail": "auto",
},
},
],
}
],
"max_tokens": 200,
}
return headers, payload
def forward(self, image_list, questions):
outputs = []
for image, question in zip(image_list, questions):
try:
base64_image = self.encode_image(image)
headers, payload = self.construct_payload(base64_image, question)
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=payload,
)
output = response.json()["choices"][0]["message"]["content"]
outputs.append(output)
except Exception as e:
print("ERROR:", e)
return None
return outputs
class LLaVA_13B(BaseModel):
def __init__(self, port_number=8000):
super().__init__()
self.url = f"http://localhost:{port_number}/generate"
@staticmethod
def encode_image_base64(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
@staticmethod
async def send_request(url, data, delay=0):
await asyncio.sleep(delay)
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as resp:
output = await resp.json()
return output
@staticmethod
async def run(url, image_list, questions):
response = []
for image, question in zip(image_list, questions):
payload = (
url,
{
"text": f"A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions. USER: <image>\n{question} ASSISTANT:",
"image_data": LLaVA_13B.encode_image_base64(image),
"sampling_params": {
"max_new_tokens": config["vlm"]["max_new_tokens"],
},
},
)
response.append(LLaVA_13B.send_request(*payload))
rets = await asyncio.gather(*response)
outputs = []
for ret in rets:
outputs.append(ret["text"])
response = None
return outputs
def forward(self, image_list, questions):
assert len(image_list) == len(questions)
return asyncio.run(self.run(self.url, image_list, questions))
class LLaVA_34B(BaseModel):
def __init__(self, port_number=8000):
super().__init__()
self.url = f"http://localhost:{port_number}/generate"
self.tokenizer = AutoTokenizer.from_pretrained("liuhaotian/llava-v1.6-34b")
@staticmethod
def encode_image_base64(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
@staticmethod
async def send_request(url, data, delay=0):
await asyncio.sleep(delay)
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as resp:
output = await resp.json()
return output
@staticmethod
async def run(url, image_list, chat_strs):
response = []
for image, chat_str in zip(image_list, chat_strs):
payload = (
url,
{
"text": chat_str,
"image_data": LLaVA_34B.encode_image_base64(image),
"sampling_params": {
"max_new_tokens": config["llava"]["max_new_tokens"],
},
},
)
response.append(LLaVA_34B.send_request(*payload))
rets = await asyncio.gather(*response)
outputs = []
for ret in rets:
outputs.append(ret["text"])
response = None
return outputs
def forward(self, image_list, questions):
assert len(image_list) == len(questions)
chat_strs = []
for question in questions:
chat = [
{"role": "system", "content": "Answer the question."},
{"role": "user", "content": "<image>\n" + question},
]
chat_str = self.tokenizer.apply_chat_template(chat, tokenize=False)
chat_str += "<|img_start|>assistant\n"
chat_strs.append(chat_str)
return asyncio.run(self.run(self.url, image_list, chat_strs))
class BLIP_2(BaseModel):
def __init__(self, port_number=8000):
super().__init__()
self.url = f"http://localhost:{port_number}/generate"
@staticmethod
def encode_image_base64(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
@staticmethod
async def send_request(url, data, delay=0):
await asyncio.sleep(delay)
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as resp:
output = await resp.json()
return output
@staticmethod
async def run(url, image_list, questions):
response = []
for image, question in zip(image_list, questions):
payload = (
url,
{
"text": f"{question}",
"image_data": BLIP_2.encode_image_base64(image),
"sampling_params": {
"max_new_tokens": config["llava"]["max_new_tokens"],
},
},
)
response.append(BLIP_2.send_request(*payload))
rets = await asyncio.gather(*response)
outputs = []
for ret in rets:
outputs.append(ret["text"])
response = None
return outputs
def forward(self, image_list, questions):
assert len(image_list) == len(questions)
return asyncio.run(self.run(self.url, image_list, questions))
class LaViLa(BaseModel):
def __init__(self, port_number=8000):
super().__init__()
self.url = f"http://localhost:{port_number}/generate"
@staticmethod
async def send_request(url, data, delay=0):
await asyncio.sleep(delay)
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as resp:
output = await resp.json()
return output
@staticmethod
async def run(url, frame_ids, path):
response = []
payload = (
url,
{
"video_name": path,
"frame_ids": frame_ids,
},
)
response.append(LaViLa.send_request(*payload))
rets = await asyncio.gather(*response)
outputs = []
for ret in rets:
outputs.append(ret["caption"])
response = None
return outputs
def forward(self, frame_ids, path):
return asyncio.run(self.run(self.url, frame_ids, path))
class Llama_3(BaseModel):
def __init__(self, port_number=8000, max_tries=1):
super().__init__()
self.temperature = config["gpt"]["temperature"]
# self.model = "NousResearch/Meta-Llama-3-8B-Instruct"
self.model = "NousResearch/Meta-Llama-3-70B-Instruct"
self.max_tries = max_tries
self.port_number = port_number
self.client = OpenAI(
base_url=f"http://localhost:{port_number}/v1",
api_key="token-abc123",
)
@staticmethod
def generate(
prompt,
model,
port_number,
frequency_penalty=0,
presence_penalty=0,
max_tokens=1000,
n=1,
temperature=1,
max_tries=3,
):
for _ in range(max_tries):
try:
client = OpenAI(
base_url=f"http://localhost:{port_number}/v1",
api_key="token-abc123",
)
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": prompt},
],
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
max_tokens=max_tokens,
n=n,
temperature=temperature,
)
output_message = completion.choices[0].message.content
return output_message
except Exception as e:
print("===")
print("ERROR:", e)
print("=== TRYING AGAIN ===")
continue
print("Unable to get response from LLM")
return None
@staticmethod
def batch_generate(x):
return Llama_3.generate(*x)
def forward(self, prompts: Union[List[str], str]):
# batched
if isinstance(prompts, list):
with mp.Pool(processes=mp.cpu_count()) as pool:
response = pool.map(
self.batch_generate,
[(prompt, self.model, self.port_number) for prompt in prompts],
)
else:
# single
response = Llama_3.generate(prompts, self.model, self.port_number)
return response