
    NgX                    |   U d Z ddlmZ ddlZddlZddlmZ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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 ddlmZ ddl m!Z! ede"e         f         Z#de$d<   	 dHdddddej%        ddi g dddddddddddddddddddId?Z&di ddfdJdBZ'dKdEZ( G dF dG          Z)dS )Lz9Provides partitioning with automatic file-type detection.    )annotationsN)IOAnyCallableLiteralOptional)	TypeAlias)DataSourceMetadataElement)detect_filetypeis_json_processable)FileType)logger)UnsupportedFileFormatError)exactly_one)check_language_args)PartitionStrategy)dependency_exists.r	   PartitionerF)pdfjpgpngheicT   )content_typefilefile_filenameurlinclude_page_breaksstrategyencodingparagraph_grouperheadersskip_infer_table_types
ssl_verifyocr_languages	languagesdetect_language_per_elementpdf_infer_table_structureextract_images_in_pdfextract_image_block_typesextract_image_block_output_dirextract_image_block_to_payloadxml_keep_tagsdata_source_metadatametadata_filenamerequest_timeouthi_res_model_name
model_namestarting_page_numberfilenameOptional[str]r   r   Optional[IO[bytes]]r   r   r   boolr    strr!   r"   /Optional[Callable[[str], str]] | Literal[False]r#   dict[str, str]r$   	list[str]r%   r&   r'   Optional[list[str]]r(   r)   r*   r+   r,   r-   r.   r/   Optional[DataSourceMetadata]r0   r1   Optional[int]r2   r3   r4   intkwargsr   c               H   t          || |           |r|rt          d          ||}t          j        d           |                    d|           |rt          j        d           t          |pg |          }|t          |||	||          \  }}n.|	i k    rt          j        d           t          | ||||	          }||                    d
           t          ||
