Views
No views yet
Falconsai/florence-2-invoice is a fine-tuned version of the microsoft/Florence-2-base-ft model. This model has been specifically trained to identify and extract key fields from invoice images. The fine-tuning process utilized a curated dataset of invoices annotated to recognize the following fields:microsoft/Florence-2-base-ft, a state-of-the-art vision model developed by Microsoft.1LoraConfig(
2 r=8,
3 lora_alpha=8,
4 target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "linear", "Conv2d", "lm_head", "fc2"],
5 task_type="CAUSAL_LM",
6 lora_dropout=0.05,
7 bias="none",
8 inference_mode=False,
9 use_rslora=True,
10 init_lora_weights="gaussian",
11 revision=REVISION
12)1import torch
2from PIL import Image
3from transformers import (
4 AdamW,
5 AutoModelForCausalLM,
6 AutoProcessor,
7 get_scheduler
8)
9def run_florence_invoice(img, task_prompt, text_input=None):
10 image = Image.open(img)
11
12 # Ensure the image is in RGB format
13 if image.mode != "RGB":
14 image = image.convert("RGB")
15
16 model_id2 = "Falconsai/florence-2-invoice"
17 model = AutoModelForCausalLM.from_pretrained(model_id2, trust_remote_code=True).eval().cuda()
18 processor = AutoProcessor.from_pretrained(model_id2, trust_remote_code=True)
19
20 with torch.no_grad():
21 if text_input is None:
22 prompt = task_prompt
23 else:
24 prompt = task_prompt + text_input
25 inputs = processor(text=prompt, images=image, return_tensors="pt")
26 generated_ids = model.generate(
27 input_ids=inputs["input_ids"].cuda(),
28 pixel_values=inputs["pixel_values"].cuda(),
29 max_new_tokens=1024,
30 num_beams=3
31 )
32 generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
33 parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
34
35 del model
36 del processor
37
38 return parsed_answer1## Call the function as follows:
2### Return all fields identified:
3img = './invoice.png'
4run_florence_invoice(img, '<OD>')
5
6### Return Specific field
7img = './invoice.png'
8results = run_florence_invoice(img, "<CAPTION_TO_PHRASE_GROUNDING>", text_input="invoice date")
9results directory of the repository.LICENSE file for more details.