
    jn'                        d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
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mZ dd	lmZ dd
lmZ ddlmZmZ  e       Ze	 	 	 	 	 ddedeeef   de	e   de	e   de	e
e ejB                  f      de	e   de	eeef      defd       Z"e	 	 	 	 	 ddedeeef   de	e   de	e   de	e
e ejB                  f      de	e   de	eeef      de
eeeeef   f   fd       Z#y)z 
Main OCR function for LiteLLM.
    N)partial)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                   K   t               }	 t        j                         }	d|d<   |t        j                  | |      \  }
}}
}
t        t        f| ||||||d|}t        j                         }t        |j                  |      }|	j                  d|       d{   }t        j                  |      r| d{   }n|}|t        d|       |S 7 87 # t        $ r}t        j                  | ||||      d}~ww xY ww)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 or
            {"type": "image_url", "image_url": "https://..."} for images
        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}"
            }
        )
        ```
    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                    i/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/litellm/ocr/main.pyr   r      s;    | J,
%%'v &+2+C+Ch,(A"Aq 

 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"C<<DDc                    t               }	 |j                  d      }	|j                  dd      }
|j                  dd      du }t        |t              st        dt        |             |j                  d      }|d	vrt        d
| d      t        j                  | |||      \  } }}}|r|}|r|}t        j                  | t        j                  |            }|t        d|       t        j                  d|  d|        t        di |}|j                  |       }i }|D ]  }||v s|j                  |      ||<    |j!                  |i |       }t        j                  d|        |	j#                  | ||
|d|       t$        j'                  | |||xs t(        |	||||||t	        |            }|S # t*        $ r}t        j,                  | ||||      d}~ww xY w)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 or
            {"type": "image_url", "image_url": "https://..."} for images
        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}"
            }
        )
        
        # Access pages
        for page in response.pages:
            print(f"Page {page.index}: {page.markdown}")
        ```
    litellm_logging_objlitellm_call_idNr   FTz7document must be a dict with 'type' and URL field, got type)document_url	image_urlzInvalid document type: z'. Must be 'document_url' or 'image_url')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   rA   litellm_paramsr   )r   r   rA   r   logging_objr   r   r   r   headersprovider_configrB   r    )r    popget
isinstancedictr+   r<   r#   r$   r   get_provider_ocr_configLlmProvidersr	   debugr   get_supported_ocr_paramsmap_ocr_paramsupdate_environment_variablesbase_llm_http_handlerr%   r
   r,   r-   )r   r   r   r   r   r   r   r.   r/   r:   r;   	_is_asyncdoc_typedynamic_api_keydynamic_api_baseocr_provider_configrB   supported_paramsr@   paramrA   r6   r7   s                          r8   r%   r%      sv   D Jc
17<Q1R)/4Et)LJJvu-5	 (D)VW[\dWeVfghh<<'886xj@ghii $$$7!	 	F"O5E %G'H "99 --.AB 	 &56I5JK  	 |4G3HI	

 .77 /GGeGT%E,2JJu,="5) &
 .<<1 = 
 	B?BSTU 	88+#2$ !4 	9 	
 ),,+.+ 3!// - 
  
$$ 3 (
 	

s   D%G  2BG   	G(	G##G()NNNNN)$__doc__r!   r&   	functoolsr   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   rQ   strfloatTimeoutr   r%   rF       r8   <module>ri      s      8 8   + - S O I 5 7 +,   ""59)-.2j
j
38nj
 c]j
 sm	j

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

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