To use the multimodal capabilities of this model and use vision you need to load the specified mmproj file, this can be found inside this model repo. (LeroyDyer/Mixtral_AI_Vision-Instruct_X)
You can load the mmproj by using the corresponding section in the interface:
image/png
Vision/multimodal capabilities:
For loading 4-bit use 4-bit mmproj file.- mmproj-Mixtral_AI_Vision-Instruct_X-Q4_0
For loading 8-bit use 8 bit mmproj file - mmproj-Mixtral_AI_Vision-Instruct_X-Q8_0
For loading 8-bit use 8 bit mmproj file - mmproj-Mixtral_AI_Vision-Instruct_X-f16
1from transformers import AutoProcessor, LlavaForConditionalGeneration
2from transformers import BitsAndBytesConfig
3import torch
45quantization_config = BitsAndBytesConfig(6 load_in_4bit=True,7 bnb_4bit_compute_dtype=torch.float16
8)91011model_id ="LeroyDyer/Mixtral_AI_Vision-Instruct_X"1213processor = AutoProcessor.from_pretrained(model_id)14model = LlavaForConditionalGeneration.from_pretrained(model_id, quantization_config=quantization_config, device_map="auto")151617import requests
18from PIL import Image
1920image1 = Image.open(requests.get("https://llava-vl.github.io/static/images/view.jpg", stream=True).raw)21image2 = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw)22display(image1)23display(image2)2425prompts =[26"USER: <image>\nWhat are the things I should be cautious about when I visit this place? What should I bring with me?\nASSISTANT:",27"USER: <image>\nPlease describe this image\nASSISTANT:",28]2930inputs = processor(prompts, images=[image1, image2], padding=True, return_tensors="pt").to("cuda")31for k,v in inputs.items():32print(k,v.shape)33
Using pipeline
python
12from transformers import pipeline
3from PIL import Image
4import requests
56model_id = LeroyDyer/Mixtral_AI_Vision-Instruct_X
7pipe = pipeline("image-to-text", model=model_id)8url ="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"910image = Image.open(requests.get(url, stream=True).raw)11question ="What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"12prompt = f"A chat between a curious human and an artificial intelligence assistant.13 The assistant gives helpful, detailed,and polite answers to the human's questions.###Human: <image>\n{question}###Assistant:"1415outputs = pipe(image, prompt=prompt, generate_kwargs={"max_new_tokens":200})16print(outputs)
Mistral ChatTemplating
Instruction format
In order to leverage instruction fine-tuning,
your prompt should be surrounded by [INST] and [/INST] tokens.
The very first instruction should begin with a begin of sentence id. The next instructions should not.
The assistant generation will be ended by the end-of-sentence token id.
python
1from transformers import AutoTokenizer
2tokenizer = AutoTokenizer.from_pretrained("LeroyDyer/Mixtral_AI_Vision-Instruct_X")34chat =[5{"role":"user","content":"Hello, how are you?"},6{"role":"assistant","content":"I'm doing great. How can I help you today?"},7{"role":"user","content":"I'd like to show off how chat templating works!"},8]910tokenizer.apply_chat_template(chat, tokenize=False)11
TextToText
python
1from transformers import AutoModelForCausalLM, AutoTokenizer
23device ="cuda"# the device to load the model onto45model = AutoModelForCausalLM.from_pretrained("LeroyDyer/Mixtral_AI_Vision-Instruct_X")6tokenizer = AutoTokenizer.from_pretrained("LeroyDyer/Mixtral_AI_Vision-Instruct_X")78messages =[9{"role":"user","content":"What is your favourite condiment?"},10{"role":"assistant","content":"Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},11{"role":"user","content":"Do you have mayonnaise recipes?"}12]1314encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")1516model_inputs = encodeds.to(device)17model.to(device)1819generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)20decoded = tokenizer.batch_decode(generated_ids)21print(decoded[0])
This mistral model was trained 2x faster with Unsloth and Huggingface's TRL library.