a
    ¡˜ëjC  ã                   @   sˆ   d dl Zd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ edƒeejdœd	d
„ƒZedƒdeeeedœdd„ƒZdS )é    N)ÚImage)Úconvert_pdf_to_images)Úget_table_tokens)ÚOCRAgent)Úrequires_dependenciesZunstructured_inference)ÚfilenameÚreturnc                 C   sj   ddl m}m} |ƒ  |  d¡r8tt| ƒƒd  d¡}nt | ¡ d¡}t	j
dd�}|j|t||ƒdd�S )	zò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 modelr   )Ú
load_agentÚtables_agentz.pdfZRGBZeng)ÚlanguageZ	dataframe)Z
ocr_tokensZresult_format)Z$unstructured_inference.models.tablesr	   r
   ÚendswithÚlistr   Úconvertr   Úopenr   Z	get_agentZrun_predictionr   )r   r	   r
   ÚimageZ	ocr_agent© r   úc/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/metrics/table_structure.pyÚimage_or_pdf_to_dataframe   s    
ÿr   Útoken_ratio)r   Útrue_table_filenameÚ	eval_funcr   c                 C   sl   ddl m} t| ƒ d¡ tjd¡}t |¡ 	t
¡ d¡ tjd¡}t t||||d� ¡ ƒ¡}| ¡ d S )zsevaluate the predicted table structure vs. actual table structure by column and row as a
    number between 0 and 1r   )Úcompare_contents_as_dfÚ )r   g      Y@)Z"unstructured_inference.models.evalr   r   ZfillnaÚreplaceÚnpÚnanÚpdZread_csvZastypeÚstrÚarrayr   ÚvaluesÚmean)r   r   r   r   Z
pred_tableZactual_tableÚresultsr   r   r   Úeval_table_transformer_for_file    s     ÿr"   )r   )Únumpyr   Zpandasr   ZPILr   Zunstructured.partition.pdfr   Z$unstructured.partition.pdf_image.ocrr   Z5unstructured.partition.utils.ocr_models.ocr_interfacer   Zunstructured.utilsr   r   Z	DataFramer   Úfloatr"   r   r   r   r   Ú<module>   s     ýü