
    Cjl=                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJrJrJrJrJr  SSKrSSKrSSKJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJ r   SSK!J"r"J#r#  \" 5       r$\#     S#S\%S\\%\4   S\\%   S\\%   S\\\&\RN                  4      S\\%   S\\\%\4      S\4S jj5       r(\#     S#S\%S\\%\4   S\\%   S\\%   S\\\&\RN                  4      S\\%   S\\\%\4      S\\\\\\4   4   4S jj5       r)\RT                  " S5      r+SSSSSSSSSS.	r,S \%S\%4S! jr-S\\%\4   S\\%\%4   4S" jr.g)$z 
Main OCR function for LiteLLM.
    N)partial)IOBase)Any	CoroutineDictOptionalUnion)verbose_logger)request_timeout)Logging)BaseOCRConfigOCRResponse)BaseLLMHTTPHandler)GenericLiteLLMParams)ProviderConfigManagerclientmodeldocumentapi_keyapi_basetimeoutcustom_llm_providerextra_headersreturnc                   #    [        5       n [        R                  " 5       n	SUS'   Uc  [        R                  " XS9u  p  n
[        [        4U UUUUUUS.UD6n[        R                  " 5       n[        UR                  U5      nU	R                  SU5      I Sh  vN n[        R                  " U5      (       a  UI Sh  vN nOUnUc  [        SU 35      eU$  N? N! [         a  n[        R                  " U UUUUS9eSnAff = f7f)a  
Async OCR function.

Args:
    model: Model name (e.g., "mistral/mistral-ocr-latest")
    document: Document to process in Mistral format:
        {"type": "document_url", "document_url": "https://..."} for PDFs/docs,
        {"type": "image_url", "image_url": "https://..."} for images, or
        {"type": "file", "file": <path/bytes/file-obj>} for local files
    api_key: Optional API key
    api_base: Optional API base URL
    timeout: Optional timeout
    custom_llm_provider: Optional custom LLM provider
    extra_headers: Optional extra headers
    **kwargs: Additional parameters (e.g., include_image_base64, pages, image_limit)

Returns:
    OCRResponse in Mistral OCR format with pages, model, usage_info, etc.

Example:
    ```python
    import litellm

    # OCR with PDF
    response = await litellm.aocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "document_url",
            "document_url": "https://arxiv.org/pdf/2201.04234"
        },
        include_image_base64=True
    )

    # OCR with image
    response = await litellm.aocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "image_url",
            "image_url": "https://example.com/image.png"
        }
    )

    # OCR with base64 encoded PDF
    response = await litellm.aocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "document_url",
            "document_url": f"data:application/pdf;base64,{base64_pdf}"
        }
    )

    # OCR with local file
    response = await litellm.aocr(
        model="mistral/mistral-ocr-latest",
        document={"type": "file", "file": "/path/to/document.pdf"}
    )
    ```
TaocrN)r   r   )r   r   r   r   r   r   r   z2Got an unexpected None response from the OCR API: r   r   original_exceptioncompletion_kwargsextra_kwargs)localsasyncioget_event_looplitellmget_llm_providerr   ocrcontextvarscopy_contextrunrun_in_executoriscoroutine
ValueError	Exceptionexception_type)r   r   r   r   r   r   r   kwargs
local_varsloop_funcctxfunc_with_contextinit_responseresponsees                    b/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/litellm/ocr/main.pyr   r      s7    J J,
%%'v &+2+C+C,(AAq 

 3'

 

 &&(#CGGT2"2249JKK}--**H$HDXJO   L +  
$$ 3 (
 	

sM   D	BC C$C  CC D	C C 
D)DDD	c                 8   [        5       n UR                  S5      n	UR                  SS5      n
UR                  SS5      SL n[        U[        5      (       d  [        S[        U5       35      eUR                  S5      nUS	:X  a  [        U5      nUR                  S5      nUS
;  a  [        SU S35      e[        R                  " U UUUS9u  n nnnU(       a  UnU(       a  Un[        R                  " U [        R                  " U5      S9nUc  [        SU 35      e[        R                  " SU  SU 35        [        S0 UD6nUR!                  U S9n0 nU H  nUU;   d  M  UR                  U5      UU'   M!     UR#                  U0 U S9n[        R                  " SU 35        U	R%                  UU UU
US.US9  [&        R)                  U UUU=(       d    [*        U	UUUUUU[	        U5      S9nU$ ! [,         a  n[        R.                  " U UUUUS9eSnAff = f)a  
Synchronous OCR function.

Args:
    model: Model name (e.g., "mistral/mistral-ocr-latest")
    document: Document to process in Mistral format:
        {"type": "document_url", "document_url": "https://..."} for PDFs/docs,
        {"type": "image_url", "image_url": "https://..."} for images, or
        {"type": "file", "file": <path/bytes/file-obj>} for local files
    api_key: Optional API key
    api_base: Optional API base URL
    timeout: Optional timeout
    custom_llm_provider: Optional custom LLM provider
    extra_headers: Optional extra headers
    **kwargs: Additional parameters (e.g., include_image_base64, pages, image_limit)

Returns:
    OCRResponse in Mistral OCR format with pages, model, usage_info, etc.

Example:
    ```python
    import litellm

    # OCR with PDF
    response = litellm.ocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "document_url",
            "document_url": "https://arxiv.org/pdf/2201.04234"
        },
        include_image_base64=True
    )

    # OCR with image
    response = litellm.ocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "image_url",
            "image_url": "https://example.com/image.png"
        }
    )

    # OCR with base64 encoded PDF
    response = litellm.ocr(
        model="mistral/mistral-ocr-latest",
        document={
            "type": "document_url",
            "document_url": f"data:application/pdf;base64,{base64_pdf}"
        }
    )

    # OCR with local file
    response = litellm.ocr(
        model="mistral/mistral-ocr-latest",
        document={"type": "file", "file": "/path/to/document.pdf"}
    )

    # Access pages
    for page in response.pages:
        print(f"Page {page.index}: {page.markdown}")
    ```
litellm_logging_objlitellm_call_idNr   FTz<document must be a dict with 'type' and URL/file field, got typefile)document_url	image_urlzInvalid document type: z0. Must be 'document_url', 'image_url', or 'file')r   r   r   r   )r   providerz#OCR is not supported for provider: zOCR call - model: z, provider: )r   )non_default_paramsoptional_paramsr   z#OCR optional_params after mapping: )r<   r   )r/   r   rC   litellm_paramsr   )r   r   rC   r   logging_objr   r   r   r   headersprovider_configrD   r    )r!   popget
isinstancedictr,   r=   %convert_file_document_to_url_documentr$   r%   r   get_provider_ocr_configLlmProvidersr
   debugr   get_supported_ocr_paramsmap_ocr_paramsupdate_from_kwargsbase_llm_http_handlerr&   r   r-   r.   )r   r   r   r   r   r   r   r/   r0   r;   r<   	_is_asyncdoc_typedynamic_api_keydynamic_api_baseocr_provider_configrD   supported_paramsrB   paramrC   r7   r8   s                          r9   r&   r&      s   R Jr
17<Q1R)/4Et)LJJvu-5	 (D))NtT\~N^_  <<' v<XFH||F+H88)( 4A A  $$ 3	
	
 %G'H "99 --.AB 	 &56I5JK  	 |4G3HI	

 .77 /GGeGT%E,2JJu,="5) &
 .<<1 = 
 	B?BSTU 	..+#2$ !4 	/ 		
 ),,+.+ 3!// - 
  
$$ 3 (
 	

s   EG2 $BG2 2
H<HHz^[\w.+-]+/[\w.+-]+$zapplication/pdfz	image/pngz
image/jpegz	image/gifz
image/webpz
image/tiffz	image/bmp)	z.pdfz.pngz.jpgz.jpegz.gifz.webpz.tiffz.tifz.bmp	file_pathc                     [         R                  R                  U 5      S   R                  5       n[        R                  U5      nU(       a  U$ [        R                  " U 5      u  p4U=(       d    S$ )zx
Determine MIME type from file path extension.

Falls back to mimetypes.guess_type, then to 'application/octet-stream'.
   application/octet-stream)ospathsplitextlower_MIME_TYPE_MAPrJ   	mimetypes
guess_type)r\   extmimeguessedr2   s        r9   get_mime_typerj   f  s\     ''

9
%a
(
.
.
0Cc"D%%i0JG000    c           	      D   U R                  S5      nUc  [        S5      eSnSn[        U[        5      (       a  [        S5      e[        U[        R
                  5      (       a  [        U5      n[        R                  R                  U5      (       d  [        SU 35      e[        U5      n[        R                  R                  U5      n[        US5       nUR                  5       nSSS5        O[        U[        5      (       a  UnO[        U[        5      (       d  [        US5      (       ag  [        US	5      (       a  [!        US	S5      nU(       a  [        U5      nUR                  5       n[        U[        5      (       a  UR#                  S
5      nO[        S[%        U5       S35      eW(       d  [        S5      eSU ;   a  U S   n[&        R)                  U5      (       d  [        SU 35      e[*        R,                  " U5      R/                  S
5      nSU SU 3nUR1                  S5      (       a.  [2        R4                  " SU S[7        U5       SU S35        SUS.$ [2        R4                  " SU S[7        U5       SU S35        SUS.$ ! , (       d  f       N= f)u  
Convert a file-type document dict to a document_url-type document dict
with an inline base64 data URI.

Accepts document dicts like:
    {"type": "file", "file": Path("/path/to/doc.pdf")}       # pathlib.Path
    {"type": "file", "file": <binary file-like object>}      # file-like object (BinaryIO)
    {"type": "file", "file": b"raw bytes"}                   # raw bytes

Bare ``str`` paths are not accepted — pass a ``pathlib.Path`` or
``open(path, "rb")`` instead. See the str check below for the rationale.

Returns:
    {"type": "document_url", "document_url": "data:<mime>;base64,<data>"}
    or {"type": "image_url", "image_url": "data:<mime>;base64,<data>"}
r>   Nzkdocument with type='file' must include a 'file' field containing a pathlib.Path, file-like object, or bytesr_   zOCR file input does not accept bare str values. Pass bytes, a pathlib.Path, or a file-like object. To OCR a local file from a path, call open(path, 'rb') yourself.zFile not found: rbreadnamezutf-8zUnsupported file input type: z6. Expected pathlib.Path, bytes, or a file-like object.z"File is empty or could not be read	mime_typezInvalid MIME type: zdata:z;base64,zimage/z;OCR file input: Converted file to image_url data URI (mime=z, size=z bytes, name=)r@   )r=   r@   z>OCR file input: Converted file to document_url data URI (mime=r?   )r=   r?   )rJ   r,   rK   strr`   PathLikera   isfileFileNotFoundErrorrj   basenameopenrn   bytesr   hasattrgetattrencoder=   _MIME_PATTERNmatchbase64	b64encodedecode
startswithr
   rP   len)	r   
file_inputrp   	file_namer\   f
file_bytesbase64_datadata_uris	            r9   rM   rM   t  s   " f%J9
 	
 0I#I*c"" ;
 	

 *bkk** 
O	ww~~i((#&6yk$BCC!),	GG$$Y/	)T"aJ #"	J	&	&
	J	'	'7:v+F+F:v&&
FD9I))4	__&
j#&&#**73J+D,<+= >C C
 	

 =>>h[)	y)).yk:;;"":.55g>Kyk+7HH%%Kws:&7}YKqR	
 $(;;Kws:&7}YKqR	
 'AAS #"s   J
J)NNNNN)/__doc__r"   r~   r'   re   r`   re	functoolsr   ior   typingr   r   r   r   r	   httpxr$   litellm._loggingr
   litellm.constantsr   *litellm.litellm_core_utils.litellm_loggingr   LiteLLMLoggingObj(litellm.llms.base_llm.ocr.transformationr   r   *litellm.llms.custom_httpx.llm_http_handlerr   litellm.types.routerr   litellm.utilsr   r   rT   rr   floatTimeoutr   r&   compiler|   rd   rj   rM   rH   rk   r9   <module>r      s8       	 	   8 8   + - S O I 5 7 +,   ""59)-.2q
q
38nq
 c]q
 sm	q

 eE5==012q
 "#q
 DcN+q
 q
 q
h  ""59)-.2{
{
38n{
 c]{
 sm	{

 eE5==012{
 "#{
 DcN+{
 ;	#sK"7889{
 {
D 

12 
1S 1S 1YBDcN YBtCQTH~ YBrk   