461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
import numpy as np
|
|
import pandas as pd
|
|
from PIL import Image
|
|
|
|
from unstructured.partition.pdf_image.ocr import get_table_tokens
|
|
from unstructured.partition.pdf_image.pdf_image_utils import convert_pdf_to_images
|
|
from unstructured.partition.utils.ocr_models.ocr_interface import OCRAgent
|
|
from unstructured.utils import requires_dependencies
|
|
|
|
|
|
@requires_dependencies("unstructured_inference")
|
|
def image_or_pdf_to_dataframe(filename: str) -> pd.DataFrame:
|
|
"""helper to JUST run table transformer on the input image/pdf file. It assumes the input is
|
|
JUST a table. This is intended to facilitate metric tracking on table structure detection ALONE
|
|
without mixing metric of element detection model"""
|
|
from unstructured_inference.models.tables import load_agent, tables_agent
|
|
|
|
load_agent()
|
|
|
|
if filename.endswith(".pdf"):
|
|
image = list(convert_pdf_to_images(filename))[0].convert("RGB")
|
|
else:
|
|
image = Image.open(filename).convert("RGB")
|
|
|
|
ocr_agent = OCRAgent.get_agent(language="eng")
|
|
|
|
return tables_agent.run_prediction(
|
|
image, ocr_tokens=get_table_tokens(image, ocr_agent), result_format="dataframe"
|
|
)
|
|
|
|
|
|
@requires_dependencies("unstructured_inference")
|
|
def eval_table_transformer_for_file(
|
|
filename: str,
|
|
true_table_filename: str,
|
|
eval_func: str = "token_ratio",
|
|
) -> float:
|
|
"""evaluate the predicted table structure vs. actual table structure by column and row as a
|
|
number between 0 and 1"""
|
|
from unstructured_inference.models.eval import compare_contents_as_df
|
|
|
|
pred_table = image_or_pdf_to_dataframe(filename).fillna("").replace(np.nan, "")
|
|
actual_table = pd.read_csv(true_table_filename).astype(str).fillna("").replace(np.nan, "")
|
|
|
|
results = np.array(
|
|
list(compare_contents_as_df(actual_table, pred_table, eval_func=eval_func).values()),
|
|
)
|
|
return results.mean() / 100.0
|