Views
No views yet
sanchit97/chart-rvr-3b model for chart-based reasoning using a vision-language interface. It loads a chart image from a URL, prompts the model with a question, and extracts the structured reasoning and final answer.<think> and <answer> tags. Inside <think>, it outputs the chart type, the data table in JSON, and reasoning steps.1from transformers import AutoProcessor, AutoModelForVision2Seq
2from PIL import Image
3import requests
4from io import BytesIO
5import torch
6from qwen_vl_utils import process_vision_info # helper from Qwen repo
7
8# Load processor and model
9processor = AutoProcessor.from_pretrained("sanchit97/chart-rvr-3b")
10model = AutoModelForVision2Seq.from_pretrained(
11 "sanchit97/chart-rvr-3b", device_map="auto", torch_dtype=torch.bfloat16
12)
13
14# Define the structured system prompt
15SYSTEM_PROMPT = """
16You are a vision-language assistant. You are given a chart image and a query about the chart.
17Think step-by-step about how to answer the query based on the chart image and then provide the final answer.
18
19### Output format
20Respond **with exactly two blocks in order and nothing else**:
21<think>
22First output the type of chart in <type>, \
23then output the underlying data table and finally, \
24think step-by-step about how to answer the query based on the chart image \
25and then provide the final answer.
26<type>
27Type of chart - one word from line, bar, stacked bar, pie, histogram, scatterplot, area, stacked area, bubble, treemap.
28</type>
29Next output the data table in the <table></table> tags
30<table>
31json table - for the chart image, output only a JSON object with: "columns": list of column headers, "rows": list-of-lists, one per data row
32No prose, no comments.
331. Respond with **only** a JSON object
342. The JSON must use exactly this schema:
35 {
36 "columns": [...],
37 "rows": [[...], [...],..., [...]]
38 }
393. Do NOT output HTML, Markdown, or commentary. Any deviation gets zero reward.
40</table>
41Provide your reasoning here in steps:
42<step-1>: Provide a description of reasoning
43<step-2>: Gather ALL the appropriate data from the chart
44<step-3>: Break down the query into smaller parts and verify each part with the data
45...
46<step-n>: Do the final calculation or reasoning to derive the answer
47</think>
48<answer>
49Final answer on a single line
50</answer>
51"""
52
53# Chart image from URL
54image_url = "https://mathmonks.com/wp-content/uploads/2023/01/Parts-Bar-Graph.jpg"
55response = requests.get(image_url)
56image = Image.open(BytesIO(response.content)).convert("RGB")
57
58# Query about the chart
59prompt = "What is the average of all the bars in the chart?"
60
61# Build multimodal chat input
62messages = [
63 {
64 "role": "system",
65 "content": SYSTEM_PROMPT
66 },
67 {
68 "role": "user",
69 "content": [
70 {"type": "image", "image": image},
71 {"type": "text", "text": prompt},
72 ],
73 },
74]
75
76# Format text and vision input
77text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
78image_inputs, video_inputs = process_vision_info(messages)
79
80inputs = processor(
81 text=text,
82 images=[image_inputs],
83 videos=video_inputs,
84 padding=True,
85 return_tensors="pt",
86).to(model.device)
87
88# Generate output
89generated_ids = model.generate(**inputs, max_new_tokens=1024)
90generated_ids_trimmed = [
91 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
92]
93output = processor.batch_decode(
94 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
95)[0]
96
97print("Generated Output: ", output)
98print("Answer: ", output.split("<answer>")[-1].split("</answer>")[0].strip())1@article{zhihong2024deepseekmath,
2 title = {{DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models}},
3 author = {Zhihong Shao and Peiyi Wang and Qihao Zhu and Runxin Xu and Junxiao Song and Mingchuan Zhang and Y. K. Li and Y. Wu and Daya Guo},
4 year = 2024,
5 eprint = {arXiv:2402.03300},
6}
71@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}