
    hB                          d dl mZmZ d dlZd dlmZmZ d dlm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 d d	lmZmZmZ d
dlmZ  G d de      Zy)    )OptionalUnionN)AsyncOpenAIOpenAI)	BaseModel)get_audio_file_name)Logging)BaseAudioTranscriptionConfig)	FileTypes)TranscriptionResponse convert_to_model_response_object extract_duration_from_srt_or_vtt   )OpenAIChatCompletionc                       e Zd Zdededeeej                  f   fdZ	de
dedeeej                  f   f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fdZ	 	 	 	 dd	edededededee   dee   fdZy)OpenAIAudioTranscriptionopenai_aclientdatatimeoutc                    K   	  |j                   j                  j                  j                  di |d|i d{   }t	        |j
                        }|j                         }||fS 7 -# t        $ r}|d}~ww xY 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es           f/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/openai/transcriptions/handler.py(make_openai_audio_transcriptions_requestzAOpenAIAudioTranscription.make_openai_audio_transcriptions_request   s     	Rn**99KKRR $+  
 <//0G#))+HH$$  	G	s8   A>7A+ A),A+ (A>)A+ +	A;4A66A;;A>openai_clientc                 d   	 t         j                  du r] |j                  j                  j                  j
                  di |d|i}t        |j                        }|j                         }||fS  |j                  j                  j
                  di |d|i}d|fS # t        $ r}|d}~ww xY w)r   Tr   Nr   )
litellmreturn_response_headersr   r   r   r   r   r   r   r    )r!   r'   r   r   r"   r   r#   r$   s           r%   -make_sync_openai_audio_transcriptions_requestzFOpenAIAudioTranscription.make_sync_openai_audio_transcriptions_request1   s    	..$6OM''66HHOO (/ 
 |334'--/((D=..==DD]t]U\]X~% 	G	s   A.B 1-B 	B/(B**B/Nmodel
audio_fileoptional_paramslitellm_paramsmodel_responsemax_retrieslogging_objapi_keyapi_baseatranscriptionprovider_configreturnc                 Z   |0|j                  ||||      }t        |t              st        d      ||d|}|du r| j	                  |||||	|
|||	      S | j                  d|	|
|||      }|j                  d|j                  |j                  j                  d|d	
       | j                  |||      \  }}t        |t              r|j                         }nt        |      j                         }|j                  t        |      |	d|i|       |dd}t!        |||d      }|S )z4
        Handle audio transcription request
        N)r,   r-   r.   r/   z+OpenAI transformation route requires a dict)r,   fileT)	r-   r   r0   r   r3   r4   clientr1   r2   Fis_asyncr3   r4   r   r1   r:   r4   r5   complete_input_dictinputr3   additional_args)r'   r   r   textr>   r@   r3   rA   original_responseopenair,   custom_llm_provideraudio_transcriptionresponse_objectmodel_response_objecthidden_paramsresponse_type)%transform_audio_transcription_request
isinstancer   
ValueErrorasync_audio_transcriptions_get_openai_clientpre_callr3   	_base_url_uri_referencer+   r   
model_dumpr   	post_callr   r   )r!   r,   r-   r.   r/   r0   r   r1   r2   r3   r4   r:   r5   r6   r   r'   _r#   stringified_responserM   final_responses                        r%   audio_transcriptionsz-OpenAIAudioTranscription.audio_transcriptionsL   s   & &"HH% /-	 I D dD) !NOO"JJ/JDT!22%-!'' 3 
 
 !% 7 7# !8 !
 	!)))33BB"&'+ 	 	
 HH' I 
8 h	*#+#6#6#8 #8h#G#R#R#T  	%j12D92	 	 	
 #(I0Pau  N\  ly  I^  1_    c
                   K   	 | j                  d||||	|      }
|j                  d |
j                  |
j                  j                  d|d       | j                  |
||       d {   \  }}||j                  d<   t        |t              r|j                         }n*t        |      }t        |      j                         }||d<   |j                  t        |      |d	|i|
       |j                  dd      }|dd}t        |||d      S 7 # t         $ r(}|j                  t"        |t%        |             |d }~ww xY ww)NTr;   r=   r?   )r   r   r   response_headersrB   durationr>   rD   r,   z	whisper-1rF   rG   rI   rJ   )r@   r3   rE   )rS   rT   r3   rU   rV   r&   model_call_detailsrP   r   rW   r   r   rX   r   getr   r    r@   str)r!   r-   r   r0   r   r2   r3   r4   r:   r1   r   r   r#   rZ   r`   actual_modelrM   r$   s                     r%   rR   z3OpenAIAudioTranscription.async_audio_transcriptions   s    2	*.*A*A!' +B +N   &.. . 8 8 G G&*+/! !  '+&S&S- 'T ' !GX
 BIK**+=>(I.'/':':'<$;HE'<('K'V'V'X$3;$Z0!!)*5!6 ="6	 "   88G[9L&28TM3DXp~  O\  lA  B  B-!.  	!!"%a& " 
 G	s<   EA$D (D)B&D ED 	E#D>>EE)NFN)NNNN)__name__
__module____qualname__r   r   r   floathttpxTimeoutr&   r   r+   rc   r   r   intLiteLLMLoggingObjr   boolr
   r\   rR   r   r]   r%   r   r      sm   #  uemm+,	0  uemm+,	N $BFTT T 	T
 T .T T T 'T #T 3-T T "">?T 
Tz "&"&>> > .	>
 > '> #> 3->r]   r   )typingr   r   ri   rF   r   r   pydanticr   r)   ,litellm.litellm_core_utils.audio_utils.utilsr   *litellm.litellm_core_utils.litellm_loggingr	   rl   8litellm.llms.base_llm.audio_transcription.transformationr
   litellm.types.utilsr   litellm.utilsr   r   r   r   r   r   r]   r%   <module>ru      sB    "  &   L S *  *I3 Ir]   