Image-Text-to-Text
Transformers
Safetensors
qwen3_5
clairic
cloudflare
systemone
multimodal
structured-output
information-extraction
custom-code
conversational
Instructions to use Cloudflare/clairic with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Cloudflare/clairic with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Cloudflare/clairic") 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") model = AutoModelForMultimodalLM.from_pretrained("Cloudflare/clairic", 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 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Cloudflare/clairic" # 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", "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
- SGLang
How to use Cloudflare/clairic 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" \ --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", "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" \ --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", "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 with Docker Model Runner:
docker model run hf.co/Cloudflare/clairic
Download clairic_runtime/large.py from Cloudflare/clairic: direct link, hf CLI and curl.
- Browser
- Download file 8.11 kB
-
https://huggingface.co/Cloudflare/clairic/resolve/main/clairic_runtime/large.py
- Command line
-
hf download hf://Cloudflare/clairic/clairic_runtime/large.py
-
curl -L -o large.py https://huggingface.co/Cloudflare/clairic/resolve/main/clairic_runtime/large.py
8.11 kB
| from __future__ import annotations | |
| from typing import TYPE_CHECKING | |
| from . import media_state_prompt, po_image_supplement, text_ar, text_list_completion | |
| from . import grounding, identifier_spelling | |
| from .backend import ClairicFlashModel | |
| from .media import media_warmup_groups | |
| if TYPE_CHECKING: | |
| from typing import Any | |
| from .api import PredictRequest | |
| from .backend import EncodedRequest | |
| class Clairic27BModel(ClairicFlashModel): | |
| RELEASE_MODEL_ID = "clairic-27b" | |
| COMPACT_LIST_PROMPTS = False | |
| DATE_ROLE_GUIDANCE = False | |
| SOURCE_ROW_ELIGIBILITY = True | |
| FINANCIAL_SERVICE_GUIDANCE = False | |
| FINANCIAL_FEE_GUIDANCE = True | |
| SPELL_VISUAL_PO_REFERENCES = True | |
| FOCUS_SPARSE_IMAGES = False | |
| PRESERVE_IMAGE_ENTITY_NAMES = True | |
| FOCUS_IMAGE_PARTY_NAMES = False | |
| LITERAL_IMAGE_PAYMENT_ROWS = True | |
| BIND_IMAGE_FINANCIAL_LINES = False | |
| IMAGE_TABLE_ROW_SCALE = 4 | |
| BIND_IMAGE_RAGGED_ROWS = False | |
| def encode(self, request: PredictRequest, **options: Any) -> EncodedRequest: | |
| supplement_disabled = options.pop("po_supplement_disabled", False) | |
| encoded = self._encode_without_po_supplement(request, **options) | |
| detail = any( | |
| ( | |
| options.get(name) | |
| for name in ( | |
| "identifier_view", | |
| "party_name_view", | |
| "physical_row_view", | |
| "identifier_spelling_disabled", | |
| "pdf_spelling_view", | |
| "po_detail_view", | |
| ) | |
| ) | |
| ) | |
| if ( | |
| type(self) is not Clairic27BModel | |
| or supplement_disabled | |
| or detail | |
| or (not po_image_supplement.selected_fields(request, encoded)) | |
| ): | |
| return encoded | |
| if ( | |
| self.settings.model_id != "clairic-27b" | |
| or self.max_concurrency != self.settings.max_concurrency | |
| or self.max_concurrency != 1 | |
| ): | |
| return encoded | |
| return po_image_supplement.prepare( | |
| request, | |
| encoded, | |
| encode_child=lambda child: self.encode(child, po_supplement_disabled=True), | |
| tokenizer=self.tokenizer, | |
| max_length=self.settings.max_length, | |
| media_groups=media_warmup_groups, | |
| ) | |
| def _encode_without_po_supplement( | |
| self, request: PredictRequest, **options: Any | |
| ) -> EncodedRequest: | |
| detail = any( | |
| ( | |
| options.get(name) | |
| for name in ( | |
| "identifier_view", | |
| "party_name_view", | |
| "physical_row_view", | |
| "identifier_spelling_disabled", | |
| "pdf_spelling_view", | |
| "po_detail_view", | |
| ) | |
| ) | |
| ) | |
| if type(self) is Clairic27BModel and options.get("po_detail_view"): | |
| options = {**options, "pdf_spelling_view": True} | |
| encoded = super().encode(request, **options) | |
| if type(self) is not Clairic27BModel or detail: | |
| return encoded | |
| (record, forecasts, tasks, typed, eos) = encoded | |
| active = {name for (name, _, _) in tasks} | |
| contracts = record.get("_meta", {}).get("identifier_spelling", {}) | |
| po_fields = { | |
| name: request.fields[name] | |
| for (name, contract) in contracts.items() | |
| if name in active | |
| and contract.get("wire") == identifier_spelling.VERSION | |
| and (len(request.images) in (1, 2)) | |
| and (not request.documents) | |
| } | |
| number_fields = { | |
| name: spec | |
| for (name, spec) in request.fields.items() | |
| if name in active | |
| and spec.get("type") == "number" | |
| and (spec.get("source_only") is True) | |
| and (not spec.get("knowledge")) | |
| and (not grounding.state_only(spec)) | |
| and (len(request.images) > 1 or request.documents) | |
| } | |
| selected = {**po_fields, **number_fields} | |
| if not selected: | |
| return encoded | |
| originals = record.get("_original_images", ()) | |
| if len(originals) < len(request.images) or ( | |
| not request.documents and len(originals) != len(request.images) | |
| ): | |
| raise ValueError( | |
| "27B scalar detail must retain every original image in order" | |
| ) | |
| (child, _, replacements, child_typed, child_eos) = super().encode( | |
| request.model_copy(update={"fields": selected}), | |
| identifier_view=True, | |
| pdf_spelling_view=True, | |
| image_views=lambda: originals, | |
| image_view_key="27b-literal-scalars-all-content2-v1", | |
| ) | |
| if child_typed is not None or child_eos != eos: | |
| raise ValueError( | |
| "27B scalar detail must retain the original scalar AR contract" | |
| ) | |
| by_name = {task[0]: task for task in replacements} | |
| if set(by_name) != set(selected) or len(replacements) != len(selected): | |
| raise ValueError( | |
| "27B scalar detail tasks differ from selected public fields" | |
| ) | |
| updated = [by_name.get(task[0], task) for task in tasks] | |
| salts = dict(record.get("_ar_task_salts", {})) | |
| for name in selected: | |
| record["fields"][name] = child["fields"][name] | |
| record["_ar_constraints"][name] = child["_ar_constraints"][name] | |
| salts[name] = child["_cache_salt"] | |
| record["_meta"]["model_document_scalar_view"] = { | |
| "version": "27b-literal-scalars-all-content2-v1", | |
| "fields": list(selected), | |
| "po_fields": list(po_fields), | |
| "number_fields": list(number_fields), | |
| "image_count": len(originals), | |
| } | |
| record.update( | |
| _ar_tasks=updated, | |
| _ar_task_salts=salts, | |
| _ar_prefix=[], | |
| _ar_group_prefixes=[], | |
| ) | |
| record["_ar_media_groups"] = media_warmup_groups( | |
| updated, | |
| default_salt=record["_cache_salt"], | |
| task_salts=salts, | |
| annotations=record["_meta"]["extraction_contract"]["fields"], | |
| ) | |
| return (record, forecasts, updated, typed, eos) | |
| def _encode_request(self, request: PredictRequest) -> EncodedRequest: | |
| encoded = super()._encode_request(request) | |
| if ( | |
| type(self) is Clairic27BModel | |
| and self.settings.model_id == "clairic-27b" | |
| and (self.max_concurrency == self.settings.max_concurrency == 1) | |
| and bool( | |
| getattr(request, "images", ()) or getattr(request, "documents", ()) | |
| ) | |
| ): | |
| encoded[0]["_first_item_list_gating"] = True | |
| fallback = encoded[0].get("_identifier_spelling_fallback") | |
| if fallback is not None: | |
| fallback["_first_item_list_gating"] = True | |
| encoded = media_state_prompt.prepare( | |
| request, | |
| encoded, | |
| tokenizer=self.tokenizer, | |
| max_length=self.settings.max_length, | |
| media_groups=media_warmup_groups, | |
| ) | |
| return encoded | |
| async def _ar_all( | |
| self, record: dict[str, Any], eos: int | |
| ) -> dict[str, dict[str, Any]]: | |
| caller_record = record | |
| if len(record["_ar_tasks"]) < 64 and (not record.get("_media_payload")): | |
| record = {**record, "_ar_prefix": None, "_ar_group_prefixes": []} | |
| if ( | |
| type(self) is Clairic27BModel | |
| and self.settings.model_id == "clairic-27b" | |
| and (self.max_concurrency == self.settings.max_concurrency == 1) | |
| and text_list_completion.eligible(record, record["_ar_tasks"]) | |
| ): | |
| predictions = await text_ar.text_ar_all(self, record, eos) | |
| if record is not caller_record: | |
| caller_record["_unexecuted_list_items"] = record[ | |
| "_unexecuted_list_items" | |
| ] | |
| return predictions | |
| return await super()._ar_all(record, eos) | |