Image-Text-to-Text
Transformers
Safetensors
qwen3_5
document-question-answering
multi-page-documents
custom_code
conversational
Instructions to use mossbee/DocSEAM with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mossbee/DocSEAM with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="mossbee/DocSEAM", trust_remote_code=True) 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, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("mossbee/DocSEAM", trust_remote_code=True) model = AutoModelForImageTextToText.from_pretrained("mossbee/DocSEAM", trust_remote_code=True, 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 mossbee/DocSEAM with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mossbee/DocSEAM" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mossbee/DocSEAM", "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/mossbee/DocSEAM
- SGLang
How to use mossbee/DocSEAM 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 "mossbee/DocSEAM" \ --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": "mossbee/DocSEAM", "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 "mossbee/DocSEAM" \ --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": "mossbee/DocSEAM", "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 mossbee/DocSEAM with Docker Model Runner:
docker model run hf.co/mossbee/DocSEAM
Download modeling_docseam.py from mossbee/DocSEAM: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/mossbee/DocSEAM/resolve/main/modeling_docseam.py
- Command line
-
hf download hf://mossbee/DocSEAM/modeling_docseam.py
-
curl -L -o modeling_docseam.py https://huggingface.co/mossbee/DocSEAM/resolve/main/modeling_docseam.py
3.88 kB
| """Transformers integration for DocSEAM on the Qwen3.5 backbone.""" | |
| from contextvars import ContextVar | |
| import torch.nn.functional as F | |
| from torch import nn | |
| from transformers.models.qwen3_5.modeling_qwen3_5 import ( | |
| Qwen3_5ForConditionalGeneration, | |
| Qwen3_5Model, | |
| Qwen3_5PreTrainedModel, | |
| ) | |
| from .docseam import DocSEAM | |
| _page_counts = ContextVar("docseam_page_counts", default=None) | |
| class DocSEAMModel(Qwen3_5Model): | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.docseam = DocSEAM( | |
| hidden_size=config.text_config.hidden_size, | |
| num_heads=config.text_config.num_attention_heads, | |
| intermediate_size=4096, | |
| num_layers=config.docseam_num_layers, | |
| cap_frac=config.docseam_cap_frac, | |
| ) | |
| self.docseam.pos_feature_mode = config.docseam_pos_feature_mode | |
| self.docseam.use_checkpoint = False | |
| def forward(self, input_ids=None, position_ids=None, pixel_values=None, **kwargs): | |
| pages = None | |
| if pixel_values is not None and input_ids is not None: | |
| image_starts = ( | |
| (input_ids[:, :-1] == self.config.vision_start_token_id) | |
| & (input_ids[:, 1:] == self.config.image_token_id) | |
| ) | |
| pages = [count for count in image_starts.sum(dim=1).tolist() if count] | |
| token = _page_counts.set(pages) | |
| try: | |
| return super().forward( | |
| input_ids=input_ids, position_ids=position_ids, pixel_values=pixel_values, **kwargs | |
| ) | |
| finally: | |
| _page_counts.reset(token) | |
| def get_video_features(self, pixel_values_videos, video_grid_thw=None, **kwargs): | |
| return super().get_image_features(pixel_values_videos, video_grid_thw, **kwargs) | |
| def get_image_features(self, pixel_values, image_grid_thw=None, **kwargs): | |
| return_dict = kwargs.pop("return_dict", getattr(self.config, "return_dict", True)) | |
| output = super().get_image_features(pixel_values, image_grid_thw, return_dict=True, **kwargs) | |
| pages = _page_counts.get() | |
| if pages is not None: | |
| if sum(pages) != len(output.pooler_output): | |
| raise ValueError("DocSEAM page counts do not match the encoded images.") | |
| grids = image_grid_thw.tolist() | |
| merge = self.visual.spatial_merge_size | |
| output.pooler_output = tuple(self.docseam( | |
| list(output.pooler_output), | |
| page_grid_dims=[(h // merge, w // merge) for _, h, w in grids], | |
| pages_per_document=pages, | |
| page_ink_masks=_ink_masks(pixel_values, grids, merge), | |
| )) | |
| return output if return_dict else output.to_tuple() | |
| def _ink_masks(pixel_values, grids, merge): | |
| """Reproduce the Stage B runtime ink estimates used by the frontier gates.""" | |
| masks = [] | |
| start = 0 | |
| for t, h, w in grids: | |
| stop = start + t * h * w | |
| patches = pixel_values[start:stop].detach().float() | |
| start = stop | |
| rows, cols = h // merge, w // merge | |
| darkness = ((1.0 - patches).clamp(0.0, 2.0) * 0.5).mean(dim=-1) | |
| darkness = darkness.view(t, h, w).mean(dim=0) | |
| darkness = darkness[: rows * merge, : cols * merge] | |
| darkness = darkness.reshape(rows, merge, cols, merge).mean(dim=(1, 3)) | |
| darkness = F.avg_pool2d(darkness[None, None], kernel_size=3, stride=1, padding=1)[0, 0] | |
| masks.append(((darkness - 0.01) / 0.12).clamp(0.0, 1.0).reshape(-1).to(pixel_values.dtype)) | |
| return masks | |
| class DocSEAMForConditionalGeneration(Qwen3_5ForConditionalGeneration): | |
| def __init__(self, config): | |
| Qwen3_5PreTrainedModel.__init__(self, config) | |
| self.model = DocSEAMModel(config) | |
| self.lm_head = nn.Linear(config.text_config.hidden_size, config.text_config.vocab_size, bias=False) | |
| self.post_init() | |