
    Ng5                        d dl mZ d dlZd dlmZmZmZmZ d dlZd dl	m
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 d d
lmZmZ dZdZdZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 d7d8d)Zd9d-Z	 	 	 	 	 	 	 d:d;d6Z dS )<    )annotationsN)IOAnyOptionalSequence)UnstructuredClient)
operationsshared)retries)Element)logger)exactly_one)elements_from_dictselements_from_jsoni  i
 g      ?i@w T.https://api.unstructured.io/general/v0/general filenameOptional[str]content_typefileOptional[IO[bytes]]file_filenameapi_urlstrapi_keymetadata_filenameretries_initial_interval[int]retries_max_intervalOptional[int]retries_exponentOptional[float]retries_max_elapsed_timeretries_connection_errorsOptional[bool]request_kwargsr   returnlist[Element]c                   t          | |           |r|rt          d          ||}t          j        d           d|v r
|dd         n|}t	          ||          }| Qt          | d          5 }t          j        |                                | 	          }ddd           n# 1 swxY w Y   n)|'|t          d
          t          j        ||	          }t          j
        t          j        dd|i|          }t          ||	||
||          }|j                            ||          }|j        dk    rt!          |j        j                  S t          d|j         d          )a  Partitions a document using the Unstructured REST API. This is equivalent to
    running the document through partition.

    See https://api.unstructured.io/general/docs for the hosted API documentation or
    https://github.com/Unstructured-IO/unstructured-api for instructions on how to run
    the API locally as a container.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    content_type
        A string defining the file content in MIME type
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    metadata_filename
        When file is not None, the filename (string) to store in element metadata. E.g. "foo.txt"
    api_url
        The URL for the Unstructured API. Defaults to the hosted Unstructured API.
    api_key
        The API key to pass to the Unstructured API.
    retries_initial_interval
        Defines the time interval (in seconds) to wait before the first retry in case of a request
        failure. Defaults to 3000. If set should be > 0.
    retries_max_interval
        Defines the maximum time interval (in seconds) to wait between retries (the interval
        between retries is increased as using exponential increase algorithm
        - this setting limits it). Defaults to 720000. If set should be > 0.
    retries_exponent
        Defines the exponential factor to increase the interval between retries. Defaults to 1.5.
        If set should be > 0.0.
    retries_max_elapsed_time
        Defines the maximum time (in seconds) to wait for retries. If exceeded, the original
        exception is raised. Defaults to 1800000. If set should be > 0.
    retries_connection_errors
        Defines whether to retry on connection errors. Defaults to True.
    request_kwargs
        Additional parameters to pass to the data field of the request to the Unstructured API.
        For example the `strategy` parameter.
    )r   r   zOnly one of metadata_filename and file_filename is specified. metadata_filename is preferred. file_filename is marked for deprecation.NzuThe file_filename kwarg will be deprecated in a future version of unstructured. Please use metadata_filename instead.z/general/v0/generali)api_key_auth
server_urlrb)content	file_namezWIf file is specified in partition_via_api, metadata_filename must be specified as well.files)partition_parameters)r$   r!   r   r#   r   sdk)requestr      )textReceive unexpected status code  from the API. )r   
ValueErrorr   warnr   openr
   Filesreadr	   PartitionRequestPartitionParametersget_retries_configgeneral	partitionstatus_coder   raw_responser4   )r   r   r   r   r   r   r   r   r   r!   r#   r$   r&   base_urlr1   fr/   reqretries_configresponses                       V/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured/partition/api.pypartition_via_apirJ      s$   n ---- 
] 
W
 
 	

  )4	
 	
 	
 !6 @ @wtt}}gH
'h
G
G
GC(D!! 	QL"  E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
	$?   T5FGGG

%#7VVeV~VV  C (";)!9!91  N {$$ %  H
 s""!x'<'ABBBBRh.BRRR
 
 	
s   0)B%%B),B)r1   r   Optional[retries.RetryConfig]c           	       
 d}|j         j        
