Instructions to use inferencerlabs/granite-vision-4.1-4b-MLX-Q9 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use inferencerlabs/granite-vision-4.1-4b-MLX-Q9 with MLX:
# Make sure mlx-vlm is installed # pip install --upgrade mlx-vlm from mlx_vlm import load, generate from mlx_vlm.prompt_utils import apply_chat_template from mlx_vlm.utils import load_config # Load the model model, processor = load("inferencerlabs/granite-vision-4.1-4b-MLX-Q9") config = load_config("inferencerlabs/granite-vision-4.1-4b-MLX-Q9") # Prepare input image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] prompt = "Describe this image." # Apply chat template formatted_prompt = apply_chat_template( processor, config, prompt, num_images=1 ) # Generate output output = generate(model, processor, formatted_prompt, image) print(output) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Pi
How to use inferencerlabs/granite-vision-4.1-4b-MLX-Q9 with Pi:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "inferencerlabs/granite-vision-4.1-4b-MLX-Q9"
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "mlx-lm": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "inferencerlabs/granite-vision-4.1-4b-MLX-Q9" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Hermes Agent
How to use inferencerlabs/granite-vision-4.1-4b-MLX-Q9 with Hermes Agent:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "inferencerlabs/granite-vision-4.1-4b-MLX-Q9"
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default inferencerlabs/granite-vision-4.1-4b-MLX-Q9
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use inferencerlabs/granite-vision-4.1-4b-MLX-Q9 with OpenClaw:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "inferencerlabs/granite-vision-4.1-4b-MLX-Q9"
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:8080/v1 \ --custom-model-id "inferencerlabs/granite-vision-4.1-4b-MLX-Q9" \ --custom-provider-id mlx-lm \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
Upload model file
Browse files- processing.py +54 -0
processing.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fractions import Fraction
|
| 2 |
+
|
| 3 |
+
from transformers import LlavaNextProcessor
|
| 4 |
+
from transformers.image_processing_utils import select_best_resolution
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class Granite4VisionProcessor(LlavaNextProcessor):
|
| 9 |
+
model_type = "granite4_vision"
|
| 10 |
+
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
image_processor=None,
|
| 14 |
+
tokenizer=None,
|
| 15 |
+
patch_size=None,
|
| 16 |
+
vision_feature_select_strategy=None,
|
| 17 |
+
chat_template=None,
|
| 18 |
+
image_token="<image>", # set the default and let users change if they have peculiar special tokens in rare cases
|
| 19 |
+
num_additional_image_tokens=0,
|
| 20 |
+
downsample_rate=None,
|
| 21 |
+
**kwargs,
|
| 22 |
+
):
|
| 23 |
+
super().__init__(image_processor=image_processor,
|
| 24 |
+
tokenizer=tokenizer,
|
| 25 |
+
patch_size=patch_size,
|
| 26 |
+
vision_feature_select_strategy=vision_feature_select_strategy,
|
| 27 |
+
chat_template=chat_template,
|
| 28 |
+
image_token=image_token,
|
| 29 |
+
num_additional_image_tokens=num_additional_image_tokens,
|
| 30 |
+
)
|
| 31 |
+
self.downsample_rate = downsample_rate
|
| 32 |
+
|
| 33 |
+
def _get_number_of_features(self, orig_height: int, orig_width: int, height: int, width: int) -> int:
|
| 34 |
+
image_grid_pinpoints = self.image_processor.image_grid_pinpoints
|
| 35 |
+
|
| 36 |
+
height_best_resolution, width_best_resolution = select_best_resolution(
|
| 37 |
+
[orig_height, orig_width], image_grid_pinpoints
|
| 38 |
+
)
|
| 39 |
+
scale_height, scale_width = height_best_resolution // height, width_best_resolution // width
|
| 40 |
+
|
| 41 |
+
patches_height = height // self.patch_size
|
| 42 |
+
patches_width = width // self.patch_size
|
| 43 |
+
if self.downsample_rate is not None:
|
| 44 |
+
ds_rate = Fraction(self.downsample_rate)
|
| 45 |
+
patches_height = int(patches_height * ds_rate)
|
| 46 |
+
patches_width = int(patches_width * ds_rate)
|
| 47 |
+
|
| 48 |
+
unpadded_features, newline_features = self._get_unpadded_features(
|
| 49 |
+
orig_height, orig_width, patches_height, patches_width, scale_height, scale_width
|
| 50 |
+
)
|
| 51 |
+
# The base patch covers the entire image (+1 for the CLS)
|
| 52 |
+
base_features = patches_height * patches_width + self.num_additional_image_tokens
|
| 53 |
+
num_image_tokens = unpadded_features + newline_features + base_features
|
| 54 |
+
return num_image_tokens
|