o
    *j0+                     @   s   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	m
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mZ d d	lmZmZmZ d d
lmZ d dlmZ dgZe Zej ej!e
j"dG dd deZ#dS )    N)AnyDictOptionalUnion)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)generate_scp_from_urlupdate_local_model)
Frameworks	ModelFileTasks)snapshot_download)
get_loggerLauraCodecTTSPipeline)module_namec                       s(  e Zd ZdZ				ddeeef deeeef  dee def fdd	Z						dd
ee
eef dee
eef dee
eef dededeeef fddZdedeeef fddZdd Zdeeef fddZ			dd
ee
eef dee
eef dee
eef defddZdd Z  ZS ) r   aa  Laura-style Codec-based TTS Inference Pipeline
    use `model` to create a TTS pipeline.

    Args:
        model (LauraCodecTTSPipeline): A model instance, or a model local dir, or a model id in the model hub.
        kwargs (dict, `optional`):
            Extra kwargs passed into the preprocessor's constructor.
    Examples:
        >>> from modelscope.pipelines import pipeline
        >>> from modelscope.utils.constant import Tasks
        >>> my_pipeline = pipeline(
        >>>    task=Tasks.text_to_speech,
        >>>    model='damo/speech_synthesizer-laura-en-libritts-16k-codec_nq2-pytorch'
        >>> )
        >>> text='nothing was to be done but to put about, and return in disappointment towards the north.'
        >>> prompt_text='one of these is context'
        >>> prompt_speech='example/prompt.wav'
        >>> print(my_pipeline(text))

    N   modelcodec_modelcodec_model_revisionngpuc                    sh  t  jdd|i| | j | _|| _|| _| ||| _ddl	m
} |jdi d| jd d| jd d| jd d| jd d|d	| jd	 d
| jd
 d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd | _dS )z=use `model` to create an asr pipeline for prediction
        r   r   )text2audio_inferencemode
output_dir
batch_sizedtyper   seednum_workers	log_levelkey_fileconfig_file
model_file	model_tagallow_variable_data_keys	streamingtext_emb_model	beam_sizesampling	continualtokenize_to_phoneexclude_promptcodec_config_filecodec_model_file
param_dictN )super__init__r   forward	model_cfgr   r   get_cmdcmdZfuncodec.binr   Zinference_funcfunasr_infer_modelscope)selfr   r   r   r   kwargsr   	__class__r0   z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/codec_based_synthesis_pipeline.pyr2   1   sj   








	















zLauraCodecTTSPipeline.__init__textprompt_textprompt_audior   r/   returnc                 C   sL   t |dkr
td|d ur|| jd< || jd< | |||}| |}|S )Nr   zThe input should not be null.r   r/   )len
ValueErrorr6   r3   postprocess)r8   r=   r>   r?   r   r/   outputresultr0   r0   r<   __call__Y   s   


zLauraCodecTTSPipeline.__call__inputsc                 C   sr   i }t t|D ].}t|dkr(|dkr(|d d d }|  d |tj< q|| d d ||| d < q|S )zPostprocessing
        r   r   valuegenkey)rangerA   cpunumpyr   Z
OUTPUT_WAV)r8   rG   rstiZ	recon_wavr0   r0   r<   rC   i   s   z!LauraCodecTTSPipeline.postprocessc                 C   s   | j d ur\| j dkr^tj| j r| j }nt| j | jd}td| tj	|t
j}tt| }tj|}tj	||d d d |d< tj	||d d d |d	< d S d S d S )
N )revisionz loading codec model from {0} ...r   model_configr#   r.   r"   r-   )r   ospathexistsr   r   loggerinfoformatjoinr   ZCONFIGURATIONjsonloadsopenreaddirname)r8   r6   r   Zconfig_pathr4   Z	model_dirr0   r0   r<   load_codec_modelv   s"   z&LauraCodecTTSPipeline.load_codec_modelc           
      C   s  | j d d }tj| j d | j d d }tj| j d | j d d }t| j d || i d|dd ddd	d
dddddddddd d|d|dd dddddddddd dddd d d d}g d}| j d }|dr| jd u r|d | _|dr| jd u r|d | _| | |D ]9}	|	| j d v r| j d |	 d urt	||	 t
rt	| j d |	 t
r||	 | j d |	  q| j d |	 ||	< q|D ]0}	|	|v r||	d urt	||	 t
rt	||	 t
r||	 ||	  n||	 ||	< ||	= q|S )NrR   r   Zmodel_workspacer#   r"   r   r   r   r   Zfloat32r   r   r   r   r    ERRORr!   r$   r%   Tr&   Fr(   r)      r'   )r*   r+   r,   r.   r-   r/   )r   r   r   r    r%   r&   r   Zsampling_rateZ	bit_widthZ	use_scaler/   r   r   )r4   rS   rT   rY   r   __contains__r   r   r_   
isinstancedictupdateget)
r8   
extra_argsZ
model_pathr   Z_model_pathZ_model_configr6   Zuser_args_dictrR   Z	user_argsr0   r0   r<   r5      s   	







zLauraCodecTTSPipeline.get_cmdc                 K   sN  t |trtd| d d\}}t|ddkr$t|dg}n|g}|dur|durt|ddkrC|t|d n|| t |trmt|ddkra|t|d n4t|\}}|| n(ddl	}	t ||	j
r||  nt |tjr|| n
tdt| d	|| jd
< || jd< | | j}
|
S )zDecoding
        zGenerate speech for: z ...)NN,   Nr   zUnsupported prompt audio type .name_and_type
raw_inputs)rc   strrV   rW   rA   splittupleappendr   torchZTensorrM   npZndarray	TypeErrortyper6   run_inference)r8   r=   r>   r?   Zforward_paramsZdata_cmdrl   Z
audio_path_rq   rE   r0   r0   r<   r3      s6   




zLauraCodecTTSPipeline.forwardc                 C   s:   | j tjkr| j|d |d |d |d d}|S td)Nrk   rl   r   r/   )Zdata_path_and_name_and_typerl   Zoutput_dir_v2r/   zmodel type is mismatching)Z	frameworkr   rq   r7   rB   )r8   r6   Z	sv_resultr0   r0   r<   ru   
  s   z#LauraCodecTTSPipeline.run_inference)NNNr   )NNNNN)NNN)__name__
__module____qualname____doc__r   r   rm   r   intr2   ro   r   rd   r   rF   listrC   r_   r5   r3   ru   __classcell__r0   r0   r:   r<   r      sb    
)

U
0)$rS   typingr   r   r   r   rZ   rM   rr   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   Zmodelscope.pipelines.builderr
   Z"modelscope.utils.audio.audio_utilsr   r   Zmodelscope.utils.constantr   r   r   Zmodelscope.utils.hubr   Zmodelscope.utils.loggerr   __all__rV   Zregister_moduleZtext_to_speechZlaura_codec_tts_inferencer   r0   r0   r0   r<   <module>   s&   