a
    ¡˜ëjŠ  ã                   @  s¨   d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZmZ er”d dlmZ d d	lmZ d d
lmZ G dd„ deƒZdS )é    )ÚannotationsN)ÚABCÚabstractmethod)ÚTYPE_CHECKING)Úlogger)Ú
env_config)ÚOCR_AGENT_MODULES_WHITELISTÚOCR_AGENT_PADDLEÚOCR_AGENT_PADDLE_OLDÚOCR_AGENT_TESSERACTÚOCR_AGENT_TESSERACT_OLD)ÚImage)ÚTextRegions)ÚLayoutElementsc                   @  s¦   e Zd ZdZedd dœdd„ƒZeejdd�dddd	œd
d„ƒƒZ	e
dddœdd„ƒZe
dddœdd„ƒZe
dddœdd„ƒZe
ddœdd„ƒZeddœdd„ƒZdS )ÚOCRAgentzIDefines the interface for an Optical Character Recognition (OCR) service.Ústr)ÚlanguageÚreturnc                 C  s   |   ¡ }|  ||¡S )z�Get the configured OCRAgent instance.

        The OCR package used by the agent is determined by the `OCR_AGENT` environment variable.
        )Ú_get_ocr_agent_cls_qnameÚget_instance)Úclsr   Zocr_agent_cls_qname© r   út/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/utils/ocr_models/ocr_interface.pyÚ	get_agent   s    zOCRAgent.get_agentN)Úmaxsizez
'OCRAgent')Úocr_agent_moduler   r   c              
   C  s’   |   dd¡\}}|tvr.td|› dt› d�ƒ‚zt |¡}t||ƒ}||ƒW S  ttfyŒ } z$t 	d|› �¡ t
dƒ‚W Y d }~n
d }~0 0 d S )NÚ.é   z+Environment variable OCR_AGENT module name z- must be set to a whitelisted module part of z!Failed to get OCRAgent instance: ziCould not get the OCRAgent instance. Please check the OCR package and the OCR_AGENT environment variable.)Úrsplitr   Ú
ValueErrorÚ	importlibÚimport_moduleÚgetattrÚImportErrorÚAttributeErrorr   ÚerrorÚRuntimeError)r   r   Úmodule_nameÚ
class_nameÚmoduleZloaded_classÚer   r   r   r   $   s     ÿÿ


ÿzOCRAgent.get_instancezPILImage.Imager   )Úimager   c                 C  s   d S ©Nr   ©Úselfr+   r   r   r   Úget_layout_elements_from_image9   s    z'OCRAgent.get_layout_elements_from_imager   c                 C  s   d S r,   r   r-   r   r   r   Úget_layout_from_image=   s    zOCRAgent.get_layout_from_imagec                 C  s   d S r,   r   r-   r   r   r   Úget_text_from_imageA   s    zOCRAgent.get_text_from_imageÚbool)r   c                 C  s   d S r,   r   )r.   r   r   r   Úis_text_sortedE   s    zOCRAgent.is_text_sortedc                  C  sD   t j} tttti}| |  ¡ ¡ }r@t 	d| › d|› d�¡ |S | S )a#  Get the fully-qualified class name of the configured OCR agent.

        The qualified name (qname) looks like:
            "unstructured.partition.utils.ocr_models.tesseract_ocr.OCRAgentTesseract"

        The qname provides the full module address and class name of the OCR agent.
        zOCR agent name zA is outdated and will be removed in a future release; please use z instead)
r   Z	OCR_AGENTr   r   r
   r	   ÚgetÚlowerr   Úwarning)Zocr_agent_qnameZqnames_by_keynameZqname_mapped_from_keynamer   r   r   r   I   s    	þÿÿz!OCRAgent._get_ocr_agent_cls_qname)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   ÚstaticmethodÚ	functoolsÚ	lru_cacher   r   r/   r0   r1   r3   r   r   r   r   r   r      s    
r   )Ú
__future__r   r=   r    Úabcr   r   Útypingr   Zunstructured.loggerr   Z#unstructured.partition.utils.configr   Z&unstructured.partition.utils.constantsr   r	   r
   r   r   ZPILr   ZPILImageZ)unstructured_inference.inference.elementsr   Z.unstructured_inference.inference.layoutelementr   r   r   r   r   r   Ú<module>   s   