a
    ¡˜ëjÚ"  ã                   @   s†   d Z 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ed
œdd„ƒZeG dd„ dƒƒZeƒ ZdS )a¿  
This module contains variables that can permitted to be tweaked by the system environment. For
example, model parameters that changes the output of an inference call. Constants do NOT belong in
this module. Constants are values that are usually names for common options (e.g., color names) or
settings that should not be altered without making a code change (e.g., definition of 1Gb of memory
in bytes). Constants should go into `./constants.py`
é    N)Ú	dataclass)Ú	lru_cache)ÚPath)ÚOptional)ÚOCR_AGENT_TESSERACTé   )Úmaxsize)ÚdirÚreturnc                 C   s    t | ƒdt d¡› � }t|ƒS )Nztmp/r   )r   ÚosÚgetpgidÚstr)r	   Útempdir© r   úb/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/utils/config.pyÚget_tempdir   s    r   c                   @   sJ  e Zd ZdZdd„ ZdGeeedœdd„Zeeedœdd	„Zee	e	dœd
d„Z
eeedœdd„Zeddœdd„Zeedœdd„ƒZeedœdd„ƒZee	dœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd „ƒZeedœd!d"„ƒZeedœd#d$„ƒZeedœd%d&„ƒZeedœd'd(„ƒZeedœd)d*„ƒZee	dœd+d,„ƒZee	dœd-d.„ƒZee	dœd/d0„ƒZee	dœd1d2„ƒZee	dœd3d4„ƒZeedœd5d6„ƒZ eedœd7d8„ƒZ!eedœd9d:„ƒZ"eedœd;d<„ƒZ#eedœd=d>„ƒZ$eedœd?d@„ƒZ%eedœdAdB„ƒZ&ee'e	 dœdCdD„ƒZ(eedœdEdF„ƒZ)dS )HÚ	ENVConfigz+class for configuring enviorment parametersc                 C   s   | j r|  | j¡ d S ©N)ÚGLOBAL_WORKING_DIR_ENABLEDÚ_setup_tmpdirÚGLOBAL_WORKING_PROCESS_DIR©Úselfr   r   r   Ú__post_init__   s    zENVConfig.__post_init__Ú )ÚvarÚdefault_valuer
   c                 C   s   t j ||¡S )zhattempt to get the value of var from the os environment; if not present return the
        default_value)r   ÚenvironÚget)r   r   r   r   r   r   Ú_get_string!   s    zENVConfig._get_stringc                 C   s   |   |¡ }rt|ƒS |S r   )r   Úint©r   r   r   Úvaluer   r   r   Ú_get_int&   s    zENVConfig._get_intc                 C   s   |   |¡ }rt|ƒS |S r   )r   Úfloatr!   r   r   r   Ú
