o
    5ôjY  ã                   @   sz  d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	Z
d dlZd dlZddlmZmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZmZmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% erldd	l&m'Z' dd
dddddddddœ
dddee(e
j)ej*f dee+ dee,ee,df f dee, dee, dee, de+dee( de+de(d e(fd!d"„Z-d#d$„ Z.e/d%kr»e.ƒ  dS dS )&é    N)ÚTYPE_CHECKINGÚOptionalÚTupleÚUnioné   )ÚFRAMES_PER_SECONDÚ
HOP_LENGTHÚN_FRAMESÚ	N_SAMPLESÚSAMPLE_RATEÚlog_mel_spectrogramÚpad_or_trim)ÚDecodingOptionsÚDecodingResult)Úadd_word_timestamps)Ú	LANGUAGESÚTO_LANGUAGE_CODEÚget_tokenizer)Ú	exact_divÚformat_timestampÚ
get_writerÚ	make_safeÚoptional_floatÚoptional_intÚstr2bool)ÚWhisper)ç        çš™™™™™É?gš™™™™™Ù?ç333333ã?gš™™™™™é?g      ð?ç333333@ç      ð¿r   TFõ   "'â€œÂ¿([{-õ   "'.ã€‚,ï¼Œ!ï¼�?ï¼Ÿ:ï¼šâ€�)]}ã€�)
ÚverboseÚtemperatureÚcompression_ratio_thresholdÚlogprob_thresholdÚno_speech_thresholdÚcondition_on_previous_textÚinitial_promptÚword_timestampsÚprepend_punctuationsÚappend_punctuationsÚmodelr   Úaudior#   r$   .r%   r&   r'   r(   r)   r*   r+   r,   c       
   ;         s*  ˆ  dd¡r	tjntj}ˆjt d¡kr+tj ¡ rt d¡ |tjkr+t d¡ tj}|tjkr4dˆd< t	|ˆj
