Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
| 4 |
|
| 5 |
model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
|
| 6 |
|
| 7 |
-
def
|
| 8 |
if image is None:
|
| 9 |
return "Please upload an image."
|
| 10 |
|
|
@@ -14,9 +14,9 @@ def extract_perfect_table(image):
|
|
| 14 |
|
| 15 |
markdown_rows = []
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
col_bounds = [0.
|
| 20 |
|
| 21 |
for page in json_export['pages']:
|
| 22 |
words_list = []
|
|
@@ -33,7 +33,7 @@ def extract_perfect_table(image):
|
|
| 33 |
rows = []
|
| 34 |
current_row = [words_list[0]]
|
| 35 |
for i in range(1, len(words_list)):
|
| 36 |
-
if abs(words_list[i]['y'] - current_row[-1]['y']) < 0.
|
| 37 |
current_row.append(words_list[i])
|
| 38 |
else:
|
| 39 |
rows.append(current_row)
|
|
@@ -41,52 +41,55 @@ def extract_perfect_table(image):
|
|
| 41 |
rows.append(current_row)
|
| 42 |
|
| 43 |
for row in rows:
|
| 44 |
-
slots
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
for w in row:
|
| 48 |
x = w['x']
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
if "Debit"
|
| 53 |
-
slots[
|
| 54 |
-
elif "Credit" in text and x > 0.75:
|
| 55 |
-
slots[3] += text + " "
|
| 56 |
-
# General bucket logic for the rest of the rows
|
| 57 |
elif x < col_bounds[0]:
|
| 58 |
-
slots[0] +=
|
| 59 |
elif x < col_bounds[1]:
|
| 60 |
-
slots[1] +=
|
| 61 |
elif x < col_bounds[2]:
|
| 62 |
-
slots[2] +=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
else:
|
| 64 |
-
slots[
|
| 65 |
|
| 66 |
clean_slots = [s.strip() for s in slots]
|
| 67 |
markdown_rows.append("| " + " | ".join(clean_slots) + " |")
|
| 68 |
|
| 69 |
return "\n".join(markdown_rows)
|
| 70 |
|
|
|
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
-
gr.Markdown("## π
|
| 73 |
-
gr.Markdown("
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
img_in = gr.Image(type="pil")
|
| 78 |
-
btn = gr.Button("Extract Table", variant="primary")
|
| 79 |
with gr.Column():
|
| 80 |
-
out = gr.Textbox(label="
|
| 81 |
copy_btn = gr.Button("π Copy Table")
|
| 82 |
copy_btn.click(None, None, None, js="""
|
| 83 |
() => {
|
| 84 |
const text = document.querySelector('#out_box textarea').value;
|
| 85 |
navigator.clipboard.writeText(text);
|
| 86 |
-
alert('
|
| 87 |
}
|
| 88 |
""")
|
| 89 |
|
| 90 |
-
btn.click(
|
| 91 |
|
| 92 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
|
| 6 |
|
| 7 |
+
def extract_6_column_table(image):
|
| 8 |
if image is None:
|
| 9 |
return "Please upload an image."
|
| 10 |
|
|
|
|
| 14 |
|
| 15 |
markdown_rows = []
|
| 16 |
|
| 17 |
+
# NEW: Boundaries for 6 columns (Account Name, Code, Statement, Group, Sub-Group, Normally)
|
| 18 |
+
# Adjusted based on your specific screenshot layout
|
| 19 |
+
col_bounds = [0.30, 0.38, 0.52, 0.68, 0.90]
|
| 20 |
|
| 21 |
for page in json_export['pages']:
|
| 22 |
words_list = []
|
|
|
|
| 33 |
rows = []
|
| 34 |
current_row = [words_list[0]]
|
| 35 |
for i in range(1, len(words_list)):
|
| 36 |
+
if abs(words_list[i]['y'] - current_row[-1]['y']) < 0.012: # Tighter row grouping
|
| 37 |
current_row.append(words_list[i])
|
| 38 |
else:
|
| 39 |
rows.append(current_row)
|
|
|
|
| 41 |
rows.append(current_row)
|
| 42 |
|
| 43 |
for row in rows:
|
| 44 |
+
# 6 slots for your specific table structure
|
| 45 |
+
slots = ["", "", "", "", "", ""]
|
| 46 |
+
row.sort(key=lambda w: w['x'])
|
| 47 |
|
| 48 |
for w in row:
|
| 49 |
x = w['x']
|
| 50 |
+
t = w['text']
|
| 51 |
|
| 52 |
+
# Forced Column Logic for "Debit/Credit"
|
| 53 |
+
if t in ["Debit", "Credit"] and x > 0.85:
|
| 54 |
+
slots[5] = t
|
|
|
|
|
|
|
|
|
|
| 55 |
elif x < col_bounds[0]:
|
| 56 |
+
slots[0] += t + " "
|
| 57 |
elif x < col_bounds[1]:
|
| 58 |
+
slots[1] += t + " "
|
| 59 |
elif x < col_bounds[2]:
|
| 60 |
+
slots[2] += t + " "
|
| 61 |
+
elif x < col_bounds[3]:
|
| 62 |
+
slots[3] += t + " "
|
| 63 |
+
elif x < col_bounds[4]:
|
| 64 |
+
slots[4] += t + " "
|
| 65 |
else:
|
| 66 |
+
slots[5] += t + " "
|
| 67 |
|
| 68 |
clean_slots = [s.strip() for s in slots]
|
| 69 |
markdown_rows.append("| " + " | ".join(clean_slots) + " |")
|
| 70 |
|
| 71 |
return "\n".join(markdown_rows)
|
| 72 |
|
| 73 |
+
# UI
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("## π 6-Column Accounting Extractor")
|
| 76 |
+
gr.Markdown("Separates Account Name, Code, Group, Sub-Group, and Normal Balance.")
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
img_in = gr.Image(type="pil")
|
| 81 |
+
btn = gr.Button("Extract Complex Table", variant="primary")
|
| 82 |
with gr.Column():
|
| 83 |
+
out = gr.Textbox(label="Result", lines=25, elem_id="out_box")
|
| 84 |
copy_btn = gr.Button("π Copy Table")
|
| 85 |
copy_btn.click(None, None, None, js="""
|
| 86 |
() => {
|
| 87 |
const text = document.querySelector('#out_box textarea').value;
|
| 88 |
navigator.clipboard.writeText(text);
|
| 89 |
+
alert('Copied! Column merges have been fixed.');
|
| 90 |
}
|
| 91 |
""")
|
| 92 |
|
| 93 |
+
btn.click(extract_6_column_table, inputs=img_in, outputs=out)
|
| 94 |
|
| 95 |
demo.launch()
|