o �J�hA�@s�ddlZddlZddlZddlmZmZmZmZm Z ddl Z ddl m Z ddl mZddlZddlmZddlZddlmZmZmZmZddlmZddlmZmZmZmZdd l Tdd l!m"Z"dd l#m$Z$e$�Z%Gd d �d e"�Z&dS)�N)�BinaryIO�Union�Tuple�List�Callable)�pipeline)�is_flash_attn_2_available)�hf_hub_download)�Progress�TimeElapsedColumn� BarColumn� TextColumn)� Namespace)� INSANELY_FAST_WHISPER_MODELS_DIR�DIARIZATION_MODELS_DIR�UVR_MODELS_DIR� OUTPUT_DIR)�*)�BaseTranscriptionPipeline)� get_loggerc s�eZdZeeeefdedededef�fdd� Ze � �dfde ee j ejfd e j d eed eeeeffd d �Ze � �fdeded e j fdd�Zdd�Zededed e j fdd��Z�ZS)�InsanelyFastWhisperInference� model_dir�diarization_model_dir� uvr_model_dir� output_dircs8t�j||||d�||_tj|jdd�|��|_dS)N)rrrrT��exist_ok)�super�__init__r�os�makedirs�get_model_paths�available_models)�selfrrrr�� __class__��WC:\pinokio\api\whisper-webui.git\app\modules\whisper\insanely_fast_whisper_inference.pyrs�z%InsanelyFastWhisperInference.__init__N�audio�progress�progress_callback�returncGsZt��}t�t|��}|j|jks|jdus|j|jkr%|� |j|j|�|ddd�t t d�t ddd�t ���;}|jd dd �|j|j|j|jd �}|j�d �rRn|j|d <|jr\dnd|d<|j|d|j|j|d�}Wd�n1svwYg} |dD]!} | dd| dd} } | dur�| } | �t| d| | d��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 Nrz>Transcribing...Progress is not shown in insanely-fast-whisper.��descz([progress.description]{task.description}�yellow1�white)�style� pulse_stylez[yellow]Transcribing...)�total)�no_speech_threshold� temperature�compression_ratio_threshold�logprob_thresholdz.en�language� translate� transcribe�taskT)�inputs�return_timestampsZchunk_length_s� batch_sizeZgenerate_kwargs�chunks� timestamp��text)rA�start�end)�time� WhisperParams� from_list�list� model_size�current_model_size�model�current_compute_type� compute_type� update_modelr r r r �add_taskr3r4r5�log_prob_threshold�endswith�lang� is_translate� chunk_lengthr=�append�Segment)r#r(r)r*�whisper_params� start_time�params�kwargs�segments�segments_result�itemrBrC� elapsed_timer&r&r'r9(sR"  ��  ��  � z'InsanelyFastWhisperInference.transcriberHrLcCs�|ddd�tj�|j|�}tj�|�rt�|�s!|j|||d�||_||_t dtj�|j|�|j|j t �r;ddinddid �|_ d S) a| Update current model setting Parameters ---------- model_size: str Size of whisper model 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..r,)rH� download_rootr)zautomatic-speech-recognition�attn_implementation�flash_attention_2�sdpa)rJ� torch_dtype�device� model_kwargsN) r�path�joinr�isdir�listdir�download_modelrKrIrrcrrJ)r#rHrLr)� model_pathr&r&r'rMts" � �z)InsanelyFastWhisperInference.update_modelcs\t��}gd�}||}t�|j�}ddg�||}�fdd�|D�}tt|�|jd�}|S)z� Get available models from models path including fine-tuned model. Returns ---------- Name set of models )zdistil-large-v2zdistil-large-v3zdistil-medium.enzdistil-small.enz.locksZ/insanely_fast_whisper_models_will_be_saved_herecsg|]}|�vr|�qSr&r&)�.0rJ�� wrong_dirsr&r'� <listcomp>�sz@InsanelyFastWhisperInference.get_model_paths.<locals>.<listcomp>)�key)�whisperr"rrhr�sorted�set�index)r#Z openai_modelsZ distil_modelsZdefault_models�existing_modelsr"r&rlr'r!�s z,InsanelyFastWhisperInference.get_model_pathsr^cCst|dd�t�d|�d|�d��tj|dd�gd�}|�d �r'd |��}nd |��}|D] }t|||d �q.dS) NrzInitializing model..z Downloading z to "z"....Tr) zmodel.safetensorsz config.jsonzgeneration_config.jsonzpreprocessor_config.jsonztokenizer.jsonztokenizer_config.jsonzadded_tokens.jsonzspecial_tokens_map.jsonz vocab.jsonZdistilzdistil-whisper/zopenai/whisper-)�repo_id�filename� local_dir)�logger�inforr � startswithr )rHr^r)Z download_listrur\r&r&r'ri�s   �z+InsanelyFastWhisperInference.download_model)�__name__� __module__� __qualname__rrrr�strr�grr r�np�ndarray�torch�Tensor�OptionalrrrrU�floatr9rMr!� staticmethodri� __classcell__r&r&r$r'rsT��������� �O��� �%���r)'rrD�numpyr��typingrrrrrr�� transformersr�transformers.utilsr�gradior�huggingface_hubr rp� rich.progressr r r r �argparser�modules.utils.pathsrrrr�modules.whisper.data_classes�+modules.whisper.base_transcription_pipeliner�modules.utils.loggerrrxrr&r&r&r'�<module>s$      
Memory