|          }t                      }|t          j        k    r&|                    |          } |d| |||||d|} n|t          j        k    r'|                    |          }! |!d| ||||||d|} na|t          j        k    r'|                    |          }" |"d| ||||||d|} n*|t          j        k    r%|                    |          }# |#d| ||||d|} n|t          j        k    r&|                    |          }$ |$d| |||||d|} n|t          j        k    r&|                    |          }% |%d| |||||d|} n|j        dk    r/|                    |          }& |&d| |d|||||p||||||d|} nO|t          j        k    rBt+          | |          st          d          |                    |          }' |'d| |d|} n|t          j        k    r&|                    |          }( |(d| |||||d|} n|t          j        k    r$|                    |          }) |)d| |||d|} n|t          j        k    r'|                    |          }* |*d| ||||||d|} n\|t          j        k    r%|                    |          }+ |+d| ||||d|} n'|t          j        k    r/|                    |          }, |,d| |d|||||p||||||d|} n|t          j        k    r'|                    |          }- |-d| ||||||d|} n|t          j        k    r(|                    |          }. |.d| |||||||d|} ny|t          j        k    r&|                    |          }/ |/d| |||||d|} nC|t          j        k    r&|                    |          }0 |0d| |||||d|} n|t          j        k    r#|                    |          }1 |1d| |||d|} n|t          j         k    r%|                    |          }2 |2d| |||||d|} n|t          j!        t          j"        fv r%|                    |          }3 |3d| |||||d|} nf|t          j#        k    r%|                    |          }4 |4d| |||||d|} n1|t          j$        k    rg } n| sdnd|  }5tK          |5 d| d          | D ]W}6||6j&        _'        ||6j&        _(        |*t          j)        |          }7|7|7j*        nd|6j&        _+        F|j*        |6j&        _+        X| S ) a5  Partitions a document into its constituent elements.

    Uses libmagic to determine the file's type and route it to the appropriate partitioning
    function. Applies the default parameters for each partitioning function. Use the document-type
    specific partitioning functions if you need access to additional kwarg options.

    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"
    url
        The url for a remote document. Pass in content_type if you want partition to treat
        the document as a specific content_type.
    include_page_breaks
        If True, the output will include page breaks if the filetype supports it
    strategy
        The strategy to use for partitioning PDF/image. Uses a layout detection model if set
        to 'hi_res', otherwise partition simply extracts the text from the document
        and processes it.
    encoding
        The encoding method used to decode the text input. If None, utf-8 will be used.
    headers
        The headers to be used in conjunction with the HTTP request if URL is set.
    skip_infer_table_types
        The document types that you want to skip table extraction with.
    ssl_verify
        If the URL parameter is set, determines whether or not partition uses SSL verification
        in the HTTP request.
    languages
        The languages present in the document, for use in partitioning and/or OCR. For partitioning
        image or pdf documents with Tesseract, you'll first need to install the appropriate
        Tesseract language pack. For other partitions, language is detected using naive Bayesian
        filter via `langdetect`. Multiple languages indicates text could be in either language.
        Additional Parameters:
            detect_language_per_element
                Detect language per element instead of at the document level.
    pdf_infer_table_structure
        Deprecated! Use `skip_infer_table_types` to opt out of table extraction for any document
        type.
        If True and strategy=hi_res, any Table Elements extracted from a PDF will include an
        additional metadata field, "text_as_html," where the value (string) is a just a
        transformation of the data into an HTML <table>.
        The "text" field for a partitioned Table Element is always present, whether True or False.
    extract_images_in_pdf
        Only applicable if `strategy=hi_res`.
        If True, any detected images will be saved in the path specified by
        'extract_image_block_output_dir' or stored as base64 encoded data within metadata fields.
        Deprecation Note: This parameter is marked for deprecation. Future versions will use
        'extract_image_block_types' for broader extraction capabilities.
    extract_image_block_types
        Only applicable if `strategy=hi_res`.
        Images of the element type(s) specified in this list (e.g., ["Image", "Table"]) will be
        saved in the path specified by 'extract_image_block_output_dir' or stored as base64
        encoded data within metadata fields.
    extract_image_block_to_payload
        Only applicable if `strategy=hi_res`.
        If True, images of the element type(s) defined in 'extract_image_block_types' will be
        encoded as base64 data and stored in two metadata fields: 'image_base64' and
        'image_mime_type'.
        This parameter facilitates the inclusion of element data directly within the payload,
        especially for web-based applications or APIs.
    extract_image_block_output_dir
        Only applicable if `strategy=hi_res` and `extract_image_block_to_payload=False`.
        The filesystem path for saving images of the element type(s)
        specified in 'extract_image_block_types'.
    xml_keep_tags
        If True, will retain the XML tags in the output. Otherwise it will simply extract
        the text from within the tags. Only applies to partition_xml.
    request_timeout
        The timeout for the HTTP request if URL is set. Defaults to None meaning no timeout and
        requests will block indefinitely.
    hi_res_model_name
        The layout detection model used when partitioning strategy is set to `hi_res`.
    model_name
        The layout detection model used when partitioning strategy is set to `hi_res`. To be
        deprecated in favor of `hi_res_model_name`.
    starting_page_number
        Indicates what page number should be assigned to the first page in the document.
        This information will be reflected in elements' metadata and can be be especially
        useful when partitioning a document that is part of a larger document.
    )r   r5   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.r0   z]The pdf_infer_table_structure kwarg is deprecated. Please use skip_infer_table_types instead.)r   r   r#   r%   r1   zUThe headers kwarg is set but the url kwarg is not. The headers kwarg will be ignored.)	file_pathr   r!   r   metadata_file_pathr   )r5   r   r!   infer_table_structurer'   r(   )r5   r   rE   r'   r(   r4   r    )r5   r   r!   r'   r(   )r5   r   r   rE   r'   r(   )r5   r   r   r!   r'   r(   image)r5   r   r   r   rE   r    r'   r2   r*   r+   r,   r-   r4   )r5   r   zDetected a JSON file that does not conform to the Unstructured schema. partition_json currently only processes serialized Unstructured output.)r5   r   r'   r(   )r5   r   r   r'   r(   )r5   r   r   rE   r'   r(   r    )r5   r   r   rE   r'   r(   r4   r    )r5   r   r!   r"   r'   r(   )r5   r   rE   r'   r(   r4   )r5   r   r!   r.   r'   r(   zInvalid filezInvalid file z. The z) file type is not supported in partition. ),r   
