| --- |
| dataset_info: |
| features: |
| - name: image |
| dtype: image |
| - name: text |
| dtype: string |
| - name: language |
| dtype: string |
| - name: kind |
| dtype: string |
| - name: style |
| dtype: string |
| - name: font |
| dtype: string |
| - name: font_size |
| dtype: int64 |
| - name: line_numbers |
| dtype: bool |
| - name: repo |
| dtype: string |
| - name: file |
| dtype: string |
| splits: |
| - name: train |
| num_bytes: 2556789548 |
| num_examples: 85634 |
| - name: validation |
| num_bytes: 322219563 |
| num_examples: 10366 |
| download_size: 3528351734 |
| dataset_size: 2879009111 |
| configs: |
| - config_name: default |
| data_files: |
| - split: train |
| path: data/train-* |
| - split: validation |
| path: data/validation-* |
| pretty_name: Code Snippet Image to Text (8 languages) |
| license: cc-by-4.0 |
| task_categories: |
| - image-to-text |
| tags: |
| - code |
| - vlm |
| - multimodal |
| - ocr |
| - syntax-highlighting |
| - code-generation |
| - image-to-text |
| size_categories: |
| - 10K<n<100K |
| --- |
| # 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** |
|
|
|  |
|
|
| **Go — complete block** |
|
|
|  |
|
|
| **Java — complete block** |
|
|
|  |
|
|
| **JavaScript — complete block** |
|
|
|  |
|
|
| **PHP — complete block** |
|
|
|  |
|
|
| **Python — complete block** |
|
|
|  |
|
|
| **Ruby — complete block** |
|
|
|  |
|
|
| ### Fragments (partial captures) |
|
|
| **JavaScript — partial fragment** |
|
|
|  |
|
|
| **Python — partial fragment** |
|
|
|  |
|
|
| ## Dataset structure |
|
|
| ### Fields |
|
|
| | field | type | description | |
| |---|---|---| |
| | `image` | `Image` | the rendered snippet image (PNG, raw bytes in Parquet — not base64) | |
| | `text` | `string` | **the prediction target** — the exact code shown, original indentation, no line numbers | |
| | `language` | `string` | C, C++, Go, Java, JavaScript, PHP, Python, Ruby | |
| | `kind` | `string` | `block` (complete) or `fragment` (partial) | |
| | `style` | `string` | Pygments color theme used to render | |
| | `font` | `string` | monospace font used | |
| | `font_size` | `int` | font size in px | |
| | `line_numbers` | `bool` | whether a line-number gutter is shown (visual only — not in `text`) | |
| | `repo` | `string` | source repository of the code | |
| | `file` | `string` | source file path | |
|
|
| ### Composition (images per language) |
|
|
| | Language | blocks | fragments | total | |
| |---|--:|--:|--:| |
| | C | 8,845 | 3,155 | 12,000 | |
| | C++ | 8,132 | 3,868 | 12,000 | |
| | Go | 9,007 | 2,993 | 12,000 | |
| | Java | 9,024 | 2,976 | 12,000 | |
| | JavaScript | 9,013 | 2,987 | 12,000 | |
| | PHP | 9,001 | 2,999 | 12,000 | |
| | Python | 9,014 | 2,986 | 12,000 | |
| | Ruby | 9,049 | 2,951 | 12,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](https://pygments.org/) `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 |
|
|
| - **CodeSearchNet** — [code-search-net/code_search_net](https://huggingface.co/datasets/code-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](https://www.kaggle.com/datasets/simiotic/github-code-snippets) (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} |
| } |
| ``` |
|
|