o
    -wi                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZ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 e eZg dZg dZG dd	 d	ZdS )
    N)datetime)AnyDictListOptionalSequenceTupleUnion)Response)generate_id)text-classificationsentiment-analysisquestion-answeringsummarizationtranslationztext2text-generationztext-generation)r   r   r   c                   @   s:  e Zd ZdZdZdee deeef de	de
de
deeeef  fd	d
Zdee fddZededeee ef deee ef deeee ef eee ef f fddZdedeee ef deee ef deeef deeeef  f
ddZdeeeef  dede
de
deee  f
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kwargsresponse
start_timetime_elapsedreturnc              
   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:   c/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/integration/huggingface/resolver.py__call__(   s<   

z3HuggingFacePipelineRequestResponseResolver.__call__c                 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    `   s   z5HuggingFacePipelineRequestResponseResolver._get_modelr   r2   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_inputsr?   rA   r:   r:   r;   rC          c                 S   rE   )generated_responsesrG   r?   rA   r:   r:   r;   rC      rH   )
isinstancelist)r   r2   r   r:   r:   r;   r%   o   s   	zHHuggingFacePipelineRequestResponseResolver._transform_task_specific_datac                 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   )rJ   rK   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   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   )lengthrL   r   r   )r   rP   
autolog_id)
r0   r6   r4   r5   r   headertablerY   r   rowr:   r:   r;   r'      s   	
	z8HuggingFacePipelineRequestResponseResolver._create_tablec                 C   s   | j S )N)rY   )r0   r:   r:   r;   get_latest_id   s   z8HuggingFacePipelineRequestResponseResolver.get_latest_id)__name__
__module____qualname____doc__rY   r   r   r   strr
   floatr   r<   r    staticmethodr	   r   r   r%   r&   r'   r]   r:   r:   r:   r;   r       sd    

8"

"

+r   )loggingr(   r   typingr   r   r   r   r   r   r	   r#   r+   (wandb.sdk.integration_utils.auto_loggingr
   wandb.sdk.lib.runidr   	getLoggerr^   r-   r   rO   r   r:   r:   r:   r;   <module>   s    $