jtd�}|jd t }ˆ  d	d
¡d
u r‡ˆjsTdˆd	< n3|rZtdƒ t|tƒ ˆj¡ |¡}ˆ |¡\}}t||j d�ˆd	< |d
ur‡tdtˆd	   ¡ › �ƒ ˆd	 }ˆ  dd¡}tˆjˆj||d�‰|	r¦|dkr¦t d¡ dtjdtf‡ ‡‡‡‡‡fdd„}d‰ttˆj
jƒ}|t t }g }g }d}|d
urßˆ d|  ¡  ¡}| !|¡ ng }dt"dt"dtjdtf‡‡fdd„}t#j#|d |dud!���ÿ}d"}ˆ|k �rõt"ˆt t ƒ}|d
d
…ˆˆt …f }t$t|ˆ ƒ} | t t }!t|tƒ ˆj¡ |¡}||d
… ˆd#< ||ƒ}"t %|"j&¡}#ˆd
u�rd|"j'ˆk}$ˆd
u�r[|"j(ˆk�r[d}$|$�rdˆ| 7 ‰�q ˆ}%g }&|# )ˆj*¡}'|'d$d
…  +¡ ddgk}(t ,|'d
d… |'d%d
… @ ¡d })|) -d%¡ t.|)ƒdk�rô|) +¡ }*|(�r¥|* /t.|#ƒ¡ d}+|*D ]0},|#|+|,… }-|-d  0¡ ˆj* }.|-d  0¡ ˆj* }/|& /|||.|  ||/|  |-|"d&�¡ |,}+�q©|(�râˆ| 7 ‰nK|#|+d%   0¡ ˆj* }0ˆ|0| 7 ‰n9|!}1|#|' 1¡  2¡  }2t.|2ƒdk�r|2d  0¡ ˆj*k�r|2d  0¡ ˆj* }0|0| }1|& /||||1 |#|"d&�¡ ˆ| 7 ‰|	�rkt3|&ˆˆ|| |
||d'� d(d)„ |&D ƒ}3t.|3ƒdk�rN|3d }|(�skt.|3ƒdk�rkt4|3d | t5 ƒ}4|4dk�rk|%|4 ‰|�r—|&D ]&}5|5d |5d |5d* }6}7}8d+t6|6ƒ› d,t6|7ƒ› d-|8› �}9tt7|9ƒƒ �qpt8|&ƒD ]#\}:}5|5d |5d k�s±|5d*   ¡ d.k�r½d.|5d*< g |5d< g |5d/< �q›| !d0d)„ t8|&t.|ƒd1�D ƒ¡ | !d2d)„ |&D ƒ¡ |�râ|"j9d3k�ræt.|ƒ}| :t$|ˆƒ|% ¡ ˆ|k �sW d
  ƒ n	1 �s w   Y  t;ˆ <|t.|ƒd
… ¡||d4�S )5aß	  
    Transcribe an audio file using Whisper

    Parameters
    ----------
    model: Whisper
        The Whisper model instance

    audio: Union[str, np.ndarray, torch.Tensor]
        The path to the audio file to open, or the audio waveform

    verbose: bool
        Whether to display the text being decoded to the console. If True, displays all the details,
        If False, displays minimal details. If None, does not display anything

    temperature: Union[float, Tuple[float, ...]]
        Temperature for sampling. It can be a tuple of temperatures, which will be successively used
        upon failures according to either `compression_ratio_threshold` or `logprob_threshold`.

    compression_ratio_threshold: float
        If the gzip compression ratio is above this value, treat as failed

    logprob_threshold: float
        If the average log probability over sampled tokens is below this value, treat as failed

    no_speech_threshold: float
        If the no_speech probability is higher than this value AND the average log probability
        over sampled tokens is below `logprob_threshold`, consider the segment as silent

    condition_on_previous_text: bool
        if True, the previous output of the model is provided as a prompt for the next window;
        disabling may make the text inconsistent across windows, but the model becomes less prone to
        getting stuck in a failure loop, such as repetition looping or timestamps going out of sync.

    word_timestamps: bool
        Extract word-level timestamps using the cross-attention pattern and dynamic time warping,
        and include the timestamps for each word in each segment.

    prepend_punctuations: str
        If word_timestamps is True, merge these punctuation symbols with the next word

    append_punctuations: str
        If word_timestamps is True, merge these punctuation symbols with the previous word

    initial_prompt: Optional[str]
        Optional text to provide as a prompt for the first window. This can be used to provide, or
        "prompt-engineer" a context for transcription, e.g. custom vocabularies or proper nouns
        to make it more likely to predict those word correctly.

    decode_options: dict
        Keyword arguments to construct `DecodingOptions` instances

    Returns
    -------
    A dictionary containing the resulting text ("text") and segment-level details ("segments"), and
    the spoken language ("language"), which is detected when `decode_options["language"]` is None.
    Úfp16TÚcpuz2Performing inference on CPU when CUDA is availablez0FP16 is not supported on CPU; using FP32 insteadF)ÚpaddingéÿÿÿÿÚlanguageNÚenz]Detecting language using up to the first 30 seconds. Use `--language` to specify the language)ÚkeyzDetected language: ÚtaskÚ
transcribe)Únum_languagesr3   r6   Ú	translatez:Word-level timestamps on translations may not be reliable.ÚsegmentÚreturnc                    sÒ   t ˆttfƒr
ˆgnˆ}d }|D ]V}i ˆ¥}|dkr'| dd ¡ | dd ¡ n| dd ¡ tdi |¤d|i¤Ž}ˆ | |¡}d}ˆ d urK|jˆ krKd}ˆd urV|jˆk rVd}ˆd ura|jˆkrad}|sf |S q|S )	Nr   Ú	beam_sizeÚpatienceÚbest_ofr$   FT© )	Ú
isinstanceÚintÚfloatÚpopr   ÚdecodeÚcompression_ratioÚavg_logprobÚno_speech_prob)r:   ZtemperaturesZdecode_resultÚtÚkwargsÚoptionsZneeds_fallback)r%   Údecode_optionsr&   r-   r'   r$   r?   úe/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/whisper/transcribe.pyÚdecode_with_fallback™   s4   ÿ