ValueErrorr   warning
setdefaultr   file_and_type_from_urlr   seekdecide_table_extraction_PartitionerLoaderr   CSVgetDOCDOCXEMLEPUBHTMLpartitioner_shortnameJSONr   MDMSGODTORGPDFPPTPPTXRSTRTFTSVTXTXLSXLSXXMLEMPTYr   metadatar   data_sourcefrom_mime_type	mime_typefiletype)8r5   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   rA   	file_typerE   partitioner_loaderpartition_csvelementspartition_docpartition_docxpartition_emailpartition_epubpartition_htmlpartition_imagepartition_jsonpartition_mdpartition_msgpartition_odtpartition_orgpartition_pdfpartition_pptpartition_pptxpartition_rstpartition_rtfpartition_tsvpartition_textpartition_xlsxpartition_xmlmsgelementout_filetypes8                                                           W/var/www/html/ai-engine/env/lib/python3.11/site-packages/unstructured/partition/auto.py	partitionr      s	   l TH#6666 
] 
W
 
 	

  )4	
 	
 	
 )+<===  
	
 	
 	

 $IO]CCI
0%!+
 
 
ii b==N5   $%0
 
 
	 		!3!  ,--HL  *..y99 = 
"7(C
 
 
 
 
hl	"	"*..y99 = 	
"7(C!5	
 	
 	
 	
 
hm	#	#+//	::!> 	
"7(C!5	
 	
 	
 	
 
hl	"	",00;;"? 
(C
 
 
 
 
hm	#	#+//	::!> 
 3"7(C
 
 
 
 
hm	#	#+//	::!> 
 3(C
 
 
 
 
	(G	3	3,00;;"? 
 3"7/=:"7&?+I+I!5
 
 
 
  
hm	#	#"H4@@@ 	Z   ,//	::!>I8$II&II	hk	!	!)--i88< 
 3"7(C
 
 
 
 
hl	"	"*..y99 = 
(C	
 

 
 
 
hl	"	"*..y99 = 	
"7(C!5	
 	
 	
 	
 
hl	"	"*..y99 = 
 3(C
 
 
 
 
hl	"	"*..y99 = 
 3"7/=:"7&?+I+I!5
 
 
 
  
hl	"	"*..y99 = 	
 3"7(C	
 	
 	
 	
 
hm	#	#+//	::!> 

 3"7(C!5

 

 

 

 
hl	"	"*..y99 = 
 3"7(C
 
 
 
 
hl	"	"*..y99 = 
 3"7(C
 
 
 
 
hl	"	"*..y99 = 
(C	
 

 
 
 
hl	"	"+//	::!> 
/(C
 
 
 
 
x|X]3	3	3+//	::!> 
"7(C!5
 
 
 
 
hl	"	"*..y99 = 
'(C
 
 
 
 
hn	$	$$,Lnn2L(2L2L(NN)NNN
 
 	
  < <"';$##2<@@LBNBZ(>(>`dG%%(1(;G%%O    returntuple[io.BytesIO, FileType]c                Z   t          j        | |||          }t          j        |j                  }|p|j                            dd           x}r?|                    d          d                                                                         }|j	        }t          |||          }||fS )N)r#   verifytimeoutzContent-Type;r   )r   r!   r   )requestsrP   ioBytesIOcontentr#   splitstriplowerr!   r   )	r   r   r#   r%   r1   responser   r!   rk   s	            r   rK   rK     s     |C_]]]H:h&''D#Qx'7';';ND'Q'QQ| B#))#..q17799??AA  HD8,WWWH>r   rk   Optional[FileType]c                \    | r| j                                         nd }|dk    r|p||vS ||vS )Nr   )namer   )rk   r$   r)   doc_types       r   rM   rM     sK    
 )1:x}""$$$dH5
 )RH<R,RR111r   c                  2    e Zd ZU dZi Zded<   ddZdd	Zd
S )rN   a  Provides uniform helpful error when a partitioner dependency is not installed.

    Used by `partition()` to encapsulate coping with the possibility the Python
    environment it is executing in may not have all dependencies installed for a
    particular partitioner.

    Provides `.get()` to access partitioners by file-type, which raises when one or
    more dependencies for that partitioner are not installed.

    The error message indicates what extra needs to be installed to enable that
    partitioner. This avoids an inconsistent variety of possibly puzzling exceptions
    arising from much deeper in the partitioner when access to the missing dependency is
    first attempted.
    zdict[FileType, Partitioner]_partitionersrl   r   r   r   c                h    || j         vr|                     |          | j         |<   | j         |         S )zReturn partitioner for `file_type`.

        Raises when one or more package dependencies for that file-type have not been
        installed.
        )r   _load_partitioner)selfrl   s     r   rP   z_PartitionerLoader.get  s;     D...,0,B,B9,M,MDy)!),,r   c                    |j         D ]0}t          |          st          |j         d|j         d          1|j        sJ t          j        |j                  }t          ||j                  S )zBLoad the partitioner for `file_type` after verifying dependencies.zg() is not available because one or more dependencies are not installed. Use: pip install "unstructured[z:]" (including quotes) to install the required dependencies)
