Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 7 |
model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
|
| 8 |
|
| 9 |
-
def
|
| 10 |
if image is None:
|
| 11 |
return "Please upload an image."
|
| 12 |
|
| 13 |
-
# Convert
|
| 14 |
img_array = np.array(image)
|
| 15 |
result = model([img_array])
|
| 16 |
|
| 17 |
-
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
return markdown_output
|
| 30 |
|
| 31 |
-
#
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("## π Accountancy Table Extractor (CPU
|
| 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
|
| 40 |
|
| 41 |
with gr.Column():
|
| 42 |
-
#
|
| 43 |
text_output = gr.Textbox(
|
| 44 |
-
label="
|
| 45 |
lines=20,
|
| 46 |
-
|
| 47 |
-
elem_id="result-box"
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|