Image-Text-to-Text
Transformers
Safetensors
nemotron_parse
feature-extraction
VLM
OCR
Parse
conversational
custom_code
Instructions to use sassoftware/NVIDIA-Nemotron-Parse-v1.2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use sassoftware/NVIDIA-Nemotron-Parse-v1.2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="sassoftware/NVIDIA-Nemotron-Parse-v1.2", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("sassoftware/NVIDIA-Nemotron-Parse-v1.2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use sassoftware/NVIDIA-Nemotron-Parse-v1.2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "sassoftware/NVIDIA-Nemotron-Parse-v1.2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sassoftware/NVIDIA-Nemotron-Parse-v1.2", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/sassoftware/NVIDIA-Nemotron-Parse-v1.2
- SGLang
How to use sassoftware/NVIDIA-Nemotron-Parse-v1.2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "sassoftware/NVIDIA-Nemotron-Parse-v1.2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sassoftware/NVIDIA-Nemotron-Parse-v1.2", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "sassoftware/NVIDIA-Nemotron-Parse-v1.2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "sassoftware/NVIDIA-Nemotron-Parse-v1.2", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use sassoftware/NVIDIA-Nemotron-Parse-v1.2 with Docker Model Runner:
docker model run hf.co/sassoftware/NVIDIA-Nemotron-Parse-v1.2
| """ | |
| Example usage of LogitsProcessors for document parsing. | |
| This example shows how to use: | |
| - TableInsertionLogitsProcessor: Force \begin{tabular} at the start of every object | |
| - RepetitionStopProcessor: Detect hallucination/repetition and force coordinate tokens | |
| """ | |
| import torch | |
| from PIL import Image, ImageDraw | |
| from transformers import AutoModel, AutoProcessor, AutoTokenizer, GenerationConfig | |
| from postprocessing import extract_classes_bboxes, transform_bbox_to_original, postprocess_text | |
| from hf_logits_processor import TableInsertionLogitsProcessor, RepetitionStopProcessor | |
| # Load model and processor | |
| model_path = "nvidia/NVIDIA-Nemotron-Parse-v1.2" or use a local path | |
| device = "cuda:0" | |
| model = AutoModel.from_pretrained( | |
| model_path, | |
| trust_remote_code=True, | |
| torch_dtype=torch.bfloat16 | |
| ).to(device).eval() | |
| tokenizer = AutoTokenizer.from_pretrained(model_path) | |
| processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) | |
| # Load image | |
| image = Image.open('example.png').convert("RGB") | |
| task_prompt = "</s><s><predict_bbox><predict_classes><output_markdown><predict_no_text_in_pic>" | |
| # Process image | |
| inputs = processor(images=[image], text=task_prompt, return_tensors="pt", add_special_tokens=False).to(device) | |
| generation_config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True) | |
| # Create the table processor - inserts \begin{tabular} after every <x_...><y_...> that starts a new object | |
| table_processor = TableInsertionLogitsProcessor( | |
| tokenizer=tokenizer, | |
| table_prefix="\\begin{tabular}" | |
| ) | |
| # Create the repetition stop processor - detects hallucination and forces <x_...> tokens | |
| repetition_processor = RepetitionStopProcessor( | |
| tokenizer=tokenizer, | |
| max_repetitions=10, # Force stop after any pattern repeats 10+ times | |
| ngram_sizes=[3, 4, 5, 6], # Check these n-gram sizes for repetition | |
| window_size=500 # Only check the last 500 tokens | |
| ) | |
| # Generate with both logits processors | |
| outputs = model.generate( | |
| **inputs, | |
| generation_config=generation_config, | |
| logits_processor=[table_processor, repetition_processor] | |
| ) | |
| # Reset processor states for next generation (important for batch processing) | |
| table_processor.reset() | |
| repetition_processor.reset() | |
| # Decode and process the generated text | |
| generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0] | |
| print(outputs) | |
| print('--------------------------------') | |
| print("Generated text:", generated_text) | |
| print('--------------------------------') | |
| classes, bboxes, texts = extract_classes_bboxes(generated_text) | |
| bboxes = [transform_bbox_to_original(bbox, image.width, image.height) for bbox in bboxes] | |
| # Specify output formats for postprocessing | |
| table_format = 'HTML' # latex | HTML | markdown | |
| text_format = 'markdown' # markdown | plain | |
| blank_text_in_figures = False # remove text inside 'Picture' class | |
| texts = [ | |
| postprocess_text( | |
| text, | |
| cls=cls, | |
| table_format=table_format, | |
| text_format=text_format, | |
| blank_text_in_figures=blank_text_in_figures | |
| ) | |
| for text, cls in zip(texts, classes) | |
| ] | |
| for cl, bb, txt in zip(classes, bboxes, texts): | |
| print(cl, ': ', txt) | |
| # OPTIONAL - Draw bounding boxes | |
| draw = ImageDraw.Draw(image) | |
| for bbox in bboxes: | |
| draw.rectangle((bbox[0], bbox[1], (max(bbox[0], bbox[2])), (max(bbox[1], bbox[3]))), outline="red", width=2) | |
| # Save or display the image | |
| image.save("output_with_boxes.jpg") | |
| # image.show() | |