Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,21 +99,23 @@ def extract_invoice_data(file_data, content_type, json_schema):
|
|
| 99 |
Extracts data from a PDF (converted to images) or an image.
|
| 100 |
Only PDFs with 1 or 2 pages are allowed.
|
| 101 |
"""
|
| 102 |
-
system_prompt = "You are an expert in document data extraction.
|
| 103 |
base64_images = []
|
| 104 |
|
| 105 |
if content_type == "application/pdf":
|
| 106 |
try:
|
|
|
|
| 107 |
images = convert_from_bytes(file_data) # Convert PDF to images
|
| 108 |
|
| 109 |
if len(images) > 2:
|
| 110 |
-
raise ValueError("PDF contains more than 2 pages.
|
| 111 |
|
| 112 |
for img in images[:2]: # Convert up to 2 pages
|
| 113 |
img_byte_arr = io.BytesIO()
|
| 114 |
-
img.save(img_byte_arr, format="PNG")
|
| 115 |
base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
| 116 |
-
base64_images.append(f"data:
|
|
|
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
logger.error(f"Error converting PDF to image: {e}")
|
|
@@ -123,6 +125,7 @@ def extract_invoice_data(file_data, content_type, json_schema):
|
|
| 123 |
# Handle direct image files
|
| 124 |
base64_encoded = base64.b64encode(file_data).decode('utf-8')
|
| 125 |
base64_images.append(f"data:{content_type};base64,{base64_encoded}")
|
|
|
|
| 126 |
|
| 127 |
# Prepare OpenAI request
|
| 128 |
openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
|
|
@@ -140,11 +143,11 @@ def extract_invoice_data(file_data, content_type, json_schema):
|
|
| 140 |
)
|
| 141 |
|
| 142 |
parsed_content = json.loads(response.choices[0].message.content.strip())
|
| 143 |
-
return parsed_content,
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
logger.error(f"Error in OpenAI processing: {e}")
|
| 147 |
-
return {"error": str(e)},
|
| 148 |
|
| 149 |
|
| 150 |
def get_content_type_from_s3(file_key):
|
|
@@ -215,7 +218,7 @@ def extract_text_from_file(
|
|
| 215 |
"message": "Document successfully stored in MongoDB",
|
| 216 |
"document_id": document_id,
|
| 217 |
"entityrefkey": entity_ref_key,
|
| 218 |
-
"base64DataResp":
|
| 219 |
"extracted_data": extracted_data
|
| 220 |
}
|
| 221 |
|
|
|
|
| 99 |
Extracts data from a PDF (converted to images) or an image.
|
| 100 |
Only PDFs with 1 or 2 pages are allowed.
|
| 101 |
"""
|
| 102 |
+
system_prompt = "You are an expert in document data extraction."
|
| 103 |
base64_images = []
|
| 104 |
|
| 105 |
if content_type == "application/pdf":
|
| 106 |
try:
|
| 107 |
+
extracted_text = extract_pdf_text(file_data)
|
| 108 |
images = convert_from_bytes(file_data) # Convert PDF to images
|
| 109 |
|
| 110 |
if len(images) > 2:
|
| 111 |
+
raise ValueError("PDF contains more than 2 pages.")
|
| 112 |
|
| 113 |
for img in images[:2]: # Convert up to 2 pages
|
| 114 |
img_byte_arr = io.BytesIO()
|
| 115 |
+
img.save(img_byte_arr, format="PNG", dpi=(300, 300))
|
| 116 |
base64_encoded = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
| 117 |
+
base64_images.append(f"data:image/png;base64,{base64_encoded}")
|
| 118 |
+
base64DataResp = f"data:image/png;base64,{base64_encoded}"
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Error converting PDF to image: {e}")
|
|
|
|
| 125 |
# Handle direct image files
|
| 126 |
base64_encoded = base64.b64encode(file_data).decode('utf-8')
|
| 127 |
base64_images.append(f"data:{content_type};base64,{base64_encoded}")
|
| 128 |
+
base64DataResp = f"data:image/png;base64,{base64_encoded}"
|
| 129 |
|
| 130 |
# Prepare OpenAI request
|
| 131 |
openai_content = [{"type": "image_url", "image_url": {"url": img_base64}} for img_base64 in base64_images]
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
parsed_content = json.loads(response.choices[0].message.content.strip())
|
| 146 |
+
return parsed_content, base64DataResp
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
logger.error(f"Error in OpenAI processing: {e}")
|
| 150 |
+
return {"error": str(e)}, base64DataResp
|
| 151 |
|
| 152 |
|
| 153 |
def get_content_type_from_s3(file_key):
|
|
|
|
| 218 |
"message": "Document successfully stored in MongoDB",
|
| 219 |
"document_id": document_id,
|
| 220 |
"entityrefkey": entity_ref_key,
|
| 221 |
+
"base64DataResp": base64DataResp,
|
| 222 |
"extracted_data": extracted_data
|
| 223 |
}
|
| 224 |
|