---
pipeline_tag: text-generation
language:
- multilingual
inference: false
license: cc-by-nc-4.0
library_name: transformers
---
Trained by Jina AI.
[Blog](https://jina.ai/news/readerlm-v2-frontier-small-language-model-for-markdown-and-json) | [Colab](https://colab.research.google.com/drive/1FfPjZwkMSocOLsEYH45B3B4NxDryKLGI?usp=sharing) | [AWS](https://aws.amazon.com/marketplace/pp/prodview-jwfct4j4rvxk2?sr=0-21&ref_=beagle&applicationId=AWSMPContessa) # ReaderLM-v2 `ReaderLM-v2` is a 1.5B parameter language model that converts raw HTML into beautifully formatted markdown or JSON with superior accuracy and improved longer context handling. Supporting multiple languages (29 in total), `ReaderLM-v2` is specialized for tasks involving HTML parsing, transformation, and text extraction. ## Model Overview - **Model Type**: Autoregressive, decoder-only transformer - **Parameter Count**: 1.54B - **Context Window**: Up to 512K tokens (combined input and output) - **Hidden Size**: 1536 - **Number of Layers**: 28 - **Query Heads**: 12 - **KV Heads**: 2 - **Head Size**: 128 - **Intermediate Size**: 8960 - **Supported Languages**: English, Chinese, Japanese, Korean, French, Spanish, Portuguese, German, Italian, Russian, Vietnamese, Thai, Arabic, and more (29 total) ## What's New in `ReaderLM-v2` `ReaderLM-v2` represents a significant leap forward from its predecessor, with several key improvements: - **Better Markdown Generation**: Thanks to its new training paradigm and higher-quality training data, the model excels at generating complex elements like code fences, nested lists, tables, and LaTeX equations. - **JSON Output**: Introduces direct HTML-to-JSON generation using predefined schemas, eliminating the need for intermediate markdown conversion. - **Longer Context Handling**: Handles up to 512K tokens combined input and output length, with improved performance on long-form content. - **Multilingual Support**: Comprehensive support across 29 languages for broader applications. - **Enhanced Stability**: Greatly alleviates degeneration issues after generating long sequences through contrastive loss during training. --- # Usage Below, you will find instructions and examples for using `ReaderLM-v2` locally using the Hugging Face Transformers library. For a more hands-on experience in a hosted environment, see the [Google Colab Notebook](https://colab.research.google.com/drive/1FfPjZwkMSocOLsEYH45B3B4NxDryKLGI?usp=sharing). ## On Google Colab The easiest way to experience `ReaderLM-v2` is through our [Colab notebook](https://colab.research.google.com/drive/1FfPjZwkMSocOLsEYH45B3B4NxDryKLGI?usp=sharing), which demonstrates HTML-to-markdown conversion, JSON extraction, and instruction-following using the HackerNews frontpage as an example. The notebook is optimized for Colab's free T4 GPU tier and requires `vllm` and `triton` for acceleration and running. Note that the free T4 GPU has limitations—it doesn't support bfloat16 or flash attention 2, leading to higher memory usage and slower processing of longer inputs. Nevertheless, ReaderLM-v2 successfully processes large documents under these constraints, achieving processing speeds of 67 tokens/s input and 36 tokens/s output. For production use, we recommend an RTX 3090/4090 for optimal performance. ## Local Usage To use `ReaderLM-v2` locally: 1. Install the necessary dependencies: ```bash pip install transformers ``` 2. Load and run the model: ```python from transformers import AutoModelForCausalLM, AutoTokenizer import re device = "cuda" # or "cpu" tokenizer = AutoTokenizer.from_pretrained("jinaai/ReaderLM-v2") model = AutoModelForCausalLM.from_pretrained("jinaai/ReaderLM-v2").to(device) ``` 3. (Optional) Pre-clean your HTML to remove scripts, styles, comments, to reduce the noise and length of the input: ```python # Patterns SCRIPT_PATTERN = r'<[ ]*script.*?\/[ ]*script[ ]*>' STYLE_PATTERN = r'<[ ]*style.*?\/[ ]*style[ ]*>' META_PATTERN = r'<[ ]*meta.*?>' COMMENT_PATTERN = r'<[ ]*!--.*?--[ ]*>' LINK_PATTERN = r'<[ ]*link.*?>' BASE64_IMG_PATTERN = r']+src="data:image/[^;]+;base64,[^"]+"[^>]*>' SVG_PATTERN = r'(