Edit model card

Model Description

Fine-tuned Florence-2 model on DocumentVQA dataset to perform question answering on document images

Get Started with the Model

1. Installation

!pip install torch transformers datasets flash_attn

2. Loading model and processor

import torch
from transformers import AutoModelForCausalLM, AutoProcessor

model = AutoModelForCausalLM.from_pretrained("sahilnishad/Florence-2-FT-DocVQA", trust_remote_code=True)
processor = AutoProcessor.from_pretrained("sahilnishad/Florence-2-FT-DocVQA", trust_remote_code=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

3. Running inference

def run_inference(task_prompt, question, image):
    prompt = task_prompt + question

    if image.mode != "RGB":
        image = image.convert("RGB")

    inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
    
    with torch.no_grad():
        generated_ids = model.generate(
            input_ids=inputs["input_ids"],
            pixel_values=inputs["pixel_values"],
            max_new_tokens=1024,
            num_beams=3
        )
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_text

4. Example

from PIL import Image
from datasets import load_dataset

data = load_dataset("HuggingFaceM4/DocumentVQA")

question = "What do you see in this image?"
image = data['train'][0]['image']
print(run_inference("<DocVQA>", question, image))

BibTeX:

@misc{sahilnishad_florence_2_ft_docvqa,
  author       = {Sahil Nishad},
  title        = {Fine-Tuning Florence-2 For Document Visual Question-Answering},
  year         = {2024},
  url          = {https://huggingface.co/sahilnishad/Florence-2-FT-DocVQA},
  note         = {Model available on HuggingFace Hub},
  howpublished = {\url{https://huggingface.co/sahilnishad/Florence-2-FT-DocVQA}},
}
Downloads last month
77
Safetensors
Model size
271M params
Tensor type
F32
·
Inference Examples
Inference API (serverless) does not yet support model repos that contain custom code.

Model tree for sahilnishad/Florence-2-FT-DocVQA

Finetuned
(5)
this model

Dataset used to train sahilnishad/Florence-2-FT-DocVQA

Space using sahilnishad/Florence-2-FT-DocVQA 1