_get_float+   s    zENVConfig._get_floatc                 C   s   |   |¡ }r| ¡ dv S |S )N)ÚtrueÚ1Út)r   Úlowerr!   r   r   r   Ú	_get_bool0   s    zENVConfig._get_boolN)Útmpdirr
   c                 C   s   t |ƒjddd� |t_d S )NT)ÚparentsÚexist_ok)r   ÚmkdirÚtempfiler   )r   r+   r   r   r   r   5   s    zENVConfig._setup_tmpdir)r
   c                 C   s   |   dd¡S )zRextra image content to add around an identified element region; measured in pixelsÚIMAGE_CROP_PADr   ©r#   r   r   r   r   r0   9   s    zENVConfig.IMAGE_CROP_PADc                 C   s   |   dd¡S )zêextra image content to add around an identified table region; measured in pixels

        The padding adds image data around an identified table bounding box for downstream table
        structure detection model use as input
        ÚTABLE_IMAGE_CROP_PADr   r1   r   r   r   r   r2   >   s    zENVConfig.TABLE_IMAGE_CROP_PADc                 C   s   |   dd¡S )z%the quantile to check for text heightÚTESSERACT_TEXT_HEIGHT_QUANTILEç      à?©r%   r   r   r   r   r3   G   s    z(ENVConfig.TESSERACT_TEXT_HEIGHT_QUANTILEc                 C   s   |   dd¡S )zÍminimum text height acceptable from tesseract OCR results

        if estimated text height from tesseract OCR results is lower than this value the image is
        scaled up to be processed again
        ÚTESSERACT_MIN_TEXT_HEIGHTé   r1   r   r   r   r   r6   L   s    z#ENVConfig.TESSERACT_MIN_TEXT_HEIGHTc                 C   s   |   dd¡S )zÐmaximum text height acceptable from tesseract OCR results

        if estimated text height from tesseract OCR results is higher than this value the image is
        scaled down to be processed again
        ÚTESSERACT_MAX_TEXT_HEIGHTéd   r1   r   r   r   r   r8   U   s    z#ENVConfig.TESSERACT_MAX_TEXT_HEIGHTc                 C   s   |   dd¡S )z%optimum text height for tesseract OCRÚTESSERACT_OPTIMUM_TEXT_HEIGHTé   r1   r   r   r   r   r:   ^   s    z'ENVConfig.TESSERACT_OPTIMUM_TEXT_HEIGHTc                 C   s   |   dd¡S )zFTesseract predictions with confidence below this threshold are ignoredÚ(TESSERACT_CHARACTER_CONFIDENCE_THRESHOLDg        r5   r   r   r   r   r<   c   s    z2ENVConfig.TESSERACT_CHARACTER_CONFIDENCE_THRESHOLDc                 C   s   |   dd¡S )z%API endpoint to use for Google VisionÚGOOGLEVISION_API_ENDPOINTr   ©r   r   r   r   r   r=   h   s    z#ENVConfig.GOOGLEVISION_API_ENDPOINTc                 C   s   |   dt¡S )zOCR Agent to useÚ	OCR_AGENT)r   r   r   r   r   r   r?   m   s    zENVConfig.OCR_AGENTc                 C   s   |   dd¡S )zˆextra image block content to add around an identified element(`Image`, `Table`) region
        horizontally; measured in pixels
        Ú'EXTRACT_IMAGE_BLOCK_CROP_HORIZONTAL_PADr   r1   r   r   r   r   r@   r   s    z1ENVConfig.EXTRACT_IMAGE_BLOCK_CROP_HORIZONTAL_PADc                 C   s   |   dd¡S )z†extra image block content to add around an identified element(`Image`, `Table`) region
        vertically; measured in pixels
        Ú%EXTRACT_IMAGE_BLOCK_CROP_VERTICAL_PADr   r1   r   r   r   r   rA   y   s    z/ENVConfig.EXTRACT_IMAGE_BLOCK_CROP_VERTICAL_PADc                 C   s   |   dd¡S )zCadds `table_as_cells` to a Table element's metadata when it is TrueÚEXTRACT_TABLE_AS_CELLSF©r*   r   r   r   r   rB   €   s    z ENVConfig.EXTRACT_TABLE_AS_CELLSc                 C   s   |   dd¡S )a9  threshold to determine if an OCR region is a sub-region of a given block
        when aggregating the text from OCR'd elements that lie within the given block

        When the intersection region area divided by self area is larger than this threshold self is
        considered a subregion of the other
        ÚOCR_LAYOUT_SUBREGION_THRESHOLDr4   r5   r   r   r   r   rD   …   s    z(ENVConfig.OCR_LAYOUT_SUBREGION_THRESHOLDc                 C   s   |   dd¡S )úRthreshold to consider the bounding boxes of two embedded images as the same regionÚ$EMBEDDED_IMAGE_SAME_REGION_THRESHOLDg333333ã?r5   r   r   r   r   rF   �   s    z.ENVConfig.EMBEDDED_IMAGE_SAME_REGION_THRESHOLDc                 C   s   |   dd¡S )aA  threshold to determine if an embedded region is a sub-region of a given block
        when aggregating the text from embedded elements that lie within the given block

        When the intersection region area divided by self area is larger than this threshold self is
        considered a subregion of the other
        Ú-EMBEDDED_TEXT_AGGREGATION_SUBREGION_THRESHOLDg®Gáz®ï?r5   r   r   r   r   rG   ”   s    z7ENVConfig.EMBEDDED_TEXT_AGGREGATION_SUBREGION_THRESHOLDc                 C   s   |   dd¡S )rE   Ú#EMBEDDED_TEXT_SAME_REGION_THRESHOLDçÍÌÌÌÌÌì?r5   r   r   r   r   rH   ž   s    z-ENVConfig.EMBEDDED_TEXT_SAME_REGION_THRESHOLDc                 C   s   |   dd¡S )zžThe threshold value (between 0.0 and 1.0) that determines the minimum overlap required
        for an annotation to be considered within the element.
        ÚPDF_ANNOTATION_THRESHOLDrI   r5   r   r   r   r   rJ   £   s    z"ENVConfig.PDF_ANNOTATION_THRESHOLDc                 C   s   |   dd¡S )zBEnable usage of GLOBAL_WORKING_DIR and GLOBAL_WORKING_PROCESS_DIR.r   FrC   r   r   r   r   r   «   s    z$ENVConfig.GLOBAL_WORKING_DIR_ENABLEDc                 C   s   |   dtt ¡ d ƒ¡S )z%Path to Unstructured cache directory.ÚGLOBAL_WORKING_DIRz.cache/unstructured)r   r   r   Úhomer   r   r   r   rK   °   s    zENVConfig.GLOBAL_WORKING_DIRc                 C   s8   t | jd�}|  d|¡}|dkr$|}| jr4|  |¡ |S )zŒPath to Unstructured cache tempdir. Overrides TMPDIR, TEMP and TMP.
        Defaults to '{GLOBAL_WORKING_DIR}/tmp/{os.getpgid(0)}'.
        )r	   r   r   )r   rK   r   r   r   )r   Zdefault_tmpdirr+   r   r   r   r   µ   s    
