SmolVLM-500M-Instruct fine-tuned with LoRA on
ChartQA for chart question answering. LoRA adapters were merged into the base model for single-artifact deployment.
1from PIL import Image
2import torch
3from transformers import AutoProcessor, AutoModelForImageTextToText
4
5model_id = "VulcanRaven/ChartQA-smolvlm"
6processor = AutoProcessor.from_pretrained(model_id)
7model = AutoModelForImageTextToText.from_pretrained(
8 model_id, torch_dtype=torch.bfloat16
9).cuda().eval()
10
11image = Image.open("chart.png").convert("RGB")
12query = "What is the highest value shown in the chart?"
13
14messages = [{
15 "role": "user",
16 "content": [
17 {"type": "image"},
18 {"type": "text", "text": f"Question: {query}\nAnswer:"}
19 ]
20}]
21text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
22inputs = processor(text=text, images=image, return_tensors="pt").to("cuda")
23inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
24
25with torch.inference_mode():
26 gen_ids = model.generate(**inputs, max_new_tokens=32, do_sample=False)
27answer = processor.tokenizer.decode(
28 gen_ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True
29).strip()
30print(f"Q: {query}\nA: {answer}")
1from peft import PeftModel
2from transformers import AutoModelForImageTextToText, AutoProcessor
3import torch
4
5base = AutoModelForImageTextToText.from_pretrained(
6 "HuggingFaceTB/SmolVLM-500M-Instruct", torch_dtype=torch.bfloat16
7)
8model = PeftModel.from_pretrained(base, "VulcanRaven/ChartQA-smolvlm")
9model = model.merge_and_unload().cuda().eval()
10processor = AutoProcessor.from_pretrained("VulcanRaven/ChartQA-smolvlm")
11
12# then run inference as above