TIGER-Lab/VisualWebInstruct
Viewer • Updated • 1.91M • 1.23k • 41
How to use TIGER-Lab/MAmmoTH-VL2 with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="TIGER-Lab/MAmmoTH-VL2")
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 AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("TIGER-Lab/MAmmoTH-VL2")
model = AutoModelForCausalLM.from_pretrained("TIGER-Lab/MAmmoTH-VL2")
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 = tokenizer.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(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use TIGER-Lab/MAmmoTH-VL2 with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "TIGER-Lab/MAmmoTH-VL2"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "TIGER-Lab/MAmmoTH-VL2",
"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 run hf.co/TIGER-Lab/MAmmoTH-VL2
How to use TIGER-Lab/MAmmoTH-VL2 with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "TIGER-Lab/MAmmoTH-VL2" \
--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": "TIGER-Lab/MAmmoTH-VL2",
"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 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 "TIGER-Lab/MAmmoTH-VL2" \
--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": "TIGER-Lab/MAmmoTH-VL2",
"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"
}
}
]
}
]
}'How to use TIGER-Lab/MAmmoTH-VL2 with Docker Model Runner:
docker model run hf.co/TIGER-Lab/MAmmoTH-VL2
MAmmoTH-VL2, the model trained with VisualWebInstruct.
llava==1.7.0.dev0 # pip install git+https://github.com/LLaVA-VL/LLaVA-NeXT.git
torch==2.5.1
To perform inference using MAmmoTH-VL2, you can use the following code snippet:
from llava.model.builder import load_pretrained_model
from llava.mm_utils import process_images
from llava.constants import DEFAULT_IMAGE_TOKEN
from llava.conversation import conv_templates
from PIL import Image
import requests
import copy
import torch
# Load MAmmoTH-VL2 model
pretrained = "TIGER-Lab/MAmmoTH-VL2"
model_name = "llava_qwen"
device = "cuda:3" # Specify a single GPU
device_map = {"": device}
# Load model
tokenizer, model, image_processor, max_length = load_pretrained_model(
pretrained,
None,
model_name,
device_map=device_map,
multimodal=True
)
model.eval()
model = model.to(device)
# Load image
image_url = "https://raw.githubusercontent.com/jymmmmm/VISUALWEBINSTRUCT/main/image.png"
image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
images = [image]
image_sizes = [[image.size[0], image.size[1]]]
# Prepare prompt
prompt = "In the picture shown below, prove ΔWXY and ΔZWY are similar. Please conclude your answer as Answer: xxx at the end if possible."
# Set up conversation template
conv_template = "qwen_2_5"
conv = copy.deepcopy(conv_templates[conv_template])
# Add question with image
question = DEFAULT_IMAGE_TOKEN + "\n" + prompt
conv.append_message(conv.roles[0], question)
conv.append_message(conv.roles[1], None)
prompt_question = conv.get_prompt()
# Prepare model inputs
inputs = tokenizer(
prompt_question,
return_tensors="pt",
padding=True,
truncation=True,
max_length=max_length
)
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
# Process image
image_tensor = process_images(images, image_processor, model.config)
if isinstance(image_tensor, list):
image_tensor = [img.to(dtype=torch.float16, device=device) for img in image_tensor]
else:
image_tensor = image_tensor.to(dtype=torch.float16, device=device)
# Generate response
with torch.no_grad():
outputs = model.generate(
input_ids,
attention_mask=attention_mask,
images=image_tensor,
image_sizes=image_sizes,
do_sample=False,
temperature=0,
max_new_tokens=512,
)
# Decode response
input_token_len = input_ids.shape[1]
response = tokenizer.batch_decode(outputs[:, input_token_len:], skip_special_tokens=True)[0]
print("Response:", response)
@article{visualwebinstruct,
title={VisualWebInstruct: Scaling up Multimodal Instruction Data through Web Search},
author = {Jia, Yiming and Li, Jiachen and Yue, Xiang and Li, Bo and Nie, Ping and Zou, Kai and Chen, Wenhu},
journal={arXiv preprint arXiv:2503.10582},
year={2025}
}