Advertisement
Guest User

Untitled

a guest
Feb 27th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. from unsloth import FastVisionModel
  2. from pypdf import PdfReader
  3. import pypdfium2 as pdfium
  4. from PIL import Image
  5. import io
  6. import json
  7. from unsloth import FastLanguageModel
  8.  
  9. model, tokenizer = FastVisionModel.from_pretrained(
  10. "lora_model",
  11. load_in_4bit=True
  12. )
  13.  
  14. def process_invoice(pdf_path):
  15.  
  16. with open(pdf_path, 'rb') as f:
  17. pdf_bytes = f.read()
  18.  
  19. pdf_document = pdfium.PdfDocument(pdf_bytes)
  20. images = []
  21.  
  22. FastLanguageModel.for_inference(model)
  23.  
  24. for page_number, page in enumerate(pdf_document):
  25. image_pil = page.render(scale=.5).to_pil()
  26. images.append(image_pil)
  27.  
  28. image_inputs = [{"type": "image", "image": image} for image in images]
  29.  
  30. user_instruction = """You are an image processing language model specialized in extracting invoice details from images. Given an image of an invoice, extract the following fields:
  31.  
  32. invoice_number: The invoice number.
  33. billing_date: The billing date.
  34. supplier_vat: The supplier's VAT number.
  35. supplier_country: The supplier's country.
  36. supplier_name: The supplier's name.
  37. customer_vat: The customer's VAT number.
  38. customer_country: The customer's country.
  39. customer_address_street: The customer's address street.
  40. customer_address_zip_code: The customer's zip code.
  41. customer_address_city: The customer's city.
  42. total_invoice_without_taxes: The total amount of the invoice without taxes.
  43. total_invoice_with_taxes: The total amount of the invoice with taxes.
  44. invoice_currency: The currency of the invoice.
  45. invoice_description: A description of the invoice.
  46. invoice_type: The type of the invoice.
  47. observation: Any additional observations.
  48. If any field is not present in the image, set its value to null.
  49.  
  50. Return the extracted data as a JSON object with the keys exactly as listed above.
  51. """
  52.  
  53. system_message = "You are an AI assistant for invoice fields extraction. You will receive the text of an invoice. You will receive a list of fields to fill in. Think step by step and extract the requested fields, always in text, one by one, with attention to detail"
  54.  
  55. messages = [
  56. {"role": "system", "content": [{"type": "text", "text": system_message}]},
  57. {"role": "user", "content": image_inputs + [{"type": "text", "text": user_instruction}]}
  58. ]
  59.  
  60. inputs = tokenizer.apply_chat_template(
  61. messages,
  62. add_generation_prompt = True,
  63. tokenize=True,
  64. return_tensors="pt").to('cuda')
  65.  
  66. print(inputs)
  67.  
  68. outputs = model.generate(
  69. inputs
  70. )
  71.  
  72. response = tokenizer.decode(outputs[0], skip_special_tokens=True)
  73.  
  74. try:
  75. extracted_data = json.loads(response)
  76. return extracted_data
  77. except:
  78. return {"error": "Failed to parse model output", "raw_output": response}
  79.  
  80. result = process_invoice("CPV 206.pdf")
  81. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement