
    :QgC              
           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      ded	ej                  fd
       Z ed      	 ddededed	efd       Zy)    N)Image)convert_pdf_to_images)get_table_tokens)OCRAgent)requires_dependenciesunstructured_inferencefilenamereturnc                 @   ddl m}m}  |        | j                  d      r't	        t        |             d   j                  d      }n$t        j                  |       j                  d      }t        j                  d      }|j                  |t        ||      d      S )	zhelper 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.pdfRGBeng)language	dataframe)
ocr_tokensresult_format)$unstructured_inference.models.tablesr   r   endswithlistr   convertr   openr   	get_agentrun_predictionr   )r	   r   r   image	ocr_agents        a/var/www/html/answerous/venv/lib/python3.12/site-packages/unstructured/metrics/table_structure.pyimage_or_pdf_to_dataframer      s    
 NL *845a8@@G

8$,,U3""E2I&&*5)<K '      true_table_filename	eval_funcc           	         ddl m} t        |       j                  d      j	                  t
        j                  d      }t        j                  |      j                  t              j                  d      j	                  t
        j                  d      }t        j                  t         ||||      j                                     }|j                         dz  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@)"unstructured_inference.models.evalr#   r   fillnareplacenpnanpdread_csvastypestrarrayr   valuesmean)r	   r    r!   r#   
pred_tableactual_tableresultss          r   eval_table_transformer_for_filer4       s     J*84;;B?GGPRSJ;;23::3?FFrJRRSUSYSY[]^Lhh#L*	RYY[\G <<>E!!r   )token_ratio)numpyr(   pandasr*   PILr   unstructured.partition.pdfr   $unstructured.partition.pdf_image.ocrr   5unstructured.partition.utils.ocr_models.ocr_interfacer   unstructured.utilsr   r-   	DataFramer   floatr4    r   r   <module>r@      s       < A J 4 /0   1( /0 #""" " 	" 1"r   