o
    xi                     @  s   d dl 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	Z	d dl
Z
d dlmZ d dlmZ eeZg dZg d	ZG d
d dZdS )    )annotationsN)Sequence)datetime)Any)Response)generate_id)text-classificationsentiment-analysisquestion-answeringsummarizationtranslationztext2text-generationztext-generation)r   r	   r
   c                   @  sR   e Zd ZdZdZd%ddZd&ddZed'ddZd(ddZ	d)d!d"Z
d#d$ ZdS )**HuggingFacePipelineRequestResponseResolverzResolver for HuggingFace's pipeline request and responses, providing necessary data transformations and formatting.

    This is based off (from wandb.sdk.integration_utils.auto_logging import RequestResponseResolver)
    NargsSequence[Any]kwargsdict[str, Any]responser   
start_timefloattime_elapsedreturndict[str, Any] | Nonec              
   C  s   zf|dd \}}|j }|tv s|dr[| |}	|	du r!W dS |	j}
ttj}| 	|||\}}| 
||||}| ||
||}tjd| }|tj|d |dd diW S td| d	 W dS  ty~ } zt| W Y d}~dS d}~ww )
a  Main call method for this class.

        :param args: list of arguments
        :param kwargs: dictionary of keyword arguments
        :param response: the response from the request
        :param start_time: time when request started
        :param time_elapsed: time elapsed for the request
        :returns: packed data as a dictionary for logging to wandb, None if an exception occurred
        N   r   WANDB_AUTOLOG_TABLE_NAMEr      )columnsdatazThe task: `zc` is not yet supported.
Please contact `wandb` to notify us if you would like support for this task)taskSUPPORTED_PIPELINE_TASKS
startswith
_get_modelname_or_pathr   nowpytzutc_transform_task_specific_data_format_data_create_tableosenvirongetwandbTableloggerwarning	Exception)selfr   r   r   r   r   pipe
input_datar   modelmodel_alias	timestampformatted_datapacked_data
table_namee r:   Z/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/huggingface/resolver.py__call__+   s<   

z3HuggingFacePipelineRequestResponseResolver.__call__
Any | Nonec                 C  s0   |j }z|j W S  ty   td | Y S w )zExtracts model from the pipeline.

        :param pipe: the HuggingFace pipeline
        :returns: Model if available, None otherwise
        zUModel does not have a `.model` attribute. Assuming `pipe.model` is the correct model.)r3   AttributeErrorr-   info)r0   r1   r3   r:   r:   r;   r    c   s   z5HuggingFacePipelineRequestResponseResolver._get_modelr   strr2   list[Any] | Any'tuple[list[Any] | Any, list[Any] | Any]c                 C  s   | dkrt |tr|n|g}dd |D }||fS | dkr?t |tr$|n|g}dd |D }t |tr5|n|g}dd |D }||fS )zTransform input and response data based on specific tasks.

        :param task: the task name
        :param input_data: the input data
        :param response: the response data
        :returns: tuple of transformed input_data and response
        r
   c                 S  s   g | ]}|j qS r:   __dict__.0r   r:   r:   r;   
<listcomp>   s    z\HuggingFacePipelineRequestResponseResolver._transform_task_specific_data.<locals>.<listcomp>conversationalc                 S     g | ]	}|j d  d qS )past_user_inputsrC   rE   r:   r:   r;   rG          c                 S  rI   )generated_responsesrK   rC   rE   r:   r:   r;   rG      rL   )
isinstancelist)r   r2   r   r:   r:   r;   r%   r   s   	zHHuggingFacePipelineRequestResponseResolver._transform_task_specific_datalist[dict[str, Any]]c                 C  s|   t |tr|n|g}t |tr|n|g}g }t||D ] \}}t |tr2t|dkr2|tvr2|d }||||d q|S )a?  Formats input data, response, and kwargs into a list of dictionaries.

        :param task: the task name
        :param input_data: the input data
        :param response: the response data
        :param kwargs: dictionary of keyword arguments
        :returns: list of dictionaries containing formatted data
        r   r   )inputr   r   )rN   rO   ziplenPIPELINES_WITH_TOP_Kappend)r0   r   r2   r   r   r6   i_textr_textr:   r:   r;   r&      s   
z7HuggingFacePipelineRequestResponseResolver._format_datar6   r4   r5   list[list[Any]]c           
   	   C  sT   g d}|g}t dd}|D ]}|||||d |d |d g}	||	 q|| _|S )a  Creates a table from formatted data, model alias, timestamp, and elapsed time.

        :param formatted_data: list of dictionaries containing formatted data
        :param model_alias: alias of the model
        :param timestamp: timestamp of the data
        :param time_elapsed: time elapsed from the beginning
        :returns: list of lists, representing a table of data. [0]th element = columns. [1]st element = data
        )IDzModel Alias	TimestampzElapsed TimeInputr   Kwargs   )lengthrQ   r   r   )r   rU   
autolog_id)
r0   r6   r4   r5   r   headertabler_   r   rowr:   r:   r;   r'      s   	
	z8HuggingFacePipelineRequestResponseResolver._create_tablec                 C  s   | j S )N)r_   )r0   r:   r:   r;   get_latest_id   s   z8HuggingFacePipelineRequestResponseResolver.get_latest_id)r   r   r   r   r   r   r   r   r   r   r   r   )r   r=   )r   r@   r2   rA   r   rA   r   rB   )
r   r@   r2   rA   r   rA   r   r   r   rP   )
r6   rP   r4   r@   r5   r   r   r   r   rX   )__name__
__module____qualname____doc__r_   r<   r    staticmethodr%   r&   r'   rc   r:   r:   r:   r;   r   #   s    

8

"+r   )
__future__r   loggingr(   collections.abcr   r   typingr   r#   r+   (wandb.sdk.integration_utils.auto_loggingr   wandb.sdk.lib.runidr   	getLoggerrd   r-   r   rT   r   r:   r:   r:   r;   <module>   s    
