o �J�h�"�@s�ddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z ddl Z ddl mZddlZddlZddlZddlZddlmZddlmZmZmZmZddlTddlmZGdd �d e�ZdS) �N)�BinaryIO�Union�Tuple�List�Callable)� VadOptions)� Namespace)�FASTER_WHISPER_MODELS_DIR�DIARIZATION_MODELS_DIR�UVR_MODELS_DIR� OUTPUT_DIR)�*)�BaseTranscriptionPipelinec s�eZdZeeeefdedededef�fdd� Ze � �dfde ee e jfd e j d eed eeeeffd d �Ze � �fdeded e j fdd�Zdd�Zedd��Zeded eefdd��Z�ZS)�FasterWhisperInference� model_dir�diarization_model_dir� uvr_model_dir� output_dircsNt�j||||d�||_tj|jdd�|��|_|��|_|j� �|_ dS)N)rrrrT)�exist_ok) �super�__init__r�os�makedirs�get_model_paths� model_paths� get_device�device�keys�available_models)�selfrrrr�� __class__��PC:\pinokio\api\whisper-webui.git\app\modules\whisper\faster_whisper_inference.pyrs�  zFasterWhisperInference.__init__N�audio�progress�progress_callback�returnc Gs�t��}t�t|��}|j|jks|jdus|j|jkr%|� |j|j|�|jj d%id|�d|j �d|j r7dnd�d|j �d|j�d |j�d |j�d |j�d |j�d |j�d|j�d|j�d|j�d|j�d|j�d|j�d|j�d|j�dd�d|j�d|j�d|j�d|j�d|j �d|j!�d|j"�d|j#�d |j$��\}}|d!d"d#�g} |D]} | j%|j&} || d$d#�|dur�|| �| �'t(�)| ��q�t��|} | | fS)&a transcribe method for faster-whisper. Parameters ---------- audio: Union[str, BinaryIO, np.ndarray] Audio path or file binary or Audio numpy array progress: gr.Progress Indicator to show progress directly in gradio. progress_callback: Optional[Callable] callback function to show progress. Can be used to update progress in the backend. *whisper_params: tuple Parameters related with whisper. This will be dealt with "WhisperParameters" data class Returns ---------- segments_result: List[Segment] list of Segment that includes start, end timestamps and transcribed text elapsed_time: float elapsed time for transcription Nr$�language�task� translate� transcribe� beam_size�log_prob_threshold�no_speech_threshold�best_of�patience� temperature�initial_prompt�compression_ratio_threshold�length_penalty�repetition_penalty�no_repeat_ngram_size�prefix�suppress_blank�suppress_tokens�max_initial_timestampZword_timestampsT�prepend_punctuations�append_punctuations�max_new_tokens� chunk_length�hallucination_silence_threshold�hotwords�language_detection_threshold�language_detection_segments�prompt_reset_on_temperaturerzLoading audio..��desczTranscribing..r")*�time� WhisperParams� from_list�list� model_size�current_model_size�model�current_compute_type� compute_type� update_modelr+�lang� is_translater,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrC�start�duration�append�SegmentZfrom_faster_whisper) rr$r%r&�whisper_params� start_time�params�segments�infoZsegments_result�segmentZ progress_n� elapsed_timer"r"r#r+(s�" �� ������� � � � � ����������������    z!FasterWhisperInference.transcriberJrNcCs|ddd�d|vr|�dd�n|}||jvrH||jvrHtd|�dtj�|j|��d��tj|tj�|j|�d �|� �|_t � d |�d ��|j||_ d }d }tj�|j||�}tj� |j �rjtj�|j �sv|t��vrxtj�|�rxd}||_tj|j|j |j|j|d�|_dS)a� Update current model setting Parameters ---------- model_size: str Size of whisper model. If you enter the huggingface repo id, it will try to download the model automatically from huggingface. compute_type: str Compute type for transcription. see more info : https://opennmt.net/CTranslate2/quantization.html progress: gr.Progress Indicator to show progress directly in gradio. rzInitializing Model..rD�/z--z+Model is not detected. Trying to download "z" from huggingface to "z ...)� local_dirz#Model is downloaded with the name "�"F� models--Systran--faster-whisper-T)rZmodel_size_or_pathZ download_rootrN�local_files_onlyN)�replacer�printr�path�joinr�huggingface_hub�snapshot_downloadr�gr�InforK�isdir�exists�faster_whisperrrMZ WhisperModelrrL)rrJrNr%Zmodel_size_dirnameraZ hf_prefixZofficial_model_pathr"r"r#rOus6   ��   �z#FasterWhisperInference.update_modelcCs�dd�t��D�}d}t�|j�}ddg}tt|�t|��}|D]}||vr/|t|�d�}|t��vr?tj � |j|�||<q!|S)z� Get available models from models path including fine-tuned model. Returns ---------- Name list of models cSsi|]}||�qSr"r")�.0rLr"r"r#� <dictcomp>�sz:FasterWhisperInference.get_model_paths.<locals>.<dictcomp>r`z.locksZ(faster_whisper_models_will_be_saved_hereN) rlrr�listdirrrI�set�len�whisperrdre)rrZfaster_whisper_prefixZexisting_modelsZ wrong_dirs� model_namer"r"r#r�s  �z&FasterWhisperInference.get_model_pathscCstj��rdSdS)N�cuda�auto)�torchrt� is_availabler"r"r"r#r�s z!FasterWhisperInference.get_device�suppress_tokens_strc CsVzt�|�}t|t�rtdd�|D��std��|WSty*}ztd��d}~ww)Ncss�|]}t|t�VqdS)N)� isinstance�int)rm�itemr"r"r#� <genexpr>�s�zDFasterWhisperInference.format_suppress_tokens_str.<locals>.<genexpr>z<Invalid Suppress Tokens. The value must be type of List[int])�ast� literal_evalryrI�all� ValueError� Exception)rxr9�er"r"r#�format_suppress_tokens_str�s ��z1FasterWhisperInference.format_suppress_tokens_str)�__name__� __module__� __qualname__r r r r �strrrh�Progressrr�np�ndarray�OptionalrrrrU�floatr+rOr� staticmethodrrzr�� __classcell__r"r"r r#rsL��������� �P��� �2  r)rrFrf�numpyr�rv�typingrrrrrrlZfaster_whisper.vadrr}Z ctranslate2rr�gradiorh�argparser�modules.utils.pathsr r r r �modules.whisper.data_classes�+modules.whisper.base_transcription_pipelinerrr"r"r"r#�<module>s"   
Memory