Image-Text-to-Text
Transformers
Safetensors
English
granite4_vision
conversational
custom_code
8-bit precision
Instructions to use brainworkup/granite-vision-4.1-4b-oQ8 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use brainworkup/granite-vision-4.1-4b-oQ8 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="brainworkup/granite-vision-4.1-4b-oQ8", 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)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("brainworkup/granite-vision-4.1-4b-oQ8", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("brainworkup/granite-vision-4.1-4b-oQ8", 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?"} ] }, ] 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=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use brainworkup/granite-vision-4.1-4b-oQ8 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "brainworkup/granite-vision-4.1-4b-oQ8" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "brainworkup/granite-vision-4.1-4b-oQ8", "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/brainworkup/granite-vision-4.1-4b-oQ8
- SGLang
How to use brainworkup/granite-vision-4.1-4b-oQ8 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 "brainworkup/granite-vision-4.1-4b-oQ8" \ --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": "brainworkup/granite-vision-4.1-4b-oQ8", "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 "brainworkup/granite-vision-4.1-4b-oQ8" \ --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": "brainworkup/granite-vision-4.1-4b-oQ8", "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 brainworkup/granite-vision-4.1-4b-oQ8 with Docker Model Runner:
docker model run hf.co/brainworkup/granite-vision-4.1-4b-oQ8
| from fractions import Fraction | |
| from transformers import LlavaNextProcessor | |
| from transformers.image_processing_utils import select_best_resolution | |
| class Granite4VisionProcessor(LlavaNextProcessor): | |
| model_type = "granite4_vision" | |
| def __init__( | |
| self, | |
| image_processor=None, | |
| tokenizer=None, | |
| patch_size=None, | |
| vision_feature_select_strategy=None, | |
| chat_template=None, | |
| image_token="<image>", # set the default and let users change if they have peculiar special tokens in rare cases | |
| num_additional_image_tokens=0, | |
| downsample_rate=None, | |
| **kwargs, | |
| ): | |
| super().__init__(image_processor=image_processor, | |
| tokenizer=tokenizer, | |
| patch_size=patch_size, | |
| vision_feature_select_strategy=vision_feature_select_strategy, | |
| chat_template=chat_template, | |
| image_token=image_token, | |
| num_additional_image_tokens=num_additional_image_tokens, | |
| ) | |
| self.downsample_rate = downsample_rate | |
| def _get_number_of_features(self, orig_height: int, orig_width: int, height: int, width: int) -> int: | |
| image_grid_pinpoints = self.image_processor.image_grid_pinpoints | |
| height_best_resolution, width_best_resolution = select_best_resolution( | |
| [orig_height, orig_width], image_grid_pinpoints | |
| ) | |
| scale_height, scale_width = height_best_resolution // height, width_best_resolution // width | |
| patches_height = height // self.patch_size | |
| patches_width = width // self.patch_size | |
| if self.downsample_rate is not None: | |
| ds_rate = Fraction(self.downsample_rate) | |
| patches_height = int(patches_height * ds_rate) | |
| patches_width = int(patches_width * ds_rate) | |
| unpadded_features, newline_features = self._get_unpadded_features( | |
| orig_height, orig_width, patches_height, patches_width, scale_height, scale_width | |
| ) | |
| # The base patch covers the entire image (+1 for the CLS) | |
| base_features = patches_height * patches_width + self.num_additional_image_tokens | |
| num_image_tokens = unpadded_features + newline_features + base_features | |
| return num_image_tokens | |