
    Ng(              	       6   d dl 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mZ d dlmZ d dlmZ d dlmZ  G d	 d
          Z G d de          Zdee         fdZ	 	 ddej        dededej        fdZ G d de          ZdedefdZdededefdZdS )    N)Mapping)
HTMLParser)StringIO)AnyCallableHashableIterableIteratorUnion)hf_hub_download)Image)LayoutElementc                       e Zd ZdZdefdZdS )LazyEvaluateInfozClass that stores the information needed to lazily evaluate a function with given arguments.
    The object stores the information needed for evaluation as a function and its arguments.
    evaluatec                 &    || _         ||f| _        d S N)r   info)selfr   argskwargss       X/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured_inference/utils.py__init__zLazyEvaluateInfo.__init__   s     6N			    N)__name__
__module____qualname____doc__r   r    r   r   r   r      s9         # # # # # # #r   r   c                   V    e Zd ZdZdddZdedeeef         fdZ	de
fdZdefd	Zd
S )LazyDicta  Class that wraps a dict and only evaluates keys of the dict when the key is accessed. Keys
    that should be evaluated lazily should use LazyEvaluateInfo objects as values. By default when
    a value is computed from a LazyEvaluateInfo object, it is converted to the raw value in the
    internal dict, so subsequent accessing of the key will produce the same value. Set cache=False
    to avoid storing the raw value.
    T)cachec                6    || _         t          |i || _        d S r   )r"   dict	_raw_dict)r   r"   r   r   s       r   r   zLazyDict.__init__   s!    
t.v..r   keyreturnc                     | j                             |          }t          |t                    r*|j        }|j        \  }} ||i |}| j        r
|| j         |<   |S r   )r%   __getitem__
isinstancer   r   r   r"   )r   r&   valuer   r   r   s         r   r)   zLazyDict.__getitem__#   sk    **3//e-.. 	,~H :LD&Hd-f--Ez ,&+s#r   c                 *    t          | j                  S r   )iterr%   r   s    r   __iter__zLazyDict.__iter__-   s    DN###r   c                 *    t          | j                  S r   )lenr%   r.   s    r   __len__zLazyDict.__len__0   s    4>"""r   N)r   r   r   r   r   r   r   r   r   r)   r
   r/   intr2   r   r   r   r!   r!      s          %) / / / / /x E2BC2G,H    $( $ $ $ $# # # # # # #r   r!   elementsc                     g d}t          |           D ];\  }}d| d|j         |_        ||_        ||t          |          z           |_        <dS )zIAsign an numeric id to the elements in the list.
    Useful for debugging)redbluegreenmagentabrown-z-:N)	enumeratetextidr1   color)r4   colorsies       r   tagrC   4   sl     :99F(## * *1"Q""!&""S[[)	* *r   
   whiteimagepadbackground_colorr'   c                     | j         \  }}|dk     rt          d          t          j        | j        ||dz  z   ||dz  z   f|          }|                    | ||f           |S )zpads an input image with the same background color around it by pad on all 4 sides

    The original image is kept intact and a new image is returned with padding added.
    r   zPCan not pad an image with negative space! Please use a positive value for `pad`.   )size
ValueErrorr   newmodepaste)rF   rG   rH   widthheightrM   s         r   pad_image_with_background_colorrR   ?   sx     JME6
Qww^
 
 	
 )EJq&372B CEU
V
VCIIec3Z   Jr   c                   .     e Zd ZdZ fdZd Zd Z xZS )
MLStripperzDsimple markup language stripper that helps to strip tags from stringc                     t                                                       |                                  d| _        d| _        t                      | _        d S )NT)superr   resetstrictconvert_charrefsr   r=   )r   	__class__s    r   r   zMLStripper.__init__U   sD    

 $JJ			r   c                 :    | j                             |           dS )zprocess input dataN)r=   write)r   ds     r   handle_datazMLStripper.handle_data\   s    	r   c                 4    | j                                         S )z+performs stripping by get the value of text)r=   getvaluer.   s    r   get_datazMLStripper.get_data`   s    y!!###r   )r   r   r   r   r   r^   ra   __classcell__)rZ   s   @r   rT   rT   R   s\        NN      $ $ $ $ $ $ $r   rT   htmlc                 p    t                      }|                    |            |                                S )zDstripping html tags from input string and return string without tags)rT   feedra   )rc   ss     r   
strip_tagsrg   e   s(    AFF4LLL::<<r   path_or_repofilenamec                     t           j                            | |          }t           j                            |          r|S t	          | |fi |S )zpReturns path to local file if it exists, otherwise treats it as a huggingface repo and
    attempts to download.)ospathjoinexistsr   )rh   ri   r   	full_paths       r   %download_if_needed_and_get_local_pathrp   l   sN     \844I	w~~i   A|X@@@@@r   )rD   rE   )rk   collections.abcr   html.parserr   ior   typingr   r   r   r	   r
   r   huggingface_hubr   PILr   .unstructured_inference.inference.layoutelementr   r   r!   rC   r3   strrR   rT   rg   rp   r   r   r   <module>ry      s   				 # # # # # # " " " " " "       E E E E E E E E E E E E E E E E + + + + + +       H H H H H H# # # # # # # ## # # # #w # # #:*(=) * * * * # ;	  [	   &$ $ $ $ $ $ $ $&S S    A As AY\ A A A A A Ar   