Image-Text-to-Text
Transformers
Safetensors
qwen3_5
clairic
cloudflare
systemone
multimodal
structured-output
information-extraction
custom-code
conversational
Instructions to use Cloudflare/clairic-flash with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Cloudflare/clairic-flash with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Cloudflare/clairic-flash") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Cloudflare/clairic-flash") model = AutoModelForMultimodalLM.from_pretrained("Cloudflare/clairic-flash", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Cloudflare/clairic-flash with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Cloudflare/clairic-flash" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Cloudflare/clairic-flash", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/Cloudflare/clairic-flash
- SGLang
How to use Cloudflare/clairic-flash with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Cloudflare/clairic-flash" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Cloudflare/clairic-flash", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Cloudflare/clairic-flash" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Cloudflare/clairic-flash", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use Cloudflare/clairic-flash with Docker Model Runner:
docker model run hf.co/Cloudflare/clairic-flash
File size: 5,721 Bytes
dc7d8aa | 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 | from __future__ import annotations
from collections.abc import Callable
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from .api import PredictRequest
from .backend import EncodedRequest
import math
import re
from PIL import Image, ImageChops, ImageStat
from .image_focus import focus_document
VIEW_ID = "identifier-content-band-v2"
_ROLE = re.compile(
"\\b(?:invoice|receipt|purchase order|order|tracking|reference|transaction|account|application)\\s+(?:number|identifier|id|code)\\b",
re.IGNORECASE,
)
_EXCLUDED_ROLE = re.compile(
"\\b(?:not|exclude|excluding|ignore|rather than)\\b[^.;:\\n]*$", re.IGNORECASE
)
def _requested_identifier(value: str) -> bool:
return any(
(
not _EXCLUDED_ROLE.search(value[: match.start()])
for match in _ROLE.finditer(value)
)
)
def fields(fields: dict[str, dict[str, Any]]) -> list[str]:
selected = []
for name, spec in fields.items():
if spec.get("type") != "text" or spec.get("knowledge"):
continue
label = re.sub("[_-]+", " ", name)
if _requested_identifier(label) or _requested_identifier(
spec.get("instructions", "")
):
selected.append(name)
return selected
def detail_view(image: Image.Image, *, max_pixels: int = 1600000) -> Image.Image:
return focus_document(image, max_pixels=max_pixels, max_scale=3.0)
def header_view(image: Image.Image, *, max_pixels: int = 1600000) -> Image.Image | None:
if image.mode != "RGB":
return None
if (
ImageChops.difference(
image, Image.new("RGB", image.size, image.getpixel((0, 0)))
).getbbox()
is None
):
return None
header = image.crop((0, 0, image.width, max(1, math.ceil(image.height * 0.4))))
scale = min(3.0, math.sqrt(max_pixels / (header.width * header.height)))
size = (max(1, int(header.width * scale)), max(1, int(header.height * scale)))
return header.resize(size, Image.Resampling.BICUBIC)
def views(
image: Image.Image,
*,
max_pixels: int = 1600000,
unfocused_image: Image.Image | None = None,
) -> tuple[Image.Image, ...]:
focused = (
image
if image is unfocused_image and max_pixels == 1600000
else detail_view(image, max_pixels=max_pixels)
)
if focused is not image:
return (focused,)
header = header_view(image, max_pixels=max_pixels)
return (image, header) if header is not None else (image,)
def content_view(
image: Image.Image, *, max_pixels: int = 1600000
) -> Image.Image | None:
if image.mode != "RGB":
return None
background = tuple(ImageStat.Stat(image.resize((64, 64))).median)
if min(background) < 160:
return None
difference = ImageChops.difference(image, Image.new("RGB", image.size, background))
channels = difference.split()
contrast = ImageChops.lighter(
ImageChops.lighter(channels[0], channels[1]), channels[2]
)
mask = contrast.point(lambda value: 255 if value >= 32 else 0)
bounds = mask.getbbox()
if bounds is None:
return None
(left, top, right, bottom) = bounds
bounds = (
max(0, left - 24),
max(0, top - 24),
min(image.width, right + 24),
min(image.height, bottom + 24),
)
(width, height) = (bounds[2] - bounds[0], bounds[3] - bounds[1])
if width * height > image.width * image.height / 2:
return None
scale = min(3.0, math.sqrt(max_pixels / (width * height)))
if scale <= 1:
return None
return image.crop(bounds).resize(
(max(1, int(width * scale)), max(1, int(height * scale))),
Image.Resampling.BICUBIC,
)
def content_views(
image: Image.Image,
*,
max_pixels: int = 1600000,
unfocused_image: Image.Image | None = None,
) -> tuple[Image.Image, ...]:
selected = (
views(image, max_pixels=max_pixels)
if unfocused_image is None
else views(image, max_pixels=max_pixels, unfocused_image=unfocused_image)
)
if selected[0] is not image:
return selected
content = content_view(image, max_pixels=max_pixels)
return (*selected, content) if content is not None else selected
def prepare(
request: PredictRequest,
encoded: EncodedRequest,
encode: Callable[..., EncodedRequest],
*,
unfocused_image: Image.Image | None = None,
) -> EncodedRequest:
selected = fields(request.fields)
if not selected or len(request.images) != 1 or request.documents:
return encoded
(record, forecasts, tasks, typed, eos) = encoded
child_fields = {name: request.fields[name] for name in selected}
child_request = request.model_copy(update={"fields": child_fields})
def image_views() -> tuple[Image.Image, ...]:
originals = record.get("_original_images")
image: Image.Image = originals[0] if originals else request.images[0].to_pil()
return (
content_views(image, unfocused_image=unfocused_image)
if image is unfocused_image
else content_views(image)
)
(child, _, replacements, _, _) = encode(
child_request, identifier_view=True, image_views=image_views
)
by_name = {task[0]: task for task in replacements}
tasks = [by_name.get(task[0], task) for task in tasks]
salts = dict(record.get("_ar_task_salts", {}))
for name in selected:
record["_ar_constraints"][name] = child["_ar_constraints"][name]
salts[name] = child["_cache_salt"]
record.update(
_ar_tasks=tasks, _ar_task_salts=salts, _ar_prefix=[], _ar_group_prefixes=[]
)
return (record, forecasts, tasks, typed, eos)
|