Instructions to use alvarobartt/SmolVLM-Instruct-Handler with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use alvarobartt/SmolVLM-Instruct-Handler with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="alvarobartt/SmolVLM-Instruct-Handler") 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("alvarobartt/SmolVLM-Instruct-Handler") model = AutoModelForMultimodalLM.from_pretrained("alvarobartt/SmolVLM-Instruct-Handler", 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 alvarobartt/SmolVLM-Instruct-Handler with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "alvarobartt/SmolVLM-Instruct-Handler" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "alvarobartt/SmolVLM-Instruct-Handler", "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/alvarobartt/SmolVLM-Instruct-Handler
- SGLang
How to use alvarobartt/SmolVLM-Instruct-Handler 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 "alvarobartt/SmolVLM-Instruct-Handler" \ --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": "alvarobartt/SmolVLM-Instruct-Handler", "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 "alvarobartt/SmolVLM-Instruct-Handler" \ --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": "alvarobartt/SmolVLM-Instruct-Handler", "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 alvarobartt/SmolVLM-Instruct-Handler with Docker Model Runner:
docker model run hf.co/alvarobartt/SmolVLM-Instruct-Handler
File size: 3,982 Bytes
ff47bc8 89baa6d ff47bc8 64f317d ff47bc8 06eb103 ff47bc8 06eb103 d1cf6f0 ff47bc8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import torch
from transformers import AutoProcessor, AutoModelForVision2Seq, GenerationConfig
from transformers.image_utils import load_image
from typing import Any, Dict
import base64
import re
from copy import deepcopy
def is_base64(s: str) -> bool:
try:
return base64.b64encode(base64.b64decode(s)).decode() == s
except Exception:
return False
def is_url(s: str) -> bool:
url_pattern = re.compile(r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+")
return bool(url_pattern.match(s))
class EndpointHandler:
def __init__(
self,
model_dir: str = "HuggingFaceTB/SmolVLM-Instruct",
**kwargs: Any, # type: ignore
) -> None:
self.processor = AutoProcessor.from_pretrained(model_dir)
self.model = AutoModelForVision2Seq.from_pretrained(
model_dir,
torch_dtype=torch.bfloat16,
_attn_implementation="eager", # "flash_attention_2",
device_map="auto",
).eval()
self.generation_config = GenerationConfig.from_pretrained(model_dir)
def __call__(self, data: Dict[str, Any]) -> Any:
if "inputs" not in data:
raise ValueError(
"The request body must contain a key 'inputs' with a list of inputs."
)
if not isinstance(data["inputs"], list):
raise ValueError(
"The request inputs must be a list of dictionaries with the keys 'text' and 'images', being a"
" string with the prompt and a list with the image URLs or base64 encodings, respectively; and"
" optionally including the key 'generation_parameters' key too."
)
predictions = []
for input in data["inputs"]:
if "text" not in input:
raise ValueError(
"The request input body must contain the key 'text' with the prompt to use."
)
if "images" not in input or (
not isinstance(input["images"], list)
and all(isinstance(i, str) for i in input["images"])
):
raise ValueError(
"The request input body must contain the key 'images' with a list of strings,"
" where each string corresponds to an image on either base64 encoding, or provided"
" as a valid URL (needs to be publicly accessible and contain a valid image)."
)
images = []
for image in input["images"]:
try:
images.append(load_image(image))
except Exception as e:
raise ValueError(
f"Provided {image=} is not valid, please make sure that's either a base64 encoding"
f" of a valid image, or a publicly accesible URL to a valid image.\nFailed with {e=}."
)
generation_config = deepcopy(self.generation_config)
generation_config.update(**input.get("generation_parameters", {"max_new_tokens": 128}))
messages = [
{
"role": "user",
"content": [{"type": "image"} for _ in images]
+ [{"type": "text", "text": input["text"]}],
},
]
prompt = self.processor.apply_chat_template(
messages, add_generation_prompt=True
)
processed_inputs = self.processor(
text=prompt, images=images, return_tensors="pt"
).to(self.model.device)
generated_ids = self.model.generate(
**processed_inputs, generation_config=generation_config
)
generated_texts = self.processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)
predictions.append(generated_texts[0])
return {"predictions": predictions} |