ýz(transcribe.<locals>.decode_with_fallbackr   ú ÚstartÚendÚtokensÚresultc              
      s@   |  ¡ }‡fdd„|D ƒ}ˆ | |ˆ |¡||j|j|j|jdœ	S )Nc                    s   g | ]	}|ˆ j k r|‘qS r?   )Úeot)Ú.0Útoken)Ú	tokenizerr?   rL   Ú
<listcomp>Ö   s    z3transcribe.<locals>.new_segment.<locals>.<listcomp>)	ÚseekrO   rP   ÚtextrQ   r$   rF   rE   rG   )ÚtolistrD   r$   rF   rE   rG   )rO   rP   rQ   rR   Ztext_tokens)rX   rV   r?   rL   Únew_segmentÒ   s   ÷ztranscribe.<locals>.new_segmentÚframes)ÚtotalÚunitÚdisabler   Úpromptéþÿÿÿr   )rO   rP   rQ   rR   )Úsegmentsr-   rV   ÚmelZ
num_framesr+   r,   Úlast_speech_timestampc                 S   s"   g | ]}|d  D ]}|d ‘qqS )ÚwordsrP   r?   )rT   ÚsÚwr?   r?   rL   rW   M  s
    ÿÿztranscribe.<locals>.<listcomp>rY   Ú[z --> z] Ú re   c                 S   s   g | ]
\}}d |i|¥‘qS )Úidr?   )rT   Úir:   r?   r?   rL   rW   g  s    
ÿÿ)rO   c                 S   s   g | ]}|d  D ]}|‘qqS )rQ   r?   )rT   r:   rU   r?   r?   rL   rW   o  s    g      à?)rY   rb   r3   )=ÚgetÚtorchÚfloat16Úfloat32ÚdeviceÚcudaÚis_availableÚwarningsÚwarnr   ÚdimsÚn_melsr
   Úshaper	   Úis_multilingualÚprintr   ÚtoÚdetect_languageÚmaxr   Útitler   r8   ÚTensorr   r   Ún_audio_ctxr   r   ÚencodeÚstripÚextendrB   ÚtqdmÚminÚtensorrQ   rG   rF   ÚgeÚtimestamp_beginrZ   ÚwhereÚadd_ÚlenÚappendÚitemÚnonzeroÚflattenr   Úroundr   r   r   Ú	enumerater$   ÚupdateÚdictrD   );r-   r.   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   rK   Údtyperc   Zcontent_framesZmel_segmentÚ_Úprobsr3   r6   rM   Zinput_strideZtime_precisionZ
all_tokensZall_segmentsZprompt_reset_sinceZinitial_prompt_tokensr[   Úpbarrd   Ztime_offsetZsegment_sizeZsegment_durationrR   rQ   Zshould_skipZprevious_seekZcurrent_segmentsZtimestamp_tokensZsingle_timestamp_endingZconsecutiveÚslicesZ
last_sliceZcurrent_sliceZsliced_tokensZstart_timestamp_posZend_timestamp_posZlast_timestamp_posÚdurationÚ
timestampsZword_end_timestampsZ
seek_shiftr:   rO   rP   rY   Úlinerk   r?   )r%   rK   r&   r-   r'   rX   r$   rV   rL   r7   %   sn  I





ÿÿü
"(ÿ
ÿÿÿÿÿ
ÿ



"
ÿÿ

