CoolFace
Datasetpublic

anisiraj/code-image-to-text

Code Snippet Image → Text A multimodal dataset for fine-tuning vision-language models (VLMs) on the task of transcribing an image of a code snippet back into its source text — syntax-aware OCR. Each example pairs a syntax-highlighted PNG of code with the exact code text that produced it. It spans 8 programming languages and deliberately mixes two capture types: block — a complete function / unit (6–45 lines). fragment — a contiguous partial view (3–14 lines) that may start or… See the full description on the dataset page: https://huggingface.co/datasets/anisiraj/code-image-to-text.

sourceHugging Facecc-by-4.0updated 3mo agoView on Hugging Face
0likes167downloads
Dataset Card

Code Snippet Image → Text

A multimodal dataset for fine-tuning vision-language models (VLMs) on the task of transcribing an image of a code snippet back into its source text — syntax-aware OCR.

Each example pairs a syntax-highlighted PNG of code with the exact code text that produced it. It spans 8 programming languages and deliberately mixes two capture types:

  • `block` — a complete function / unit (6–45 lines).
  • `fragment` — a contiguous partial view (3–14 lines) that may start or end mid-statement, simulating someone screenshotting only part of a snippet (possibly with a line or two of surrounding context). This makes the model robust to partial inputs.
  • Total examples: 96,000 · Languages: 8 · Splits: train (85,634) / validation (10,366)
  • Target column: text (the exact code; never contains line numbers).

Samples

Complete blocks

C — complete block

C — complete block

C++ — complete block

C++ — complete block

Go — complete block

Go — complete block

Java — complete block

Java — complete block

JavaScript — complete block

JavaScript — complete block

PHP — complete block

PHP — complete block

Python — complete block

Python — complete block

Ruby — complete block

Ruby — complete block

Fragments (partial captures)

JavaScript — partial fragment

JavaScript — partial fragment

Python — partial fragment

Python — partial fragment

Dataset structure

Fields

fieldtypedescription
imageImagethe rendered snippet image (PNG, raw bytes in Parquet — not base64)
textstringthe prediction target — the exact code shown, original indentation, no line numbers
languagestringC, C++, Go, Java, JavaScript, PHP, Python, Ruby
kindstringblock (complete) or fragment (partial)
stylestringPygments color theme used to render
fontstringmonospace font used
font_sizeintfont size in px
line_numbersboolwhether a line-number gutter is shown (visual only — not in text)
repostringsource repository of the code
filestringsource file path

Composition (images per language)

Languageblocksfragmentstotal
C8,8453,15512,000
C++8,1323,86812,000
Go9,0072,99312,000
Java9,0242,97612,000
JavaScript9,0132,98712,000
PHP9,0012,99912,000
Python9,0142,98612,000
Ruby9,0492,95112,000

Splits

train / validation, split by repository — a function and any fragments derived from it always land in the same split, so there is no train/validation leakage.

Load

python
from datasets import load_dataset

ds = load_dataset("anisiraj/code-image-to-text")        # DatasetDict with 'train' and 'validation'
print(ds)
ex = ds["train"][0]
print(ex["language"], ex["kind"])  # e.g. 'Python' 'block'
ex["image"]                        # PIL.Image.Image
ex["text"]                         # the code string to predict

Stream (no full download)

python
ds = load_dataset("anisiraj/code-image-to-text", split="train", streaming=True)
for ex in ds.take(5):
    print(ex["language"], ex["kind"], ex["image"].size)

One split only

python
val = load_dataset("anisiraj/code-image-to-text", split="validation")

View the images

python
ds = load_dataset("anisiraj/code-image-to-text", split="train")

# Jupyter / notebook — renders inline:
ds[0]["image"]

# Save or open in an OS image viewer:
img = ds[0]["image"]
img.save("example.png")
img.show()

Show a grid of samples with matplotlib:

python
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 3, figsize=(20, 8))
for ax, ex in zip(axes.ravel(), ds.select(range(6))):
    ax.imshow(ex["image"]); ax.axis("off")
    ax.set_title(f"{ex['language']} / {ex['kind']}")
plt.tight_layout(); plt.show()

Filter

python
# Python complete blocks only
py_blocks = ds.filter(lambda r: r["language"] == "Python" and r["kind"] == "block")

# fragments only
fragments = ds.filter(lambda r: r["kind"] == "fragment")

# one language
go = ds.filter(lambda r: r["language"] == "Go")

How images are stored

Images use the HF Image feature: in the Parquet shards each is a struct<bytes: binary, path: string> holding the raw PNG bytes (PNG signature 89 50 4E 47), not base64. datasets decodes them to PIL.Image on access. To get the undecoded bytes:

python
from datasets import Image
raw = load_dataset("anisiraj/code-image-to-text", split="train").cast_column("image", Image(decode=False))
raw[0]["image"]["bytes"][:8]   # b'\x89PNG\r\n\x1a\n'

Fine-tune a VLM (image → text)

python
from datasets import load_dataset
ds = load_dataset("anisiraj/code-image-to-text")

PROMPT = "Transcribe the code shown in this image exactly, preserving indentation."

def to_chat(ex):
    return {"image": ex["image"], "prompt": PROMPT, "target": ex["text"]}

train = ds["train"].map(to_chat)
# Feed `image` + `prompt` through your VLM's processor/chat template
# (e.g. Qwen2-VL, Idefics3, Llava, MiniCPM-V) and train to produce `target`.

Rendering & augmentation

Rendered with Pygments ImageFormatter: 18 themes (light & dark) × 3 monospace fonts × 5 font sizes (14–20) × line-numbers on/off × 3 paddings. Balanced to an equal number of images per language. The line-number gutter, when present, is visual only and is never part of the text target.

Sources & license

  • CodeSearchNetcode-search-net/code_search_net: Go, Java, JavaScript, PHP, Python, Ruby (complete functions from open-source GitHub repos).
  • GitHub Code Snippets by Bugout.dev / Simiotic — kaggle (CC BY 4.0): C, C++ blocks.

Released under CC BY 4.0. The underlying code originates from public GitHub repositories under their respective open-source licenses; please retain attribution to the sources above.

Citation

bibtex
@misc{code_image_to_text,
  title  = {Code Snippet Image to Text},
  author = {anisiraj},
  year   = {2026},
  url    = {https://huggingface.co/datasets/anisiraj/code-image-to-text}
}