Instructions to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="HuggingFaceTB/SmolVLM2-2.2B-Instruct") 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, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct") model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct") 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
- vLLM
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "HuggingFaceTB/SmolVLM2-2.2B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "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/HuggingFaceTB/SmolVLM2-2.2B-Instruct
- SGLang
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct 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 "HuggingFaceTB/SmolVLM2-2.2B-Instruct" \ --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": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "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 "HuggingFaceTB/SmolVLM2-2.2B-Instruct" \ --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": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "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 HuggingFaceTB/SmolVLM2-2.2B-Instruct with Docker Model Runner:
docker model run hf.co/HuggingFaceTB/SmolVLM2-2.2B-Instruct
Several questions on the same video
Hello,
How can I ask several questions on the same video?
I tried this:
try:
messages = [
{
"role": "user",
"content": [
{"type": "video", "path": video_path},
{"type": "text", "text": "Describe this video"}
]
},
{
"role": "user",
"content": [
{"type": "video", "path": video_path},
{"type": "text", "text": "how many people on the video ?"}
]
},
{
"role": "user",
"content": [
{"type": "video", "path": video_path},
{"type": "text", "text": "Is there any violence on the video ? (answer "yes" or "no"."}
]
}
]
But, i have a bug:
(smolvlm2-env) F:\CODE\smolvlm2>python smol4.py
Loading checkpoint shards: 100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 2/2 [00:03<00:00, 1.54s/it]
----- DΓ©but de l'itΓ©ration 1 -----
Message: {'role': 'user', 'content': [{'type': 'video', 'path': 'P:\1.mp4'}, {'type': 'text', 'text': 'Describe this video'}]}
Erreur: string indices must be integers
(it works with a single question)
Thx in advance
Hi @delphijb ,
I have been trying to see how this can be supported. For batch inference, right now you need to do this: (UPDATE: this is already supported, just need to add padding=True)
video_path= "example.mp4"
conversation1 = [
{
"role": "user",
"content": [
{"type": "video", "path": video_path},
{"type": "text", "text": "Describe this video"}
]
}
]
conversation2 = [
{
"role": "user",
"content": [
{"type": "video", "path": video_path},
{"type": "text", "text": "Summarize this video"}
]
}
]
conversations = [conversation1, conversation2]
batched_inputs = processor.apply_chat_template(
conversations,
add_generation_prompt=True,
tokenize=True,
padding=True,
return_dict=True,
return_tensors="pt"
).to(model.device, dtype=torch.bfloat16)
batch_inputs.append(inputs)
torch.cuda.reset_peak_memory_stats()
generated_ids = model.generate(**batched_inputs, do_sample=True, max_new_tokens=1024)
peak_mem = torch.cuda.max_memory_allocated()
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
print(generated_texts[0], '\n\n', generated_texts[1])
print(f"Peak GPU memory: {peak_mem / 1024**3:.2f} GB")
The main issue with asking multiple questions about one video is that the "video" is part of the user prompt. Therefore, this is not possible to support, as you basically want to "swap out" part of the user prompt and keep the other parts the same. As such, I think batch inference is basically what you would get anyway, as you would need to duplicate the image/video anyway.
You could also use the multi-turn conversation capability of the model, but this is sequential, not parallel.