o
    ưi?!                     @   s   d dl mZmZmZmZ d dlZd dlmZmZ d dl	m
Z
 d dlZer*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mZmZ ddlmZ G dd deZdS )    )TYPE_CHECKINGOptionalUnioncastN)AsyncOpenAIOpenAI)	BaseModel)ClientSession)get_audio_file_name)Logging)BaseAudioTranscriptionConfig)	FileTypes)TranscriptionResponse convert_to_model_response_object extract_duration_from_srt_or_vtt   )OpenAIChatCompletionc                   @   s   e Zd Zdededeeejf fddZ	de
dedeeejf fddZ			
				ddededededededededee dee dedee ded defddZ										ddedededededee dee ded fddZd	S )OpenAIAudioTranscriptionopenai_aclientdatatimeoutc              
      s`   z |j jjjdi |d|iI dH }t|j}| }||fW S  ty/ } z|d}~ww )
        Helper to:
        - call openai_aclient.audio.transcriptions.with_raw_response when litellm.return_response_headers is True
        - call openai_aclient.audio.transcriptions.create by default
        r   N )audiotranscriptionswith_raw_responsecreatedictheadersparse	Exception)selfr   r   r   raw_responser   responseer   r   a/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/openai/transcriptions/handler.py(make_openai_audio_transcriptions_request   s   

zAOpenAIAudioTranscription.make_openai_audio_transcriptions_requestopenai_clientc              
   C   s   z5t jdu r#|jjjjdi |d|i}t|j}| }||fW S |jjjdi |d|i}d|fW S  t	yC } z|d}~ww )r   Tr   Nr   )
litellmZreturn_response_headersr   r   r   r   r   r   r   r    )r!   r'   r   r   r"   r   r#   r$   r   r   r%   -make_sync_openai_audio_transcriptions_request5   s"   



zFOpenAIAudioTranscription.make_sync_openai_audio_transcriptions_requestNFmodel
audio_fileoptional_paramslitellm_paramsmodel_responsemax_retrieslogging_objapi_keyapi_baseatranscriptionprovider_configshared_sessionr	   returnc                 C   s   |dur|j ||||d}tt|j}n||d|}|du r.| j|||||	|
||||d
S | jd|	|
|||d}|jd|j|jj	d|dd	 | j
|||d
\}}t|tr\| }nt|d }|jt||	d|i|d |dd}t|||dd}|S )z4
        Handle audio transcription request
        N)r*   r+   r,   r-   )r*   fileT)
r+   r   r.   r   r1   r2   clientr/   r0   r5   F)is_asyncr1   r2   r   r/   r8   r2   r3   complete_input_dictinputr1   additional_args)r'   r   r   textr;   r=   r1   r>   original_responseopenair*   Zcustom_llm_provideraudio_transcriptionZresponse_objectZmodel_response_objecthidden_paramsZresponse_type)Z%transform_audio_transcription_requestr   r   r   async_audio_transcriptions_get_openai_clientpre_callr1   	_base_url_uri_referencer)   
isinstancer   
model_dumpr   	post_callr
   r   )r!   r*   r+   r,   r-   r.   r   r/   r0   r1   r2   r8   r3   r4   r5   Ztransformed_datar   r'   _r#   stringified_responserG   Zfinal_responser   r   r%   audio_transcriptionsP   sn   
	



z-OpenAIAudioTranscription.audio_transcriptionsc              
      s  zg| j d||||	||
d}|jd |j|jjd|dd | j|||dI d H \}}||jd< t|tr9|	 }nt
|}t|d	 }||d< |jt||d	|i|d
 |dd}|dd}t|||ddW S  ty } z|jt|t|d |d }~ww )NT)r9   r1   r2   r   r/   r8   r5   r:   r<   )r   r   r   response_headersr?   Z_audio_transcription_durationr;   rA   r*   z	whisper-1rC   rD   rE   rF   )r=   r1   rB   )rI   rJ   r1   rK   rL   r&   Zmodel_call_detailsrM   r   rN   r   r   rO   r
   getr   r    r=   str)r!   r+   r   r.   r   r0   r1   r2   r8   r/   r5   r   r   r#   rQ   durationZactual_modelrG   r$   r   r   r%   rH      s`   	



z3OpenAIAudioTranscription.async_audio_transcriptions)NFNN)NNNNN)__name__
__module____qualname__r   r   r   floathttpxTimeoutr&   r   r)   rU   r   r   intLiteLLMLoggingObjr   boolr   rR   rH   r   r   r   r%   r      s    

'	

^r   )typingr   r   r   r   r[   rC   r   r   Zpydanticr   r(   Zaiohttpr	   Z,litellm.litellm_core_utils.audio_utils.utilsr
   Z*litellm.litellm_core_utils.litellm_loggingr   r^   Z8litellm.llms.base_llm.audio_transcription.transformationr   Zlitellm.types.utilsr   Zlitellm.utilsr   r   r   r   r   r   r   r   r%   <module>   s    