o
    ߥi                     @   s   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 d d	lmZ d d
lmZmZ dgZe
jejejdG dd deZdS )    N)Dict)Models)
TorchModel)Tensor)MODELS)SpaceGeneratorSpaceModelBase)MultiWOZBPETextField)Config)	ModelFileTasksSpaceForDialogModeling)module_namec                       sD   e Zd Zdef fddZdeeef deeef fddZ  ZS )r   	model_dirc              	      s   t  j|g|R i | ddlm} | _|dttj	
 jtj _ddl}d|vs5|d dkr<|j r<dnd j_|d	t j jd
 _tj j jd _tj| j j jd _ fdd}| j| j jdd _ j  dS )aw  initialize the test generation model from the `model_dir` path.

        Args:
            model_dir (`str`):
                The model path.
            text_field (`BPETextField`, *optional*, defaults to `MultiWOZBPETextField`):
                The text field.
            config (`Config`, *optional*, defaults to config in model hub):
                The config.
        r   )MultiWOZTrainerconfigNdevicegpuTF
text_field)r   r   )reader)r   r   r   	generatorc                    s&   ddl }|| }  jjr|  S | S )z/
            numpy array -> tensor
            r   N)torchtensorr   use_gpucuda)arrayr   self _/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/space/dialog_modeling.py	to_tensor:   s   
z2SpaceForDialogModeling.__init__.<locals>.to_tensor)modelr    r   r   	evaluator)super__init__1modelscope.trainers.nlp.space.trainer.gen_trainerr   r   popr
   	from_fileospathjoinr   CONFIGURATIONr   r   r   is_availabler   r	   r   r   creater   r   r!   trainerload)r   r   argskwargsr   r   r    	__class__r   r   r$      sP   zSpaceForDialogModeling.__init__inputreturnc                 C   sB   |d }|d }|d }|d }|d }| j j|||||d}|S )aK  return the result by the model

        Args:
            input (Dict[str, Tensor]): the preprocessed data

        Returns:
            Dict[str, Tensor]: results
                Example:
                    {
                        'labels': array([1,192,321,12]), # lable
                        'resp': array([293,1023,123,1123]), #vocab label for response
                        'bspn': array([123,321,2,24,1 ]),
                        'aspn': array([47,8345,32,29,1983]),
                        'db': array([19, 24, 20]),
                    }

        Examples:
            >>> from modelscope.hub.snapshot_download import snapshot_download
            >>> from modelscope.models.nlp import SpaceForDialogModeling
            >>> from modelscope.preprocessors import DialogModelingPreprocessor
            >>> cache_path = snapshot_download('damo/nlp_space_dialog-modeling')
            >>> preprocessor = DialogModelingPreprocessor(model_dir=cache_path)
            >>> model = SpaceForDialogModeling(model_dir=cache_path,
                    text_field=preprocessor.text_field,
                    config=preprocessor.config)
            >>> print(model(preprocessor({
                    'user_input': 'i would like a taxi from saint john 's college to pizza hut fen ditton .',
                    'history': {}
                })))
        
first_turnbatch	prompt_idlabelshistory)r6   r7   r8   r9   old_pv_turn)r.   forward)r   r4   r6   r7   r8   r9   r;   pv_turnr   r   r   r<   J   s    zSpaceForDialogModeling.forward)	__name__
__module____qualname__strr$   r   r   r<   __classcell__r   r   r2   r   r      s    *4)r(   typingr   modelscope.metainfor   modelscope.modelsr   modelscope.models.baser   modelscope.models.builderr   modelscope.models.nlp.spacer   r   modelscope.preprocessors.nlpr	   modelscope.utils.configr
   modelscope.utils.constantr   r   __all__register_moduletask_oriented_conversationspace_modelingr   r   r   r   r   <module>   s   