o
    }oil                     @   s   d dl Z d dlZd dlZd dl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 ejddd	d
 ZG dd dZG dd dZG dd dZdS )    N)Trainer)EncDecCTCModelBPE)context_biasing)WSHyp)
rnnt_utilsmodule)scopec                  C   s*   t jdd} | tddd |  } | S )Nstt_en_conformer_ctc_small)
model_name   cpu)devicesaccelerator)r   from_pretrainedset_trainerr   eval)model r   b/home/ubuntu/.local/lib/python3.10/site-packages/tests/collections/asr/test_asr_context_biasing.pyconformer_ctc_bpe_model   s   r   c                   @   s   e Zd Zejjdd ZdS )TestContextGraphCTCc                 C   s   dg dg dggg}t jdd}|| |jdksJ |jdks$J |jjd jr-J |jjd jd jd	 js<J |jjd jd jd	 jdksMJ |jjd jd
 jd js\J |jjd jd
 jd jdksmJ d S )Ngpu)   ▁gpu)r      ▁p   ▁ui   blank_id   r   r   r   r   r   )	r   ContextGraphCTCadd_to_graph	num_nodesblank_tokenrootnextis_endword)selfcontext_biasing_listcontext_graphr   r   r   test_graph_building'   s   
"&z'TestContextGraphCTC.test_graph_buildingN)__name__
__module____qualname__pytestmarkunitr+   r   r   r   r   r   &   s    r   c                   @   s$   e Zd Zejjejjdd ZdS )TestCTCWordSpotterc              	   C   s  |}t j|d}d}|j|}|j|gdddd j  }||ggg}t	j
|jjd}	|	| t	j||	||jjdd	d
d}
t|
dksJJ t	j||	||jjddd
d}
t|
dks`J |
d j|ksiJ |
d jdksrJ |
d jdks{J t|
d jddksJ d S )Nz asr/test/an4/wav/cen3-mjwl-b.wavnineteenr   T)
batch_sizereturn_hypothesesr   r   g      @        g333333?)	blank_idxbeam_threshold	cb_weightctc_ali_token_weightg      @	         g_vO!@)ospathjoin	tokenizertext_to_ids
transcribe
alignmentsr   numpyr   r    decodingr   r!   run_word_spotterlenr'   start_frame	end_frameroundscore)r(   test_data_dirr   	asr_modelaudio_file_pathtarget_texttarget_tokenizationctc_logprobsr)   r*   
ws_resultsr   r   r   test_run_word_spotter6   sB   
		z(TestCTCWordSpotter.test_run_word_spotterN)r,   r-   r.   r/   r0   r1   with_downloadsrT   r   r   r   r   r2   5   s    r2   c                   @   s4   e Zd Zejjejjdd Zejjdd ZdS )TestContextBiasingUtilsc                 C   s   |}|j j}tdddddg}tdd||g}tj|||d|d	\}}|d
ks*J |dks0J tjt	
ddgdt	
g dd}tj|||d|d	\}}|d
ksTJ |dksZJ tjg dg d}tj|||d|d	\}}|dkstJ |dkszJ d S )Nr   g      @r      )r'   rL   rI   rJ   x      ctc)decoder_typer7   gpr6   )r   r   rW      )
y_sequencerL   	timestamprnnt )rF   r   r   nparrayr   merge_alignment_with_ws_hypsr   
Hypothesistorchtensor)r(   r   rN   r7   rS   preds	pred_textraw_textr   r   r   !test_merge_alignment_with_ws_hypsb   sR   


z9TestContextBiasingUtils.test_merge_alignment_with_ws_hypsc                 C   s   d}g d}t jddd2}|| |d t|j|}t|d dt|d dt|d	 dfd
ks7J W d    d S 1 sBw   Y  d S )Nzv{"audio_filepath": "test.wav", "duration": 1.0, "text": "a new gpu for nvidia", "pred_text": "a new gpu for invidia"}
)r   r   nvidiawzutf-8)modeencodingr   r=   r   rW   )g      ?g      ?gK=U?)tempfileNamedTemporaryFilewriteseekr   compute_fscorenamerK   )r(   recog_manifestcontext_wordsffscore_statsr   r   r   test_compute_fscore   s   

2"z+TestContextBiasingUtils.test_compute_fscoreN)	r,   r-   r.   r/   r0   r1   rU   rk   rz   r   r   r   r   rV   a   s    1rV   )r>   rp   rE   rb   r/   rf   lightning.pytorchr   nemo.collections.asr.modelsr   nemo.collections.asr.partsr   Anemo.collections.asr.parts.context_biasing.ctc_based_word_spotterr    nemo.collections.asr.parts.utilsr   fixturer   r   r2   rV   r   r   r   r   <module>   s   

,