
    h                         d Z ddlmZmZ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 ddlmZmZ dd	lmZmZ d
dlmZmZ ddlmZ  G d de      Zy)zP
Translates from OpenAI's `/v1/audio/transcriptions` to Deepgram's `/v1/listen`
    )ListOptionalUnion)	urlencode)HeadersResponse)process_audio_file)BaseLLMExceptionget_secret_str)AllMessageValues&OpenAIAudioTranscriptionOptionalParams)	FileTypesTranscriptionResponse   )AudioTranscriptionRequestDataBaseAudioTranscriptionConfig   DeepgramExceptionc                   &   e Zd Zdedee   fdZdededededef
dZ	ded	e
d
eeef   defdZdededededef
dZdedefdZ	 ddee   dee   dedededee   defdZdefdZdededefdZ	 	 dd
ededee   dededee   dee   defdZy) DeepgramAudioTranscriptionConfigmodelreturnc                     dgS )Nlanguage )selfr   s     t/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/deepgram/audio_transcription/transformation.pyget_supported_openai_paramsz<DeepgramAudioTranscriptionConfig.get_supported_openai_params   s     |    non_default_paramsoptional_paramsdrop_paramsc                 l    | j                  |      }|j                         D ]  \  }}||v s|||<    |S N)r    items)r   r"   r#   r   r$   supported_paramskvs           r   map_openai_paramsz2DeepgramAudioTranscriptionConfig.map_openai_params    sG      ;;EB&,,.DAq$$%&" / r!   error_messagestatus_codeheadersc                     t        |||      S )N)messager-   r.   r   )r   r,   r-   r.   s       r   get_error_classz0DeepgramAudioTranscriptionConfig.get_error_class-   s     !!{G
 	
r!   
audio_filelitellm_paramsc                 F    t        |      }t        |j                  d      S )a  
        Processes the audio file input based on its type and returns AudioTranscriptionRequestData.
        
        For Deepgram, the binary audio data is sent directly as the request body.

        Args:
            audio_file: Can be a file path (str), a tuple (filename, file_content), or binary data (bytes).

        Returns:
            AudioTranscriptionRequestData with binary data and no files.
        N)datafiles)r	   r   file_content)r   r   r2   r#   r3   processed_audios         r   %transform_audio_transcription_requestzFDeepgramAudioTranscriptionConfig.transform_audio_transcription_request4   s)    & -Z8 - --
 	
r!   raw_responsec                 j   	 |j                         }|d   d   d   }|d   d   }|d   }t        |      }d|d<   d	|d
<   |d   d   |d<   d|v r#|d   D cg c]  }|d   |d   |d   d c}|d<   ||_        |S c c}w # t        $ r)}t	        dt        |       d|j                         d}~ww xY w)z_
        Transforms the raw response from Deepgram to the TranscriptionResponse format
        resultschannelsr   alternatives
transcript)text
transcribetaskenglishr   metadatadurationwordswordstartend)rG   rH   rI   z&Error transforming Deepgram response: z
Response: N)jsonr   _hidden_params	Exception
ValueErrorstrr@   )	r   r:   response_jsonfirst_channelfirst_alternativer@   responserG   es	            r   &transform_audio_transcription_responsezGDeepgramAudioTranscriptionConfig.transform_audio_transcription_responseP   s   #	(--/M *)4Z@CM -n =a @ %\2D -$7H  ,HV Z  $1#<Z#HHZ  ++ !2' :% "&\DM$u+V%! '4H#O%  	8Q\M^M^L_` 	s*   AB  A;,B  ;B   	B2	$B--B2Napi_baseapi_keystreamc                     |t        d      xs d}|j                  d      }d|i}| j                  ||      }|j                  |       | d}	t	        |      }
|	 d|
 }|S )NDEEPGRAM_API_BASEzhttps://api.deepgram.com/v1/r   z/listen?)r   rstrip_build_query_paramsupdater   )r   rU   rV   r   r#   r3   rW   all_query_paramsadditional_paramsbase_urlquery_stringurls               r   get_complete_urlz1DeepgramAudioTranscriptionConfig.get_complete_url|   s     23T7T  ??3' $U+ !44_eL 12 Zw' !12
!L>*
r!   c                 j    t        |t              rt        |      j                         S t        |      S )z
        Formats a parameter value for use in query string.

        Args:
            value: The parameter value to format

        Returns:
            Formatted string value
        )
isinstanceboolrN   lower)r   values     r   _format_param_valuez4DeepgramAudioTranscriptionConfig._format_param_value   s+     eT"u:##%%5zr!   c                     i }| j                  ||| j                  |            }|j                         D ]  \  }}| j                  |      }|||<    |S )a  
        Builds a dictionary of query parameters from optional_params.

        Args:
            optional_params: Dictionary of optional parameters
            model: Model name

        Returns:
            Dictionary of filtered and formatted query parameters
        )r#   r   openai_params)get_provider_specific_paramsr    r'   rj   )r   r#   r   query_paramsprovider_specific_paramskeyri   formatted_values           r   r]   z4DeepgramAudioTranscriptionConfig._build_query_params   so     #'#D#D+::5A $E $
  388:JC"66u=O /L ;
 r!   messagesc                 .    |xs t        d      }dd| iS )NDEEPGRAM_API_KEYAuthorizationzToken r   )r   r.   r   rr   r#   r3   rV   rU   s           r   validate_environmentz5DeepgramAudioTranscriptionConfig.validate_environment   s*     ?^,>?vgY/
 	
r!   r&   )NN)__name__
__module____qualname__rN   r   r   r    dictrg   r+   intr   r   r
   r1   r   r   r9   r   r   rT   r   rd   rj   r]   r   rv   r   r!   r   r   r      s   	4	5
   	
  

 
/2
=B4==Q
	


 
 	

 
 
'
8** 
*f "&3- # 	
    
<C 4   @ "&"&

 
 '(	

 
 
 #
 3-
 

r!   r   N)__doc__typingr   r   r   urllib.parser   httpxr   r   ,litellm.litellm_core_utils.audio_utils.utilsr	   )litellm.llms.base_llm.chat.transformationr
   litellm.secret_managers.mainr   litellm.types.llms.openair   r   litellm.types.utilsr   r   +base_llm.audio_transcription.transformationr   r   common_utilsr   r   r   r!   r   <module>r      sE    ) ( " # K F 7 A -t
'C t
r!   