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_model.py from Cloudflare/clairic: direct link, hf CLI and curl.
- Browser
- Download file 4.76 kB
-
https://huggingface.co/Cloudflare/clairic/resolve/main/clairic_model.py
- Command line
-
hf download hf://Cloudflare/clairic/clairic_model.py
-
curl -L -o clairic_model.py https://huggingface.co/Cloudflare/clairic/resolve/main/clairic_model.py
4.76 kB
| """Clairic's Transformers loader and System One extraction interface.""" | |
| from __future__ import annotations | |
| import asyncio | |
| import base64 | |
| import io | |
| import json | |
| from pathlib import Path | |
| from typing import Any, Mapping | |
| from clairic_runtime.api import PredictRequest, serialize_response | |
| from clairic_runtime.backend import ClairicFlashModel | |
| from clairic_runtime.errors import ClairRequestError | |
| from clairic_runtime.large import Clairic27BModel | |
| from clairic_runtime.settings import RuntimeSettings | |
| def _request(value: Mapping[str, Any] | PredictRequest) -> PredictRequest: | |
| if isinstance(value, PredictRequest): | |
| return value | |
| from PIL import Image, ImageOps | |
| request = dict(value) | |
| if "state" in request and not isinstance(request["state"], str): | |
| request["state"] = json.dumps(request["state"], ensure_ascii=False) | |
| images = request.get("images") | |
| if isinstance(images, (list, tuple)): | |
| images = list(images) | |
| for index, image in enumerate(images): | |
| if isinstance(image, Image.Image): | |
| buffer = io.BytesIO() | |
| ImageOps.exif_transpose(image).convert("RGBA").save( | |
| buffer, format="PNG" | |
| ) | |
| images[index] = "data:image/png;base64," + base64.b64encode( | |
| buffer.getvalue() | |
| ).decode("ascii") | |
| request["images"] = images | |
| return PredictRequest.model_validate(request) | |
| class ClairicModel: | |
| """A model and its typed extraction head, with synchronous and async APIs.""" | |
| def __init__(self, runtime: ClairicFlashModel, public_name: str) -> None: | |
| self.runtime = runtime | |
| self.model_id = public_name | |
| names = {public_name, runtime.model_id} | |
| self._model_selectors = names | {"@cf/cloudflare/" + name for name in names} | |
| self.processor = runtime.processor | |
| self.backbone = runtime.engine.backbone | |
| self.head = runtime.engine.head | |
| self.device = runtime.engine.device | |
| async def predict_async( | |
| self, request: Mapping[str, Any] | PredictRequest | |
| ) -> dict[str, Any]: | |
| validated = _request(request) | |
| if validated.model is not None and validated.model not in self._model_selectors: | |
| raise ClairRequestError( | |
| f"Unsupported model '{validated.model}'. Use '{self.model_id}'." | |
| ) | |
| output = await self.runtime.predict_async(validated) | |
| return serialize_response(validated, output, self.model_id) | |
| def predict(self, request: Mapping[str, Any] | PredictRequest) -> dict[str, Any]: | |
| """Return structured values and scores for a System One: Extract request.""" | |
| try: | |
| asyncio.get_running_loop() | |
| except RuntimeError: | |
| return asyncio.run(self.predict_async(request)) | |
| raise RuntimeError( | |
| "Use await model.predict_async(request) inside an active event loop" | |
| ) | |
| def eval(self) -> ClairicModel: | |
| self.backbone.eval() | |
| self.head.eval() | |
| return self | |
| def close(self) -> None: | |
| self.runtime.shutdown() | |
| def load_release_model( | |
| model_path: str | Path, | |
| device: Any = "cuda", | |
| dtype: Any = None, | |
| max_length: int = 65_273, | |
| **from_pretrained_kwargs: Any, | |
| ) -> tuple[ClairicModel, Any]: | |
| """Load a local snapshot or a Hugging Face repository with Transformers.""" | |
| path = Path(model_path) | |
| if not path.is_dir(): | |
| from huggingface_hub import snapshot_download | |
| path = Path(snapshot_download(str(model_path))) | |
| configuration = json.loads((path / "clair_head_config.json").read_text()) | |
| large = int(configuration["hidden_size"]) == 5120 | |
| model_id = "clairic-27b" if large else "clairic-flash" | |
| settings = RuntimeSettings( | |
| model_path=str(path), | |
| version="1", | |
| model_id=model_id, | |
| max_length=max_length, | |
| max_concurrency=1, | |
| ) | |
| implementation = Clairic27BModel if large else ClairicFlashModel | |
| runtime = implementation.load( | |
| settings, device=str(device), dtype=dtype, **from_pretrained_kwargs | |
| ) | |
| return ClairicModel( | |
| runtime, "clairic" if large else "clairic-flash" | |
| ), runtime.processor | |
| def systemone( | |
| model: ClairicModel, | |
| processor: Any, | |
| request: Mapping[str, Any] | PredictRequest, | |
| max_length: int | None = None, | |
| ) -> dict[str, Any]: | |
| """Answer a System One: Extract request with typed fields and confidence.""" | |
| if processor is not model.processor: | |
| raise ValueError("Use the processor returned with this model") | |
| if max_length is not None and max_length != model.runtime.settings.max_length: | |
| raise ValueError("Set max_length when calling load_release_model") | |
| return model.predict(request) | |