Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,8 @@ import cv2
|
|
| 5 |
import numpy as np
|
| 6 |
import re
|
| 7 |
import csv
|
| 8 |
-
import
|
|
|
|
| 9 |
|
| 10 |
# Synonyms for features
|
| 11 |
feature_synonyms = {
|
|
@@ -88,21 +89,18 @@ def ocr_extract(image):
|
|
| 88 |
# Parse the extracted text
|
| 89 |
parsed_data = parse_text(text)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
writer = csv.DictWriter(
|
| 94 |
writer.writeheader()
|
| 95 |
writer.writerow(parsed_data)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
csv_bytes.name = "extracted_bill.csv"
|
| 101 |
-
|
| 102 |
-
# Prepare display text
|
| 103 |
display_text = "\n".join(f"{k}: {v}" for k, v in parsed_data.items())
|
| 104 |
|
| 105 |
-
return display_text,
|
| 106 |
|
| 107 |
iface = gr.Interface(
|
| 108 |
fn=ocr_extract,
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import re
|
| 7 |
import csv
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
# Synonyms for features
|
| 12 |
feature_synonyms = {
|
|
|
|
| 89 |
# Parse the extracted text
|
| 90 |
parsed_data = parse_text(text)
|
| 91 |
|
| 92 |
+
# Create a temp file to save CSV
|
| 93 |
+
tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", encoding="utf-8")
|
| 94 |
+
writer = csv.DictWriter(tmpfile, fieldnames=parsed_data.keys())
|
| 95 |
writer.writeheader()
|
| 96 |
writer.writerow(parsed_data)
|
| 97 |
+
tmpfile_path = tmpfile.name
|
| 98 |
+
tmpfile.close()
|
| 99 |
|
| 100 |
+
# Prepare display text as before
|
|
|
|
|
|
|
|
|
|
| 101 |
display_text = "\n".join(f"{k}: {v}" for k, v in parsed_data.items())
|
| 102 |
|
| 103 |
+
return display_text, tmpfile_path # Return path string, not BytesIO
|
| 104 |
|
| 105 |
iface = gr.Interface(
|
| 106 |
fn=ocr_extract,
|