üÿ
ÿÿüÿø
ÿÿ
$€ÿþÿÿ �
ð€ü ýr7   c                     sª  ddl m‰ ‡fdd„} tjtjd�}|jddtdd	� |jd
d| dd� |jdtd dd� |jdtj 	¡ r8dnddd� |jddtddd� |jddtdg d¢dd� |jd t
d!d"d� |jd#td$d$d%gd&d� |jd'td tt ¡ ƒtd(d)„ t ¡ D ƒƒ d*d� |jd+td,d-d� |jd.td/d0d� |jd1td/d2d� |jd3td d4d� |jd5td d6d� |jd7td8d9d� |jd:td d;d� |jd<t
d!d=d� |jd>t
d!d?d� |jd@tdAdBd� |jdCtdDdEd� |jdFtdGdHd� |jdItdJdKd� |jdLt
dMdNd� |jdOtdPdQd� |jdRtdSdTd� |jdUt
dMdVd� |jdWtd dXd� |jdYtd dZd� |jd[td d\d� |jd]td,d^d� | ¡ j‰ ˆ  d_¡}ˆ  d`¡}ˆ  da¡}ˆ  db¡}ˆ  dc¡}tj|d!dd� | de¡�r‰ˆ df dgv�r‰ˆ df d u�r…t |› dhˆ df › di�¡ djˆ df< ˆ  dk¡}ˆ  dl¡ }d u�r¢tt |dm|¡ƒ}n|g}ˆ  dn¡ }	d,k�r´t |	¡ ddol m}
 |
|||dp�}t||ƒ}g dq¢}ˆ dr �sã|D ]}ˆ | �rá|  ds|› dt�¡ �qÑˆ du �ròˆ dv �sòt dw¡ ˆ dx �rˆ dv �rt dy¡ ‡ fdzd{„|D ƒ}ˆ  d¡D ]C}zt!||fdk|iˆ ¤Ž}|||fi |¤Ž W �q t"�yR } zt# $¡  t%d||› d}t&|ƒj'› d~t|ƒ› �ƒ W Y d }~�qd }~ww d S )Nr   ©Úavailable_modelsc                    s,   | ˆ ƒ v st j | ¡r| S tdˆ ƒ › d�ƒ‚)Nzmodel should be one of z or path to a model checkpoint)ÚosÚpathÚexistsÚ
ValueError)Únamer›   r?   rL   Úvalid_model_nameƒ  s
   ÿzcli.<locals>.valid_model_name)Úformatter_classr.   ú+zaudio file(s) to transcribe)ÚnargsÚtypeÚhelpz--modelÚsmallz name of the Whisper model to use)Údefaultr¦   r§   z--model_dirz>the path to save model files; uses ~/.cache/whisper by default)r¦   r©   r§   z--devicerq   r0   z#device to use for PyTorch inference)r©   r§   z--output_dirz-oÚ.zdirectory to save the outputsz--output_formatz-fÚall)ÚtxtÚvttÚsrtÚtsvÚjsonr«   zSformat of the output file; if not specified, all available formats will be produced)r¦   r©   Úchoicesr§   z	--verboseTz4whether to print out the progress and debug messagesz--taskr7   r9   zawhether to perform X->X speech recognition ('transcribe') or X->English translation ('translate')z
--languagec                 S   s   g | ]}|  ¡ ‘qS r?   )r}   )rT   Úkr?   r?   rL   rW   •  s    zcli.<locals>.<listcomp>zHlanguage spoken in the audio, specify None to perform language detectionz--temperaturer   ztemperature to use for samplingz	--best_ofé   z<number of candidates when sampling with non-zero temperaturez--beam_sizezHnumber of beams in beam search, only applicable when temperature is zeroz
--patiencez”optional patience value to use in beam decoding, as in https://arxiv.org/abs/2204.05424, the default (1.0) is equivalent to conventional beam searchz--length_penaltyz…optional token length penalty coefficient (alpha) as in https://arxiv.org/abs/1609.08144, uses simple length normalization by defaultz--suppress_tokensz-1z„comma-separated list of token ids to suppress during sampling; '-1' will suppress most special characters except common punctuationsz--initial_promptz:optional text to provide as a prompt for the first window.z--condition_on_previous_textzÏif True, provide the previous output of the model as a prompt for the next window; disabling may make the text inconsistent across windows, but the model becomes less prone to getting stuck in a failure loopz--fp16z5whether to perform inference in fp16; True by defaultz#--temperature_increment_on_fallbackr   zhtemperature to increase when falling back when the decoding fails to meet either of the thresholds belowz--compression_ratio_thresholdr   zUif the gzip compression ratio is higher than this value, treat the decoding as failedz--logprob_thresholdr    zUif the average log probability is lower than this value, treat the decoding as failedz--no_speech_thresholdr   zžif the probability of the <|nospeech|> token is higher than this value AND the decoding has failed due to `logprob_threshold`, consider the segment as silencez--word_timestampsFzQ(experimental) extract word-level timestamps and refine the results based on themz--prepend_punctuationsr!   zNif word_timestamps is True, merge these punctuation symbols with the next wordz--append_punctuationsr"   zRif word_timestamps is True, merge these punctuation symbols with the previous wordz--highlight_wordszT(requires --word_timestamps True) underline each word as it is spoken in srt and vttz--max_line_widthze(requires --word_timestamps True) the maximum number of characters in a line before breaking the linez--max_line_countzJ(requires --word_timestamps True) the maximum number of lines in a segmentz--max_words_per_linezk(requires --word_timestamps True, no effect with --max_line_width) the maximum number of words in a segmentz	--threadsz]number of threads used by torch for CPU inference; supercedes MKL_NUM_THREADS/OMP_NUM_THREADSr-   Ú	model_dirÚ
output_dirÚoutput_formatrp   )Úexist_okz.enr3   >   ZEnglishr4   z) is an English-only model but receipted 'z'; using English instead.r4   r$   Z!temperature_increment_on_fallbackgzo ð?Úthreads)Ú
load_model)rp   Údownload_root)Úhighlight_wordsÚmax_line_countÚmax_line_widthÚmax_words_per_liner*   z--z  requires --word_timestamps Truer¼   r½   z7--max_line_count has no effect without --max_line_widthr¾   z8--max_words_per_line has no effect with --max_line_widthc                    s   i | ]}|ˆ   |¡“qS r?   )rC   )rT   Úarg)Úargsr?   rL   Ú
<dictcomp>Û  s    zcli.<locals>.<dictcomp>z	Skipping z due to z: )(ri   rœ   ÚargparseÚArgumentParserÚArgumentDefaultsHelpFormatterÚadd_argumentÚstrrm   rq   rr   r   Úsortedr   Úkeysr   rB   r   r   Ú
parse_argsÚ__dict__rC   r�   ÚmakedirsÚendswithrs   rt   ÚtupleÚnpÚarangeÚset_num_threadsr¹   r   Úerrorr7   Ú	ExceptionÚ	tracebackÚ	print_excry   r¦   Ú__name__)r¢   ÚparserÚ
model_namer´   rµ   r¶   rp   r$   Ú	incrementr¸   r¹   r-   ÚwriterZword_optionsÚoptionZwriter_argsÚ
audio_pathrR   Úer?   )rÀ   rœ   rL   Úcli€  s˜   2





ÿ




€

2€þürÝ   Ú__main__)0rÂ   r�   rÓ   rs   Útypingr   r   r   r   ÚnumpyrÎ   rm   rƒ   r.   r   r   r	   r
   r   r   r   Údecodingr   r   Útimingr   rV   r   r   r   Úutilsr   r   r   r   r   r   r   r-   r   rÆ   Úndarrayr~   ÚboolrB   r7   rÝ   rÕ   r?   r?   r?   rL   Ú<module>   sp    $	$
óÿþüûúùø	÷
öõô
ó  ]e
ÿ