importable_package_dependenciesr   ImportErrorpartitioner_function_name
extra_nameis_partitionable	importlibimport_modulepartitioner_module_qnamegetattr)r   rl   pkg_namepartitioner_modules       r   r   z$_PartitionerLoader._load_partitioner  s     "A 	 	H$X.. ! : = =2;2F= = =   ))))&4Y5WXX)9+NOOOr   N)rl   r   r   r   )__name__
__module____qualname____doc__r   __annotations__rP   r   rG   r   r   rN   rN     sa            24M3333	- 	- 	- 	-P P P P P Pr   rN   )N)8r5   r6   r   r6   r   r7   r   r6   r   r6   r   r8   r    r9   r!   r6   r"   r:   r#   r;   r$   r<   r%   r8   r&   r6   r'   r=   r(   r8   r)   r8   r*   r8   r+   r=   r,   r6   r-   r8   r.   r8   r/   r>   r0   r6   r1   r?   r2   r6   r3   r6   r4   r@   rA   r   )r   r9   r   r6   r#   r;   r%   r8   r1   r?   r   r   )rk   r   r$   r<   r)   r8   r   r8   )*r   
__future__r   r   r   typingr   r   r   r   r   r   typing_extensionsr	   unstructured.documents.elementsr
   r    unstructured.file_utils.filetyper   r   unstructured.file_utils.modelr   unstructured.loggerr   unstructured.partition.commonr   $unstructured.partition.common.commonr   "unstructured.partition.common.langr   &unstructured.partition.utils.constantsr   unstructured.utilsr   listr   r   AUTOr   rK   rM   rN   rG   r   r   <module>r      sG   ? ? ? " " " " " "     				 7 7 7 7 7 7 7 7 7 7 7 7 7 7  ' ' ' ' ' ' G G G G G G G G Q Q Q Q Q Q Q Q 2 2 2 2 2 2 & & & & & & D D D D D D < < < < < < B B B B B B D D D D D D 0 0 0 0 0 0!#tG}"45 5 5 5 5 #r #' $#' %%*"IM (E(E(E#'%)(-&+"'5948+09='+%)'+ $ !9r r r r r rn #' %)    (2 2 2 2"-P -P -P -P -P -P -P -P -P -Pr   