Spaces:
Running
Running
Showing errors
Browse files
app.py
CHANGED
|
@@ -228,6 +228,13 @@ def extract_invoice_data(file):
|
|
| 228 |
# Extract text with OCR
|
| 229 |
text_blocks = extract_text_from_image(image)
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
# Extract different sections
|
| 232 |
header = find_header_info(text_blocks)
|
| 233 |
details = find_line_items(text_blocks)
|
|
@@ -237,14 +244,21 @@ def extract_invoice_data(file):
|
|
| 237 |
result = {
|
| 238 |
"header": header,
|
| 239 |
"details": details,
|
| 240 |
-
"footer": footer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
}
|
| 242 |
|
| 243 |
return json.dumps(result, indent=2, ensure_ascii=False)
|
| 244 |
|
| 245 |
except Exception as e:
|
|
|
|
| 246 |
return json.dumps({
|
| 247 |
"error": str(e),
|
|
|
|
|
|
|
| 248 |
"message": "Failed to process invoice"
|
| 249 |
}, indent=2)
|
| 250 |
|
|
@@ -360,6 +374,6 @@ if __name__ == "__main__":
|
|
| 360 |
demo.launch(
|
| 361 |
server_name="0.0.0.0",
|
| 362 |
server_port=7860,
|
| 363 |
-
share=
|
| 364 |
show_api=True
|
| 365 |
)
|
|
|
|
| 228 |
# Extract text with OCR
|
| 229 |
text_blocks = extract_text_from_image(image)
|
| 230 |
|
| 231 |
+
# Check if OCR extracted any text
|
| 232 |
+
if not text_blocks:
|
| 233 |
+
return json.dumps({
|
| 234 |
+
"error": "No text detected",
|
| 235 |
+
"message": "Could not extract any text from the image. Please ensure the image is clear and contains text."
|
| 236 |
+
}, indent=2)
|
| 237 |
+
|
| 238 |
# Extract different sections
|
| 239 |
header = find_header_info(text_blocks)
|
| 240 |
details = find_line_items(text_blocks)
|
|
|
|
| 244 |
result = {
|
| 245 |
"header": header,
|
| 246 |
"details": details,
|
| 247 |
+
"footer": footer,
|
| 248 |
+
"debug_info": {
|
| 249 |
+
"total_text_blocks": len(text_blocks),
|
| 250 |
+
"sample_text": [block['text'] for block in text_blocks[:5]]
|
| 251 |
+
}
|
| 252 |
}
|
| 253 |
|
| 254 |
return json.dumps(result, indent=2, ensure_ascii=False)
|
| 255 |
|
| 256 |
except Exception as e:
|
| 257 |
+
import traceback
|
| 258 |
return json.dumps({
|
| 259 |
"error": str(e),
|
| 260 |
+
"error_type": type(e).__name__,
|
| 261 |
+
"traceback": traceback.format_exc(),
|
| 262 |
"message": "Failed to process invoice"
|
| 263 |
}, indent=2)
|
| 264 |
|
|
|
|
| 374 |
demo.launch(
|
| 375 |
server_name="0.0.0.0",
|
| 376 |
server_port=7860,
|
| 377 |
+
share=False,
|
| 378 |
show_api=True
|
| 379 |
)
|