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