pratyyush commited on
Commit
9d2ded2
Β·
verified Β·
1 Parent(s): 321e4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
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 extract_perfect_table(image):
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
- # Precise Column Boundaries (Adjusted for 4-column accountancy papers)
18
- # Bucket 1: < 22% | Bucket 2: 22%-62% | Bucket 3: 62%-82% | Bucket 4: > 82%
19
- col_bounds = [0.22, 0.62, 0.82]
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.015:
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
- row.sort(key=lambda w: w['x']) # Ensure words in row are left-to-right
 
46
 
47
  for w in row:
48
  x = w['x']
49
- text = w['text']
50
 
51
- # Logic to handle the "Debit Credit" header merging
52
- if "Debit" in text and x > 0.55 and x < 0.75:
53
- slots[2] += text + " "
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] += text + " "
59
  elif x < col_bounds[1]:
60
- slots[1] += text + " "
61
  elif x < col_bounds[2]:
62
- slots[2] += text + " "
 
 
 
 
63
  else:
64
- slots[3] += text + " "
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("## πŸ“‘ Final Accountancy Table Extractor")
73
- gr.Markdown("Fixed header alignment for Debit/Credit columns.")
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="Copy-Paste Ready", lines=20, elem_id="out_box")
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('Table copied! Header columns are now separated.');
87
  }
88
  """)
89
 
90
- btn.click(extract_perfect_table, inputs=img_in, outputs=out)
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()