-
Notifications
You must be signed in to change notification settings - Fork 84
/
lmm.py
492 lines (450 loc) · 16.5 KB
/
lmm.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import asyncio
import base64
import json
import re
from typing import Any, Dict, List, Literal, Optional, Type, Union
from openai import AsyncOpenAI
from pydantic import BaseModel, ConfigDict, Field
from inference.core.entities.requests.cogvlm import CogVLMInferenceRequest
from inference.core.env import (
LOCAL_INFERENCE_API_URL,
WORKFLOWS_REMOTE_API_TARGET,
WORKFLOWS_REMOTE_EXECUTION_MAX_STEP_CONCURRENT_REQUESTS,
)
from inference.core.managers.base import ModelManager
from inference.core.utils.image_utils import encode_image_to_jpeg_bytes, load_image
from inference.core.workflows.constants import PARENT_ID_KEY, ROOT_PARENT_ID_KEY
from inference.core.workflows.core_steps.common.entities import StepExecutionMode
from inference.core.workflows.core_steps.common.utils import load_core_model
from inference.core.workflows.entities.base import (
Batch,
OutputDefinition,
WorkflowImageData,
)
from inference.core.workflows.entities.types import (
BATCH_OF_DICTIONARY_KIND,
BATCH_OF_IMAGE_METADATA_KIND,
BATCH_OF_PARENT_ID_KIND,
BATCH_OF_STRING_KIND,
DICTIONARY_KIND,
STRING_KIND,
WILDCARD_KIND,
ImageInputField,
StepOutputImageSelector,
WorkflowImageSelector,
WorkflowParameterSelector,
)
from inference.core.workflows.prototypes.block import (
BlockResult,
WorkflowBlock,
WorkflowBlockManifest,
)
from inference_sdk import InferenceHTTPClient
from inference_sdk.http.utils.iterables import make_batches
GPT_4V_MODEL_TYPE = "gpt_4v"
COG_VLM_MODEL_TYPE = "cog_vlm"
NOT_DETECTED_VALUE = "not_detected"
JSON_MARKDOWN_BLOCK_PATTERN = re.compile(r"```json\n([\s\S]*?)\n```")
class LMMConfig(BaseModel):
max_tokens: int = Field(default=450)
gpt_image_detail: Literal["low", "high", "auto"] = Field(
default="auto",
description="To be used for GPT-4V only.",
)
gpt_model_version: str = Field(default="gpt-4o")
LONG_DESCRIPTION = """
Ask a question to a Large Multimodal Model (LMM) with an image and text.
You can specify arbitrary text prompts to an LMMBlock.
The LLMBlock supports two LMMs:
- OpenAI's GPT-4 with Vision, and;
- CogVLM.
You need to provide your OpenAI API key to use the GPT-4 with Vision model. You do not
need to provide an API key to use CogVLM.
_If you want to classify an image into one or more categories, we recommend using the
dedicated LMMForClassificationBlock._
"""
class BlockManifest(WorkflowBlockManifest):
model_config = ConfigDict(
json_schema_extra={
"name": "LMM",
"short_description": "Run a large multimodal model such as ChatGPT-4v or CogVLM.",
"long_description": LONG_DESCRIPTION,
"license": "Apache-2.0",
"block_type": "model",
}
)
type: Literal["LMM"]
images: Union[WorkflowImageSelector, StepOutputImageSelector] = ImageInputField
prompt: Union[WorkflowParameterSelector(kind=[STRING_KIND]), str] = Field(
description="Holds unconstrained text prompt to LMM mode",
examples=["my prompt", "$inputs.prompt"],
)
lmm_type: Union[
WorkflowParameterSelector(kind=[STRING_KIND]), Literal["gpt_4v", "cog_vlm"]
] = Field(
description="Type of LMM to be used", examples=["gpt_4v", "$inputs.lmm_type"]
)
lmm_config: LMMConfig = Field(
default_factory=lambda: LMMConfig(), description="Configuration of LMM"
)
remote_api_key: Union[
WorkflowParameterSelector(kind=[STRING_KIND]), Optional[str]
] = Field(
default=None,
description="Holds API key required to call LMM model - in current state of development, we require OpenAI key when `lmm_type=gpt_4v` and do not require additional API key for CogVLM calls.",
examples=["xxx-xxx", "$inputs.api_key"],
)
json_output: Optional[Dict[str, str]] = Field(
default=None,
description="Holds dictionary that maps name of requested output field into its description",
examples=[{"count": "number of cats in the picture"}, "$inputs.json_output"],
)
@classmethod
def accepts_batch_input(cls) -> bool:
return True
@classmethod
def describe_outputs(cls) -> List[OutputDefinition]:
return [
OutputDefinition(name="parent_id", kind=[BATCH_OF_PARENT_ID_KIND]),
OutputDefinition(name="root_parent_id", kind=[BATCH_OF_PARENT_ID_KIND]),
OutputDefinition(name="image", kind=[BATCH_OF_IMAGE_METADATA_KIND]),
OutputDefinition(name="structured_output", kind=[BATCH_OF_DICTIONARY_KIND]),
OutputDefinition(name="raw_output", kind=[BATCH_OF_STRING_KIND]),
OutputDefinition(name="*", kind=[WILDCARD_KIND]),
]
def get_actual_outputs(self) -> List[OutputDefinition]:
result = [
OutputDefinition(name="parent_id", kind=[BATCH_OF_PARENT_ID_KIND]),
OutputDefinition(name="root_parent_id", kind=[BATCH_OF_PARENT_ID_KIND]),
OutputDefinition(name="image", kind=[BATCH_OF_IMAGE_METADATA_KIND]),
OutputDefinition(name="structured_output", kind=[DICTIONARY_KIND]),
OutputDefinition(name="raw_output", kind=[STRING_KIND]),
]
if self.json_output is None:
return result
for key in self.json_output.keys():
result.append(OutputDefinition(name=key, kind=[WILDCARD_KIND]))
return result
class LMMBlock(WorkflowBlock):
def __init__(
self,
model_manager: ModelManager,
api_key: Optional[str],
step_execution_mode: StepExecutionMode,
):
self._model_manager = model_manager
self._api_key = api_key
self._step_execution_mode = step_execution_mode
@classmethod
def get_init_parameters(cls) -> List[str]:
return ["model_manager", "api_key", "step_execution_mode"]
@classmethod
def get_manifest(cls) -> Type[WorkflowBlockManifest]:
return BlockManifest
async def run(
self,
images: Batch[WorkflowImageData],
prompt: str,
lmm_type: str,
lmm_config: LMMConfig,
remote_api_key: Optional[str],
json_output: Optional[Dict[str, str]],
) -> BlockResult:
if self._step_execution_mode is StepExecutionMode.LOCAL:
return await self.run_locally(
images=images,
prompt=prompt,
lmm_type=lmm_type,
lmm_config=lmm_config,
remote_api_key=remote_api_key,
json_output=json_output,
)
elif self._step_execution_mode is StepExecutionMode.REMOTE:
return await self.run_remotely(
images=images,
prompt=prompt,
lmm_type=lmm_type,
lmm_config=lmm_config,
remote_api_key=remote_api_key,
json_output=json_output,
)
else:
raise ValueError(
f"Unknown step execution mode: {self._step_execution_mode}"
)
async def run_locally(
self,
images: Batch[WorkflowImageData],
prompt: str,
lmm_type: str,
lmm_config: LMMConfig,
remote_api_key: Optional[str],
json_output: Optional[Dict[str, str]],
) -> BlockResult:
if json_output:
prompt = (
f"{prompt}\n\nVALID response format is JSON:\n"
f"{json.dumps(json_output, indent=4)}"
)
images_prepared_for_processing = [
image.to_inference_format(numpy_preferred=True) for image in images
]
if lmm_type == GPT_4V_MODEL_TYPE:
raw_output = await run_gpt_4v_llm_prompting(
image=images_prepared_for_processing,
prompt=prompt,
remote_api_key=remote_api_key,
lmm_config=lmm_config,
)
else:
raw_output = await get_cogvlm_generations_locally(
image=images_prepared_for_processing,
prompt=prompt,
model_manager=self._model_manager,
api_key=self._api_key,
)
structured_output = turn_raw_lmm_output_into_structured(
raw_output=raw_output,
expected_output=json_output,
)
predictions = [
{
"raw_output": raw["content"],
"image": raw["image"],
"structured_output": structured,
**structured,
}
for raw, structured in zip(raw_output, structured_output)
]
for prediction, image in zip(predictions, images):
prediction[PARENT_ID_KEY] = image.parent_metadata.parent_id
prediction[ROOT_PARENT_ID_KEY] = (
image.workflow_root_ancestor_metadata.parent_id
)
return predictions
async def run_remotely(
self,
images: Batch[WorkflowImageData],
prompt: str,
lmm_type: str,
lmm_config: LMMConfig,
remote_api_key: Optional[str],
json_output: Optional[Dict[str, str]],
) -> BlockResult:
if json_output:
prompt = (
f"{prompt}\n\nVALID response format is JSON:\n"
f"{json.dumps(json_output, indent=4)}"
)
inference_images = [i.to_inference_format() for i in images]
if lmm_type == GPT_4V_MODEL_TYPE:
raw_output = await run_gpt_4v_llm_prompting(
image=inference_images,
prompt=prompt,
remote_api_key=remote_api_key,
lmm_config=lmm_config,
)
else:
raw_output = await get_cogvlm_generations_from_remote_api(
image=inference_images,
prompt=prompt,
api_key=self._api_key,
)
structured_output = turn_raw_lmm_output_into_structured(
raw_output=raw_output,
expected_output=json_output,
)
predictions = [
{
"raw_output": raw["content"],
"image": raw["image"],
"structured_output": structured,
**structured,
}
for raw, structured in zip(raw_output, structured_output)
]
for prediction, image in zip(predictions, images):
prediction[PARENT_ID_KEY] = image.parent_metadata.parent_id
prediction[ROOT_PARENT_ID_KEY] = (
image.workflow_root_ancestor_metadata.parent_id
)
return predictions
async def run_gpt_4v_llm_prompting(
image: List[Dict[str, Any]],
prompt: str,
remote_api_key: Optional[str],
lmm_config: LMMConfig,
) -> List[Dict[str, str]]:
if remote_api_key is None:
raise ValueError(
"Step that involves GPT-4V prompting requires OpenAI API key which was not provided."
)
return await execute_gpt_4v_requests(
image=image,
remote_api_key=remote_api_key,
prompt=prompt,
lmm_config=lmm_config,
)
async def execute_gpt_4v_requests(
image: List[dict],
remote_api_key: str,
prompt: str,
lmm_config: LMMConfig,
) -> List[Dict[str, str]]:
client = AsyncOpenAI(api_key=remote_api_key)
results = []
images_batches = list(
make_batches(
iterable=image,
batch_size=WORKFLOWS_REMOTE_EXECUTION_MAX_STEP_CONCURRENT_REQUESTS,
)
)
for image_batch in images_batches:
batch_coroutines = []
for image in image_batch:
coroutine = execute_gpt_4v_request(
client=client,
image=image,
prompt=prompt,
lmm_config=lmm_config,
)
batch_coroutines.append(coroutine)
batch_results = await asyncio.gather(*batch_coroutines)
results.extend(batch_results)
return results
async def execute_gpt_4v_request(
client: AsyncOpenAI,
image: Dict[str, Any],
prompt: str,
lmm_config: LMMConfig,
) -> Dict[str, str]:
loaded_image, _ = load_image(image)
image_metadata = {"width": loaded_image.shape[1], "height": loaded_image.shape[0]}
base64_image = base64.b64encode(encode_image_to_jpeg_bytes(loaded_image)).decode(
"ascii"
)
response = await client.chat.completions.create(
model=lmm_config.gpt_model_version,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
"detail": lmm_config.gpt_image_detail,
},
},
],
}
],
max_tokens=lmm_config.max_tokens,
)
return {"content": response.choices[0].message.content, "image": image_metadata}
async def get_cogvlm_generations_locally(
image: List[dict],
prompt: str,
model_manager: ModelManager,
api_key: Optional[str],
) -> List[Dict[str, Any]]:
serialised_result = []
for single_image in image:
loaded_image, _ = load_image(single_image)
image_metadata = {
"width": loaded_image.shape[1],
"height": loaded_image.shape[0],
}
inference_request = CogVLMInferenceRequest(
image=single_image,
prompt=prompt,
api_key=api_key,
)
model_id = load_core_model(
model_manager=model_manager,
inference_request=inference_request,
core_model="cogvlm",
)
result = await model_manager.infer_from_request(model_id, inference_request)
serialised_result.append(
{
"content": result.response,
"image": image_metadata,
}
)
return serialised_result
async def get_cogvlm_generations_from_remote_api(
image: List[dict],
prompt: str,
api_key: Optional[str],
) -> List[Dict[str, Any]]:
if WORKFLOWS_REMOTE_API_TARGET == "hosted":
raise ValueError(
f"Chosen remote execution of CogVLM model in Roboflow Hosted API mode, but remote execution "
f"is only possible for self-hosted option."
)
client = InferenceHTTPClient.init(
api_url=LOCAL_INFERENCE_API_URL,
api_key=api_key,
)
raw_output = []
images_batches = list(
make_batches(
iterable=image,
batch_size=WORKFLOWS_REMOTE_EXECUTION_MAX_STEP_CONCURRENT_REQUESTS,
)
)
for image_batch in images_batches:
batch_coroutines, batch_image_metadata = [], []
for image in image_batch:
loaded_image, _ = load_image(image)
image_metadata = {
"width": loaded_image.shape[1],
"height": loaded_image.shape[0],
}
batch_image_metadata.append(image_metadata)
coroutine = client.prompt_cogvlm_async(
visual_prompt=image["value"],
text_prompt=prompt,
)
batch_coroutines.append(coroutine)
batch_results = await asyncio.gather(*batch_coroutines)
raw_output.extend(
[
{"content": br["response"], "image": bm}
for br, bm in zip(batch_results, batch_image_metadata)
]
)
return raw_output
def turn_raw_lmm_output_into_structured(
raw_output: List[Dict[str, Any]],
expected_output: Optional[Dict[str, str]],
) -> List[dict]:
if expected_output is None:
return [{} for _ in range(len(raw_output))]
return [
try_parse_lmm_output_to_json(
output=r["content"],
expected_output=expected_output,
)
for r in raw_output
]
def try_parse_lmm_output_to_json(
output: str, expected_output: Dict[str, str]
) -> Union[list, dict]:
json_blocks_found = JSON_MARKDOWN_BLOCK_PATTERN.findall(output)
if len(json_blocks_found) == 0:
return try_parse_json(output, expected_output=expected_output)
result = []
for json_block in json_blocks_found:
result.append(
try_parse_json(content=json_block, expected_output=expected_output)
)
return result if len(result) > 1 else result[0]
def try_parse_json(content: str, expected_output: Dict[str, str]) -> dict:
try:
data = json.loads(content)
return {key: data.get(key, NOT_DETECTED_VALUE) for key in expected_output}
except Exception:
return {key: NOT_DETECTED_VALUE for key in expected_output}