Views
No views yet

1from transformers import AutoProcessor, AutoModelForImageTextToText
2import torch
3
4model_path = "HuggingFaceTB/SmolVLM2-2.2B-Base"
5processor = AutoProcessor.from_pretrained(model_path)
6model = AutoModelForImageTextToText.from_pretrained(
7 model_path,
8 torch_dtype=torch.bfloat16,
9 _attn_implementation="flash_attention_2"
10).to("cuda")1messages = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
6 {"type": "text", "text": "Can you describe this image?"},
7 ]
8 },
9]
10
11inputs = processor.apply_chat_template(
12 messages,
13 add_generation_prompt=True,
14 tokenize=True,
15 return_dict=True,
16 return_tensors="pt",
17).to(model.device, dtype=torch.bfloat16)
18
19generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
20generated_texts = processor.batch_decode(
21 generated_ids,
22 skip_special_tokens=True,
23)
24print(generated_texts[0])1messages = [
2 {
3 "role": "user",
4 "content": [
5 {"type": "video", "path": "path_to_video.mp4"},
6 {"type": "text", "text": "Describe this video in detail"}
7 ]
8 },
9]
10
11inputs = processor.apply_chat_template(
12 messages,
13 add_generation_prompt=True,
14 tokenize=True,
15 return_dict=True,
16 return_tensors="pt",
17).to(model.device, dtype=torch.bfloat16)
18
19generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
20generated_texts = processor.batch_decode(
21 generated_ids,
22 skip_special_tokens=True,
23)
24
25print(generated_texts[0])1import torch
2
3
4messages = [
5 {
6 "role": "user",
7 "content": [
8 {"type": "text", "text": "What is the similarity between these two images?"},
9 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
10 {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"},
11 ]
12 },
13]
14
15inputs = processor.apply_chat_template(
16 messages,
17 add_generation_prompt=True,
18 tokenize=True,
19 return_dict=True,
20 return_tensors="pt",
21).to(model.device, dtype=torch.bfloat16)
22
23generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
24generated_texts = processor.batch_decode(
25 generated_ids,
26 skip_special_tokens=True,
27)
28print(generated_texts[0])1@article{marafioti2025smolvlm,
2 title={SmolVLM: Redefining small and efficient multimodal models},
3 author={Andrés Marafioti and Orr Zohar and Miquel Farré and Merve Noyan and Elie Bakouch and Pedro Cuenca and Cyril Zakka and Loubna Ben Allal and Anton Lozhkov and Nouamane Tazi and Vaibhav Srivastav and Joshua Lochner and Hugo Larcher and Mathieu Morlon and Lewis Tunstall and Leandro von Werra and Thomas Wolf},
4 journal={arXiv preprint arXiv:2504.05299},
5 year={2025}
6}