Instructions to use mistral-experimental/pixtral-12b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mistral-experimental/pixtral-12b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="mistral-experimental/pixtral-12b") 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("mistral-experimental/pixtral-12b") model = AutoModelForMultimodalLM.from_pretrained("mistral-experimental/pixtral-12b", 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=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use mistral-experimental/pixtral-12b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mistral-experimental/pixtral-12b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mistral-experimental/pixtral-12b", "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/mistral-experimental/pixtral-12b
- SGLang
How to use mistral-experimental/pixtral-12b 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 "mistral-experimental/pixtral-12b" \ --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": "mistral-experimental/pixtral-12b", "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 "mistral-experimental/pixtral-12b" \ --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": "mistral-experimental/pixtral-12b", "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 mistral-experimental/pixtral-12b with Docker Model Runner:
docker model run hf.co/mistral-experimental/pixtral-12b
Find solution to use this version with text and image : fake_image ;)
Not a good choice but i don't find other choice
import torch
from transformers import AutoProcessor, LlavaForConditionalGeneration
from transformers import BitsAndBytesConfig
from PIL import Image
Configuration de quantification en 4 bits
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16
)
Chargement du modèle et du processeur avec la quantification en 4 bits
model_id = "mistral-community/pixtral-12b"
model = LlavaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.float16,
device_map="cuda:0", # Forcer l'utilisation de la 3090
quantization_config=quantization_config
)
processor = AutoProcessor.from_pretrained(model_id)
Fonction pour charger et traiter des images locales
def load_and_process_images(img_paths, target_size=(224, 224)):
images = []
for img_path in img_paths:
try:
img = Image.open(img_path).convert("RGB") # Convertir au format RGB
img = img.resize(target_size) # Redimensionner l'image
images.append(img)
except Exception as e:
print(f"Erreur lors du traitement de l'image {img_path} :", e)
return images
Test 1 : Description d'une image locale
try:
IMG_PATHS = ["robot.jpg"] # Chemin local de l'image
PROMPT = "[INST]Décrivez cette image.\n[IMG][/INST]"
images = load_and_process_images(IMG_PATHS)
inputs = processor(text=PROMPT, images=images, return_tensors="pt").to("cuda:0")
with torch.no_grad():
generate_ids = model.generate(**inputs, max_new_tokens=500)
output_description = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print("Test 1 - Description d'une image locale :", output_description)
except Exception as e:
print("Erreur lors du Test 1 :", e)
Test 2 : Répondre à une question avec une image locale
try:
IMG_PATHS = ["fake_image.jpg"] # Chemin local de l'image
PROMPT = "[INST]Quelle est la capitale de la France ?\n[IMG][/INST]"
images = load_and_process_images(IMG_PATHS)
inputs = processor(text=PROMPT, images=images, return_tensors="pt").to("cuda:0")
with torch.no_grad():
generate_ids = model.generate(**inputs, max_new_tokens=500)
output_question = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print("Test 2 - Question avec une image locale :", output_question)
except Exception as e:
print("Erreur lors du Test 2 :", e)