LEAHWA commited on
Commit
a56ae2d
·
verified ·
1 Parent(s): 915ae2a

Update OCR.py

Browse files
Files changed (1) hide show
  1. OCR.py +0 -18
OCR.py CHANGED
@@ -5,45 +5,27 @@ from PIL import Image
5
  import io
6
  import re
7
  HF_TOKEN = os.environ.get("HF_TOKEN")
8
- # Set environment variable
9
  os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
10
 
11
- # Model and device setup
12
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
13
  model_id = "google/paligemma-3b-mix-224"
14
-
15
- # Load model and processor
16
  model = PaliGemmaForConditionalGeneration.from_pretrained(model_id).to(device)
17
  processor = AutoProcessor.from_pretrained(model_id)
18
 
19
  def extract_text_from_image(image_content):
20
  image = Image.open(io.BytesIO(image_content))
21
-
22
- # Prompt for detecting text
23
  prompt = "Extract all relevant details from this invoice."
24
-
25
- # Prepare inputs for the model
26
  inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
27
  input_len = inputs["input_ids"].shape[-1]
28
-
29
  with torch.inference_mode():
30
- # Generate the output
31
  generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
32
  generation = generation[0][input_len:]
33
  decoded = processor.decode(generation, skip_special_tokens=True)
34
-
35
  return decoded
36
 
37
- def extract_text_from_pdf(pdf_content):
38
- # For simplicity, let's assume you're converting the PDF to images first
39
- # You may use libraries like pdf2image to convert PDF pages to images
40
- # Then call extract_text_from_image for each image
41
- pass
42
 
43
  def extract_invoice_details(text):
44
- # Implement your logic to extract invoice details from the text
45
  details = {}
46
- # Example extraction logic
47
  details['Invoice Number'] = re.search(r'Invoice Number: (\S+)', text).group(1) if re.search(r'Invoice Number: (\S+)', text) else 'N/A'
48
  details['Amount'] = re.search(r'Total Amount Due: (\S+)', text).group(1) if re.search(r'Total Amount Due: (\S+)', text) else 'N/A'
49
  details['Invoice Date'] = re.search(r'Invoice Date: (\S+)', text).group(1) if re.search(r'Invoice Date: (\S+)', text) else 'N/A'
 
5
  import io
6
  import re
7
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
8
  os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
9
 
 
10
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
  model_id = "google/paligemma-3b-mix-224"
 
 
12
  model = PaliGemmaForConditionalGeneration.from_pretrained(model_id).to(device)
13
  processor = AutoProcessor.from_pretrained(model_id)
14
 
15
  def extract_text_from_image(image_content):
16
  image = Image.open(io.BytesIO(image_content))
 
 
17
  prompt = "Extract all relevant details from this invoice."
 
 
18
  inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
19
  input_len = inputs["input_ids"].shape[-1]
 
20
  with torch.inference_mode():
 
21
  generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
22
  generation = generation[0][input_len:]
23
  decoded = processor.decode(generation, skip_special_tokens=True)
 
24
  return decoded
25
 
 
 
 
 
 
26
 
27
  def extract_invoice_details(text):
 
28
  details = {}
 
29
  details['Invoice Number'] = re.search(r'Invoice Number: (\S+)', text).group(1) if re.search(r'Invoice Number: (\S+)', text) else 'N/A'
30
  details['Amount'] = re.search(r'Total Amount Due: (\S+)', text).group(1) if re.search(r'Total Amount Due: (\S+)', text) else 'N/A'
31
  details['Invoice Date'] = re.search(r'Invoice Date: (\S+)', text).group(1) if re.search(r'Invoice Date: (\S+)', text) else 'N/A'