Spaces:
Sleeping
Sleeping
Commit
·
c59cd27
1
Parent(s):
e0d71f5
changes
Browse files
app.py
CHANGED
|
@@ -632,9 +632,11 @@ def _html_escape(s: str) -> str:
|
|
| 632 |
# ----------------------------------------------------------------------
|
| 633 |
# Main OCR wrapper for Gradio
|
| 634 |
#
|
|
|
|
|
|
|
| 635 |
def run_ocr(image, xml_file, apply_gray, apply_bin, highlight_metric):
|
| 636 |
if image is None:
|
| 637 |
-
return None, "", None
|
| 638 |
|
| 639 |
pil_img = Image.fromarray(image).convert("RGB")
|
| 640 |
if apply_gray:
|
|
@@ -688,7 +690,22 @@ def run_ocr(image, xml_file, apply_gray, apply_bin, highlight_metric):
|
|
| 688 |
predicted_html = seg_html
|
| 689 |
|
| 690 |
overlay_img = draw_boxes(pil_img, boxes) if boxes else pil_img
|
| 691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
|
| 693 |
|
| 694 |
# ----------------------------------------------------------------------
|
|
@@ -721,14 +738,40 @@ def create_gradio_interface():
|
|
| 721 |
predictions_output = gr.HTML(label="Predictions (HTML)")
|
| 722 |
df_output = gr.DataFrame(label="Token Scores", interactive=False)
|
| 723 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
run_btn.click(
|
| 725 |
fn=run_ocr,
|
| 726 |
inputs=[image_input, xml_input, apply_gray_checkbox, apply_bin_checkbox, metric_radio],
|
| 727 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
)
|
| 729 |
|
| 730 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
|
| 734 |
if __name__ == "__main__":
|
|
|
|
| 632 |
# ----------------------------------------------------------------------
|
| 633 |
# Main OCR wrapper for Gradio
|
| 634 |
#
|
| 635 |
+
import tempfile
|
| 636 |
+
|
| 637 |
def run_ocr(image, xml_file, apply_gray, apply_bin, highlight_metric):
|
| 638 |
if image is None:
|
| 639 |
+
return None, "", None, "", None, None
|
| 640 |
|
| 641 |
pil_img = Image.fromarray(image).convert("RGB")
|
| 642 |
if apply_gray:
|
|
|
|
| 690 |
predicted_html = seg_html
|
| 691 |
|
| 692 |
overlay_img = draw_boxes(pil_img, boxes) if boxes else pil_img
|
| 693 |
+
|
| 694 |
+
# Clean text for editing (strip HTML)
|
| 695 |
+
clean_pred_text = re.sub(r"<[^>]+>", "", predicted_html)
|
| 696 |
+
|
| 697 |
+
# Save outputs to temporary files
|
| 698 |
+
tmp_dir = tempfile.mkdtemp()
|
| 699 |
+
txt_path = os.path.join(tmp_dir, "ocr_prediction.txt")
|
| 700 |
+
csv_path = os.path.join(tmp_dir, "token_scores.csv")
|
| 701 |
+
|
| 702 |
+
with open(txt_path, "w", encoding="utf-8") as f:
|
| 703 |
+
f.write(clean_pred_text)
|
| 704 |
+
|
| 705 |
+
if df_all is not None and not df_all.empty:
|
| 706 |
+
df_all.to_csv(csv_path, index=False, encoding="utf-8")
|
| 707 |
+
|
| 708 |
+
return overlay_img, predicted_html, df_all, clean_pred_text, txt_path, csv_path
|
| 709 |
|
| 710 |
|
| 711 |
# ----------------------------------------------------------------------
|
|
|
|
| 738 |
predictions_output = gr.HTML(label="Predictions (HTML)")
|
| 739 |
df_output = gr.DataFrame(label="Token Scores", interactive=False)
|
| 740 |
|
| 741 |
+
# 📝 Editable textbox for user corrections
|
| 742 |
+
editable_text = gr.Textbox(label="Edit Recognized Text", lines=8, interactive=True)
|
| 743 |
+
|
| 744 |
+
# 📁 Download buttons
|
| 745 |
+
download_text = gr.File(label="Download Edited Text (.txt)")
|
| 746 |
+
download_csv = gr.File(label="Download Token Scores (.csv)")
|
| 747 |
+
|
| 748 |
+
# Run OCR
|
| 749 |
run_btn.click(
|
| 750 |
fn=run_ocr,
|
| 751 |
inputs=[image_input, xml_input, apply_gray_checkbox, apply_bin_checkbox, metric_radio],
|
| 752 |
+
outputs=[
|
| 753 |
+
overlay_output,
|
| 754 |
+
predictions_output,
|
| 755 |
+
df_output,
|
| 756 |
+
editable_text,
|
| 757 |
+
download_text,
|
| 758 |
+
download_csv,
|
| 759 |
+
],
|
| 760 |
)
|
| 761 |
|
| 762 |
+
# Function to save user-edited text
|
| 763 |
+
def save_edited_text(text):
|
| 764 |
+
import tempfile, os
|
| 765 |
+
tmp_dir = tempfile.mkdtemp()
|
| 766 |
+
path = os.path.join(tmp_dir, "edited_ocr_text.txt")
|
| 767 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 768 |
+
f.write(text)
|
| 769 |
+
return path
|
| 770 |
|
| 771 |
+
# Update download file when user edits
|
| 772 |
+
editable_text.change(fn=save_edited_text, inputs=editable_text, outputs=download_text)
|
| 773 |
+
|
| 774 |
+
return demo
|
| 775 |
|
| 776 |
|
| 777 |
if __name__ == "__main__":
|