
    NgC              
           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dS )    N)Image)convert_pdf_to_images)get_table_tokens)OCRAgent)requires_dependenciesunstructured_inferencefilenamereturnc                    ddl m}m}  |             |                     d          r6t	          t          |                     d                             d          }n't          j        |                               d          }t          j
        d          }|                    |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        `/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured/metrics/table_structure.pyimage_or_pdf_to_dataframer      s    
 NMMMMMMMJLLL   4*84455a8@@GG
8$$,,U33"E222I&&*5)<<K '       token_ratiotrue_table_filename	eval_funcc           	         ddl m} t          |                               d                              t
          j        d          }t          j        |          	                    t                                        d                              t
          j        d          }t          j        t           ||||                                                              }|                                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_filer5       s     JIIIII*844;;B??GGPRSSJ;233::3??FFrJJRRSUSY[]^^Lh##L*	RRRYY[[\\ G <<>>E!!r   )r    )numpyr)   pandasr+   PILr   unstructured.partition.pdfr   $unstructured.partition.pdf_image.ocrr   5unstructured.partition.utils.ocr_models.ocr_interfacer   unstructured.utilsr   r.   	DataFramer   floatr5    r   r   <module>r@      s                 < < < < < < A A A A A A J J J J J J 4 4 4 4 4 4 /00     10( /00 #" """ " 	" " " 10" " "r   