z$ENVConfig.GLOBAL_WORKING_PROCESS_DIRc                 C   s   |   dd¡S )z)Analysis dump object detection skip flag.ÚANALYSIS_DUMP_OD_SKIPFrC   r   r   r   r   rM   Â   s    zENVConfig.ANALYSIS_DUMP_OD_SKIPc                 C   s   |   dd¡S )z(Analysis draw bboxes on pages skip flag.ÚANALYSIS_BBOX_SKIPFrC   r   r   r   r   rN   Ç   s    zENVConfig.ANALYSIS_BBOX_SKIPc                 C   s   |   dd¡S )z@Flag for drawing the analysis bboxes on a single image (as grid)ÚANALYSIS_BBOX_DRAW_GRIDFrC   r   r   r   r   rO   Ì   s    z!ENVConfig.ANALYSIS_BBOX_DRAW_GRIDc                 C   s   |   dd¡S )zMFlag for drawing the caption above the analysed page (for e.g. layout source)ÚANALYSIS_BBOX_DRAW_CAPTIONTrC   r   r   r   r   rP   Ñ   s    z$ENVConfig.ANALYSIS_BBOX_DRAW_CAPTIONc                 C   s   |   dd¡}|dkrdS |S )zAnalaysis bbox resize valueÚANALYSIS_BBOX_RESIZEg      ð¿Nr5   )r   Úresizer   r   r   rQ   Ö   s    zENVConfig.ANALYSIS_BBOX_RESIZEc                 C   s   |   dd¡S )zJThe format for analysed pages with bboxes drawn on them. Default is 'png'.ÚANALYSIS_BBOX_FORMATZpngr>   r   r   r   r   rS   Þ   s    zENVConfig.ANALYSIS_BBOX_FORMAT)r   )*Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r#   r$   r%   Úboolr*   r   Úpropertyr0   r2   r3   r6   r8   r:   r<   r=   r?   r@   rA   rB   rD   rF   rG   rH   rJ   r   rK   r   rM   rN   rO   rP   r   rQ   rS   r   r   r   r   r      sv   		r   )rW   r   r/   Zdataclassesr   Ú	functoolsr   Úpathlibr   Útypingr   Z&unstructured.partition.utils.constantsr   r   r   r   Z
env_configr   r   r   r   Ú<module>   s    K