o
    ॵio
                     @   s   d dl Z d dlmZmZm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 d d	lmZ d d
lmZ dgZejejejdG dd deeZdS )    N)AnyDictUnion)	Pipelines)Model)PolyLMForTextGeneration)
OutputKeys)Pipeline)	PIPELINES)Tasks)PipelineStreamingOutputMixinPolyLMTextGenerationPipeline)module_namec                       s   e Zd ZdZdeeef f fddZdd Zdede	ee
f fd	d
Zde	ee
f de	ee
f fddZde	ee
f fddZ  ZS )r   a   A polyglot large language for text generation pipeline.

    Examples:

    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> polylm_13b_model_id = 'damo/nlp_polylm_13b_text_generation'
    >>> input_text = "Beijing is the capital of China.
Translate this sentence from English to Chinese."
    >>> kwargs = {"do_sample": False, "num_beams": 4, "max_new_tokens": 128, "early_stopping": True, "eos_token_id": 2}
    >>> pipeline_ins = pipeline(Tasks.text_generation, model=polylm_13b_model_id)
    >>> result = pipeline_ins(input_text, **kwargs)
    >>> print(result['text'])
    >>>
    modelc                    s8   t |trt|fi |n|}t jdd|i| dS )a%  Use `model` and `preprocessor` to create a generation pipeline for prediction.

        Args:
            model (str or Model): Supply either a local model dir which supported the text generation task,
            or a model id from the model hub, or a torch model instance.
            preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
            the model if supplied.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.
        r   N )
isinstancestrr   super__init__)selfr   kwargs	__class__r   l/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/polylm_text_generation_pipeline.pyr   '   s   z%PolyLMTextGenerationPipeline.__init__c                 K   s
   i |i fS Nr   )r   pipeline_parametersr   r   r   _sanitize_parameters6      
z1PolyLMTextGenerationPipeline._sanitize_parametersinputreturnc                 C   s   |S r   r   )r   r   r   r   r   
preprocess9   s   z'PolyLMTextGenerationPipeline.preprocessinputsc                 K   s>   t   | j|fi |W  d    S 1 sw   Y  d S r   )torchno_gradr   )r   r!   forward_paramsr   r   r   forward<   s   
$z$PolyLMTextGenerationPipeline.forwardc                 K   s
   t j|iS r   )r   TEXT)r   r   r   r   r   r   postprocessA   r   z(PolyLMTextGenerationPipeline.postprocess)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r    r%   r'   __classcell__r   r   r   r   r      s    

)ostypingr   r   r   r"   modelscope.metainfor   modelscope.models.baser   modelscope.models.nlpr   modelscope.outputsr   modelscope.pipelines.baser	   modelscope.pipelines.builderr
   modelscope.utils.constantr   !modelscope.utils.streaming_outputr   __all__register_moduletext_generationpolylm_text_generationr   r   r   r   r   <module>   s    