hf-tuner/docvqa-10k-donut
Viewer • Updated • 10k • 26
How to use hf-tuner/donut-base-finetuned-docvqa with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="hf-tuner/donut-base-finetuned-docvqa") # Load model directly
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("hf-tuner/donut-base-finetuned-docvqa")
model = AutoModel.from_pretrained("hf-tuner/donut-base-finetuned-docvqa")How to use hf-tuner/donut-base-finetuned-docvqa with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "hf-tuner/donut-base-finetuned-docvqa"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "hf-tuner/donut-base-finetuned-docvqa",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/hf-tuner/donut-base-finetuned-docvqa
How to use hf-tuner/donut-base-finetuned-docvqa with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "hf-tuner/donut-base-finetuned-docvqa" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "hf-tuner/donut-base-finetuned-docvqa",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'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 "hf-tuner/donut-base-finetuned-docvqa" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "hf-tuner/donut-base-finetuned-docvqa",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use hf-tuner/donut-base-finetuned-docvqa with Docker Model Runner:
docker model run hf.co/hf-tuner/donut-base-finetuned-docvqa
This model is a fine-tuned version of naver-clova-ix/donut-base on hf-tuner/docvqa-10k-donut dataset.
Donut consists of a vision encoder (Swin Transformer) and a text decoder (BART).
Given an image, the encoder first encodes the image into a tensor of embeddings (of shape batch_size, seq_len, hidden_size),
after which the decoder autoregressively generates text, conditioned on the encoding of the encoder.

import torch
from PIL import Image
from transformers import DonutProcessor, VisionEncoderDecoderConfig, VisionEncoderDecoderModel
model_id = "hf-tuner/donut-base-finetuned-docvqa"
config = VisionEncoderDecoderConfig.from_pretrained(model_id)
processor = DonutProcessor.from_pretrained(model_id)
device = "cuda" if torch.cuda.is_available() else "cpu"
config.dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = VisionEncoderDecoderModel.from_pretrained(ckpt, config=config)
model.to(device)
PROMPT_TEMPLATE = "<doc_vqa><s><s_question>{}</s_question><s_answer>"
def predict(image, question):
prompt = PROMPT_TEMPLATE.format(question)
pixel_values = processor(image,
return_tensors="pt"
).pixel_values.to(device)
decoder_input_ids = processor.tokenizer(prompt,
add_special_tokens=False,
return_tensors="pt"
).input_ids.to(device)
generated_ids = model.generate(pixel_values,
decoder_input_ids=decoder_input_ids,
max_length=64,
bad_words_ids=[[processor.tokenizer.unk_token_id]]
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
output_json = processor.token2json(generated_text)
return output_json
output = predict(image=Image.open('doc-image.png'), question="Which is the date of the approval form?")
# {"question": "Which is the date of the approval form?", "answer": "april 5, 1995"}
The following hyperparameters were used during training:
Base model
naver-clova-ix/donut-base