t          d ||||| fD                       rd
fd}
r
j        
j        nt          }t          j        |p |d	t                    |p |d
t                    |p |dt                    |p |dt                              }	t          j        d|	| | n|          }|S )a  Constructs a RetryConfig object from the provided parameters. If any of the parameters
    are None, the default values are taken from the SDK configuration or the default constants.

    If all parameters are None, returns None (and the SDK-managed defaults are used within the
    client)

    The solution is not perfect as the RetryConfig object does not include the defaults by
    itself so we might need to construct it basing on our defaults.

    Parameters
    ----------
    retries_connection_errors
        Defines whether to retry on connection errors. If not set the
        DEFAULT_RETRIES_CONNECTION_ERRORS constant is used.
    retries_exponent
        Defines the exponential factor to increase the interval between retries.
        If set, should be > 0.0 (otherwise the DEFAULT_RETRIES_EXPONENT constant is used)
    retries_initial_interval
        Defines the time interval to wait before the first retry in case of a request failure.
        If set, should be > 0 (otherwise the DEFAULT_RETRIES_INITIAL_INTERVAL_SEC constant is used)
    retries_max_elapsed_time
        Defines the maximum time to wait for retries. If exceeded, the original exception is raised.
        If set, should be > 0 (otherwise the DEFAULT_RETRIES_MAX_ELAPSED_TIME_SEC constant is used)
    retries_max_interval
        Defines the maximum time interval to wait between retries. If set, should be > 0
        (otherwise the DEFAULT_RETRIES_MAX_INTERVAL_SEC constant is used)
    sdk
        The UnstructuredClient object to take the default values from.
    Nc              3     K   | ]}|d uV  	d S Nr7   ).0settings     rI   	<genexpr>z%get_retries_config.<locals>.<genexpr>   s;       	 	 	t	 	 	 	 	 	    setting_namer   default_valuer   r'   c                >    rt          j        |           x}r|S |S rN   )getattrbackoff)rS   rT   setting_valuesdk_default_retries_configs      rI   get_backoff_defaultz/get_retries_config.<locals>.get_backoff_default   s3    ) )$+,F,NP\$]$]]= )((  rR   initial_intervalmax_intervalexponentmax_elapsed_time)r[   r\   r]   r^   rW   )strategyrW   retry_connection_errors)rS   r   rT   r   r'   r   )sdk_configurationretry_configanyr`   !DEFAULT_RETRIES_CONNECTION_ERRORSr   BackoffStrategy$DEFAULT_RETRIES_INITIAL_INTERVAL_SEC DEFAULT_RETRIES_MAX_INTERVAL_SECDEFAULT_RETRIES_EXPONENT$DEFAULT_RETRIES_MAX_ELAPSED_TIME_SECRetryConfig)r$   r!   r   r#   r   r1   rG   rZ   "default_retries_connneciton_errorsbackoff_strategyrY   s             @rI   r?   r?      s_   J N!$!6!C
 	 	 % $%
	 	 	 	 	 1
	! 	! 	! 	! 	! 	! *3*BN '>> 3	 	+ #2( a&&'9;_`` % Y&&~7WXX !]$7$7
D\$]$] ) a&&'9;_``
 
 
" !,$ -8 *)7
 
 
 rR   	filenamesOptional[list[str]]content_typesr/   Optional[Sequence[IO[bytes]]]file_filenamesmetadata_filenameslist[list[Element]]c                   d|d}|r|rt          d          ||}t          j        d           | |r/t          |          t          |           k    rt          d          t	          j                    5 fd| D             }g }	t          |          D ]3\  }
}| |
         }|||
         nd}|	                    d|||ff           4t          j	        ||||		          }ddd           n# 1 swxY w Y   n||r/t          |          t          |          k    rt          d
          |st          d          t          |          t          |          k    rt          d          g }	t          |          D ]3\  }
}|||
         nd}||
         }|	                    d|||ff           4t          j	        ||||		          }|j
        dk    r]g }|                                }t          |d         t                    s|g}|D ]$}|                    t          |                     %|S t          d|j
         d          )a  Partitions multiple documents using the Unstructured REST API by batching
    the documents into a single HTTP request.

    See https://api.unstructured.io/general/docs for the hosted API documentation or
    https://github.com/Unstructured-IO/unstructured-api for instructions on how to run
    the API locally as a container.

    Parameters
    ----------
    filenames
        A list of strings defining the target filename paths.
    content_types
        A list of strings defining the file contents in MIME types.
    files
        A list of file-like object using "rb" mode --> open(filename, "rb").
    metadata_filename
        When file is not None, the filename (string) to store in element metadata. E.g. "foo.txt"
    api_url
        The URL for the Unstructured API. Defaults to the hosted Unstructured API.
    api_key
        The API key to pass to the Unstructured API.
    request_kwargs
        Additional parameters to pass to the data field of the request to the Unstructured API.
        For example the `strategy` parameter.
    zapplication/json)ACCEPTzUNSTRUCTURED-API-KEYzOnly one of metadata_filenames and file_filenames is specified. metadata_filenames is preferred. file_filenames is marked for deprecation.NzwThe file_filenames kwarg will be deprecated in a future version of unstructured. Please use metadata_filenames instead.z6content_types and filenames must have the same length.c                V    g | ]%}                     t          |d                     &S )r,   )enter_contextr:   )rO   rE   stacks     rI   
<listcomp>z.partition_multiple_via_api.<locals>.<listcomp>"  s/    KKKAU((a77KKKrR   r/   )headersdatar/   z2content_types and files must have the same length.z8metadata_filenames must be specified if files are passedz7metadata_filenames and files must have the same length.r3   r   r5   r6   )r8   r   r9   len
contextlib	ExitStack	enumerateappendrequestspostrB   json
isinstancelistr   )rm   ro   r/   rq   r   r   rr   r&   rz   _filesir   r   r   rH   _file	documentsresponse_listdocumentrx   s                      @rI   partition_multiple_via_apir      s4   H % ' G
  
n 
Y
 
 	

 !+5	
 	
 	

  	WS//3y>>AAUVVV!## 	uKKKKKKKEF$U++ I I4$Q<3@3L}Q//RVw4(FGHHHH}#	  H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  
	 	SS//3u::==QRRR! 	XWXXX#$$E

22VWWW!%(( 	F 	FHAu/</H=++dL)!,HMM7Xul$CDEEEE=	
 
 
 s""	  -*D11 	,*OM% 	< 	<H0::;;;;Rh.BRRR
 
 	
s   8A,C00C47C4)NNNNr   r   NNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r    r$   r%   r&   r   r'   r(   )r$   r%   r!   r"   r   r    r#   r    r   r    r1   r   r'   rK   )NNNNr   r   N)rm   rn   ro   rn   r/   rp   rq   rn   r   r   r   r   rr   rn   r&   r   r'   rs   )!
__future__r   r}   typingr   r   r   r   r   unstructured_clientr   unstructured_client.modelsr	   r
   unstructured_client.utilsr   unstructured.documents.elementsr   unstructured.loggerr   $unstructured.partition.common.commonr   unstructured.staging.baser   r   rf   rg   rh   ri   rd   rJ   r?   r   r7   rR   rI   <module>r      s   " " " " " "     . . . . . . . . . . . .  2 2 2 2 2 2 9 9 9 9 9 9 9 9 - - - - - - 3 3 3 3 3 3 & & & & & & < < < < < < M M M M M M M M (, $#)   '. $$( ! #"& $#'C'+&**.(,.204q
 q
 q
 q
 q
hY Y Y Yz &*)-+/*.C.2m
 m
 m
 m
 m
 m
 m
rR   