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)IntentBPETextField)Config)	ModelFileTasksSpaceForDialogIntent)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t j jd _tj j jd _tj| j j jd _ fdd	}| j| j jd
 _ j  dS )ac  initialize the test generation model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
            text_field (`BPETextField`, *optional*, defaults to `IntentBPETextField`):
                The text field.
            config (`Config`, *optional*, defaults to config in model hub):
                The config.
        r   )IntentTrainerconfig
text_field)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 h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/nlp/space/dialog_intent_prediction.py	to_tensor5   s   
z0SpaceForDialogIntent.__init__.<locals>.to_tensor)modelr   r   r   N)super__init__4modelscope.trainers.nlp.space.trainer.intent_trainerr   r   popr
   	from_fileospathjoinr   CONFIGURATIONr   r	   r   r   creater   r   r   trainerload)r   r   argskwargsr   r   	__class__r   r   r!      s<   zSpaceForDialogIntent.__init__inputreturnc                 C   s,   ddl }| j|}||d d}d|iS )a8  return the result by the model

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

        Returns:
            Dict[str, Tensor]: results
                Example:
                    {
                        'pred': array([2.62349960e-03 4.12110658e-03 4.12748595e-05 3.77560973e-05
                                1.08599677e-04 1.72710388e-05 2.95618793e-05 1.93638436e-04
                                6.45841064e-05 1.15997791e-04 5.11605394e-05 9.87020373e-01
                                2.66957268e-05 4.72324500e-05 9.74208378e-05], dtype=float32),
                    }
        Example:
            >>> from modelscope.hub.snapshot_download import snapshot_download
            >>> from modelscope.models.nlp import SpaceForDialogIntent
            >>> from modelscope.preprocessors import DialogIntentPredictionPreprocessor
            >>> cache_path = snapshot_download('damo/nlp_space_dialog-intent-prediction')
            >>> preprocessor = DialogIntentPredictionPreprocessor(model_dir=cache_path)
            >>> model = SpaceForDialogIntent(
                    model_dir=cache_path,
                    text_field=preprocessor.text_field,
                    config=preprocessor.config)
            >>> print(model(preprocessor("What do I need to do for the card activation?")))
        r   Npred)numpyr*   forwardsqueeze)r   r0   npr2   r   r   r   r4   D   s   zSpaceForDialogIntent.forward)	__name__
__module____qualname__strr!   r   r   r4   __classcell__r   r   r.   r   r      s    *.)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_intentr   r   r   r   r   <module>   s   