Views
No views yet






1import requests
2from PIL import Image
3from io import BytesIO
4import base64
5import matplotlib.pyplot as plt
6from vllm import LLM, SamplingParams
7
8QA_PROMPT = """Please answer the question using the chart image.
9
10Question: [QUESTION]
11
12Please first generate your reasoning process and then provide the user with the answer. Use the following format:
13
14<think>
15... your thinking process here ...
16</think>
17<answer>
18... your final answer (entity(s) or number) ...
19</answer>"""
20
21def get_image_from_url(image_url):
22 try:
23 response = requests.get(image_url, stream=True)
24 response.raise_for_status()
25 return Image.open(BytesIO(response.content))
26 except Exception as e:
27 print(f"Error with image: {e}")
28 return None
29
30def get_answer(image_url, question, display=True):
31 image = get_image_from_url(image_url)
32
33 if display:
34 plt.figure(figsize=(10, 8))
35 plt.imshow(image)
36 plt.axis('off')
37 plt.show()
38
39 if not image:
40 return "Error downloading image"
41
42 buffered = BytesIO()
43 image.save(buffered, format=image.format or 'JPEG')
44 encoded_image = base64.b64encode(buffered.getvalue()).decode('utf-8')
45
46 messages = [{
47 "role": "user",
48 "content": [
49 {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}},
50 {"type": "text", "text": QA_PROMPT.replace("[QUESTION]", question)}
51 ]
52 }]
53
54 response = llm.chat([messages], sampling_params=SamplingParams(temperature=0, max_tokens=500))
55 return response[0].outputs[0].text
56
57# Initialize the LLM
58llm = LLM(
59 model="bespokelabs/Bespoke-MiniChart-7B",
60 tokenizer_mode="auto",
61 max_model_len=15000,
62 tensor_parallel_size=1,
63 gpu_memory_utilization=0.9,
64 mm_processor_kwargs={"max_pixels": 1600*28*28},
65 seed=2025,
66 trust_remote_code=True,
67)
68
69# Running inference
70image_url = "https://github.com/bespokelabsai/minichart-playground-examples/blob/main/images/ilyc9wk4jf8b1.png?raw=true"
71question = "How many global regions maintained their startup funding losses below 30% in 2022?"
72
73print("\n\n=================Model Output:===============\n\n", get_answer(image_url, question))@misc{bespoke_minichart_7b,
title = {Bespoke-MiniChart-7B: pushing the frontiers of open VLMs for chart understanding},
author = {Liyan Tang and Shreyas Pimpalgaonkar and Kartik Sharma and Alexandros G. Dimakis and Mahesh Sathiamoorthy and Greg Durrett},
howpublished = {blog post},
year = {2025},
url={https://huggingface.co/bespokelabs/Bespoke-MiniChart-7B},
}