File size: 12,680 Bytes
19757f6 | 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 | # /// script
# requires-python = ">=3.11"
# dependencies = [
# "datasets>=4.0.0",
# "huggingface-hub",
# "pillow",
# "vllm",
# "transformers",
# "tqdm",
# "toolz",
# "torch",
# ]
# ///
"""
Extract structured JSON from images using LiquidAI's LFM2.5-VL-1.6B-Extract with vLLM.
LFM2.5-VL-1.6B-Extract (1.6B = LFM2 1.2B LM + SigLIP2 0.4B vision) is a compact
vision-language model purpose-built for *schema-guided* extraction: you give it a
list of fields, it returns a flat JSON object with those fields filled from the image.
It reports 99.6 JSON-validity / F1 on its benchmark, beating similarly-sized VLMs.
Unlike the markdown-OCR scripts here, this one needs a SCHEMA (a field list). Pass
`--schema` as inline JSON, a URL, or a file path, mapping field names to short
descriptions:
--schema '{"invoice_number": "the invoice number", "total": "the total amount"}'
Model: https://huggingface.co/LiquidAI/LFM2.5-VL-1.6B-Extract
Docs: https://docs.liquid.ai/deployment/gpu-inference/vllm
HF Jobs note: run on the vLLM image so the CUDA toolkit + prebuilt FlashInfer kernels
are present and startup is fast (it reuses the image's CUDA-matched vLLM build):
hf jobs uv run --flavor l4x1 --secrets HF_TOKEN \
--image vllm/vllm-openai --python /usr/bin/python3 \
-e PYTHONPATH=/usr/local/lib/python3.12/dist-packages \
https://huggingface.co/datasets/uv-scripts/ocr/raw/main/lfm2-vl-extract.py \
INPUT OUTPUT --schema '{"title": "the document title", "date": "any date shown"}'
It also runs on the default uv image, just with a slower first-time vLLM build. Deps are
left unpinned so uv resolves a vLLM that supports the LFM2-VL (transformers 5) architecture,
and FlashInfer sampling is disabled (VLLM_USE_FLASHINFER_SAMPLER=0, see below) so the engine
never JIT-compiles a kernel that needs nvcc — absent from the default image.
"""
import argparse
import base64
import io
import json
import logging
import os
import sys
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Union
from urllib.request import urlopen
# Disable vLLM's FlashInfer top-k/top-p sampler before the engine starts: it JIT-compiles
# at warmup and needs nvcc (absent from the default uv image). Harmless for greedy decoding.
os.environ.setdefault("VLLM_USE_FLASHINFER_SAMPLER", "0")
import torch
from datasets import load_dataset
from huggingface_hub import DatasetCard, login
from PIL import Image
from toolz import partition_all
from tqdm import tqdm
from vllm import LLM, SamplingParams
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
DEFAULT_MODEL = "LiquidAI/LFM2.5-VL-1.6B-Extract"
def check_cuda_availability() -> None:
if not torch.cuda.is_available():
logger.error("CUDA is not available. This script requires a GPU.")
logger.error("Run on Hugging Face Jobs with: hf jobs uv run --flavor l4x1 ...")
sys.exit(1)
logger.info(f"CUDA is available. GPU: {torch.cuda.get_device_name()}")
def load_schema_arg(value: str) -> Dict[str, str]:
"""Resolve --schema (inline JSON, URL, or file path) into a {field: description} dict."""
text = value.strip()
if text.startswith("http://") or text.startswith("https://"):
logger.info(f"Loading schema from URL: {text}")
text = urlopen(text).read().decode("utf-8")
elif not text.startswith("{") and not text.startswith("["):
if os.path.exists(text):
logger.info(f"Loading schema from file: {text}")
with open(text) as f:
text = f.read()
parsed = json.loads(text)
# Accept {"field": "description"} or ["field1", "field2"]
if isinstance(parsed, list):
return {str(field): "" for field in parsed}
if isinstance(parsed, dict):
return {str(k): str(v) for k, v in parsed.items()}
raise ValueError("--schema must be a JSON object {field: description} or a JSON list of field names.")
def build_system_prompt(schema: Dict[str, str]) -> str:
"""LFM2.5-VL-Extract prompt: a field list in the system message → flat JSON out."""
lines = []
for field, desc in schema.items():
lines.append(f"{field}: {desc}" if desc else field)
fields_block = "\n".join(lines)
return (
f"Extract the following from the image:\n\n{fields_block}\n\n"
"Respond with only a JSON object."
)
def image_to_data_uri(image: Union[Image.Image, Dict[str, Any], str]) -> str:
if isinstance(image, dict) and "bytes" in image:
image = Image.open(io.BytesIO(image["bytes"]))
elif isinstance(image, str):
image = Image.open(image)
if image.mode != "RGB":
image = image.convert("RGB")
buf = io.BytesIO()
image.save(buf, format="PNG")
return f"data:image/png;base64,{base64.b64encode(buf.getvalue()).decode()}"
def make_message(image: Any, system_prompt: str) -> List[Dict]:
data_uri = image_to_data_uri(image)
return [
{"role": "system", "content": system_prompt},
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": data_uri}}]},
]
def parse_json_output(text: str) -> tuple[Optional[Any], bool]:
"""Return (parsed, ok). Strips ```json fences if present."""
stripped = text.strip()
if stripped.startswith("```"):
stripped = stripped.split("\n", 1)[-1]
if stripped.endswith("```"):
stripped = stripped.rsplit("```", 1)[0]
stripped = stripped.strip()
try:
return json.loads(stripped), True
except (json.JSONDecodeError, ValueError):
return None, False
def main(
input_dataset: str,
output_dataset: str,
schema: str,
image_column: str = "image",
output_column: str = "extraction",
split: str = "train",
max_samples: Optional[int] = None,
shuffle: bool = False,
seed: int = 42,
batch_size: int = 16,
model: str = DEFAULT_MODEL,
max_model_len: int = 4096,
max_tokens: int = 1024,
private: bool = False,
hf_token: Optional[str] = None,
) -> None:
check_cuda_availability()
HF_TOKEN = hf_token or os.environ.get("HF_TOKEN")
if HF_TOKEN:
login(token=HF_TOKEN)
schema_dict = load_schema_arg(schema)
system_prompt = build_system_prompt(schema_dict)
logger.info(f"Extraction fields: {list(schema_dict.keys())}")
logger.info(f"Loading dataset: {input_dataset} (split={split})")
dataset = load_dataset(input_dataset, split=split)
if shuffle:
dataset = dataset.shuffle(seed=seed)
if max_samples:
dataset = dataset.select(range(min(max_samples, len(dataset))))
logger.info(f"Processing {len(dataset)} examples")
if image_column not in dataset.column_names:
logger.error(f"Image column '{image_column}' not found. Columns: {dataset.column_names}")
sys.exit(1)
logger.info(f"Loading model: {model}")
llm = LLM(
model=model,
max_model_len=max_model_len,
limit_mm_per_prompt={"image": 1},
enforce_eager=True,
)
sampling_params = SamplingParams(temperature=0.0, max_tokens=max_tokens)
all_outputs: List[str] = []
n_valid = 0
images = dataset[image_column]
for batch in tqdm(list(partition_all(batch_size, images)), desc="Extracting"):
batch_messages = [make_message(img, system_prompt) for img in batch]
outputs = llm.chat(batch_messages, sampling_params)
for out in outputs:
text = out.outputs[0].text.strip()
parsed, ok = parse_json_output(text)
if ok:
n_valid += 1
all_outputs.append(json.dumps(parsed, ensure_ascii=False))
else:
all_outputs.append(text) # keep raw on parse failure
logger.info(f"Valid JSON: {n_valid}/{len(all_outputs)}")
dataset = dataset.add_column(output_column, all_outputs)
inference_entry = {
"model": model,
"column_name": output_column,
"task": "schema-guided extraction",
"fields": list(schema_dict.keys()),
"timestamp": datetime.now(timezone.utc).isoformat(),
"script": "lfm2-vl-extract.py",
}
if "inference_info" in dataset.column_names:
def update_info(example):
try:
existing = json.loads(example["inference_info"]) if example["inference_info"] else []
except (json.JSONDecodeError, TypeError):
existing = []
existing.append(inference_entry)
return {"inference_info": json.dumps(existing)}
dataset = dataset.map(update_info)
else:
dataset = dataset.add_column(
"inference_info", [json.dumps([inference_entry])] * len(dataset)
)
logger.info(f"Pushing to {output_dataset}")
dataset.push_to_hub(output_dataset, private=private, token=HF_TOKEN)
card_text = f"""---
tags:
- uv-script
- extraction
- lfm2-vl
- json
---
# Structured extraction with LFM2.5-VL-1.6B-Extract
JSON fields extracted from images in [{input_dataset}](https://huggingface.co/datasets/{input_dataset})
using [{model}](https://huggingface.co/{model}).
- **Source**: `{input_dataset}` (split `{split}`)
- **Model**: `{model}`
- **Fields**: {", ".join(f"`{k}`" for k in schema_dict.keys())}
- **Output column**: `{output_column}` (JSON string per row)
- **Valid JSON**: {n_valid}/{len(all_outputs)}
- **Date**: {datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")}
Generated with the [uv-scripts/ocr](https://huggingface.co/datasets/uv-scripts/ocr) `lfm2-vl-extract.py` script.
"""
try:
card = DatasetCard(card_text)
card.push_to_hub(output_dataset, token=HF_TOKEN)
except Exception as e:
logger.warning(f"Could not push dataset card: {e}")
logger.info("Done! Extraction complete.")
logger.info(f"Dataset: https://huggingface.co/datasets/{output_dataset}")
if __name__ == "__main__":
if len(sys.argv) == 1:
print("LFM2.5-VL-1.6B-Extract — schema-guided JSON extraction from images")
print("\nUsage:")
print(" uv run lfm2-vl-extract.py INPUT OUTPUT --schema SCHEMA [options]")
print("\nExample:")
print(' uv run lfm2-vl-extract.py my-images my-extractions \\')
print(' --schema \'{"title": "the document title", "date": "any date shown"}\'')
print("\n --schema accepts inline JSON, a URL, or a file path.")
print("\nFor full help: uv run lfm2-vl-extract.py --help")
sys.exit(0)
parser = argparse.ArgumentParser(
description="Schema-guided JSON extraction from images using LFM2.5-VL-1.6B-Extract",
)
parser.add_argument("input_dataset", help="Input dataset ID (with images)")
parser.add_argument("output_dataset", help="Output dataset ID")
parser.add_argument(
"--schema", required=True,
help="Fields to extract: inline JSON {field: description}, a URL, or a file path",
)
parser.add_argument("--image-column", default="image", help="Image column (default: image)")
parser.add_argument("--output-column", default="extraction", help="Output column (default: extraction)")
parser.add_argument("--split", default="train", help="Dataset split (default: train)")
parser.add_argument("--max-samples", type=int, help="Limit number of samples")
parser.add_argument("--shuffle", action="store_true", help="Shuffle before sampling")
parser.add_argument("--seed", type=int, default=42, help="Shuffle seed (default: 42)")
parser.add_argument("--batch-size", type=int, default=16, help="Batch size (default: 16)")
parser.add_argument("--model", default=DEFAULT_MODEL, help=f"Model (default: {DEFAULT_MODEL})")
parser.add_argument("--max-model-len", type=int, default=4096, help="Max context length (default: 4096)")
parser.add_argument("--max-tokens", type=int, default=1024, help="Max output tokens (default: 1024)")
parser.add_argument("--private", action="store_true", help="Make output dataset private")
parser.add_argument("--hf-token", help="HF token (or set HF_TOKEN)")
args = parser.parse_args()
main(
input_dataset=args.input_dataset,
output_dataset=args.output_dataset,
schema=args.schema,
image_column=args.image_column,
output_column=args.output_column,
split=args.split,
max_samples=args.max_samples,
shuffle=args.shuffle,
seed=args.seed,
batch_size=args.batch_size,
model=args.model,
max_model_len=args.max_model_len,
max_tokens=args.max_tokens,
private=args.private,
hf_token=args.hf_token,
)
|