pratyyush commited on
Commit
cdadefd
Β·
verified Β·
1 Parent(s): 993cb76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -1,52 +1,63 @@
1
  import gradio as gr
2
  from doctr.models import ocr_predictor
3
- from PIL import Image
4
  import numpy as np
 
5
 
6
- # Load a lightweight CPU model
7
  model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
8
 
9
- def extract_as_markdown(image):
10
  if image is None:
11
  return "Please upload an image."
12
 
13
- # Convert PIL to format DocTR understands
14
  img_array = np.array(image)
15
  result = model([img_array])
16
 
17
- markdown_output = "| Text | Confidence |\n| --- | --- |\n"
18
-
19
- # Export results and build a table structure
20
  json_export = result.export()
21
 
22
  for page in json_export['pages']:
23
  for block in page['blocks']:
24
  for line in block['lines']:
25
- line_text = " ".join([word['value'] for word in line['words']])
26
- # Formatting as a markdown row
27
- markdown_output += f"| {line_text} | βœ“ |\n"
 
 
28
 
29
  return markdown_output
30
 
31
- # Professional UI with standard Copy Button
32
  with gr.Blocks() as demo:
33
- gr.Markdown("## πŸ“‘ Accountancy Table Extractor (CPU Optimized)")
34
- gr.Markdown("Extracts text into a Markdown table. Copy the result and paste directly into Excel or Word.")
35
 
36
  with gr.Row():
37
  with gr.Column():
38
- img_input = gr.Image(type="pil", label="Upload Paper")
39
- btn = gr.Button("Extract Table", variant="primary")
40
 
41
  with gr.Column():
42
- # show_copy_button=True is the most reliable way to copy on CPU
43
  text_output = gr.Textbox(
44
- label="Markdown Table Output",
45
  lines=20,
46
- show_copy_button=True,
47
- elem_id="result-box"
48
  )
49
 
50
- btn.click(extract_as_markdown, inputs=img_input, outputs=text_output)
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  demo.launch()
 
1
  import gradio as gr
2
  from doctr.models import ocr_predictor
 
3
  import numpy as np
4
+ from PIL import Image
5
 
6
+ # Initialize the model once
7
  model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
8
 
9
+ def extract_all_tables(image):
10
  if image is None:
11
  return "Please upload an image."
12
 
13
+ # Convert image for the model
14
  img_array = np.array(image)
15
  result = model([img_array])
16
 
17
+ # Reconstruct lines based on vertical position to keep tables intact
18
+ markdown_output = ""
 
19
  json_export = result.export()
20
 
21
  for page in json_export['pages']:
22
  for block in page['blocks']:
23
  for line in block['lines']:
24
+ # Join words in a line with spaces
25
+ words = [word['value'] for word in line['words']]
26
+ line_text = " | ".join(words) # Use pipe for table structure
27
+ markdown_output += f"| {line_text} |\n"
28
+ markdown_output += "\n---\n\n" # Separator for different blocks/tables
29
 
30
  return markdown_output
31
 
32
+ # UI without the 'show_copy_button' error
33
  with gr.Blocks() as demo:
34
+ gr.Markdown("## πŸ“‘ Accountancy Table Extractor (CPU)")
 
35
 
36
  with gr.Row():
37
  with gr.Column():
38
+ img_input = gr.Image(type="pil", label="Upload Accountancy Paper")
39
+ btn = gr.Button("Extract All Tables", variant="primary")
40
 
41
  with gr.Column():
42
+ # Standard Textbox without the failing argument
43
  text_output = gr.Textbox(
44
+ label="Extracted Table Data",
45
  lines=20,
46
+ elem_id="output-text"
 
47
  )
48
 
49
+ # Custom Copy Button
50
+ copy_btn = gr.Button("πŸ“‹ Copy to Clipboard")
51
+
52
+ # JavaScript to handle the copy action
53
+ copy_btn.click(None, None, None, js="""
54
+ () => {
55
+ const text = document.querySelector('#output-text textarea').value;
56
+ navigator.clipboard.writeText(text);
57
+ alert('Copied to clipboard!');
58
+ }
59
+ """)
60
+
61
+ btn.click(extract_all_tables, inputs=img_input, outputs=text_output)
62
 
63
  demo.launch()